Skip to content

Commit bc1f1ef

Browse files
Stop removing substs from Adt type in coherence
1 parent 5e2c549 commit bc1f1ef

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

Diff for: 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

Diff for: compiler/rustc_hir_analysis/src/coherence/orphan.rs

+16-12
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,8 +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());
286+
// FIXME: Someone needs to just turn these into `Subdiag`s and attach
287+
// them to the `Diag` after creating the error.
288+
let mut opaque = vec![];
289+
let mut foreign = vec![];
290+
let mut name = vec![];
291+
let mut pointer = vec![];
292+
let mut ty_diag = vec![];
293+
let mut adt = vec![];
288294
let mut sugg = None;
289295
for &(mut ty, is_target_ty) in &tys {
290296
let span = if matches!(is_target_ty, IsFirstInputType::Yes) {
@@ -296,15 +302,6 @@ fn emit_orphan_check_error<'tcx>(
296302
};
297303

298304
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-
};
308305

309306
fn push_to_foreign_or_name<'tcx>(
310307
is_foreign: bool,
@@ -366,6 +363,10 @@ fn emit_orphan_check_error<'tcx>(
366363
}
367364
pointer.push(errors::OnlyCurrentTraitsPointer { span, pointer: ty });
368365
}
366+
ty::Adt(adt_def, _) => adt.push(errors::OnlyCurrentTraitsAdt {
367+
span,
368+
name: tcx.def_path_str(adt_def.did()),
369+
}),
369370
_ => ty_diag.push(errors::OnlyCurrentTraitsTy { span, ty }),
370371
}
371372
}
@@ -379,6 +380,7 @@ fn emit_orphan_check_error<'tcx>(
379380
name,
380381
pointer,
381382
ty: ty_diag,
383+
adt,
382384
sugg,
383385
},
384386
_ if self_ty.is_primitive() => errors::OnlyCurrentTraits::Primitive {
@@ -389,6 +391,7 @@ fn emit_orphan_check_error<'tcx>(
389391
name,
390392
pointer,
391393
ty: ty_diag,
394+
adt,
392395
sugg,
393396
},
394397
_ => errors::OnlyCurrentTraits::Arbitrary {
@@ -399,6 +402,7 @@ fn emit_orphan_check_error<'tcx>(
399402
name,
400403
pointer,
401404
ty: ty_diag,
405+
adt,
402406
sugg,
403407
},
404408
};

Diff for: compiler/rustc_hir_analysis/src/errors.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,8 @@ pub enum OnlyCurrentTraits<'a> {
13951395
#[subdiagnostic]
13961396
ty: Vec<OnlyCurrentTraitsTy<'a>>,
13971397
#[subdiagnostic]
1398+
adt: Vec<OnlyCurrentTraitsAdt>,
1399+
#[subdiagnostic]
13981400
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
13991401
},
14001402
#[diag(hir_analysis_only_current_traits_primitive, code = E0117)]
@@ -1415,6 +1417,8 @@ pub enum OnlyCurrentTraits<'a> {
14151417
#[subdiagnostic]
14161418
ty: Vec<OnlyCurrentTraitsTy<'a>>,
14171419
#[subdiagnostic]
1420+
adt: Vec<OnlyCurrentTraitsAdt>,
1421+
#[subdiagnostic]
14181422
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
14191423
},
14201424
#[diag(hir_analysis_only_current_traits_arbitrary, code = E0117)]
@@ -1435,6 +1439,8 @@ pub enum OnlyCurrentTraits<'a> {
14351439
#[subdiagnostic]
14361440
ty: Vec<OnlyCurrentTraitsTy<'a>>,
14371441
#[subdiagnostic]
1442+
adt: Vec<OnlyCurrentTraitsAdt>,
1443+
#[subdiagnostic]
14381444
sugg: Option<OnlyCurrentTraitsPointerSugg<'a>>,
14391445
},
14401446
}
@@ -1445,7 +1451,6 @@ pub struct OnlyCurrentTraitsOpaque {
14451451
#[primary_span]
14461452
pub span: Span,
14471453
}
1448-
14491454
#[derive(Subdiagnostic)]
14501455
#[label(hir_analysis_only_current_traits_foreign)]
14511456
pub struct OnlyCurrentTraitsForeign {
@@ -1477,6 +1482,14 @@ pub struct OnlyCurrentTraitsTy<'a> {
14771482
pub ty: Ty<'a>,
14781483
}
14791484

1485+
#[derive(Subdiagnostic)]
1486+
#[label(hir_analysis_only_current_traits_adt)]
1487+
pub struct OnlyCurrentTraitsAdt {
1488+
#[primary_span]
1489+
pub span: Span,
1490+
pub name: String,
1491+
}
1492+
14801493
#[derive(Subdiagnostic)]
14811494
#[multipart_suggestion(
14821495
hir_analysis_only_current_traits_pointer_sugg,

0 commit comments

Comments
 (0)