-
Notifications
You must be signed in to change notification settings - Fork 13k
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
type alias impl trait: add tests showing that hidden type only outlives lifetimes that occur in bounds #103876
Merged
Merged
Changes from all commits
Commits
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 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
25 changes: 25 additions & 0 deletions
25
src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds.rs
This file contains 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 @@ | ||
// check-pass | ||
|
||
#![feature(type_alias_impl_trait)] | ||
|
||
trait Callable { | ||
type Output; | ||
fn call() -> Self::Output; | ||
} | ||
|
||
impl<'a> Callable for &'a () { | ||
type Output = impl Sized; | ||
fn call() -> Self::Output {} | ||
} | ||
|
||
fn test<'a>() -> impl Sized { | ||
<&'a () as Callable>::call() | ||
} | ||
|
||
fn want_static<T: 'static>(_: T) {} | ||
|
||
fn test2<'a>() { | ||
want_static(<&'a () as Callable>::call()); | ||
} | ||
|
||
fn main() {} |
38 changes: 38 additions & 0 deletions
38
src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.rs
This file contains 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,38 @@ | ||
#![feature(type_alias_impl_trait)] | ||
|
||
trait Callable { | ||
type Output; | ||
fn call(x: Self) -> Self::Output; | ||
} | ||
|
||
trait PlusOne { | ||
fn plus_one(&mut self); | ||
} | ||
|
||
impl<'a> PlusOne for &'a mut i32 { | ||
fn plus_one(&mut self) { | ||
**self += 1; | ||
} | ||
} | ||
|
||
impl<T: PlusOne> Callable for T { | ||
type Output = impl PlusOne; | ||
fn call(t: T) -> Self::Output { t } | ||
} | ||
|
||
fn test<'a>(y: &'a mut i32) -> impl PlusOne { | ||
<&'a mut i32 as Callable>::call(y) | ||
//~^ ERROR hidden type for `impl PlusOne` captures lifetime that does not appear in bounds | ||
} | ||
|
||
fn main() { | ||
let mut z = 42; | ||
let mut thing = test(&mut z); | ||
let mut thing2 = test(&mut z); | ||
thing.plus_one(); | ||
assert_eq!(z, 43); | ||
thing2.plus_one(); | ||
assert_eq!(z, 44); | ||
thing.plus_one(); | ||
assert_eq!(z, 45); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/test/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.stderr
This file contains 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,16 @@ | ||
error[E0700]: hidden type for `impl PlusOne` captures lifetime that does not appear in bounds | ||
--> $DIR/imply_bounds_from_bounds_param.rs:24:5 | ||
| | ||
LL | fn test<'a>(y: &'a mut i32) -> impl PlusOne { | ||
| -- hidden type `<&'a mut i32 as Callable>::Output` captures the lifetime `'a` as defined here | ||
LL | <&'a mut i32 as Callable>::call(y) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: to declare that `impl PlusOne` captures `'a`, you can add an explicit `'a` lifetime bound | ||
| | ||
LL | fn test<'a>(y: &'a mut i32) -> impl PlusOne + 'a { | ||
| ++++ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0700`. |
This file contains 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,38 @@ | ||
// check-pass | ||
|
||
#![feature(type_alias_impl_trait)] | ||
fn foo() { | ||
struct Foo<'a> { | ||
x: &'a mut u8, | ||
} | ||
impl<'a> Foo<'a> { | ||
fn foo(&self) -> impl Sized {} | ||
} | ||
// use site | ||
let mut x = 5; | ||
let y = Foo { x: &mut x }; | ||
let z = y.foo(); | ||
let _a = &x; // invalidate the `&'a mut`in `y` | ||
let _b = z; // this should *not* check that `'a` in the type `Foo<'a>::foo::opaque` is live | ||
} | ||
|
||
fn bar() { | ||
struct Foo<'a> { | ||
x: &'a mut u8, | ||
} | ||
|
||
// desugared | ||
type FooX<'a> = impl Sized; | ||
impl<'a> Foo<'a> { | ||
fn foo(&self) -> FooX<'a> {} | ||
} | ||
|
||
// use site | ||
let mut x = 5; | ||
let y = Foo { x: &mut x }; | ||
let z = y.foo(); | ||
let _a = &x; // invalidate the `&'a mut`in `y` | ||
let _b = z; // this should *not* check that `'a` in the type `Foo<'a>::foo::opaque` is live | ||
} | ||
|
||
fn main() {} |
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.
that comment seems wrong? If you have
FooX<'a>
with an explicit'a
the error feels correct? 🤔can you add a comment to the top of the file explaining what we're testing here?
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.
that's the thing... lifetimes on type aliases should not matter for anything. Only lifetimes in the aliased type. Once/if we avoid adding uncaptured lifetimes to opaque types, this would automatically end up compiling. It may end up working with #103491 or with that plus some small changes.