forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#71896 - spastorino:existential-assoc-types-va…
…riance, r=nikomatsakis Relate existential associated types with variance Invariant Fixes rust-lang#71550 rust-lang#72315 r? @nikomatsakis The test case reported in that issue now errors with the following message ... ``` error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 'a in function call due to conflicting requirements --> /tmp/test.rs:25:5 | 25 | bad(&Bar(PhantomData), x) | ^^^^^^^^^^^^^^^^^^^^^^^^^ | note: first, the lifetime cannot outlive the lifetime `'a` as defined on the function body at 24:11... --> /tmp/test.rs:24:11 | 24 | fn extend<'a, T>(x: &'a T) -> &'static T { | ^^ note: ...so that reference does not outlive borrowed content --> /tmp/test.rs:25:28 | 25 | bad(&Bar(PhantomData), x) | ^ = note: but, the lifetime must be valid for the static lifetime... note: ...so that the types are compatible --> /tmp/test.rs:25:9 | 25 | bad(&Bar(PhantomData), x) | ^^^^^^^^^^^^^^^^^ = note: expected `&'static T` found `&T` error: aborting due to previous error For more information about this error, try `rustc --explain E0495`. ``` I could also add that test case if we want to have a weaponized one too.
- Loading branch information
Showing
6 changed files
with
52 additions
and
5 deletions.
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
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
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
12 changes: 12 additions & 0 deletions
12
src/test/ui/variance/variance-associated-types2.nll.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,12 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/variance-associated-types2.rs:13:12 | ||
| | ||
LL | fn take<'a>(_: &'a u32) { | ||
| -- lifetime `'a` defined here | ||
LL | let _: Box<dyn Foo<Bar = &'a u32>> = make(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static` | ||
| | ||
= help: consider replacing `'a` with `'static` | ||
|
||
error: aborting due to previous error | ||
|
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,17 @@ | ||
// Test that dyn Foo<Bar = T> is invariant with respect to T. | ||
// Failure to enforce invariance here can be weaponized, see #71550 for details. | ||
|
||
trait Foo { | ||
type Bar; | ||
} | ||
|
||
fn make() -> Box<dyn Foo<Bar = &'static u32>> { | ||
panic!() | ||
} | ||
|
||
fn take<'a>(_: &'a u32) { | ||
let _: Box<dyn Foo<Bar = &'a u32>> = make(); | ||
//~^ ERROR mismatched types [E0308] | ||
} | ||
|
||
fn main() {} |
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,18 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/variance-associated-types2.rs:13:42 | ||
| | ||
LL | let _: Box<dyn Foo<Bar = &'a u32>> = make(); | ||
| ^^^^^^ lifetime mismatch | ||
| | ||
= note: expected trait object `dyn Foo<Bar = &'a u32>` | ||
found trait object `dyn Foo<Bar = &'static u32>` | ||
note: the lifetime `'a` as defined on the function body at 12:9... | ||
--> $DIR/variance-associated-types2.rs:12:9 | ||
| | ||
LL | fn take<'a>(_: &'a u32) { | ||
| ^^ | ||
= note: ...does not necessarily outlive the static lifetime | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |