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.
Enforce
Sized
return types on Fn*
bounds
In a `fn() -> Out` bound, enforce `Out: Sized` to avoid unsoundness. Fix rust-lang#82633.
- Loading branch information
Showing
3 changed files
with
195 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#![feature(unboxed_closures)] | ||
|
||
trait A { | ||
fn a() where Self: Sized; | ||
} | ||
|
||
mod a { | ||
use A; | ||
|
||
pub fn foo<F: FnOnce<()>>() where F::Output: A { | ||
F::Output::a() | ||
} | ||
|
||
pub fn bar<F: FnOnce() -> R, R: ?Sized>() {} | ||
|
||
pub fn baz<F: FnOnce<()>>() where F::Output: A, F::Output: Sized { | ||
F::Output::a() | ||
} | ||
} | ||
|
||
mod b { | ||
use A; | ||
|
||
pub fn foo<F: Fn<()>>() where F::Output: A { | ||
F::Output::a() | ||
} | ||
|
||
pub fn bar<F: Fn() -> R, R: ?Sized>() {} | ||
|
||
pub fn baz<F: Fn<()>>() where F::Output: A, F::Output: Sized { | ||
F::Output::a() | ||
} | ||
} | ||
|
||
mod c { | ||
use A; | ||
|
||
pub fn foo<F: FnMut<()>>() where F::Output: A { | ||
F::Output::a() | ||
} | ||
|
||
pub fn bar<F: FnMut() -> R, R: ?Sized>() {} | ||
|
||
pub fn baz<F: FnMut<()>>() where F::Output: A, F::Output: Sized { | ||
F::Output::a() | ||
} | ||
} | ||
|
||
impl A for Box<dyn A> { | ||
fn a() {} | ||
} | ||
|
||
fn main() { | ||
a::foo::<fn() -> dyn A>(); //~ERROR E0277 | ||
a::bar::<fn() -> dyn A, _>(); //~ERROR E0277 | ||
a::baz::<fn() -> dyn A>(); //~ERROR E0277 | ||
a::foo::<fn() -> Box<dyn A>>(); // ok | ||
a::bar::<fn() -> Box<dyn A>, _>(); // ok | ||
a::baz::<fn() -> Box<dyn A>>(); // ok | ||
b::foo::<fn() -> dyn A>(); //~ERROR E0277 | ||
b::bar::<fn() -> dyn A, _>(); //~ERROR E0277 | ||
b::baz::<fn() -> dyn A>(); //~ERROR E0277 | ||
b::foo::<fn() -> Box<dyn A>>(); // ok | ||
b::bar::<fn() -> Box<dyn A>, _>(); // ok | ||
b::baz::<fn() -> Box<dyn A>>(); // ok | ||
c::foo::<fn() -> dyn A>(); //~ERROR E0277 | ||
c::bar::<fn() -> dyn A, _>(); //~ERROR E0277 | ||
c::baz::<fn() -> dyn A>(); //~ERROR E0277 | ||
c::foo::<fn() -> Box<dyn A>>(); // ok | ||
c::bar::<fn() -> Box<dyn A>, _>(); // ok | ||
c::baz::<fn() -> Box<dyn A>>(); // ok | ||
} |
99 changes: 99 additions & 0 deletions
99
src/test/ui/closures/closure-return-type-must-be-sized.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,99 @@ | ||
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time | ||
--> $DIR/closure-return-type-must-be-sized.rs:54:5 | ||
| | ||
LL | a::foo::<fn() -> dyn A>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `(dyn A + 'static)` | ||
|
||
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time | ||
--> $DIR/closure-return-type-must-be-sized.rs:55:5 | ||
| | ||
LL | pub fn bar<F: FnOnce() -> R, R: ?Sized>() {} | ||
| ------------- required by this bound in `a::bar` | ||
... | ||
LL | a::bar::<fn() -> dyn A, _>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `(dyn A + 'static)` | ||
|
||
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time | ||
--> $DIR/closure-return-type-must-be-sized.rs:56:5 | ||
| | ||
LL | pub fn baz<F: FnOnce<()>>() where F::Output: A, F::Output: Sized { | ||
| ----- required by this bound in `a::baz` | ||
... | ||
LL | a::baz::<fn() -> dyn A>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `(dyn A + 'static)` | ||
|
||
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time | ||
--> $DIR/closure-return-type-must-be-sized.rs:60:5 | ||
| | ||
LL | pub fn foo<F: Fn<()>>() where F::Output: A { | ||
| ------ required by this bound in `b::foo` | ||
... | ||
LL | b::foo::<fn() -> dyn A>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `(dyn A + 'static)` | ||
|
||
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time | ||
--> $DIR/closure-return-type-must-be-sized.rs:61:5 | ||
| | ||
LL | pub fn bar<F: Fn() -> R, R: ?Sized>() {} | ||
| --------- required by this bound in `b::bar` | ||
... | ||
LL | b::bar::<fn() -> dyn A, _>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `(dyn A + 'static)` | ||
|
||
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time | ||
--> $DIR/closure-return-type-must-be-sized.rs:62:5 | ||
| | ||
LL | pub fn baz<F: Fn<()>>() where F::Output: A, F::Output: Sized { | ||
| ----- required by this bound in `b::baz` | ||
... | ||
LL | b::baz::<fn() -> dyn A>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `(dyn A + 'static)` | ||
|
||
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time | ||
--> $DIR/closure-return-type-must-be-sized.rs:66:5 | ||
| | ||
LL | pub fn foo<F: FnMut<()>>() where F::Output: A { | ||
| --------- required by this bound in `c::foo` | ||
... | ||
LL | c::foo::<fn() -> dyn A>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `(dyn A + 'static)` | ||
|
||
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time | ||
--> $DIR/closure-return-type-must-be-sized.rs:67:5 | ||
| | ||
LL | pub fn bar<F: FnMut() -> R, R: ?Sized>() {} | ||
| ------------ required by this bound in `c::bar` | ||
... | ||
LL | c::bar::<fn() -> dyn A, _>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `(dyn A + 'static)` | ||
|
||
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time | ||
--> $DIR/closure-return-type-must-be-sized.rs:68:5 | ||
| | ||
LL | pub fn baz<F: FnMut<()>>() where F::Output: A, F::Output: Sized { | ||
| ----- required by this bound in `c::baz` | ||
... | ||
LL | c::baz::<fn() -> dyn A>(); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time | ||
| | ||
= help: the trait `Sized` is not implemented for `(dyn A + 'static)` | ||
|
||
error: aborting due to 9 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |