gccrs: Implement `unsafe_cell` lang item
gccrs adds support for the `unsafe_cell` language item, preventing const marking on types containing it.
GCC’s Rust front-end (gccrs) now recognizes the unsafe_cell language item. This change prevents types containing unsafe_cell from being marked as constant, aligning with Rust’s semantics for such types. This commit includes new functions for checking the presence of unsafe_cell and updates the compiler’s language item registry, along with a new test case.
In Details
The gccrs compiler has integrated the unsafe_cell language item, a fundamental concept in Rust's safety model. This commit ensures that any type, including ADTs, tuples, arrays, and slices, that transitively contains an unsafe_cell is correctly identified and not marked as const. This prevents potential unsoundness, as unsafe_cell implies interior mutability. The implementation involves adding checks in type resolution and ensuring unsafe_cell is registered in gccrs's language item map. Niche-filling optimizations are deferred as they are not yet implemented in gccrs.
- unsafe_cell
- A Rust language construct that allows for interior mutability within a type that would otherwise be immutable. It signifies that the type's internal state can be modified even when accessed through a shared reference, requiring careful handling by the programmer.
- lang item
- A fundamental concept or feature in a programming language, such as
unsafe_cell,Copy, orSized, that the compiler must understand and manage.gccrsmaintains a registry of these items. - const
- In Rust, a
constitem is a value that is known at compile time and can be evaluated entirely during compilation. Types containingunsafe_cellcannot be marked asconstdue to their mutable nature. - interior mutability
- The ability to mutate data even when there are shared references to it. This is typically achieved through types like
Cell,RefCell, andUnsafeCellin Rust, and requiresunsafecode. - ADT
- Abstract Data Type. A type defined by its behavior (operations) rather than its specific data representation. In Rust, this includes structs and enums.