Skip to content

Commit 44d45cf

Browse files
committed
Add expansion info to assoc type implied bounds
1 parent 5eb3bb4 commit 44d45cf

20 files changed

Lines changed: 59 additions & 46 deletions

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ fn associated_type_bounds<'tcx>(
5959
item_ty,
6060
hir_bounds,
6161
&[],
62-
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
62+
ImpliedBoundsContext::AssociatedType(assoc_item_def_id),
6363
span,
6464
);
6565
icx.lowerer().add_default_traits(
6666
&mut bounds,
6767
item_ty,
6868
hir_bounds,
6969
&[],
70-
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
70+
ImpliedBoundsContext::AssociatedType(assoc_item_def_id),
7171
span,
7272
);
7373

@@ -387,15 +387,15 @@ fn opaque_type_bounds<'tcx>(
387387
item_ty,
388388
hir_bounds,
389389
&[],
390-
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
390+
ImpliedBoundsContext::ImplTrait,
391391
span,
392392
);
393393
icx.lowerer().add_default_traits(
394394
&mut bounds,
395395
item_ty,
396396
hir_bounds,
397397
&[],
398-
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
398+
ImpliedBoundsContext::ImplTrait,
399399
span,
400400
);
401401
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
181181
return;
182182
}
183183
}
184-
ImpliedBoundsContext::TyParam(..) | ImpliedBoundsContext::AssociatedTypeOrImplTrait => {
185-
}
184+
ImpliedBoundsContext::TyParam(..)
185+
| ImpliedBoundsContext::AssociatedType(..)
186+
| ImpliedBoundsContext::TraitObject
187+
| ImpliedBoundsContext::TraitAscription
188+
| ImpliedBoundsContext::ImplTrait => {}
186189
}
187190
let collected = collect_sizedness_bounds(tcx, hir_bounds, where_bounds, context, span);
188191
if let Some(span) = collected.sized.maybe.or(collected.sized.negative)
@@ -195,7 +198,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
195198
add_trait_bound(tcx, bounds, self_ty, meta_sized_did, span);
196199
} else if !collected.any() {
197200
let span = match context {
198-
ImpliedBoundsContext::TyParam(def) => {
201+
ImpliedBoundsContext::TraitDef(def)
202+
| ImpliedBoundsContext::TyParam(def)
203+
| ImpliedBoundsContext::AssociatedType(def) => {
199204
self.tcx().with_stable_hashing_context(|hcx| {
200205
span.mark_with_reason(
201206
None,
@@ -214,7 +219,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
214219
add_trait_bound(tcx, bounds, self_ty, meta_sized_did, span);
215220
}
216221
ImpliedBoundsContext::TyParam(..)
217-
| ImpliedBoundsContext::AssociatedTypeOrImplTrait => {
222+
| ImpliedBoundsContext::AssociatedType(..)
223+
| ImpliedBoundsContext::TraitObject
224+
| ImpliedBoundsContext::TraitAscription
225+
| ImpliedBoundsContext::ImplTrait => {
218226
// If there are no explicit sizedness bounds on a parameter then add a default
219227
// `Sized` bound.
220228
let sized_did = tcx.require_lang_item(hir::LangItem::Sized, span);

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
8484
.map(|&trait_ref| hir::GenericBound::Trait(trait_ref))
8585
.collect::<Vec<_>>(),
8686
&[],
87-
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
87+
ImpliedBoundsContext::TraitObject,
8888
span,
8989
);
9090

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,11 @@ pub(crate) enum ImpliedBoundsContext {
6565
TraitDef(LocalDefId),
6666
/// An implied bound is added to a type parameter
6767
TyParam(LocalDefId),
68-
/// An implied bound being added in any other context
69-
AssociatedTypeOrImplTrait,
68+
/// Associated type bounds
69+
AssociatedType(LocalDefId),
70+
ImplTrait,
71+
TraitObject,
72+
TraitAscription,
7073
}
7174

7275
/// A path segment that is semantically allowed to have generic arguments.
@@ -3064,7 +3067,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
30643067
self_ty,
30653068
hir_bounds,
30663069
&[],
3067-
ImpliedBoundsContext::AssociatedTypeOrImplTrait,
3070+
ImpliedBoundsContext::TraitAscription,
30683071
hir_ty.span,
30693072
);
30703073
self.register_trait_ascription_bounds(bounds, hir_ty.hir_id, hir_ty.span);

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3571,7 +3571,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
35713571
}
35723572
}
35733573
let mut a = "a";
3574-
let mut this = "this bound";
3574+
let mut this = "this bound".to_owned();
35753575
let mut note = None;
35763576
let mut help = None;
35773577
if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder() {
@@ -3598,12 +3598,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
35983598
tcx.visible_parent_map(()).get(&def_id).is_some()
35993599
};
36003600
if tcx.is_lang_item(def_id, LangItem::Sized) {
3601-
if let Some(DesugaringKind::ImpliedBound { .. }) =
3601+
if let Some(DesugaringKind::ImpliedBound { def }) =
36023602
span.desugaring_kind()
36033603
{
36043604
a = "an implicit `Sized`";
3605-
this =
3606-
"the implicit `Sized` requirement on this type parameter";
3605+
this = format!(
3606+
"the implicit `Sized` requirement on this {}",
3607+
tcx.def_kind(def).descr(def)
3608+
);
36073609
}
36083610
if let Some(hir::Node::TraitItem(hir::TraitItem {
36093611
generics,

tests/ui/associated-types/issue-63593.current.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
44
LL | type This = Self;
55
| ^^^^ doesn't have a size known at compile-time
66
|
7-
note: required by a bound in `MyTrait::This`
7+
note: required by an implicit `Sized` bound in `MyTrait::This`
88
--> $DIR/issue-63593.rs:13:5
99
|
1010
LL | type This = Self;
11-
| ^^^^^^^^^^^^^^^^^ required by this bound in `MyTrait::This`
11+
| ^^^^^^^^^^^^^^^^^ required by the implicit `Sized` requirement on this associated type in `MyTrait::This`
1212
help: consider further restricting `Self`
1313
|
1414
LL | trait MyTrait: Sized {

tests/ui/associated-types/issue-63593.next.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
44
LL | type This = Self;
55
| ^^^^ doesn't have a size known at compile-time
66
|
7-
note: required by a bound in `MyTrait::This`
7+
note: required by an implicit `Sized` bound in `MyTrait::This`
88
--> $DIR/issue-63593.rs:13:5
99
|
1010
LL | type This = Self;
11-
| ^^^^^^^^^^^^^^^^^ required by this bound in `MyTrait::This`
11+
| ^^^^^^^^^^^^^^^^^ required by the implicit `Sized` requirement on this associated type in `MyTrait::This`
1212
help: consider further restricting `Self`
1313
|
1414
LL | trait MyTrait: Sized {

tests/ui/drop/dropck-normalize-errors.nll.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ note: required because it appears within the type `BDecoder`
4747
|
4848
LL | pub struct BDecoder {
4949
| ^^^^^^^^
50-
note: required by a bound in `Decode::Decoder`
50+
note: required by an implicit `Sized` bound in `Decode::Decoder`
5151
--> $DIR/dropck-normalize-errors.rs:8:5
5252
|
5353
LL | type Decoder;
54-
| ^^^^^^^^^^^^^ required by this bound in `Decode::Decoder`
54+
| ^^^^^^^^^^^^^ required by the implicit `Sized` requirement on this associated type in `Decode::Decoder`
5555
help: consider relaxing the implicit `Sized` restriction
5656
|
5757
LL | type Decoder: ?Sized;

tests/ui/drop/dropck-normalize-errors.polonius.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ note: required because it appears within the type `BDecoder`
4747
|
4848
LL | pub struct BDecoder {
4949
| ^^^^^^^^
50-
note: required by a bound in `Decode::Decoder`
50+
note: required by an implicit `Sized` bound in `Decode::Decoder`
5151
--> $DIR/dropck-normalize-errors.rs:8:5
5252
|
5353
LL | type Decoder;
54-
| ^^^^^^^^^^^^^ required by this bound in `Decode::Decoder`
54+
| ^^^^^^^^^^^^^ required by the implicit `Sized` requirement on this associated type in `Decode::Decoder`
5555
help: consider relaxing the implicit `Sized` restriction
5656
|
5757
LL | type Decoder: ?Sized;

tests/ui/dyn-compatibility/assoc_type_bounds_implicit_sized.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ LL | type Item = dyn Trait;
55
| ^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Trait + 'static)`
8-
note: required by a bound in `TraitWithAType::Item`
8+
note: required by an implicit `Sized` bound in `TraitWithAType::Item`
99
--> $DIR/assoc_type_bounds_implicit_sized.rs:4:5
1010
|
1111
LL | type Item;
12-
| ^^^^^^^^^^ required by this bound in `TraitWithAType::Item`
12+
| ^^^^^^^^^^ required by the implicit `Sized` requirement on this associated type in `TraitWithAType::Item`
1313
help: consider relaxing the implicit `Sized` restriction
1414
|
1515
LL | type Item: ?Sized;

0 commit comments

Comments
 (0)