Skip to content

Assert that ADTs have the right number of args #123214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ hir_analysis_not_supported_delegation =
{$descr} is not supported yet
.label = callee defined here
hir_analysis_only_current_traits_adt = `{$name}` is not defined in the current crate
hir_analysis_only_current_traits_arbitrary = only traits defined in the current crate can be implemented for arbitrary types
hir_analysis_only_current_traits_foreign = this is not defined in the current crate because this is a foreign trait
Expand Down
28 changes: 16 additions & 12 deletions compiler/rustc_hir_analysis/src/coherence/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::errors;
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_middle::ty::{self, AliasKind, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{self, AliasKind, TyCtxt, TypeVisitableExt};
use rustc_span::def_id::LocalDefId;
use rustc_span::Span;
use rustc_trait_selection::traits::{self, IsFirstInputType};
Expand Down Expand Up @@ -283,8 +283,14 @@ fn emit_orphan_check_error<'tcx>(
let self_ty = trait_ref.self_ty();
Err(match err {
traits::OrphanCheckErr::NonLocalInputType(tys) => {
let (mut opaque, mut foreign, mut name, mut pointer, mut ty_diag) =
(Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new());
// FIXME: Someone needs to just turn these into `Subdiag`s and attach
// them to the `Diag` after creating the error.
let mut opaque = vec![];
let mut foreign = vec![];
let mut name = vec![];
let mut pointer = vec![];
let mut ty_diag = vec![];
let mut adt = vec![];
let mut sugg = None;
for &(mut ty, is_target_ty) in &tys {
let span = if matches!(is_target_ty, IsFirstInputType::Yes) {
Expand All @@ -296,15 +302,6 @@ fn emit_orphan_check_error<'tcx>(
};

ty = tcx.erase_regions(ty);
ty = match ty.kind() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah lol, thanks :) I encountered this in #117164 and was a bit weirded out by it, glad it's gone.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💀

// Remove the type arguments from the output, as they are not relevant.
// You can think of this as the reverse of `resolve_vars_if_possible`.
// That way if we had `Vec<MyType>`, we will properly attribute the
// problem to `Vec<T>` and avoid confusing the user if they were to see
// `MyType` in the error.
ty::Adt(def, _) => Ty::new_adt(tcx, *def, ty::List::empty()),
_ => ty,
};

fn push_to_foreign_or_name<'tcx>(
is_foreign: bool,
Expand Down Expand Up @@ -366,6 +363,10 @@ fn emit_orphan_check_error<'tcx>(
}
pointer.push(errors::OnlyCurrentTraitsPointer { span, pointer: ty });
}
ty::Adt(adt_def, _) => adt.push(errors::OnlyCurrentTraitsAdt {
span,
name: tcx.def_path_str(adt_def.did()),
}),
_ => ty_diag.push(errors::OnlyCurrentTraitsTy { span, ty }),
}
}
Expand All @@ -379,6 +380,7 @@ fn emit_orphan_check_error<'tcx>(
name,
pointer,
ty: ty_diag,
adt,
sugg,
},
_ if self_ty.is_primitive() => errors::OnlyCurrentTraits::Primitive {
Expand All @@ -389,6 +391,7 @@ fn emit_orphan_check_error<'tcx>(
name,
pointer,
ty: ty_diag,
adt,
sugg,
},
_ => errors::OnlyCurrentTraits::Arbitrary {
Expand All @@ -399,6 +402,7 @@ fn emit_orphan_check_error<'tcx>(
name,
pointer,
ty: ty_diag,
adt,
sugg,
},
};
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,8 @@ pub enum OnlyCurrentTraits<'a> {
#[subdiagnostic]
ty: Vec<OnlyCurrentTraitsTy<'a>>,
#[subdiagnostic]
adt: Vec<OnlyCurrentTraitsAdt>,
#[subdiagnostic]
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
},
#[diag(hir_analysis_only_current_traits_primitive, code = E0117)]
Expand All @@ -1415,6 +1417,8 @@ pub enum OnlyCurrentTraits<'a> {
#[subdiagnostic]
ty: Vec<OnlyCurrentTraitsTy<'a>>,
#[subdiagnostic]
adt: Vec<OnlyCurrentTraitsAdt>,
#[subdiagnostic]
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
},
#[diag(hir_analysis_only_current_traits_arbitrary, code = E0117)]
Expand All @@ -1435,6 +1439,8 @@ pub enum OnlyCurrentTraits<'a> {
#[subdiagnostic]
ty: Vec<OnlyCurrentTraitsTy<'a>>,
#[subdiagnostic]
adt: Vec<OnlyCurrentTraitsAdt>,
#[subdiagnostic]
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
},
}
Expand All @@ -1445,7 +1451,6 @@ pub struct OnlyCurrentTraitsOpaque {
#[primary_span]
pub span: Span,
}

#[derive(Subdiagnostic)]
#[label(hir_analysis_only_current_traits_foreign)]
pub struct OnlyCurrentTraitsForeign {
Expand Down Expand Up @@ -1477,6 +1482,14 @@ pub struct OnlyCurrentTraitsTy<'a> {
pub ty: Ty<'a>,
}

#[derive(Subdiagnostic)]
#[label(hir_analysis_only_current_traits_adt)]
pub struct OnlyCurrentTraitsAdt {
#[primary_span]
pub span: Span,
pub name: String,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(
hir_analysis_only_current_traits_pointer_sugg,
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,13 @@ impl<'tcx> Ty<'tcx> {

#[inline]
pub fn new_adt(tcx: TyCtxt<'tcx>, def: AdtDef<'tcx>, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
debug_assert_eq!(
tcx.generics_of(def.did()).count(),
args.len(),
"wrong number of args for ADT: {:#?} vs {:#?}",
tcx.generics_of(def.did()).params,
args
);
Ty::new(tcx, Adt(def, args))
}

Expand Down