-
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
Prevent copies for unsized fn params #111330
Conversation
The place could be modified in between the move and the call. For example: #![feature(unsized_fn_params)]
fn f(x: [i32], _: ()) {
assert_eq!(&x, [1]);
}
fn main() {
let mut x: Box<[i32]> = Box::new([1]);
f(*x, {x = Box::new([2]) as Box<[i32]>; let _ = x;});
} And a slightly different variant: #![feature(unsized_fn_params)]
fn f(x: [i32], _: ()) {
assert_eq!([1], x);
}
fn main() {
let mut x: Box<[i32]> = Box::new([1]);
f(*x, x[0] = 2);
} |
Hmm, I think the relevant example is this one: #![feature(unsized_fn_params)]
fn uns(x: [i32], _: ()) {}
fn foo(mut x: [i32]) {
uns(x, { x[0] = 1; () });
} As far as I can tell there is no Mir that we can generate for I'm tempted that we should still merge this, with an appropriate test and comment added (will do that tomorrow). The feature is already unsound, and removing this bug is a pre-requisite to unimplementing unsized locals (which I do think we should do) |
You mean |
Yes. This isn't a Mir transform though - the "bug" in this PR is that the above example will incorrectly compile and |
Can you describe what is the end goal here? For the standard library, unsized locals aren't necessary except for the currently unstable It might be easier to fix the alignment issue. |
@tmiasko I'll submit an MCP tomorrow for removing unsized locals from rustc completely (and will explain the motivation for that in there). Under the assumption that that's desirable, I feel quite strongly that we should not block that choice on this bug. The fact that unsized_fn_params cannot be implemented correctly in Mir without reliance on an extremely broken feature is unfortunate. Blocking the removal of the broken feature on that situation is imo not a reasonable response though. |
They aren't necessary at all, std only uses the The end goal is to have
That's a bug in MIR building after this PR, but done correctly (well, using unsized locals) before this PR? |
Yes, although I'd slightly object to the use of "correctly" and "unsized locals" in the same sentence :P |
As of now, the |
// Generate let tmp0 = arg0 | ||
let operand = unpack!( | ||
block = this.as_temp(block, scope, &this.thir[arg], Mutability::Mut) | ||
); | ||
|
||
// Return the operand *tmp0 to be used as the call argument | ||
let place = Place { | ||
local: operand, | ||
projection: tcx.mk_place_elems(&[PlaceElem::Deref]), | ||
}; | ||
|
||
return block.and(Operand::Move(place)); |
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.
Note that this was previously moving the box, avoiding problems with evaluation order and unsized temporaries.
So basically you are saying, Also removing |
I've updated the PR - it should now not regress any cases that previously did not require unsized locals to work |
This comment has been minimized.
This comment has been minimized.
In general, I would suggest fixing |
@tmiasko that seems like a reasonable thing to want - is there a reason it has to happen as a part of this PR? I don't really interpret this PR as making anything worse than the status quo |
What is the motivation behind this change? If you argue that correct MIR building in this situation requires "unsized locals or major refactors", why de facto propose to accept this code under |
The motivation for this change is to unblock the removal of unsized locals. We could also just not land this and then remove unsized locals anyway, in which case this code will ICE. That's fine by me too, maybe it's preferable. Mostly what I'm trying to avoid is us getting into a situation where we can't remove unsized locals until someone sits down and does the work of fixing this properly |
As I indicated earlier this isn't sufficient to avoid unsized locals, see for example #111355. I can take a look into updating the feature gate. |
Sure; I'll close this then |
Fixes #111175 .
Regression test added too.
r? rust-lang/mir-opt or @RalfJung