-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
arbitrary_self_type: insert implied Receiver bound on Deref #138952
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ use rustc_hir::def::{DefKind, Res}; | |
use rustc_lint_defs::builtin::UNUSED_ASSOCIATED_TYPE_BOUNDS; | ||
use rustc_middle::ty::elaborate::ClauseWithSupertraitSpan; | ||
use rustc_middle::ty::{ | ||
self, BottomUpFolder, DynKind, ExistentialPredicateStableCmpExt as _, Ty, TyCtxt, TypeFoldable, | ||
TypeVisitableExt, Upcast, | ||
self, BottomUpFolder, DynKind, ExistentialPredicateStableCmpExt as _, Interner as _, Ty, | ||
TyCtxt, TypeFoldable, TypeVisitableExt, Upcast, | ||
}; | ||
use rustc_span::{ErrorGuaranteed, Span}; | ||
use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility; | ||
|
@@ -112,9 +112,29 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { | |
// let _: &dyn Alias<Assoc = u32> = /* ... */; | ||
// ``` | ||
let mut projection_bounds = FxIndexMap::default(); | ||
let mut auto_fill_bounds: SmallVec<[_; 4]> = smallvec![]; | ||
let mut push_auto_fill_receiver_bound = |pred: ty::Binder<'tcx, _>| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit overkill. Just take the projection and replace its def id with the one for At that point, this doesn't need to be a callback, just an additional call to |
||
let receiver_target_did = | ||
tcx.require_lang_item(rustc_hir::LangItem::ReceiverTarget, None); | ||
let receiver_trait_ref = | ||
tcx.anonymize_bound_vars(pred.map_bound(|proj: ty::ProjectionPredicate<'_>| { | ||
tcx.trait_ref_and_own_args_for_alias( | ||
receiver_target_did, | ||
proj.projection_term.args, | ||
) | ||
.0 | ||
})); | ||
let receiver_pred = pred.map_bound(|mut pred| { | ||
pred.projection_term.def_id = receiver_target_did; | ||
pred | ||
}); | ||
auto_fill_bounds.push((receiver_target_did, receiver_trait_ref, receiver_pred, span)); | ||
}; | ||
for (proj, proj_span) in elaborated_projection_bounds { | ||
debug!(?proj, "elaborated proj bound"); | ||
let proj_did = proj.skip_binder().projection_term.def_id; | ||
let key = ( | ||
proj.skip_binder().projection_term.def_id, | ||
proj_did, | ||
tcx.anonymize_bound_vars( | ||
proj.map_bound(|proj| proj.projection_term.trait_ref(tcx)), | ||
), | ||
|
@@ -142,6 +162,9 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { | |
) | ||
.emit(); | ||
} | ||
if tcx.is_lang_item(proj_did, rustc_hir::LangItem::DerefTarget) { | ||
push_auto_fill_receiver_bound(proj); | ||
} | ||
} | ||
|
||
let principal_trait = regular_traits.into_iter().next(); | ||
|
@@ -221,6 +244,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { | |
if !projection_bounds.contains_key(&key) { | ||
projection_bounds.insert(key, (pred, supertrait_span)); | ||
} | ||
if tcx.is_lang_item( | ||
pred.skip_binder().def_id(), | ||
rustc_hir::LangItem::DerefTarget, | ||
) { | ||
// We need to fill in a `Receiver<Target = ?>` bound because | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is missing the more important part that a supertrait like |
||
// neither `Receiver` nor `Deref` is an auto-trait. | ||
push_auto_fill_receiver_bound(pred); | ||
} | ||
} | ||
|
||
self.check_elaborated_projection_mentions_input_lifetimes( | ||
|
@@ -233,6 +264,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { | |
} | ||
} | ||
} | ||
for (did, trait_ref, pred, span) in auto_fill_bounds { | ||
if !projection_bounds.contains_key(&(did, trait_ref)) { | ||
projection_bounds.insert((did, trait_ref), (pred, span)); | ||
} | ||
} | ||
|
||
// `dyn Trait<Assoc = Foo>` desugars to (not Rust syntax) `dyn Trait where | ||
// <Self as Trait>::Assoc = Foo`. So every `Projection` clause is an | ||
|
@@ -264,6 +300,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { | |
} | ||
}) | ||
.collect(); | ||
debug!(?missing_assoc_types, ?projection_bounds); | ||
|
||
if let Err(guar) = self.check_for_required_assoc_tys( | ||
principal_trait.as_ref().map_or(smallvec![], |(_, spans)| spans.clone()), | ||
|
@@ -378,6 +415,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { | |
.collect::<SmallVec<[_; 8]>>(); | ||
v.sort_by(|a, b| a.skip_binder().stable_cmp(tcx, &b.skip_binder())); | ||
let existential_predicates = tcx.mk_poly_existential_predicates(&v); | ||
debug!(?existential_predicates); | ||
|
||
// Use explicitly-specified region bound, unless the bound is missing. | ||
let region_bound = if !lifetime.is_elided() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -721,26 +721,23 @@ impl<'tcx> Ty<'tcx> { | |
) -> Ty<'tcx> { | ||
if cfg!(debug_assertions) { | ||
let projection_count = obj.projection_bounds().count(); | ||
let expected_count: usize = obj | ||
.principal_def_id() | ||
.into_iter() | ||
.flat_map(|principal_def_id| { | ||
// NOTE: This should agree with `needed_associated_types` in | ||
// dyn trait lowering, or else we'll have ICEs. | ||
elaborate::supertraits( | ||
tcx, | ||
ty::Binder::dummy(ty::TraitRef::identity(tcx, principal_def_id)), | ||
) | ||
.map(|principal| { | ||
tcx.associated_items(principal.def_id()) | ||
.in_definition_order() | ||
.filter(|item| item.kind == ty::AssocKind::Type) | ||
.filter(|item| !item.is_impl_trait_in_trait()) | ||
.filter(|item| !tcx.generics_require_sized_self(item.def_id)) | ||
.count() | ||
}) | ||
let expected_count = obj.principal_def_id().map_or(0, |principal_def_id| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this changed? |
||
// NOTE: This should agree with `needed_associated_types` in | ||
// dyn trait lowering, or else we'll have ICEs. | ||
elaborate::supertraits( | ||
tcx, | ||
ty::Binder::dummy(ty::TraitRef::identity(tcx, principal_def_id)), | ||
) | ||
.map(|principal| { | ||
tcx.associated_items(principal.def_id()) | ||
.in_definition_order() | ||
.filter(|item| item.kind == ty::AssocKind::Type) | ||
.filter(|item| !item.is_impl_trait_in_trait()) | ||
.filter(|item| !tcx.generics_require_sized_self(item.def_id)) | ||
.count() | ||
}) | ||
.sum(); | ||
.sum() | ||
}); | ||
assert_eq!( | ||
projection_count, expected_count, | ||
"expected {obj:?} to have {expected_count} projections, \ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you importing
Interner
? There should be no reason to use that trait.