-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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 #120019 - lcnr:fn-wf, r=BoxyUwU
fix fn/const items implied bounds and wf check (rebase) A rebase of #104098, see that PR for discussion. This is pretty much entirely the work of `@aliemjay.` I received his permission for this rebase. --- These are two distinct changes (edit: actually three, see below): 1. Wf-check all fn item args. This is a soundness fix. Fixes #104005 2. Use implied bounds from impl header in borrowck of associated functions/consts. This strictly accepts more code and helps to mitigate the impact of other breaking changes. Fixes #98852 Fixes #102611 The first is a breaking change and will likely have a big impact without the the second one. See the first commit for how it breaks libstd. Landing the second one without the first will allow more incorrect code to pass. For example an exploit of #104005 would be as simple as: ```rust use core::fmt::Display; trait ExtendLt<Witness> { fn extend(self) -> Box<dyn Display>; } impl<T: Display> ExtendLt<&'static T> for T { fn extend(self) -> Box<dyn Display> { Box::new(self) } } fn main() { let val = (&String::new()).extend(); println!("{val}"); } ``` The third change is to to check WF of user type annotations before normalizing them (fixes #104764, fixes #104763). It is mutually dependent on the second change above: an attempt to land it separately in #104746 caused several crater regressions that can all be mitigated by using the implied from the impl header. It is also necessary for the soundness of associated consts that use the implied bounds of impl header. See #104763 and how the third commit fixes the soundness issue in `tests/ui/wf/wf-associated-const.rs` that was introduces by the previous commit. r? types
- Loading branch information
Showing
23 changed files
with
425 additions
and
123 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// The method `assert_static` should be callable only for static values, | ||
// because the impl has an implied bound `where T: 'static`. | ||
|
||
// check-fail | ||
|
||
trait AnyStatic<Witness>: Sized { | ||
fn assert_static(self) {} | ||
} | ||
|
||
impl<T> AnyStatic<&'static T> for T {} | ||
|
||
fn main() { | ||
(&String::new()).assert_static(); | ||
//~^ ERROR temporary value dropped while borrowed | ||
} |
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[E0716]: temporary value dropped while borrowed | ||
--> $DIR/fn-item-check-trait-ref.rs:13:7 | ||
| | ||
LL | (&String::new()).assert_static(); | ||
| --^^^^^^^^^^^^^------------------ temporary value is freed at the end of this statement | ||
| | | | ||
| | creates a temporary value which is freed while still in use | ||
| argument requires that borrow lasts for `'static` | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0716`. |
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,57 @@ | ||
// Regression test for #104005. | ||
// | ||
// Previously, different borrowck implementations used to disagree here. | ||
// The status of each is documented on `fn test_*`. | ||
|
||
// check-fail | ||
|
||
use std::fmt::Display; | ||
|
||
trait Displayable { | ||
fn display(self) -> Box<dyn Display>; | ||
} | ||
|
||
impl<T: Display> Displayable for (T, Option<&'static T>) { | ||
fn display(self) -> Box<dyn Display> { | ||
Box::new(self.0) | ||
} | ||
} | ||
|
||
fn extend_lt<T, U>(val: T) -> Box<dyn Display> | ||
where | ||
(T, Option<U>): Displayable, | ||
{ | ||
Displayable::display((val, None)) | ||
} | ||
|
||
// AST: fail | ||
// HIR: pass | ||
// MIR: pass | ||
pub fn test_call<'a>(val: &'a str) { | ||
extend_lt(val); | ||
//~^ ERROR borrowed data escapes outside of function | ||
} | ||
|
||
// AST: fail | ||
// HIR: fail | ||
// MIR: pass | ||
pub fn test_coercion<'a>() { | ||
let _: fn(&'a str) -> _ = extend_lt; | ||
//~^ ERROR lifetime may not live long enough | ||
} | ||
|
||
// AST: fail | ||
// HIR: fail | ||
// MIR: fail | ||
pub fn test_arg() { | ||
fn want<I, O>(_: I, _: impl Fn(I) -> O) {} | ||
want(&String::new(), extend_lt); | ||
//~^ ERROR temporary value dropped while borrowed | ||
} | ||
|
||
// An exploit of the unsoundness. | ||
fn main() { | ||
let val = extend_lt(&String::from("blah blah blah")); | ||
//~^ ERROR temporary value dropped while borrowed | ||
println!("{}", val); | ||
} |
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,43 @@ | ||
error[E0521]: borrowed data escapes outside of function | ||
--> $DIR/fn-item-check-type-params.rs:31:5 | ||
| | ||
LL | pub fn test_call<'a>(val: &'a str) { | ||
| -- --- `val` is a reference that is only valid in the function body | ||
| | | ||
| lifetime `'a` defined here | ||
LL | extend_lt(val); | ||
| ^^^^^^^^^^^^^^ | ||
| | | ||
| `val` escapes the function body here | ||
| argument requires that `'a` must outlive `'static` | ||
|
||
error: lifetime may not live long enough | ||
--> $DIR/fn-item-check-type-params.rs:39:12 | ||
| | ||
LL | pub fn test_coercion<'a>() { | ||
| -- lifetime `'a` defined here | ||
LL | let _: fn(&'a str) -> _ = extend_lt; | ||
| ^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static` | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/fn-item-check-type-params.rs:48:11 | ||
| | ||
LL | want(&String::new(), extend_lt); | ||
| ------^^^^^^^^^^^^^------------- temporary value is freed at the end of this statement | ||
| | | | ||
| | creates a temporary value which is freed while still in use | ||
| argument requires that borrow lasts for `'static` | ||
|
||
error[E0716]: temporary value dropped while borrowed | ||
--> $DIR/fn-item-check-type-params.rs:54:26 | ||
| | ||
LL | let val = extend_lt(&String::from("blah blah blah")); | ||
| -----------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-- temporary value is freed at the end of this statement | ||
| | | | ||
| | creates a temporary value which is freed while still in use | ||
| argument requires that borrow lasts for `'static` | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
Some errors have detailed explanations: E0521, E0716. | ||
For more information about an error, try `rustc --explain E0521`. |
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
16 changes: 16 additions & 0 deletions
16
tests/ui/implied-bounds/implied-bounds-on-trait-hierarchy-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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error[E0597]: `x` does not live long enough | ||
--> $DIR/implied-bounds-on-trait-hierarchy-1.rs:34:23 | ||
| | ||
LL | let x = "Hello World".to_string(); | ||
| - binding `x` declared here | ||
LL | subs_to_soup((x.as_str(), &mut d)); | ||
| ^ borrowed value does not live long enough | ||
LL | | ||
LL | } | ||
| - `x` dropped here while still borrowed | ||
LL | println!("{}", d); | ||
| - borrow later used here | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0597`. |
Oops, something went wrong.