-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Implement Unsized Rvalues #51131
Merged
Merged
Implement Unsized Rvalues #51131
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9f0168a
Add notes on unsized argument errors.
qnighy cd0476a
Add Builder::array_alloca.
qnighy 7f05304
Add #![feature(unsized_locals)].
qnighy e2b95cb
Lift some Sized checks.
qnighy 800f2c1
Implement simple codegen for unsized rvalues.
qnighy c72e87e
Add an unstable-book article about unsized_locals.
qnighy 438edc3
Update the unstable book regarding [e; dyn n].
qnighy a0c422a
Remove a now-unnecessary paragraph.
qnighy 6e15e7c
Integrate PassMode::UnsizedIndirect into PassMode::Indirect.
qnighy c488d59
Integrate OperandValue::UnsizedRef into OperandValue::Ref.
qnighy 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
180 changes: 180 additions & 0 deletions
180
src/doc/unstable-book/src/language-features/unsized-locals.md
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,180 @@ | ||
# `unsized_locals` | ||
|
||
The tracking issue for this feature is: [#48055] | ||
|
||
[#48055]: /~https://github.com/rust-lang/rust/issues/48055 | ||
|
||
------------------------ | ||
|
||
This implements [RFC1909]. When turned on, you can have unsized arguments and locals: | ||
|
||
[RFC1909]: /~https://github.com/rust-lang/rfcs/blob/master/text/1909-coercions.md | ||
|
||
```rust | ||
#![feature(unsized_locals)] | ||
|
||
use std::any::Any; | ||
|
||
fn main() { | ||
let x: Box<dyn Any> = Box::new(42); | ||
let x: dyn Any = *x; | ||
// ^ unsized local variable | ||
// ^^ unsized temporary | ||
foo(x); | ||
} | ||
|
||
fn foo(_: dyn Any) {} | ||
// ^^^^^^ unsized argument | ||
``` | ||
|
||
The RFC still forbids the following unsized expressions: | ||
|
||
```rust,ignore | ||
#![feature(unsized_locals)] | ||
|
||
use std::any::Any; | ||
|
||
struct MyStruct<T: ?Sized> { | ||
content: T, | ||
} | ||
|
||
struct MyTupleStruct<T: ?Sized>(T); | ||
|
||
fn answer() -> Box<dyn Any> { | ||
Box::new(42) | ||
} | ||
|
||
fn main() { | ||
// You CANNOT have unsized statics. | ||
static X: dyn Any = *answer(); // ERROR | ||
const Y: dyn Any = *answer(); // ERROR | ||
|
||
// You CANNOT have struct initialized unsized. | ||
MyStruct { content: *answer() }; // ERROR | ||
MyTupleStruct(*answer()); // ERROR | ||
(42, *answer()); // ERROR | ||
|
||
// You CANNOT have unsized return types. | ||
fn my_function() -> dyn Any { *answer() } // ERROR | ||
|
||
// You CAN have unsized local variables... | ||
let mut x: dyn Any = *answer(); // OK | ||
// ...but you CANNOT reassign to them. | ||
x = *answer(); // ERROR | ||
|
||
// You CANNOT even initialize them separately. | ||
let y: dyn Any; // OK | ||
y = *answer(); // ERROR | ||
|
||
// Not mentioned in the RFC, but by-move captured variables are also Sized. | ||
let x: dyn Any = *answer(); | ||
(move || { // ERROR | ||
let y = x; | ||
})(); | ||
|
||
// You CAN create a closure with unsized arguments, | ||
// but you CANNOT call it. | ||
// This is an implementation detail and may be changed in the future. | ||
let f = |x: dyn Any| {}; | ||
f(*answer()); // ERROR | ||
} | ||
``` | ||
|
||
However, the current implementation allows `MyTupleStruct(..)` to be unsized. This will be fixed in the future. | ||
|
||
## By-value trait objects | ||
|
||
With this feature, you can have by-value `self` arguments without `Self: Sized` bounds. | ||
|
||
```rust | ||
#![feature(unsized_locals)] | ||
|
||
trait Foo { | ||
fn foo(self) {} | ||
} | ||
|
||
impl<T: ?Sized> Foo for T {} | ||
|
||
fn main() { | ||
let slice: Box<[i32]> = Box::new([1, 2, 3]); | ||
<[i32] as Foo>::foo(*slice); | ||
} | ||
``` | ||
|
||
And `Foo` will also be object-safe. However, this object-safety is not yet implemented. | ||
|
||
```rust,ignore | ||
#![feature(unsized_locals)] | ||
|
||
trait Foo { | ||
fn foo(self) {} | ||
} | ||
|
||
impl<T: ?Sized> Foo for T {} | ||
|
||
fn main () { | ||
let slice: Box<dyn Foo> = Box::new([1, 2, 3]); | ||
// doesn't compile yet | ||
<dyn Foo as Foo>::foo(*slice); | ||
} | ||
``` | ||
|
||
Unfortunately, this is not implemented yet. | ||
|
||
One of the objectives of this feature is to allow `Box<dyn FnOnce>`, instead of `Box<dyn FnBox>` in the future. See [#28796] for details. | ||
|
||
[#28796]: /~https://github.com/rust-lang/rust/issues/28796 | ||
|
||
## Variable length arrays | ||
|
||
The RFC also describes an extension to the array literal syntax: `[e; dyn n]`. In the syntax, `n` isn't necessarily a constant expression. The array is dynamically allocated on the stack and has the type of `[T]`, instead of `[T; n]`. | ||
|
||
```rust,ignore | ||
#![feature(unsized_locals)] | ||
|
||
fn mergesort<T: Ord>(a: &mut [T]) { | ||
let mut tmp = [T; dyn a.len()]; | ||
// ... | ||
} | ||
|
||
fn main() { | ||
let mut a = [3, 1, 5, 6]; | ||
mergesort(&mut a); | ||
assert_eq!(a, [1, 3, 5, 6]); | ||
} | ||
``` | ||
|
||
VLAs are not implemented yet. The syntax isn't final, either. We may need an alternative syntax for Rust 2015 because, in Rust 2015, expressions like `[e; dyn(1)]` would be ambiguous. One possible alternative proposed in the RFC is `[e; n]`: if `n` captures one or more local variables, then it is considered as `[e; dyn n]`. | ||
|
||
## Advisory on stack usage | ||
|
||
It's advised not to casually use the `#![feature(unsized_locals)]` feature. Typical use-cases are: | ||
|
||
- When you need a by-value trait objects. | ||
- When you really need a fast allocation of small temporary arrays. | ||
|
||
Another pitfall is repetitive allocation and temporaries. Currently the compiler simply extends the stack frame every time it encounters an unsized assignment. So for example, the code | ||
|
||
```rust | ||
#![feature(unsized_locals)] | ||
|
||
fn main() { | ||
let x: Box<[i32]> = Box::new([1, 2, 3, 4, 5]); | ||
let _x = {{{{{{{{{{*x}}}}}}}}}}; | ||
} | ||
``` | ||
|
||
and the code | ||
|
||
```rust | ||
#![feature(unsized_locals)] | ||
|
||
fn main() { | ||
for _ in 0..10 { | ||
let x: Box<[i32]> = Box::new([1, 2, 3, 4, 5]); | ||
let _x = *x; | ||
} | ||
} | ||
``` | ||
|
||
will unnecessarily extend the stack frame. |
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
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
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.
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.
So this is because of
rust-call
and tuples? Annoying.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.
Yes, unfortunately.
Args
itself is Sized (at least for now) so we can't even pass an unsized rvalue in the last argument.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.
You can remove the requirement from
Args
but we should maybe try to make it so it's possible for a tuple for have all elements!Sized
but not be usable as a value or place type (I'm not sure how though).