rust/compiler/rustc_middle/src/ty
bors 9b72238eb8 Auto merge of #128543 - RalfJung:const-interior-mut, r=fee1-dead
const-eval interning: accept interior mutable pointers in final value

…but keep rejecting mutable references

This fixes https://github.com/rust-lang/rust/issues/121610 by no longer firing the lint when there is a pointer with interior mutability in the final value of the constant. On stable, such pointers can be created with code like:
```rust
pub enum JsValue {
    Undefined,
    Object(Cell<bool>),
}
impl Drop for JsValue {
    fn drop(&mut self) {}
}
// This does *not* get promoted since `JsValue` has a destructor.
// However, the outer scope rule applies, still giving this 'static lifetime.
const UNDEFINED: &JsValue = &JsValue::Undefined;
```
It's not great to accept such values since people *might* think that it is legal to mutate them with unsafe code. (This is related to how "infectious" `UnsafeCell` is, which is a [wide open question](https://github.com/rust-lang/unsafe-code-guidelines/issues/236).) However, we [explicitly document](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) that things created by `const` are immutable. Furthermore, we also accept the following even more questionable code without any lint today:
```rust
let x: &'static Option<Cell<i32>> = &None;
```
This is even more questionable since it does *not* involve a `const`, and yet still puts the data into immutable memory. We could view this as promotion [potentially introducing UB](https://github.com/rust-lang/unsafe-code-guidelines/issues/493). However, we've accepted this since ~forever and it's [too late to reject this now](https://github.com/rust-lang/rust/pull/122789); the pattern is just too useful.

So basically, if you think that `UnsafeCell` should be tracked fully precisely, then you should want the lint we currently emit to be removed, which this PR does. If you think `UnsafeCell` should "infect" surrounding `enum`s, the big problem is really https://github.com/rust-lang/unsafe-code-guidelines/issues/493 which does not trigger the lint -- the cases the lint triggers on are actually the "harmless" ones as there is an explicit surrounding `const` explaining why things end up being immutable.

What all this goes to show is that the hard error added in https://github.com/rust-lang/rust/pull/118324 (later turned into the future-compat lint that I am now suggesting we remove) was based on some wrong assumptions, at least insofar as it concerns shared references. Furthermore, that lint does not help at all for the most problematic case here where the potential UB is completely implicit. (In fact, the lint is actively in the way of [my preferred long-term strategy](https://github.com/rust-lang/unsafe-code-guidelines/issues/493#issuecomment-2028674105) for dealing with this UB.) So I think we should go back to square one and remove that error/lint for shared references. For mutable references, it does seem to work as intended, so we can keep it. Here it serves as a safety net in case the static checks that try to contain mutable references to the inside of a const initializer are not working as intended; I therefore made the check ICE to encourage users to tell us if that safety net is triggered.

Closes https://github.com/rust-lang/rust/issues/122153 by removing the lint.

Cc `@rust-lang/opsem` `@rust-lang/lang`
2024-09-14 21:11:04 +00:00
..
consts Use assert_matches around the compiler 2024-08-11 12:25:39 -04:00
context Reformat use declarations. 2024-07-29 08:26:52 +10:00
inhabitedness Non-exhaustive structs may be empty 2024-09-02 21:16:37 +02:00
print Rollup merge of #130250 - compiler-errors:useless-conversion, r=jieyouxu 2024-09-12 20:37:17 +10:00
abstract_const.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
adjustment.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
adt.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
assoc.rs Differentiate between methods and associated functions 2024-08-10 00:54:16 +00:00
cast.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
closure.rs Don't store region in CapturedPlace 2024-09-05 08:42:50 -04:00
codec.rs const-eval interning: accpt interior mutable pointers in final value (but keep rejecting mutable references) 2024-09-10 10:26:16 +02:00
consts.rs Re-enable ConstArgKind::Path lowering by default 2024-09-12 13:56:01 -04:00
context.rs Rollup merge of #130199 - compiler-errors:by-move, r=cjgillot 2024-09-14 11:53:12 +10:00
diagnostics.rs Use is_lang_item more 2024-08-13 16:44:53 -04:00
elaborate_impl.rs Uplift elaboration 2024-07-07 11:28:01 -04:00
erase_regions.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
error.rs Shrink TyKind::FnPtr. 2024-08-09 14:33:25 +10:00
fast_reject.rs Use DeepRejectCtxt to quickly reject ParamEnv candidates 2024-09-02 19:59:18 +03:00
flags.rs Streamline some inputs/output traversals. 2024-08-12 16:03:18 +10:00
fold.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
generic_args.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
generics.rs Fix elided_named_lifetimes in code 2024-08-24 19:21:32 +03:00
impls_ty.rs const-eval interning: accpt interior mutable pointers in final value (but keep rejecting mutable references) 2024-09-10 10:26:16 +02:00
instance.rs chore: Fix typos in 'compiler' (batch 2) 2024-09-02 07:50:22 +02:00
intrinsic.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
layout.rs Simplify some nested if statements 2024-09-11 13:45:23 -04:00
list.rs Use unsafe extern blocks throughout the compiler 2024-08-26 19:51:05 -04:00
mod.rs Revert ed7bdbb17b 2024-09-06 19:06:59 +02:00
normalize_erasing_regions.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
opaque_types.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
parameterized.rs Revert "Auto merge of #127537 - veluca93:struct_tf, r=BoxyUwU" 2024-09-01 16:35:53 +02:00
pattern.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
predicate.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
region.rs Remove needless returns detected by clippy in the compiler 2024-09-09 13:32:22 +02:00
relate.rs Fix relations 2024-07-17 10:46:10 -04:00
rvalue_scopes.rs rescope temp lifetime in let-chain into IfElse 2024-09-11 04:10:00 +08:00
structural_impls.rs Shrink TyKind::FnPtr. 2024-08-09 14:33:25 +10:00
sty.rs Auto merge of #129403 - scottmcm:only-array-simd, r=compiler-errors 2024-09-10 22:47:40 +00:00
trait_def.rs Use DeepRejectCtxt to quickly reject ParamEnv candidates 2024-09-02 19:59:18 +03:00
typeck_results.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
util.rs Make Ty::boxed_ty return an Option 2024-09-06 00:30:36 +03:00
visit.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
vtable.rs miri: make vtable addresses not globally unique 2024-08-06 19:09:31 +02:00
walk.rs Streamline some inputs/output traversals. 2024-08-12 16:03:18 +10:00