Coder Social home page Coder Social logo

Comments (3)

Nilstrieb avatar Nilstrieb commented on May 28, 2024 3

closing as not a bug

from rust.

saethlin avatar saethlin commented on May 28, 2024

This code is trying to execute UB. It happened to work as you wanted before, by sheer luck. The problem is reported by running the code in Miri, which you can do via the playground or locally. The error is this:

error: Undefined Behavior: trying to retag from <2677> for Unique permission at alloc1188[0x0], but that tag only grants SharedReadOnly permission for this location
  --> src/main.rs:15:15
   |
15 |             f(&mut *self.0);
   |               ^^^^^^^^^^^^
   |               |
   |               trying to retag from <2677> for Unique permission at alloc1188[0x0], but that tag only grants SharedReadOnly permission for this location
   |               this error occurs as part of retag at alloc1188[0x0..0x1]
   |
   = help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
   = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <2677> was created by a SharedReadOnly retag at offsets [0x0..0x1]
  --> src/main.rs:7:14
   |
7  |         Self(t as *const T as *mut T)
   |              ^
   = note: BACKTRACE (of the first span):
   = note: inside `ResMut::<A>::patch_by::<{closure@src/main.rs:45:19: 45:22}>` at src/main.rs:15:15: 15:27
note: inside `foo`
  --> src/main.rs:44:5
   |
44 | /     ResMut::new(&v[0])
45 | |         .patch_by(|x| x.a = B::Bar).unwrap();
   | |___________________________________^
note: inside `main`
  --> src/main.rs:39:5
   |
39 |     foo(&v);
   |     ^^^^^^^

from rust.

Soveu avatar Soveu commented on May 28, 2024

Well, in more simple terms, you're just modifying immutable data - that is instant UB in Rust.
Maybe what you're looking for is UnsafeCell or some kind of its derivative (RefCell, Mutex, RwLock)

from rust.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.