-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Lint on invalid usage of UnsafeCell::raw_get
in reference casting
#115166
Lint on invalid usage of UnsafeCell::raw_get
in reference casting
#115166
Conversation
fn as_mut<T>(x: &T) -> &mut T { | ||
unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) } | ||
//~^ ERROR casting `&T` to `&mut T` is undefined behavior | ||
} | ||
|
||
fn as_mut_i32(x: &i32) -> &mut i32 { | ||
unsafe { &mut *std::cell::UnsafeCell::raw_get(x as *const _ as *const _) } | ||
//~^ ERROR casting `&T` to `&mut T` is undefined behavior | ||
} |
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.
@RalfJung can you confirm (or deny) that we can lint on those expressions ?
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.
Those expressions are definitely bogus.
Can you confirm that the lint will not fire if the type of x
is changed to &Cell<i32>
?
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 I can.
I already added some tests below to make sure we don't lint on them, but added a test with &Cell<i32>
anyway (just to be on the safe side).
fc348de
to
89800a2
Compare
@bors r+ |
☀️ Test successful - checks-actions |
Finished benchmarking commit (4e5b31c): comparison URL. Overall result: ✅ improvements - no action needed@rustbot label: -perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 628.074s -> 628.991s (0.15%) |
This PR proposes to take into account
UnsafeCell::raw_get
method call for non-Freeze types for theinvalid_reference_casting
lint.The goal of this is to catch those kind of invalid reference casting:
r? @est31