-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
5
commits into
rust-lang:master
Choose a base branch
from
fmease:obj-lt-def-gat
base: master
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.
+482
−251
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ac1b201
Propagate the object lifetime defaults of GAT's own params
fmease 0d034e1
Don't needlessly search for already-found HIR generic param
fmease a0c12b4
Propagate the object lifetime default to the self ty of resolved proj…
fmease b31ee55
Tweak output of `#[rustc_object_lifetime_default]`
fmease 352dfcb
Compute object lifetime defaults for type-relative assoc ty paths, too
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
552 changes: 334 additions & 218 deletions
552
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
13 changes: 13 additions & 0 deletions
13
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,13 @@ | ||
// 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`. | ||
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() {} |
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
// 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>) {} | ||
} | ||
|
||
fn main() {} |
12 changes: 12 additions & 0 deletions
12
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,12 @@ | ||
// Properly deduce the object lifetime default in type-relative generic associated type *paths*. | ||
// issue: <https://github.com/rust-lang/rust/issues/115379> | ||
//@ check-pass | ||
|
||
trait Outer { type Ty<'a, T: 'a + ?Sized>; } | ||
trait Inner {} | ||
|
||
fn f<'r, T: Outer>(x: T::Ty<'r, dyn Inner + 'r>) { g::<T>(x) } | ||
// Deduce `dyn Inner + 'r` from bound `'a` on ty param `T` of assoc ty `Ty` | ||
fn g<'r, T: Outer>(x: T::Ty<'r, dyn Inner>) {} | ||
|
||
fn main() {} |
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.