Skip to content

Access to mutable static within items inside unsafe blocks leads to poor user experience #35716

Closed

Description

@DanielShaulov

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.

struct A{
    x: u32
}

fn main() {
    unsafe {
        static mut NUM: u32 = 7;
        static a: A = A {
            x: NUM
        };
    }
}

https://is.gd/OgeJ7e

Activity

  1. TimNN commented on Aug 16, 2016

    @TimNN
    Contributor

    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 unsafe or should it first check that statics cannot be used in other statics.

    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.

  2. DanielShaulov commented on Aug 16, 2016

    @DanielShaulov
    Author

    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?

  3. TimNN commented on Aug 16, 2016

    @TimNN
    Contributor

    let a = ... is an expression / statement.

    static A: Foo = ... is (the definition of) an item.

    Items are basically anything you can write outside of a function (type's, traits, impl's, static's, const's, ..., for details see the reference).

    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.

  4. TimNN commented on Aug 16, 2016

    @TimNN
    Contributor

    To quote the reference on items inside of functions / blocks (emphasis mine):

    Declaring an item — a function, enumeration, struct, type, static, trait, implementation or module — locally within a statement block is simply a way of restricting its scope to a narrow region containing all of its uses; it is otherwise identical in meaning to declaring the item outside the statement block.

  5. DanielShaulov commented on Aug 17, 2016

    @DanielShaulov
    Author

    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.

  6. Mark-Simulacrum commented on May 11, 2017

    @Mark-Simulacrum
    Member

    Marking as a diagnostics issue, to fix this we need to improve the error about unsafe blocks not affecting items in them.

  7. changed the title [-]Wrong error when trying to use a mutable static in another static[/-] [+]Access to mutable static within items inside unsafe blocks leads to poor user experience[/+] on Jun 22, 2017
  8. petrochenkov commented on Jan 30, 2018

    @petrochenkov
    Contributor

    Copying from #47864:

    Interestingly, before Rust 1.6 unsafe blocks worked syntactically and the code was accepted - https://godbolt.org/g/7mv1jQ.
    This probably changed accidentally when items were outlined into side tables in HIR.

  9. added
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    on Nov 15, 2019
  10. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jun 11, 2020
  11. workingjubilee commented on May 5, 2024

    @workingjubilee
    Member

    This example now does the following callout:

       Compiling playground v0.0.1 (/playground)
    warning: unnecessary `unsafe` block
     --> src/main.rs:6:5
      |
    6 |     unsafe {
      |     ^^^^^^ unnecessary `unsafe` block
      |
      = note: `#[warn(unused_unsafe)]` on by default
    
    error[E0133]: use of mutable static is unsafe and requires unsafe function or block
     --> src/main.rs:9:16
      |
    6 |     unsafe {
      |     ------ items do not inherit unsafety from separate enclosing items
    ...
    9 |             x: NUM
      |                ^^^ use of mutable static
      |
      = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
    
  12. workingjubilee commented on May 5, 2024

    @workingjubilee
    Member

    That was added in 1.67 by #103964

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions