-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Access to mutable static within items inside unsafe blocks leads to poor user experience #35716
Comments
So I think there are two issues here: One is mostly an ordering issue - which checks should be done first - by the compiler. That is, should the compiler first check for the required The second is whether expressions inside of items defined inside of unsafe blocks should be considered as being inside an unsafe block: fn foo() {
unsafe {
static FOO: Foo = /* is this considered to be in an unsafe block? */;
fn bar() {
/* is this considered to be in an unsafe block? */;
}
}
}
unsafe fn unsafe_foo() {
static FOO: Foo = /* is this considered to be in an unsafe block? */;
fn bar() {
/* is this considered to be in an unsafe block? */;
}
} As far I know, these kinds of expressions are currently not considered to be inside of an unsafe block. I don't know whether anyone really thought about that behaviour when items inside of blocks were added to the language (although my initial guess, without any research, would be that this was probably not considered, since this is a pretty uncommon patter I believe, I would not have thought of writing code like this). Whether the current behaviour should be considered the correct behaviour or not I don't know, I can see arguments for both sides, although I would intuitively lean more towards the current behaviour. |
The following compiles: struct A{
x: u8
}
fn main() {
let x: *const u8 = 0 as *const u8;
unsafe {
let a = A{
x: *x
};
}
} What is the difference between initializing a static struct and a regular one? |
Items are basically anything you can write outside of a function ( An item defined inside of a block / function is basically the same as if the item was defined outside the function / block with "special" name that is only available inside the block / function where the item was defined. So initialising a "static struct" basically happens outside the function (and any unsafe blocks) they were defined in. |
To quote the reference on items inside of functions / blocks (emphasis mine):
|
OK, that makes sense, thanks for the clarification. I don't think this behavior should change. It might be helpful to detect that an unsafe behavior is occurring inside a new item inside an unsafe block and give a note that the unsafe block doesn't extend there. Other than that, this issue can be closed. |
Marking as a diagnostics issue, to fix this we need to improve the error about unsafe blocks not affecting items in them. |
Copying from #47864:
|
This example now does the following callout:
|
That was added in 1.67 by #103964 |
The following produces an error about needing an unsafe block while already being in an unsafe block. It should produce an error about not being able to use a static value in another static.
https://is.gd/OgeJ7e
The text was updated successfully, but these errors were encountered: