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.
Detect when
'static
obligation might come from an impl
Address rust-lang#71341.
- Loading branch information
Showing
13 changed files
with
431 additions
and
302 deletions.
There are no files selected for viewing
465 changes: 270 additions & 195 deletions
465
src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs
Large diffs are not rendered by default.
Oops, something went wrong.
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
23 changes: 4 additions & 19 deletions
23
src/test/ui/associated-types/cache/project-fn-ret-contravariant.transmute.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 |
---|---|---|
@@ -1,26 +1,11 @@ | ||
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements | ||
error[E0759]: cannot infer an appropriate lifetime | ||
--> $DIR/project-fn-ret-contravariant.rs:38:8 | ||
| | ||
LL | bar(foo, x) | ||
| ^^^ | ||
| | ||
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the function body at 37:8... | ||
--> $DIR/project-fn-ret-contravariant.rs:37:8 | ||
| | ||
LL | fn baz<'a,'b>(x: &'a u32) -> &'static u32 { | ||
| ^^ | ||
note: ...so that reference does not outlive borrowed content | ||
--> $DIR/project-fn-ret-contravariant.rs:38:13 | ||
| | ||
LL | bar(foo, x) | ||
| ^ | ||
= note: but, the lifetime must be valid for the static lifetime... | ||
note: ...so that reference does not outlive borrowed content | ||
--> $DIR/project-fn-ret-contravariant.rs:38:4 | ||
| | ||
| ------- this data with lifetime `'a`... | ||
LL | bar(foo, x) | ||
| ^^^^^^^^^^^ | ||
| ----^^^---- ...is captured and required to live as long as `'static` here | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0495`. | ||
For more information about this error, try `rustc --explain E0759`. |
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
28 changes: 5 additions & 23 deletions
28
src/test/ui/associated-types/cache/project-fn-ret-invariant.transmute.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 |
---|---|---|
@@ -1,30 +1,12 @@ | ||
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements | ||
error[E0759]: cannot infer an appropriate lifetime | ||
--> $DIR/project-fn-ret-invariant.rs:49:9 | ||
| | ||
LL | bar(foo, x) | ||
| ^^^ | ||
| | ||
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the function body at 45:8... | ||
--> $DIR/project-fn-ret-invariant.rs:45:8 | ||
| | ||
LL | fn baz<'a, 'b>(x: Type<'a>) -> Type<'static> { | ||
| ^^ | ||
note: ...so that the expression is assignable | ||
--> $DIR/project-fn-ret-invariant.rs:49:14 | ||
| | ||
LL | bar(foo, x) | ||
| ^ | ||
= note: expected `Type<'_>` | ||
found `Type<'a>` | ||
= note: but, the lifetime must be valid for the static lifetime... | ||
note: ...so that the expression is assignable | ||
--> $DIR/project-fn-ret-invariant.rs:49:5 | ||
| | ||
| -------- this data with lifetime `'a`... | ||
... | ||
LL | bar(foo, x) | ||
| ^^^^^^^^^^^ | ||
= note: expected `Type<'static>` | ||
found `Type<'_>` | ||
| ----^^^---- ...is captured and required to live as long as `'static` here | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0495`. | ||
For more information about this error, try `rustc --explain E0759`. |
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 |
---|---|---|
@@ -1,30 +1,17 @@ | ||
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements | ||
error[E0759]: cannot infer an appropriate lifetime | ||
--> $DIR/dyn-trait.rs:20:16 | ||
| | ||
LL | static_val(x); | ||
| ^ | ||
| | ||
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the function body at 19:26... | ||
--> $DIR/dyn-trait.rs:19:26 | ||
| | ||
LL | fn with_dyn_debug_static<'a>(x: Box<dyn Debug + 'a>) { | ||
| ^^ | ||
note: ...so that the expression is assignable | ||
--> $DIR/dyn-trait.rs:20:16 | ||
| | ||
| ------------------- this data with lifetime `'a`... | ||
LL | static_val(x); | ||
| ^ | ||
= note: expected `std::boxed::Box<dyn std::fmt::Debug>` | ||
found `std::boxed::Box<(dyn std::fmt::Debug + 'a)>` | ||
= note: but, the lifetime must be valid for the static lifetime... | ||
note: ...so that the types are compatible | ||
| ^ ...is captured here... | ||
| | ||
note: ...and is required to live as long as `'static` here | ||
--> $DIR/dyn-trait.rs:20:5 | ||
| | ||
LL | static_val(x); | ||
| ^^^^^^^^^^ | ||
= note: expected `StaticTrait` | ||
found `StaticTrait` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0495`. | ||
For more information about this error, try `rustc --explain E0759`. |
25 changes: 4 additions & 21 deletions
25
src/test/ui/nll/user-annotations/constant-in-expr-inherent-1.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 |
---|---|---|
@@ -1,28 +1,11 @@ | ||
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements | ||
error[E0759]: cannot infer an appropriate lifetime | ||
--> $DIR/constant-in-expr-inherent-1.rs:8:5 | ||
| | ||
LL | <Foo<'a>>::C | ||
| ^^^^^^^^^^^^ | ||
| | ||
note: first, the lifetime cannot outlive the lifetime `'a` as defined on the function body at 7:8... | ||
--> $DIR/constant-in-expr-inherent-1.rs:7:8 | ||
| | ||
LL | fn foo<'a>(_: &'a u32) -> &'static u32 { | ||
| ^^ | ||
note: ...so that the types are compatible | ||
--> $DIR/constant-in-expr-inherent-1.rs:8:5 | ||
| | ||
LL | <Foo<'a>>::C | ||
| ^^^^^^^^^^^^ | ||
= note: expected `Foo<'_>` | ||
found `Foo<'a>` | ||
= note: but, the lifetime must be valid for the static lifetime... | ||
note: ...so that reference does not outlive borrowed content | ||
--> $DIR/constant-in-expr-inherent-1.rs:8:5 | ||
| | ||
| ------- this data with lifetime `'a`... | ||
LL | <Foo<'a>>::C | ||
| ^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^ ...is captured and required to live as long as `'static` here | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0495`. | ||
For more information about this error, try `rustc --explain E0759`. |
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 |
---|---|---|
@@ -1,29 +1,11 @@ | ||
error[E0495]: cannot infer an appropriate lifetime for borrow expression due to conflicting requirements | ||
error[E0759]: cannot infer an appropriate lifetime | ||
--> $DIR/regions-addr-of-self.rs:7:37 | ||
| | ||
LL | pub fn chase_cat(&mut self) { | ||
| --------- this data with an anonymous lifetime `'_`... | ||
LL | let p: &'static mut usize = &mut self.cats_chased; | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 6:5... | ||
--> $DIR/regions-addr-of-self.rs:6:5 | ||
| | ||
LL | / pub fn chase_cat(&mut self) { | ||
LL | | let p: &'static mut usize = &mut self.cats_chased; | ||
LL | | *p += 1; | ||
LL | | } | ||
| |_____^ | ||
note: ...so that reference does not outlive borrowed content | ||
--> $DIR/regions-addr-of-self.rs:7:37 | ||
| | ||
LL | let p: &'static mut usize = &mut self.cats_chased; | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
= note: but, the lifetime must be valid for the static lifetime... | ||
note: ...so that reference does not outlive borrowed content | ||
--> $DIR/regions-addr-of-self.rs:7:37 | ||
| | ||
LL | let p: &'static mut usize = &mut self.cats_chased; | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| ^^^^^^^^^^^^^^^^^^^^^ ...is captured and required to live as long as `'static` here | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0495`. | ||
For more information about this error, try `rustc --explain E0759`. |
14 changes: 14 additions & 0 deletions
14
src/test/ui/suggestions/impl-on-dyn-trait-with-implicit-static-bound-without-suggestion.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,14 @@ | ||
trait OtherTrait<'a> {} | ||
impl<'a> OtherTrait<'a> for &'a () {} | ||
|
||
trait ObjectTrait {} | ||
|
||
impl dyn ObjectTrait { | ||
fn use_self(&self) -> &() { panic!() } | ||
} | ||
|
||
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a { | ||
val.use_self() //~ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
18 changes: 18 additions & 0 deletions
18
...est/ui/suggestions/impl-on-dyn-trait-with-implicit-static-bound-without-suggestion.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,18 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-without-suggestion.rs:11:9 | ||
| | ||
LL | val.use_self() | ||
| ^^^^^^^^ lifetime mismatch | ||
| | ||
= note: expected reference `&(dyn ObjectTrait + 'static)` | ||
found reference `&(dyn ObjectTrait + 'a)` | ||
note: the lifetime `'a` as defined on the function body at 10:11... | ||
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound-without-suggestion.rs:10:11 | ||
| | ||
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a { | ||
| ^^ | ||
= note: ...does not necessarily outlive the static lifetime | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
37 changes: 37 additions & 0 deletions
37
src/test/ui/suggestions/impl-on-dyn-trait-with-implicit-static-bound.fixed
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,37 @@ | ||
// run-rustfix | ||
#![allow(dead_code)] | ||
|
||
mod foo { | ||
trait OtherTrait<'a> {} | ||
impl<'a> OtherTrait<'a> for &'a () {} | ||
|
||
trait ObjectTrait {} | ||
trait MyTrait { | ||
fn use_self(&self) -> &(); | ||
} | ||
|
||
impl MyTrait for dyn ObjectTrait + '_ { | ||
fn use_self(&self) -> &() { panic!() } | ||
} | ||
|
||
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a { | ||
val.use_self() //~ ERROR cannot infer an appropriate lifetime | ||
} | ||
} | ||
|
||
mod bar { | ||
trait ObjectTrait {} | ||
trait MyTrait { | ||
fn use_self(&self) -> &(); | ||
} | ||
|
||
impl MyTrait for dyn ObjectTrait + '_ { | ||
fn use_self(&self) -> &() { panic!() } | ||
} | ||
|
||
fn use_it<'a>(val: &'a dyn ObjectTrait) -> &'a () { | ||
val.use_self() //~ ERROR cannot infer an appropriate lifetime | ||
} | ||
} | ||
|
||
fn main() {} |
37 changes: 37 additions & 0 deletions
37
src/test/ui/suggestions/impl-on-dyn-trait-with-implicit-static-bound.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,37 @@ | ||
// run-rustfix | ||
#![allow(dead_code)] | ||
|
||
mod foo { | ||
trait OtherTrait<'a> {} | ||
impl<'a> OtherTrait<'a> for &'a () {} | ||
|
||
trait ObjectTrait {} | ||
trait MyTrait { | ||
fn use_self(&self) -> &(); | ||
} | ||
|
||
impl MyTrait for dyn ObjectTrait { | ||
fn use_self(&self) -> &() { panic!() } | ||
} | ||
|
||
fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a { | ||
val.use_self() //~ ERROR cannot infer an appropriate lifetime | ||
} | ||
} | ||
|
||
mod bar { | ||
trait ObjectTrait {} | ||
trait MyTrait { | ||
fn use_self(&self) -> &(); | ||
} | ||
|
||
impl MyTrait for dyn ObjectTrait { | ||
fn use_self(&self) -> &() { panic!() } | ||
} | ||
|
||
fn use_it<'a>(val: &'a dyn ObjectTrait) -> &'a () { | ||
val.use_self() //~ ERROR cannot infer an appropriate lifetime | ||
} | ||
} | ||
|
||
fn main() {} |
29 changes: 29 additions & 0 deletions
29
src/test/ui/suggestions/impl-on-dyn-trait-with-implicit-static-bound.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,29 @@ | ||
error[E0759]: cannot infer an appropriate lifetime | ||
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:18:13 | ||
| | ||
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> impl OtherTrait<'a> + 'a { | ||
| ------------------- this data with lifetime `'a`... | ||
LL | val.use_self() | ||
| ^^^^^^^^ ...is captured and required to live as long as `'static` here | ||
| | ||
help: this `impl` introduces an implicit `'static` requirement, consider changing it | ||
| | ||
LL | impl MyTrait for dyn ObjectTrait + '_ { | ||
| ^^^^ | ||
|
||
error[E0759]: cannot infer an appropriate lifetime | ||
--> $DIR/impl-on-dyn-trait-with-implicit-static-bound.rs:33:13 | ||
| | ||
LL | fn use_it<'a>(val: &'a dyn ObjectTrait) -> &'a () { | ||
| ------------------- this data with lifetime `'a`... | ||
LL | val.use_self() | ||
| ^^^^^^^^ ...is captured and required to live as long as `'static` here | ||
| | ||
help: this `impl` introduces an implicit `'static` requirement, consider changing it | ||
| | ||
LL | impl MyTrait for dyn ObjectTrait + '_ { | ||
| ^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0759`. |