-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Properly deduce object lifetime defaults in projections & trait refs #129543
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
Open
fmease
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
fmease:obj-lt-def-gat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+460
−186
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7a89da3
Propagate the object lifetime defaults of GAT's own params
fmease 30ad2c1
Don't needlessly search for already-found HIR generic param
fmease 0f8c502
Propagate the object lifetime default to the self ty of resolved proj…
fmease 0f27113
Tweak `#[rustc_object_lifetime_default]`
fmease File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
391 changes: 241 additions & 150 deletions
391
compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound | ||
| --> $DIR/issue-89188-gat-hrtb.rs:27:56 | ||
| | | ||
| LL | &'s mut T::Shim<dyn for<'t> Fn(&'s mut T::Shim<dyn for<'u> Trait<'s, 't, 'u>>)>, | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound | ||
| --> $DIR/issue-89188-gat-hrtb.rs:27:56 | ||
| | | ||
| LL | &'s mut T::Shim<dyn for<'t> Fn(&'s mut T::Shim<dyn for<'u> Trait<'s, 't, 'u>>)>, | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
|
||
| error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound | ||
| --> $DIR/issue-89188-gat-hrtb.rs:27:56 | ||
| | | ||
| LL | &'s mut T::Shim<dyn for<'t> Fn(&'s mut T::Shim<dyn for<'u> Trait<'s, 't, 'u>>)>, | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0228`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/ui/object-lifetime/object-lifetime-default-assoc-ty-self-ty-static.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Check that we correctly deduce object lifetime defaults inside self types of qualified paths. | ||
| //@ check-pass | ||
|
|
||
| trait Outer { type Ty; } | ||
| trait Inner {} | ||
|
|
||
| impl<'a> Outer for dyn Inner + 'a { type Ty = &'a (); } | ||
|
|
||
| // We deduce `dyn Inner + 'static` from absence of any bounds on self ty param of trait `Outer`. | ||
| // | ||
| // Prior to PR rust-lang/rust#129543, assoc tys weren't considered *eligible generic containers* and | ||
| // thus we'd use the *ambient object lifetime default* induced by the reference type ctor `&`, | ||
| // namely `'r`. Now however, the assoc ty shadows/overwrites that ambient default to `'static`. | ||
| fn f<'r>(x: &'r <dyn Inner as Outer>::Ty) { g(x) } | ||
| fn g<'r>(x: &'r <dyn Inner + 'static as Outer>::Ty) {} | ||
|
|
||
| fn main() {} |
15 changes: 15 additions & 0 deletions
15
tests/ui/object-lifetime/object-lifetime-default-assoc-ty-self-ty.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Check that we correctly deduce object lifetime defaults inside self types of qualified paths. | ||
| //@ check-pass | ||
| //@ revisions: bound clause | ||
|
|
||
| #[cfg(bound)] trait Outer<'a>: 'a { type Ty; } | ||
| #[cfg(clause)] trait Outer<'a> where Self: 'a { type Ty; } | ||
| trait Inner {} | ||
|
|
||
| impl<'a> Outer<'a> for dyn Inner + 'a { type Ty = &'a (); } | ||
|
|
||
| fn f<'r>(x: <dyn Inner + 'r as Outer<'r>>::Ty) { g(x) } | ||
| // We deduce `dyn Inner + 'r` from bound `'a` on self ty param of trait `Outer`. | ||
| fn g<'r>(x: <dyn Inner as Outer<'r>>::Ty) {} | ||
|
|
||
| fn main() {} |
57 changes: 57 additions & 0 deletions
57
tests/ui/object-lifetime/object-lifetime-default-gat-resolved.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Check that we correctly deduce object lifetime defaults inside resolved GAT *paths*. | ||
| // issue: <https://github.com/rust-lang/rust/issues/115379> | ||
| //@ check-pass | ||
|
|
||
| mod own { // the object lifetime default comes from the own generics | ||
| trait Outer { | ||
| type Ty<'a, T: ?Sized + 'a>; | ||
| } | ||
| impl Outer for () { | ||
| type Ty<'a, T: ?Sized + 'a> = &'a T; | ||
| } | ||
| trait Inner {} | ||
|
|
||
| fn f<'r>(x: <() as Outer>::Ty<'r, dyn Inner + 'r>) { g(x) } | ||
| // We deduce `dyn Inner + 'r` from bound `'a` on ty param `T` of assoc ty `Ty`. | ||
| fn g<'r>(_: <() as Outer>::Ty<'r, dyn Inner>) {} | ||
| } | ||
|
|
||
| mod parent { // the object lifetime default comes from the parent generics | ||
| trait Outer<'a> { | ||
| type Ty<T: ?Sized + 'a>; | ||
| } | ||
| impl<'a> Outer<'a> for () { | ||
| type Ty<T: ?Sized + 'a> = &'a T; | ||
| } | ||
| trait Inner {} | ||
|
|
||
| fn f<'r>(x: <() as Outer<'r>>::Ty<dyn Inner + 'r>) { g(x) } | ||
| // We deduce `dyn Inner + 'r` from bound `'a` on ty param `T` of assoc ty `Ty`. | ||
| fn g<'r>(_: <() as Outer<'r>>::Ty<dyn Inner>) {} | ||
| } | ||
|
|
||
| mod complex { | ||
| // We need to perform several delicate index calculations to map between the middle::ty and | ||
| // the HIR representation of generic args. This is a smoke test. | ||
|
|
||
| trait Outer<'_pad0, 'a, '_pad1, _Pad0> { | ||
| type Ty<'_pad2, 'b, '_pad3, _Pad1, T: ?Sized + 'a, _Pad2, U: ?Sized + 'b>; | ||
| } | ||
| impl<'a, _Pad0> Outer<'_, 'a, '_, _Pad0> for () { | ||
| type Ty<'_pad2, 'b, '_pad3, _Pad1, T: ?Sized + 'a, _Pad2, U: ?Sized + 'b> = (&'a T, &'b U); | ||
| } | ||
| trait Inner {} | ||
|
|
||
| fn f<'r, 's>( | ||
| x: <() as Outer<'static, 'r, 'static, ()>>:: | ||
| Ty<'static, 's, 'static, (), dyn Inner + 'r, (), dyn Inner + 's>, | ||
| ) { | ||
| g(x) | ||
| } | ||
| fn g<'r, 's>( | ||
| _: <() as Outer<'static, 'r, 'static, ()>>:: | ||
| Ty<'static, 's, 'static, (), dyn Inner, (), dyn Inner>, | ||
| ) {} | ||
| } | ||
|
|
||
| fn main() {} | ||
10 changes: 10 additions & 0 deletions
10
tests/ui/object-lifetime/object-lifetime-default-gat-type-relative.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| trait Outer { type Ty<'a, T: 'a + ?Sized>; } | ||
| trait Inner {} | ||
|
|
||
| // FIXME: Ideally, we would deduce `dyn Inner + 'r` from bound `'a` on ty param `T` of assoc ty `Ty` | ||
| // but for that we'd need to somehow obtain the resolution of the type-relative path `T::Ty` | ||
| // from HIR ty lowering (it resolves to `<T as Outer>::Ty`). | ||
| fn f<'r, T: Outer>(x: T::Ty<'r, dyn Inner>) {} | ||
| //~^ ERROR lifetime bound for this object type cannot be deduced from context | ||
|
|
||
| fn main() {} |
9 changes: 9 additions & 0 deletions
9
tests/ui/object-lifetime/object-lifetime-default-gat-type-relative.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound | ||
| --> $DIR/object-lifetime-default-gat-type-relative.rs:7:33 | ||
| | | ||
| LL | fn f<'r, T: Outer>(x: T::Ty<'r, dyn Inner>) {} | ||
| | ^^^^^^^^^ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0228`. |
26 changes: 26 additions & 0 deletions
26
tests/ui/object-lifetime/object-lifetime-default-trait-ref.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Check that we correctly deduce object lifetime defaults inside *trait refs*! | ||
| // For the longest time these examples used to get rejected as "inderminate" due to an off-by-one. | ||
| //@ check-pass | ||
|
|
||
| trait Inner {} | ||
| trait Outer<'a, T: 'a + ?Sized> { type Project where Self: Sized; } | ||
|
|
||
| fn bound0<'r, T>() where T: Outer<'r, dyn Inner + 'r> { bound1::<'r, T>() } | ||
| // We deduce `dyn Inner + 'r` from bound `'a` on ty param `T` of trait `Outer`. | ||
| fn bound1<'r, T>() where T: Outer<'r, dyn Inner> {} | ||
|
|
||
| fn dyn0<'r>(x: Box<dyn Outer<'r, dyn Inner + 'r>>) { dyn1(x) } | ||
| // We deduce `dyn Inner + 'r` from bound `'a` on ty param `T` of trait `Outer`. | ||
| fn dyn1<'r>(_: Box<dyn Outer<'r, dyn Inner>>) {} | ||
|
|
||
| fn impl0<'r>(x: impl Outer<'r, dyn Inner + 'r>) { impl1(x) } | ||
| // We deduce `dyn Inner + 'r` from bound `'a` on ty param `T` of trait `Outer`. | ||
| fn impl1<'r>(_: impl Outer<'r, dyn Inner>) {} // deduce `dyn Inner + 'r` | ||
|
|
||
| fn proj<'r>(x: <() as Outer<'r, dyn Inner + 'r>>::Project) { proj1(x) } | ||
| // We deduce `dyn Inner + 'r` from bound `'a` on ty param `T` of trait `Outer`. | ||
| fn proj1<'r>(_: <() as Outer<'r, dyn Inner>>::Project) {} | ||
|
|
||
| impl<'a, T: 'a + ?Sized> Outer<'a, T> for () { type Project = &'a T; } | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Also add a test with both self and parent lifetimes
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.
Do you mean one where the assoc ty is
type AssocTy<'own, T: ?Sized + 'own + 'parent>;ortype AssocTy<'own, T: ?Sized + 'own, U: ?Sized + 'parent>;or something else entirely?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 had in mind just
type AssocTy<'own, T: ?Sized + 'own>(basically identical to the first example, but showing that the extra lifetime doesn't throw it off), and ideally some sort of failure tests of cases that are using the wrong lifetime.