Skip to content

Commit 40116ad

Browse files
committed
Auto merge of rust-lang#123214 - compiler-errors:subst, r=estebank
Assert that ADTs have the right number of args We're doing it for many other types, let's also do ADTs 😇
2 parents 7e0ed43 + f487d83 commit 40116ad

File tree

4 files changed

+87
-129
lines changed

4 files changed

+87
-129
lines changed

compiler/rustc_hir_analysis/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ hir_analysis_not_supported_delegation =
295295
{$descr} is not supported yet
296296
.label = callee defined here
297297
298+
hir_analysis_only_current_traits_adt = `{$name}` is not defined in the current crate
299+
298300
hir_analysis_only_current_traits_arbitrary = only traits defined in the current crate can be implemented for arbitrary types
299301
300302
hir_analysis_only_current_traits_foreign = this is not defined in the current crate because this is a foreign trait

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+69-91
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::errors;
55
use rustc_errors::ErrorGuaranteed;
66
use rustc_hir as hir;
7-
use rustc_middle::ty::{self, AliasKind, Ty, TyCtxt, TypeVisitableExt};
7+
use rustc_middle::ty::{self, AliasKind, TyCtxt, TypeVisitableExt};
88
use rustc_span::def_id::LocalDefId;
99
use rustc_span::Span;
1010
use rustc_trait_selection::traits::{self, IsFirstInputType};
@@ -283,9 +283,14 @@ fn emit_orphan_check_error<'tcx>(
283283
let self_ty = trait_ref.self_ty();
284284
Err(match err {
285285
traits::OrphanCheckErr::NonLocalInputType(tys) => {
286-
let (mut opaque, mut foreign, mut name, mut pointer, mut ty_diag) =
287-
(Vec::new(), Vec::new(), Vec::new(), Vec::new(), Vec::new());
288-
let mut sugg = None;
286+
let mut diag = tcx.dcx().create_err(match self_ty.kind() {
287+
ty::Adt(..) => errors::OnlyCurrentTraits::Outside { span: sp, note: () },
288+
_ if self_ty.is_primitive() => {
289+
errors::OnlyCurrentTraits::Primitive { span: sp, note: () }
290+
}
291+
_ => errors::OnlyCurrentTraits::Arbitrary { span: sp, note: () },
292+
});
293+
289294
for &(mut ty, is_target_ty) in &tys {
290295
let span = if matches!(is_target_ty, IsFirstInputType::Yes) {
291296
// Point at `D<A>` in `impl<A, B> for C<B> in D<A>`
@@ -296,113 +301,86 @@ fn emit_orphan_check_error<'tcx>(
296301
};
297302

298303
ty = tcx.erase_regions(ty);
299-
ty = match ty.kind() {
300-
// Remove the type arguments from the output, as they are not relevant.
301-
// You can think of this as the reverse of `resolve_vars_if_possible`.
302-
// That way if we had `Vec<MyType>`, we will properly attribute the
303-
// problem to `Vec<T>` and avoid confusing the user if they were to see
304-
// `MyType` in the error.
305-
ty::Adt(def, _) => Ty::new_adt(tcx, *def, ty::List::empty()),
306-
_ => ty,
307-
};
308-
309-
fn push_to_foreign_or_name<'tcx>(
310-
is_foreign: bool,
311-
foreign: &mut Vec<errors::OnlyCurrentTraitsForeign>,
312-
name: &mut Vec<errors::OnlyCurrentTraitsName<'tcx>>,
313-
span: Span,
314-
sname: &'tcx str,
315-
) {
316-
if is_foreign {
317-
foreign.push(errors::OnlyCurrentTraitsForeign { span })
318-
} else {
319-
name.push(errors::OnlyCurrentTraitsName { span, name: sname });
320-
}
321-
}
322304

323305
let is_foreign =
324306
!trait_ref.def_id.is_local() && matches!(is_target_ty, IsFirstInputType::No);
325307

326308
match *ty.kind() {
327309
ty::Slice(_) => {
328-
push_to_foreign_or_name(
329-
is_foreign,
330-
&mut foreign,
331-
&mut name,
332-
span,
333-
"slices",
334-
);
310+
if is_foreign {
311+
diag.subdiagnostic(
312+
tcx.dcx(),
313+
errors::OnlyCurrentTraitsForeign { span },
314+
);
315+
} else {
316+
diag.subdiagnostic(
317+
tcx.dcx(),
318+
errors::OnlyCurrentTraitsName { span, name: "slices" },
319+
);
320+
}
335321
}
336322
ty::Array(..) => {
337-
push_to_foreign_or_name(
338-
is_foreign,
339-
&mut foreign,
340-
&mut name,
341-
span,
342-
"arrays",
343-
);
323+
if is_foreign {
324+
diag.subdiagnostic(
325+
tcx.dcx(),
326+
errors::OnlyCurrentTraitsForeign { span },
327+
);
328+
} else {
329+
diag.subdiagnostic(
330+
tcx.dcx(),
331+
errors::OnlyCurrentTraitsName { span, name: "arrays" },
332+
);
333+
}
344334
}
345335
ty::Tuple(..) => {
346-
push_to_foreign_or_name(
347-
is_foreign,
348-
&mut foreign,
349-
&mut name,
350-
span,
351-
"tuples",
352-
);
336+
if is_foreign {
337+
diag.subdiagnostic(
338+
tcx.dcx(),
339+
errors::OnlyCurrentTraitsForeign { span },
340+
);
341+
} else {
342+
diag.subdiagnostic(
343+
tcx.dcx(),
344+
errors::OnlyCurrentTraitsName { span, name: "tuples" },
345+
);
346+
}
353347
}
354348
ty::Alias(ty::Opaque, ..) => {
355-
opaque.push(errors::OnlyCurrentTraitsOpaque { span })
349+
diag.subdiagnostic(tcx.dcx(), errors::OnlyCurrentTraitsOpaque { span });
356350
}
357351
ty::RawPtr(ptr_ty, mutbl) => {
358352
if !self_ty.has_param() {
359-
let mut_key = mutbl.prefix_str();
360-
sugg = Some(errors::OnlyCurrentTraitsPointerSugg {
361-
wrapper_span: self_ty_span,
362-
struct_span: full_impl_span.shrink_to_lo(),
363-
mut_key,
364-
ptr_ty,
365-
});
353+
diag.subdiagnostic(
354+
tcx.dcx(),
355+
errors::OnlyCurrentTraitsPointerSugg {
356+
wrapper_span: self_ty_span,
357+
struct_span: full_impl_span.shrink_to_lo(),
358+
mut_key: mutbl.prefix_str(),
359+
ptr_ty,
360+
},
361+
);
366362
}
367-
pointer.push(errors::OnlyCurrentTraitsPointer { span, pointer: ty });
363+
diag.subdiagnostic(
364+
tcx.dcx(),
365+
errors::OnlyCurrentTraitsPointer { span, pointer: ty },
366+
);
367+
}
368+
ty::Adt(adt_def, _) => {
369+
diag.subdiagnostic(
370+
tcx.dcx(),
371+
errors::OnlyCurrentTraitsAdt {
372+
span,
373+
name: tcx.def_path_str(adt_def.did()),
374+
},
375+
);
376+
}
377+
_ => {
378+
diag.subdiagnostic(tcx.dcx(), errors::OnlyCurrentTraitsTy { span, ty });
368379
}
369-
_ => ty_diag.push(errors::OnlyCurrentTraitsTy { span, ty }),
370380
}
371381
}
372382

373-
let err_struct = match self_ty.kind() {
374-
ty::Adt(..) => errors::OnlyCurrentTraits::Outside {
375-
span: sp,
376-
note: (),
377-
opaque,
378-
foreign,
379-
name,
380-
pointer,
381-
ty: ty_diag,
382-
sugg,
383-
},
384-
_ if self_ty.is_primitive() => errors::OnlyCurrentTraits::Primitive {
385-
span: sp,
386-
note: (),
387-
opaque,
388-
foreign,
389-
name,
390-
pointer,
391-
ty: ty_diag,
392-
sugg,
393-
},
394-
_ => errors::OnlyCurrentTraits::Arbitrary {
395-
span: sp,
396-
note: (),
397-
opaque,
398-
foreign,
399-
name,
400-
pointer,
401-
ty: ty_diag,
402-
sugg,
403-
},
404-
};
405-
tcx.dcx().emit_err(err_struct)
383+
diag.emit()
406384
}
407385
traits::OrphanCheckErr::UncoveredTy(param_ty, local_type) => {
408386
let mut sp = sp;

compiler/rustc_hir_analysis/src/errors.rs

+9-38
Original file line numberDiff line numberDiff line change
@@ -1376,26 +1376,14 @@ pub struct TyParamSome<'a> {
13761376
}
13771377

13781378
#[derive(Diagnostic)]
1379-
pub enum OnlyCurrentTraits<'a> {
1379+
pub enum OnlyCurrentTraits {
13801380
#[diag(hir_analysis_only_current_traits_outside, code = E0117)]
13811381
Outside {
13821382
#[primary_span]
13831383
#[label(hir_analysis_only_current_traits_label)]
13841384
span: Span,
13851385
#[note(hir_analysis_only_current_traits_note)]
13861386
note: (),
1387-
#[subdiagnostic]
1388-
opaque: Vec<OnlyCurrentTraitsOpaque>,
1389-
#[subdiagnostic]
1390-
foreign: Vec<OnlyCurrentTraitsForeign>,
1391-
#[subdiagnostic]
1392-
name: Vec<OnlyCurrentTraitsName<'a>>,
1393-
#[subdiagnostic]
1394-
pointer: Vec<OnlyCurrentTraitsPointer<'a>>,
1395-
#[subdiagnostic]
1396-
ty: Vec<OnlyCurrentTraitsTy<'a>>,
1397-
#[subdiagnostic]
1398-
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
13991387
},
14001388
#[diag(hir_analysis_only_current_traits_primitive, code = E0117)]
14011389
Primitive {
@@ -1404,18 +1392,6 @@ pub enum OnlyCurrentTraits<'a> {
14041392
span: Span,
14051393
#[note(hir_analysis_only_current_traits_note)]
14061394
note: (),
1407-
#[subdiagnostic]
1408-
opaque: Vec<OnlyCurrentTraitsOpaque>,
1409-
#[subdiagnostic]
1410-
foreign: Vec<OnlyCurrentTraitsForeign>,
1411-
#[subdiagnostic]
1412-
name: Vec<OnlyCurrentTraitsName<'a>>,
1413-
#[subdiagnostic]
1414-
pointer: Vec<OnlyCurrentTraitsPointer<'a>>,
1415-
#[subdiagnostic]
1416-
ty: Vec<OnlyCurrentTraitsTy<'a>>,
1417-
#[subdiagnostic]
1418-
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
14191395
},
14201396
#[diag(hir_analysis_only_current_traits_arbitrary, code = E0117)]
14211397
Arbitrary {
@@ -1424,18 +1400,6 @@ pub enum OnlyCurrentTraits<'a> {
14241400
span: Span,
14251401
#[note(hir_analysis_only_current_traits_note)]
14261402
note: (),
1427-
#[subdiagnostic]
1428-
opaque: Vec<OnlyCurrentTraitsOpaque>,
1429-
#[subdiagnostic]
1430-
foreign: Vec<OnlyCurrentTraitsForeign>,
1431-
#[subdiagnostic]
1432-
name: Vec<OnlyCurrentTraitsName<'a>>,
1433-
#[subdiagnostic]
1434-
pointer: Vec<OnlyCurrentTraitsPointer<'a>>,
1435-
#[subdiagnostic]
1436-
ty: Vec<OnlyCurrentTraitsTy<'a>>,
1437-
#[subdiagnostic]
1438-
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
14391403
},
14401404
}
14411405

@@ -1445,7 +1409,6 @@ pub struct OnlyCurrentTraitsOpaque {
14451409
#[primary_span]
14461410
pub span: Span,
14471411
}
1448-
14491412
#[derive(Subdiagnostic)]
14501413
#[label(hir_analysis_only_current_traits_foreign)]
14511414
pub struct OnlyCurrentTraitsForeign {
@@ -1477,6 +1440,14 @@ pub struct OnlyCurrentTraitsTy<'a> {
14771440
pub ty: Ty<'a>,
14781441
}
14791442

1443+
#[derive(Subdiagnostic)]
1444+
#[label(hir_analysis_only_current_traits_adt)]
1445+
pub struct OnlyCurrentTraitsAdt {
1446+
#[primary_span]
1447+
pub span: Span,
1448+
pub name: String,
1449+
}
1450+
14801451
#[derive(Subdiagnostic)]
14811452
#[multipart_suggestion(
14821453
hir_analysis_only_current_traits_pointer_sugg,

compiler/rustc_middle/src/ty/sty.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,13 @@ impl<'tcx> Ty<'tcx> {
16241624

16251625
#[inline]
16261626
pub fn new_adt(tcx: TyCtxt<'tcx>, def: AdtDef<'tcx>, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
1627+
debug_assert_eq!(
1628+
tcx.generics_of(def.did()).count(),
1629+
args.len(),
1630+
"wrong number of args for ADT: {:#?} vs {:#?}",
1631+
tcx.generics_of(def.did()).params,
1632+
args
1633+
);
16271634
Ty::new(tcx, Adt(def, args))
16281635
}
16291636

0 commit comments

Comments
 (0)