Skip to content

Commit d6b6488

Browse files
committed
Handle statics in MIR typeck with user type annotations
1 parent ad48d52 commit d6b6488

File tree

13 files changed

+33
-16
lines changed

13 files changed

+33
-16
lines changed

src/librustc_mir/borrow_check/type_check/mod.rs

-14
Original file line numberDiff line numberDiff line change
@@ -366,20 +366,6 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
366366
);
367367
}
368368
}
369-
} else if let Some(static_def_id) = constant.check_static_ptr(tcx) {
370-
let unnormalized_ty = tcx.type_of(static_def_id);
371-
let locations = location.to_locations();
372-
let normalized_ty = self.cx.normalize(unnormalized_ty, locations);
373-
let literal_ty = constant.literal.ty.builtin_deref(true).unwrap().ty;
374-
375-
if let Err(terr) = self.cx.eq_types(
376-
normalized_ty,
377-
literal_ty,
378-
locations,
379-
ConstraintCategory::Boring,
380-
) {
381-
span_mirbug!(self, constant, "bad static type {:?} ({:?})", constant, terr);
382-
}
383369
}
384370

385371
if let ty::FnDef(def_id, substs) = constant.literal.ty.kind {

src/librustc_mir_build/build/expr/as_constant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2121
let Expr { ty, temp_lifetime: _, span, kind } = expr;
2222
match kind {
2323
ExprKind::Scope { region_scope: _, lint_level: _, value } => this.as_constant(value),
24-
ExprKind::Literal { literal, user_ty } => {
24+
ExprKind::Literal { literal, user_ty } |
25+
ExprKind::StaticRef { literal, user_ty, .. } => {
2526
let user_ty = user_ty.map(|user_ty| {
2627
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
2728
span,
@@ -32,7 +33,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3233
assert_eq!(literal.ty, ty);
3334
Constant { span, user_ty, literal }
3435
}
35-
ExprKind::StaticRef { literal, .. } => Constant { span, user_ty: None, literal },
3636
_ => span_bug!(span, "expression is not a valid constant {:?}", kind),
3737
}
3838
}

src/librustc_mir_build/hair/cx/expr.rs

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::hair::*;
66
use rustc_hir as hir;
77
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
88
use rustc_index::vec::Idx;
9+
use rustc_infer::infer::RegionVariableOrigin;
910
use rustc_middle::mir::interpret::Scalar;
1011
use rustc_middle::mir::BorrowKind;
1112
use rustc_middle::ty::adjustment::{
@@ -742,13 +743,26 @@ fn convert_path_expr<'a, 'tcx>(
742743
let ty = cx.tcx.static_ptr_ty(id);
743744
let ptr = cx.tcx.alloc_map.lock().create_static_alloc(id);
744745
let temp_lifetime = cx.region_scope_tree.temporary_scope(expr.hir_id.local_id);
746+
747+
// Keep regions around in a user type annotation for MIR typeck:
748+
let static_ty = cx.tcx.type_of(id);
749+
let static_ptr_ty = if cx.tcx.is_mutable_static(id) {
750+
cx.tcx.mk_mut_ptr(static_ty)
751+
} else {
752+
let origin = RegionVariableOrigin::AddrOfRegion(expr.span);
753+
let region = cx.infcx.next_region_var(origin);
754+
cx.tcx.mk_imm_ref(region, static_ty)
755+
};
756+
let user_ty = cx.infcx.canonicalize_user_type_annotation(&UserType::Ty(static_ptr_ty));
757+
745758
ExprKind::Deref {
746759
arg: Expr {
747760
ty,
748761
temp_lifetime,
749762
span: expr.span,
750763
kind: ExprKind::StaticRef {
751764
literal: ty::Const::from_scalar(cx.tcx, Scalar::Ptr(ptr.into()), ty),
765+
user_ty: Some(user_ty),
752766
def_id: id,
753767
},
754768
}

src/librustc_mir_build/hair/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ crate enum ExprKind<'tcx> {
275275
/// info for diagnostics.
276276
StaticRef {
277277
literal: &'tcx Const<'tcx>,
278+
user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
278279
def_id: DefId,
279280
},
280281
LlvmInlineAsm {

src/test/mir-opt/const-promotion-extern-static/rustc.BAR-promoted[0].ConstProp.after.mir

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ promoted[0] in BAR: &[&i32; 1] = {
1313
// + val: Value(Scalar(alloc0+0))
1414
// mir::Constant
1515
// + span: $DIR/const-promotion-extern-static.rs:9:33: 9:34
16+
// + user_ty: UserType(0)
1617
// + literal: Const { ty: &i32, val: Value(Scalar(alloc0+0)) }
1718
_2 = _3; // bb0[1]: scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
1819
_1 = [move _2]; // bb0[2]: scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35

src/test/mir-opt/const-promotion-extern-static/rustc.BAR.PromoteTemps.diff

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
- // MIR for `BAR` before PromoteTemps
22
+ // MIR for `BAR` after PromoteTemps
33

4+
| User Type Annotations
5+
| 0: Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], value: Ty(&i32) } at $DIR/const-promotion-extern-static.rs:9:33: 9:34
6+
|
47
static mut BAR: *const &i32 = {
58
let mut _0: *const &i32; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:9:17: 9:28
69
let mut _1: &[&i32]; // in scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35
@@ -25,6 +28,7 @@
2528
+ // + val: Unevaluated(DefId(0:6 ~ const_promotion_extern_static[317d]::BAR[0]), [], Some(promoted[0]))
2629
// mir::Constant
2730
- // + span: $DIR/const-promotion-extern-static.rs:9:33: 9:34
31+
- // + user_ty: UserType(0)
2832
- // + literal: Const { ty: &i32, val: Value(Scalar(alloc0+0)) }
2933
- _4 = &(*_5); // bb0[6]: scope 0 at $DIR/const-promotion-extern-static.rs:9:32: 9:34
3034
- _3 = [move _4]; // bb0[7]: scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:35

src/test/mir-opt/const-promotion-extern-static/rustc.FOO-promoted[0].ConstProp.after.mir

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ promoted[0] in FOO: &[&i32; 1] = {
1515
// + val: Value(Scalar(alloc2+0))
1616
// mir::Constant
1717
// + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43
18+
// + user_ty: UserType(0)
1819
// + literal: Const { ty: &i32, val: Value(Scalar(alloc2+0)) }
1920
_2 = _3; // bb0[1]: scope 0 at $DIR/const-promotion-extern-static.rs:13:41: 13:43
2021
_1 = [move _2]; // bb0[2]: scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46

src/test/mir-opt/const-promotion-extern-static/rustc.FOO.PromoteTemps.diff

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
- // MIR for `FOO` before PromoteTemps
22
+ // MIR for `FOO` after PromoteTemps
33

4+
| User Type Annotations
5+
| 0: Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], value: Ty(&i32) } at $DIR/const-promotion-extern-static.rs:13:42: 13:43
6+
|
47
static mut FOO: *const &i32 = {
58
let mut _0: *const &i32; // return place in scope 0 at $DIR/const-promotion-extern-static.rs:13:17: 13:28
69
let mut _1: &[&i32]; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46
@@ -27,6 +30,7 @@
2730
+ // + val: Unevaluated(DefId(0:7 ~ const_promotion_extern_static[317d]::FOO[0]), [], Some(promoted[0]))
2831
// mir::Constant
2932
- // + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43
33+
- // + user_ty: UserType(0)
3034
- // + literal: Const { ty: &i32, val: Value(Scalar(alloc2+0)) }
3135
- _4 = &(*_5); // bb0[6]: scope 1 at $DIR/const-promotion-extern-static.rs:13:41: 13:43
3236
- _3 = [move _4]; // bb0[7]: scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46

src/test/mir-opt/const_allocation/32bit/rustc.main.ConstProp.after.mir

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() -> () {
1414
// + val: Value(Scalar(alloc0+0))
1515
// mir::Constant
1616
// + span: $DIR/const_allocation.rs:8:5: 8:8
17+
// + user_ty: UserType(0)
1718
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&str])], val: Value(Scalar(alloc0+0)) }
1819
_1 = (*_2); // bb0[3]: scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1920
StorageDead(_2); // bb0[4]: scope 0 at $DIR/const_allocation.rs:8:8: 8:9

src/test/mir-opt/const_allocation/64bit/rustc.main.ConstProp.after.mir

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() -> () {
1414
// + val: Value(Scalar(alloc0+0))
1515
// mir::Constant
1616
// + span: $DIR/const_allocation.rs:8:5: 8:8
17+
// + user_ty: UserType(0)
1718
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&str])], val: Value(Scalar(alloc0+0)) }
1819
_1 = (*_2); // bb0[3]: scope 0 at $DIR/const_allocation.rs:8:5: 8:8
1920
StorageDead(_2); // bb0[4]: scope 0 at $DIR/const_allocation.rs:8:8: 8:9

src/test/mir-opt/const_allocation2/64bit/rustc.main.ConstProp.after.mir

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() -> () {
1414
// + val: Value(Scalar(alloc0+0))
1515
// mir::Constant
1616
// + span: $DIR/const_allocation2.rs:5:5: 5:8
17+
// + user_ty: UserType(0)
1718
// + literal: Const { ty: &&[(std::option::Option<i32>, &[&u8])], val: Value(Scalar(alloc0+0)) }
1819
_1 = (*_2); // bb0[3]: scope 0 at $DIR/const_allocation2.rs:5:5: 5:8
1920
StorageDead(_2); // bb0[4]: scope 0 at $DIR/const_allocation2.rs:5:8: 5:9

src/test/mir-opt/const_allocation3/64bit/rustc.main.ConstProp.after.mir

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() -> () {
1414
// + val: Value(Scalar(alloc0+0))
1515
// mir::Constant
1616
// + span: $DIR/const_allocation3.rs:5:5: 5:8
17+
// + user_ty: UserType(0)
1718
// + literal: Const { ty: &&Packed, val: Value(Scalar(alloc0+0)) }
1819
_1 = (*_2); // bb0[3]: scope 0 at $DIR/const_allocation3.rs:5:5: 5:8
1920
StorageDead(_2); // bb0[4]: scope 0 at $DIR/const_allocation3.rs:5:8: 5:9

src/test/mir-opt/const_prop/read_immutable_static/rustc.main.ConstProp.diff

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// + val: Value(Scalar(alloc0+0))
2323
// mir::Constant
2424
// + span: $DIR/read_immutable_static.rs:7:13: 7:16
25+
// + user_ty: UserType(0)
2526
// + literal: Const { ty: &u8, val: Value(Scalar(alloc0+0)) }
2627
- _2 = (*_3); // bb0[4]: scope 0 at $DIR/read_immutable_static.rs:7:13: 7:16
2728
+ _2 = const 2u8; // bb0[4]: scope 0 at $DIR/read_immutable_static.rs:7:13: 7:16
@@ -39,6 +40,7 @@
3940
// + val: Value(Scalar(alloc0+0))
4041
// mir::Constant
4142
// + span: $DIR/read_immutable_static.rs:7:19: 7:22
43+
// + user_ty: UserType(1)
4244
// + literal: Const { ty: &u8, val: Value(Scalar(alloc0+0)) }
4345
- _4 = (*_5); // bb0[8]: scope 0 at $DIR/read_immutable_static.rs:7:19: 7:22
4446
- _1 = Add(move _2, move _4); // bb0[9]: scope 0 at $DIR/read_immutable_static.rs:7:13: 7:22

0 commit comments

Comments
 (0)