diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 50ccbd50d9711..7c1ce2571f20a 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -1257,6 +1257,20 @@ impl<'tcx> ThirBuildCx<'tcx> { Res::Def(DefKind::Static { .. }, id) => { // this is &raw for extern static or static mut, and & for other statics let ty = self.tcx.static_ptr_ty(id, self.typing_env); + + // If this is behind a `&raw`, set `ty` as a raw ptr to discern it from normal ref + let ty = if let hir::Node::Expr(&hir::Expr { + kind: hir::ExprKind::AddrOf(hir::BorrowKind::Raw, mutbl, _), + .. + }) = self.tcx.parent_hir_node(expr.hir_id) + && let ty::Ref(_, ty, mutbl_) = ty.kind() + && mutbl == *mutbl_ + { + Ty::new_ptr(self.tcx, *ty, mutbl) + } else { + ty + }; + let kind = if self.tcx.is_thread_local_static(id) { ExprKind::ThreadLocalRef(id) } else { diff --git a/compiler/rustc_mir_transform/src/promote_consts.rs b/compiler/rustc_mir_transform/src/promote_consts.rs index 7d631e96c32a7..9a70c50b441dc 100644 --- a/compiler/rustc_mir_transform/src/promote_consts.rs +++ b/compiler/rustc_mir_transform/src/promote_consts.rs @@ -760,10 +760,12 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> { } let num_stmts = self.source[loc.block].statements.len(); - let new_temp = self.promoted.local_decls.push(LocalDecl::new( - self.source.local_decls[temp].ty, - self.source.local_decls[temp].source_info.span, - )); + let new_local_decl = { + let temp = &self.source.local_decls[temp]; + let local_decl = LocalDecl::new(temp.ty, temp.source_info.span); + if temp.mutability == Mutability::Not { local_decl.immutable() } else { local_decl } + }; + let new_temp = self.promoted.local_decls.push(new_local_decl); debug!("promote({:?} @ {:?}/{:?}, {:?})", temp, loc, num_stmts, self.keep_original); diff --git a/tests/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-pre-optimizations.after.mir b/tests/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-pre-optimizations.after.mir index 344851bb08839..4d441a74f8e32 100644 --- a/tests/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-pre-optimizations.after.mir +++ b/tests/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-pre-optimizations.after.mir @@ -2,9 +2,9 @@ const BAR::promoted[0]: &[&i32; 1] = { let mut _0: &[&i32; 1]; - let mut _1: [&i32; 1]; + let _1: [&i32; 1]; let mut _2: &i32; - let mut _3: &i32; + let _3: &i32; bb0: { _3 = const {ALLOC0: &i32}; diff --git a/tests/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-pre-optimizations.after.mir b/tests/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-pre-optimizations.after.mir index 72cb64e275e36..5ff19b948fa25 100644 --- a/tests/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-pre-optimizations.after.mir +++ b/tests/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-pre-optimizations.after.mir @@ -2,9 +2,9 @@ const FOO::promoted[0]: &[&i32; 1] = { let mut _0: &[&i32; 1]; - let mut _1: [&i32; 1]; + let _1: [&i32; 1]; let mut _2: &i32; - let mut _3: *const i32; + let _3: *const i32; bb0: { _3 = const {ALLOC0: *const i32};