-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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 #115538 - lcnr:fn-def-wf, r=compiler-errors
check `FnDef` return type for WF better version of #106807, fixes #84533 (mostly). It's not perfect given that we still ignore WF requirements involving bound regions but I wasn't able to quickly write an example, so even if theoretically exploitable, it should be far harder to trigger. This is strictly more restrictive than checking the return type for WF as part of the builtin `FnDef: FnOnce` impl (#106807) and moving to this approach in the future will not break any code. ~~It also agrees with my theoretical view of how this should behave~~ r? types
- Loading branch information
Showing
8 changed files
with
153 additions
and
45 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 was deleted.
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
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,44 @@ | ||
// Regression test for #84533. | ||
|
||
use std::marker::PhantomData; | ||
|
||
fn foo<'b, 'a>() -> PhantomData<&'b &'a ()> { | ||
PhantomData | ||
} | ||
|
||
fn extend_lifetime<'a, 'b, T: ?Sized>(x: &'a T) -> &'b T { | ||
let f = foo::<'b, 'a>; | ||
f.baz(x) | ||
//~^ ERROR lifetime may not live long enough | ||
} | ||
|
||
trait Foo<'a, 'b, T: ?Sized> { | ||
fn baz(self, s: &'a T) -> &'b T; | ||
} | ||
impl<'a, 'b, R, F, T: ?Sized> Foo<'a, 'b, T> for F | ||
where | ||
F: Fn() -> R, | ||
R: ProofForConversion<'a, 'b, T>, | ||
{ | ||
fn baz(self, s: &'a T) -> &'b T { | ||
self().convert(s) | ||
} | ||
} | ||
|
||
trait ProofForConversion<'a, 'b, T: ?Sized> { | ||
fn convert(self, s: &'a T) -> &'b T; | ||
} | ||
impl<'a, 'b, T: ?Sized> ProofForConversion<'a, 'b, T> for PhantomData<&'b &'a ()> { | ||
fn convert(self, s: &'a T) -> &'b T { | ||
s | ||
} | ||
} | ||
|
||
fn main() { | ||
let d; | ||
{ | ||
let x = "Hello World".to_string(); | ||
d = extend_lifetime(&x); | ||
} | ||
println!("{}", d); | ||
} |
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 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/wf-fn-def-check-sig-1.rs:11:5 | ||
| | ||
LL | fn extend_lifetime<'a, 'b, T: ?Sized>(x: &'a T) -> &'b T { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
LL | let f = foo::<'b, 'a>; | ||
LL | f.baz(x) | ||
| ^^^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a` | ||
| | ||
= help: consider adding the following bound: `'a: 'b` | ||
|
||
error: aborting due to 1 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,44 @@ | ||
// Regression test for #84533 involving higher-ranked regions | ||
// in the return type. | ||
use std::marker::PhantomData; | ||
|
||
fn foo<'c, 'b, 'a>(_: &'c ()) -> (&'c (), PhantomData<&'b &'a ()>) { | ||
(&(), PhantomData) | ||
} | ||
|
||
fn extend_lifetime<'a, 'b, T: ?Sized>(x: &'a T) -> &'b T { | ||
let f = foo; | ||
f.baz(x) | ||
//~^ ERROR lifetime may not live long enough | ||
} | ||
|
||
trait Foo<'a, 'b, T: ?Sized> { | ||
fn baz(self, s: &'a T) -> &'b T; | ||
} | ||
impl<'a, 'b, R, F, T: ?Sized> Foo<'a, 'b, T> for F | ||
where | ||
F: for<'c> Fn(&'c ()) -> (&'c (), R), | ||
R: ProofForConversion<'a, 'b, T>, | ||
{ | ||
fn baz(self, s: &'a T) -> &'b T { | ||
self(&()).1.convert(s) | ||
} | ||
} | ||
|
||
trait ProofForConversion<'a, 'b, T: ?Sized> { | ||
fn convert(self, s: &'a T) -> &'b T; | ||
} | ||
impl<'a, 'b, T: ?Sized> ProofForConversion<'a, 'b, T> for PhantomData<&'b &'a ()> { | ||
fn convert(self, s: &'a T) -> &'b T { | ||
s | ||
} | ||
} | ||
|
||
fn main() { | ||
let d; | ||
{ | ||
let x = "Hello World".to_string(); | ||
d = extend_lifetime(&x); | ||
} | ||
println!("{}", d); | ||
} |
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 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/wf-fn-def-check-sig-2.rs:11:5 | ||
| | ||
LL | fn extend_lifetime<'a, 'b, T: ?Sized>(x: &'a T) -> &'b T { | ||
| -- -- lifetime `'b` defined here | ||
| | | ||
| lifetime `'a` defined here | ||
LL | let f = foo; | ||
LL | f.baz(x) | ||
| ^^^^^^^^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a` | ||
| | ||
= help: consider adding the following bound: `'a: 'b` | ||
|
||
error: aborting due to 1 previous error | ||
|