Skip to content

Commit 153e843

Browse files
committed
fix Rvalue::ty for ThreadLocalRef
1 parent c1766c6 commit 153e843

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

compiler/rustc_middle/src/mir/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1007,13 +1007,13 @@ impl<'tcx> LocalDecl<'tcx> {
10071007
}
10081008

10091009
/// Returns `Some` if this is a reference to a static item that is used to
1010-
/// access that static
1010+
/// access that static.
10111011
pub fn is_ref_to_static(&self) -> bool {
10121012
matches!(self.local_info, Some(box LocalInfo::StaticRef { .. }))
10131013
}
10141014

1015-
/// Returns `Some` if this is a reference to a static item that is used to
1016-
/// access that static
1015+
/// Returns `Some` if this is a reference to a thread-local static item that is used to
1016+
/// access that static.
10171017
pub fn is_ref_to_thread_local(&self) -> bool {
10181018
match self.local_info {
10191019
Some(box LocalInfo::StaticRef { is_thread_local, .. }) => is_thread_local,

compiler/rustc_middle/src/mir/tcx.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,14 @@ impl<'tcx> Rvalue<'tcx> {
152152
tcx.mk_ty(ty::Array(operand.ty(local_decls, tcx), count))
153153
}
154154
Rvalue::ThreadLocalRef(did) => {
155+
let static_ty = tcx.type_of(did);
155156
if tcx.is_mutable_static(did) {
156-
tcx.mk_mut_ptr(tcx.type_of(did))
157+
tcx.mk_mut_ptr(static_ty)
158+
} else if tcx.is_foreign_item(did) {
159+
tcx.mk_imm_ptr(static_ty)
157160
} else {
158-
tcx.mk_imm_ref(tcx.lifetimes.re_static, tcx.type_of(did))
161+
// FIXME: These things don't *really* have 'static lifetime.
162+
tcx.mk_imm_ref(tcx.lifetimes.re_static, static_ty)
159163
}
160164
}
161165
Rvalue::Ref(reg, bk, ref place) => {

compiler/rustc_middle/src/ty/util.rs

+1
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ impl<'tcx> TyCtxt<'tcx> {
530530
let static_ty = self.normalize_erasing_regions(ty::ParamEnv::empty(), self.type_of(def_id));
531531

532532
// Make sure that accesses to unsafe statics end up using raw pointers.
533+
// For thread-locals, this needs to be kept in sync with `Rvalue::ty`.
533534
if self.is_mutable_static(def_id) {
534535
self.mk_mut_ptr(static_ty)
535536
} else if self.is_foreign_item(def_id) {

src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].ConstProp.after.mir

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ promoted[0] in FOO: &[&i32; 1] = {
44
let mut _0: &[&i32; 1]; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
55
let mut _1: [&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
66
let mut _2: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:32: 13:45
7-
let mut _3: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
7+
let mut _3: *const i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
88

99
bb0: {
10-
_3 = const {alloc2: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
10+
_3 = const {alloc2: *const i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
1111
// ty::Const
12-
// + ty: &i32
12+
// + ty: *const i32
1313
// + val: Value(Scalar(alloc2))
1414
// mir::Constant
1515
// + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43
16-
// + literal: Const { ty: &i32, val: Value(Scalar(alloc2)) }
17-
_2 = _3; // scope 0 at $DIR/const-promotion-extern-static.rs:13:41: 13:43
16+
// + literal: Const { ty: *const i32, val: Value(Scalar(alloc2)) }
17+
_2 = &(*_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:41: 13:43
1818
_1 = [move _2]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
1919
_0 = &_1; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
2020
return; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46

src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let mut _2: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
88
let _3: [&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
99
let mut _4: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:32: 13:45
10-
let _5: &i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
10+
let _5: *const i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
1111
+ let mut _6: &[&i32; 1]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
1212
scope 1 {
1313
}
@@ -18,16 +18,16 @@
1818
- StorageLive(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
1919
- StorageLive(_4); // scope 0 at $DIR/const-promotion-extern-static.rs:13:32: 13:45
2020
- StorageLive(_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
21-
- _5 = const {alloc2: &i32}; // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
21+
- _5 = const {alloc2: *const i32}; // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43
2222
+ _6 = const FOO::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
2323
// ty::Const
24-
- // + ty: &i32
24+
- // + ty: *const i32
2525
- // + val: Value(Scalar(alloc2))
2626
+ // + ty: &[&i32; 1]
2727
+ // + val: Unevaluated(WithOptConstParam { did: DefId(0:7 ~ const_promotion_extern_static[317d]::FOO), const_param_did: None }, [], Some(promoted[0]))
2828
// mir::Constant
2929
- // + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43
30-
- // + literal: Const { ty: &i32, val: Value(Scalar(alloc2)) }
30+
- // + literal: Const { ty: *const i32, val: Value(Scalar(alloc2)) }
3131
- _4 = &(*_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:41: 13:43
3232
- _3 = [move _4]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
3333
- _2 = &_3; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46

0 commit comments

Comments
 (0)