-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
miri flags UB in Vec::swap_remove
#90055
Comments
cc @rust-lang/miri |
The underlying issue is in fn main() {
let mut a = 0;
let mut r = &mut a;
unsafe { std::ptr::replace(&mut r as *mut _, r); }
} |
This is actually tricky, I think -- an unexpected consequence of the Stacked Borrows rules. When There is a sense in which the reborrow of
I am surprised by this error, since usually mutable references are reborrowed, not moved. But it seems here it somehow knows not to reborrow? |
cc #89966 -- could be related? Fully-qualifying the
|
Ah, yes that's it. Not that the exact error makes such a big difference, but now at least it is more clear that the reborrow makes later uses of |
…olnay Fix MIRI UB in `Vec::swap_remove` Fixes rust-lang#90055 I find it weird that `Vec::swap_remove` read the last element to the stack just to immediately put it back in the `Vec` in place of the one at index `index`. It seems much more natural to me to just read the element at position `index` and then move the last element in its place. I guess this might also slightly improve codegen.
…olnay Fix MIRI UB in `Vec::swap_remove` Fixes rust-lang#90055 I find it weird that `Vec::swap_remove` read the last element to the stack just to immediately put it back in the `Vec` in place of the one at index `index`. It seems much more natural to me to just read the element at position `index` and then move the last element in its place. I guess this might also slightly improve codegen.
Type inference will prevent reborrowing from happening. |
Could we say that a mutable reference that comes from a type parameter shouldn't be protected? |
That seems a bit strange, to have a particular instance of a generic function behave differently than if that same instance had been written monomorphically. |
Well, generic function is different from an monomorphic function. In a monomorphic function you can dereference the reference, but (if there're no I.e. I would expect a |
Sure, a monomorphic function can be changed to do more things. But when a generic and monomorphic function are the same in the source code, they also should behave the same.
They use completely different types, so them behaving the same does not even make sense? Like, e.g., To give another example, if you |
…olnay Fix MIRI UB in `Vec::swap_remove` Fixes rust-lang#90055 I find it weird that `Vec::swap_remove` read the last element to the stack just to immediately put it back in the `Vec` in place of the one at index `index`. It seems much more natural to me to just read the element at position `index` and then move the last element in its place. I guess this might also slightly improve codegen.
rustup; add swap_remove test Adds a test for rust-lang/rust#90055
The following code (playground):
run under miri reports UB:
The text was updated successfully, but these errors were encountered: