-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
rustdoc: correctly deal with self ty params when eliding default object lifetimes #115276
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 |
---|---|---|
|
@@ -1959,31 +1959,44 @@ fn can_elide_trait_object_lifetime_bound<'tcx>( | |
#[derive(Debug)] | ||
pub(crate) enum ContainerTy<'tcx> { | ||
Ref(ty::Region<'tcx>), | ||
Regular { ty: DefId, args: ty::Binder<'tcx, &'tcx [ty::GenericArg<'tcx>]>, arg: usize }, | ||
Regular { | ||
ty: DefId, | ||
args: ty::Binder<'tcx, &'tcx [ty::GenericArg<'tcx>]>, | ||
has_self: bool, | ||
arg: usize, | ||
}, | ||
} | ||
|
||
impl<'tcx> ContainerTy<'tcx> { | ||
fn object_lifetime_default(self, tcx: TyCtxt<'tcx>) -> ObjectLifetimeDefault<'tcx> { | ||
match self { | ||
Self::Ref(region) => ObjectLifetimeDefault::Arg(region), | ||
Self::Regular { ty: container, args, arg: index } => { | ||
Self::Regular { ty: container, args, has_self, arg: index } => { | ||
let (DefKind::Struct | ||
| DefKind::Union | ||
| DefKind::Enum | ||
| DefKind::TyAlias { .. } | ||
| DefKind::Trait | ||
| DefKind::AssocTy | ||
| DefKind::Variant) = tcx.def_kind(container) | ||
| DefKind::Trait) = tcx.def_kind(container) | ||
else { | ||
return ObjectLifetimeDefault::Empty; | ||
}; | ||
|
||
let generics = tcx.generics_of(container); | ||
let param = generics.params[index].def_id; | ||
let default = tcx.object_lifetime_default(param); | ||
debug_assert_eq!(generics.parent_count, 0); | ||
|
||
// If the container is a trait object type, the arguments won't contain the self type but the | ||
// generics of the corresponding trait will. In such a case, offset the index by one. | ||
// For comparison, if the container is a trait inside a bound, the arguments do contain the | ||
// self type. | ||
let offset = | ||
if !has_self && generics.parent.is_none() && generics.has_self { 1 } else { 0 }; | ||
let param = generics.params[index + offset].def_id; | ||
Comment on lines
+1991
to
+1993
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 here is the most important part of the PR. It fixes the off-by-one error that leads to the ICE, see also #115276 (comment). Basically, if the container type is a trait object type (e.g. in If the 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. Please add this explanation as a comment. |
||
|
||
let default = tcx.object_lifetime_default(param); | ||
match default { | ||
rbv::ObjectLifetimeDefault::Param(lifetime) => { | ||
// The index is relative to the parent generics but since we don't have any, | ||
// we don't need to translate it. | ||
let index = generics.param_def_id_to_index[&lifetime]; | ||
let arg = args.skip_binder()[index as usize].expect_region(); | ||
ObjectLifetimeDefault::Arg(arg) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,22 @@ pub trait HigherRankedBoundTrait1<'e> where for<'l> Self: 'e + 'l {} | |
pub trait AmbiguousBoundTrait<'a, 'b>: 'a + 'b {} | ||
|
||
pub struct AmbiguousBoundWrapper<'a, 'b, T: ?Sized + 'a + 'b>(&'a T, &'b T); | ||
|
||
// Trait objects inside of another trait object, a trait bound or an associated type. | ||
|
||
pub trait Inner {} | ||
pub trait Outer<T: ?Sized> {} | ||
pub trait Base { | ||
type Type<T: ?Sized>; | ||
} | ||
impl Base for () { | ||
type Type<T: ?Sized> = (); | ||
} | ||
|
||
pub type NestedTraitObjects = dyn Outer<dyn Inner>; | ||
|
||
pub fn apit_rpit(o: impl Outer<dyn Inner>) -> impl Outer<dyn Inner> { | ||
o | ||
} | ||
|
||
pub type AssocTy = <() as Base>::Type<dyn Inner>; | ||
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 still correctly elides the #115379 is about the lifetime constraints on the assoc ty itself which rustc doesn't respect (and rustdoc now doesn't either for now). |
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.
I've removed
AssocTy
for now (as well as thoseSome(assoc_ty.def_id)
s) since rustc doesn't handle them either. I have investigated #115379 a bit and the compiler just skips them, too. Still unclear if it's a bug / oversight (GATs are younger than object lifetime defaults, so it's possible) or intentional.Once this question has been resolved, I can properly implement elision in rustdoc for them, too.
Variant
was never correct.Btw, here is the corresponding rustc code:
rust/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Lines 1476 to 1488 in 784916c