Change:
The code below compiles as of 1.46, but I'm not sure it should, since it dereferences a raw pointer in a const block, which is only allowed in nightly with the flag:
modify labels to +A-raw-pointers +A-const-fn
cc
Ah, that looks like the const version of the old "uninhabited match" problem... The MIR for is So somehow the deref already vanished, and hence the const analysis cannot catch it. But doesn't unsafety checking also happen on the MIR? Why does that work?
It seems that whenever the dereference is not bound to a variable, it compiles, but whenever bound to a variable, it fails to compile:
I don't believe this is an unsoundness. Unsafe code is required to cause anything here. That's the final MIR, since without this will fail to compile, it must be there for unsafety checking, thus it must also be there for const checking. Not sure why we fail to catch this.
Agreed. This is a case of "oops a nightly-only feature is available on stable", which is bad, but it is not an unsoundness.
Assigning and removing as in the prioritization working group.
The initial MIR looks like this: So probably the issue is that const-checking ignores ?
oh... yea.. we ignored it on purpose... "because it doesn't actually do anything" :upsidedownface: I guess we should visit all places within statements that we otherwise ignore during const-checking?
Well, at least we need to visit . ;)
Do we really? The check is done in , which should be called even for the projection in the place in the fake read.
Oh wait, we don't call unconditionally! That seems like... a problem.^^
Proposed a fix:

Discussion:
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) { trace!("visit_statement: statement={:?} location={:?}", statement, location); <del> match statement.kind { StatementKind::Assign(..) | StatementKind::SetDiscriminant { .. } => { self.super_statement(statement, location); } </del> <ins> self.super_statement(statement, location); match statement.kind { </ins> StatementKind::LlvmInlineAsm { .. } => { <del> self.super_statement(statement, location); </del> self.check_op(ops::InlineAsm); } <del> StatementKind::FakeRead(..) </del> <ins> StatementKind::Assign(..) | StatementKind::SetDiscriminant { .. } | StatementKind::FakeRead(..) </ins> | StatementKind::StorageLive(_) | StatementKind::StorageDead(_) | StatementKind::Retag { .. }

| LL |     unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } } |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <ins> help: skipping check that does not even have a feature gate --> $DIR/const_refers_to_static_cross_crate.rs:32:20 | LL |     unsafe { match static_cross_crate::OPT_ZERO { Some(ref u) => u, None => panic!() } } |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ </ins> help: skipping check for `const_panic` feature --> $DIR/const_refers_to_static_cross_crate.rs:32:77 |

const VALUE: u8 = unsafe { *REG_ADDR }; //~^ ERROR dereferencing raw pointers in constants is unstable <ins> const unsafe fn unreachable() -> ! { use std::convert::Infallible; const INFALLIBLE: *const Infallible = [].as_ptr(); match *INFALLIBLE {} //~^ ERROR dereferencing raw pointers in constant functions is unstable const BAD: () = unsafe { match *INFALLIBLE {} }; //~^ ERROR dereferencing raw pointers in constants is unstable } </ins> fn main() { }

= note: see issue #51911 <https://github.com/rust-lang/rust/issues/51911> for more information = help: add `#![feature(const_raw_ptr_deref)]` to the crate attributes to enable <del> error: aborting due to previous error </del> <ins> error[E0658]: dereferencing raw pointers in constant functions is unstable --> $DIR/E0396.rs:12:11 | LL |     match *INFALLIBLE {} |           ^^^^^^^^^^^ | = note: see issue #51911 <https://github.com/rust-lang/rust/issues/51911> for more information = help: add `#![feature(const_raw_ptr_deref)]` to the crate attributes to enable error[E0658]: dereferencing raw pointers in constants is unstable --> $DIR/E0396.rs:15:36 | LL |     const BAD: () = unsafe { match *INFALLIBLE {} }; |                                    ^^^^^^^^^^^ | = note: see issue #51911 <https://github.com/rust-lang/rust/issues/51911> for more information = help: add `#![feature(const_raw_ptr_deref)]` to the crate attributes to enable error: aborting due to 3 previous errors </ins> For more information about this error, try `rustc --explain E0658`.