-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
Do not add implied outlives bounds containing external regions/params only #153027
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
Draft
ShoyuVanilla
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
ShoyuVanilla:external-implied-bounds
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.
Draft
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 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
29 changes: 29 additions & 0 deletions
29
tests/ui/implied-bounds/implied-bounds-on-external-params-1.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,29 @@ | ||
| // A regression test for https://github.com/rust-lang/rust/issues/151637. | ||
|
|
||
| struct Wrap<T: 'static>(T); | ||
|
|
||
| fn error1<T>(x: T) { | ||
| Wrap(x); | ||
| //~^ ERROR: the parameter type `T` may not live long enough | ||
| } | ||
|
|
||
| // We used to add implied bound `T: 'static` from the closure's return type | ||
| // when borrow checking the closure, which resulted in allowing non-wf return | ||
| // type. Implied bounds which contains only the external regions or type params | ||
| // from the parents should not be implied. That would make those bounds in needs | ||
| // propagated as closure requirements and either proved or make borrowck error | ||
| // from the parent's body. | ||
| fn error2<T>(x: T) { | ||
| || Wrap(x); | ||
| //~^ ERROR: the parameter type `T` may not live long enough | ||
| } | ||
|
|
||
| fn no_error1<T: 'static>(x: T) { | ||
| || Wrap(x); | ||
| } | ||
|
|
||
| fn no_error2<T>(x: T, _: &'static T) { | ||
| || Wrap(x); | ||
| } | ||
|
|
||
| fn main() {} |
31 changes: 31 additions & 0 deletions
31
tests/ui/implied-bounds/implied-bounds-on-external-params-1.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,31 @@ | ||
| error[E0310]: the parameter type `T` may not live long enough | ||
| --> $DIR/implied-bounds-on-external-params-1.rs:6:5 | ||
| | | ||
| LL | Wrap(x); | ||
| | ^^^^^^^ | ||
| | | | ||
| | the parameter type `T` must be valid for the static lifetime... | ||
| | ...so that the type `T` will meet its required lifetime bounds | ||
| | | ||
| help: consider adding an explicit lifetime bound | ||
| | | ||
| LL | fn error1<T: 'static>(x: T) { | ||
| | +++++++++ | ||
|
|
||
| error[E0310]: the parameter type `T` may not live long enough | ||
| --> $DIR/implied-bounds-on-external-params-1.rs:17:8 | ||
| | | ||
| LL | || Wrap(x); | ||
| | ^^^^^^^ | ||
| | | | ||
| | the parameter type `T` must be valid for the static lifetime... | ||
| | ...so that the type `T` will meet its required lifetime bounds | ||
| | | ||
| help: consider adding an explicit lifetime bound | ||
| | | ||
| LL | fn error2<T: 'static>(x: T) { | ||
| | +++++++++ | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0310`. |
60 changes: 60 additions & 0 deletions
60
tests/ui/implied-bounds/implied-bounds-on-external-params-2.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,60 @@ | ||
| // A regression test for https://github.com/rust-lang/rust/issues/151637. | ||
|
|
||
| type Payload = Box<i32>; | ||
|
|
||
| trait StaticToStatic { | ||
| fn static_to_static(self) -> &'static Payload | ||
| where | ||
| Self: 'static; | ||
| } | ||
| impl<'a> StaticToStatic for &'a Payload { | ||
| fn static_to_static(self) -> &'static Payload | ||
| where | ||
| Self: 'static, | ||
| { | ||
| self // Legal. 'a must be 'static due to Self: 'static | ||
| } | ||
| } | ||
|
|
||
| struct Wrap<T: StaticToStatic + 'static>(T); | ||
|
|
||
| trait ToStatic { | ||
| fn to_static(self) -> &'static Payload; | ||
| } | ||
| impl<T: StaticToStatic> ToStatic for Wrap<T> { | ||
| fn to_static(self) -> &'static Payload { | ||
| self.0.static_to_static() // Legal. T: 'static is implied by Wrap<T> | ||
| } | ||
| } | ||
|
|
||
| // Trait to allow mentioning FnOnce without mentioning the return type directly | ||
| trait MyFnOnce { | ||
| type MyOutput; | ||
| fn my_call_once(self) -> Self::MyOutput; | ||
| } | ||
| impl<F: FnOnce() -> T, T> MyFnOnce for F { | ||
| type MyOutput = T; | ||
| fn my_call_once(self) -> T { | ||
| self() | ||
| } | ||
| } | ||
|
|
||
| fn call<F: MyFnOnce<MyOutput: ToStatic>>(f: F) -> &'static Payload { | ||
| f.my_call_once().to_static() | ||
| } | ||
|
|
||
| fn extend<T: StaticToStatic>(x: T) -> &'static Payload { | ||
| let c = move || { | ||
| // Probably should be illegal, since Wrap requires T: 'static | ||
| Wrap(x) | ||
| //~^ ERROR: the parameter type `T` may not live long enough | ||
| }; | ||
| call(c) | ||
| } | ||
|
|
||
| fn main() { | ||
| let x = Box::new(Box::new(1)); | ||
| let y = extend(&*x); | ||
| drop(x); | ||
| println!("{y}"); | ||
| } |
17 changes: 17 additions & 0 deletions
17
tests/ui/implied-bounds/implied-bounds-on-external-params-2.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,17 @@ | ||
| error[E0310]: the parameter type `T` may not live long enough | ||
| --> $DIR/implied-bounds-on-external-params-2.rs:49:9 | ||
| | | ||
| LL | Wrap(x) | ||
| | ^^^^^^^ | ||
| | | | ||
| | the parameter type `T` must be valid for the static lifetime... | ||
| | ...so that the type `T` will meet its required lifetime bounds | ||
| | | ||
| help: consider adding an explicit lifetime bound | ||
| | | ||
| LL | fn extend<T: StaticToStatic + 'static>(x: T) -> &'static Payload { | ||
| | +++++++++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0310`. |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Filtering external bounds here regresses this
rust/tests/ui/impl-trait/issues/issue-104815.rs
Line 63 in eeb94be