From 5e0fab6da5ef58249b5755627964f0a5c2cbd65f Mon Sep 17 00:00:00 2001 From: b-naber Date: Tue, 8 Feb 2022 12:14:44 +0100 Subject: [PATCH 1/7] use ConstantKind::Val in StaticRef --- compiler/rustc_mir_build/src/build/expr/as_constant.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index 79ac09d523d07..1136844377ab0 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -27,7 +27,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { Constant { span, user_ty, literal: literal.into() } } ExprKind::StaticRef { literal, .. } => { - Constant { span, user_ty: None, literal: literal.into() } + let const_val = literal.val.try_to_value().unwrap_or_else(|| { + bug!("expected `ConstKind::Value`, but found {:?}", literal.val) + }); + let literal = ConstantKind::Val(const_val, literal.ty); + + Constant { span, user_ty: None, literal } } ExprKind::ConstBlock { value } => { Constant { span: span, user_ty: None, literal: value.into() } From c612ef8f48a0bedc854807e3d5734f4935c7bda4 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 9 Feb 2022 11:43:04 +0100 Subject: [PATCH 2/7] treat mir::ConstantKind::Val correctly in check_static_ptr --- Cargo.lock | 39 +++------------------------- compiler/rustc_middle/src/mir/mod.rs | 2 +- 2 files changed, 4 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b330155d14483..fe265a05146fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -369,7 +369,7 @@ dependencies = [ "tar", "tempfile", "termcolor", - "toml_edit", + "toml", "unicode-width", "unicode-xid", "url 2.2.2", @@ -455,7 +455,7 @@ dependencies = [ "serde_json", "tar", "termcolor", - "toml_edit", + "toml", "url 2.2.2", ] @@ -710,16 +710,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "combine" -version = "4.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50b727aacc797f9fc28e355d21f34709ac4fc9adecfe470ad07b8f4464f53062" -dependencies = [ - "bytes", - "memchr", -] - [[package]] name = "commoncrypto" version = "0.2.0" @@ -1949,15 +1939,6 @@ dependencies = [ "unicase", ] -[[package]] -name = "kstring" -version = "1.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b310ccceade8121d7d77fee406160e457c2f4e7c7982d589da3499bc7ea4526" -dependencies = [ - "serde", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -3165,14 +3146,13 @@ dependencies = [ "tokio-stream", "tokio-util", "toml", - "toml_edit", "url 2.2.2", "walkdir", ] [[package]] name = "rls-analysis" -version = "0.18.3" +version = "0.18.2" dependencies = [ "derive-new", "env_logger 0.9.0", @@ -5171,19 +5151,6 @@ dependencies = [ "serde", ] -[[package]] -name = "toml_edit" -version = "0.13.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "744e9ed5b352340aa47ce033716991b5589e23781acb97cad37d4ea70560f55b" -dependencies = [ - "combine", - "indexmap", - "itertools 0.10.1", - "kstring", - "serde", -] - [[package]] name = "topological-sort" version = "0.1.0" diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index e80dfab7c48ec..24b08be40749d 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2522,7 +2522,7 @@ pub enum ConstantKind<'tcx> { impl<'tcx> Constant<'tcx> { pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option { - match self.literal.const_for_ty()?.val().try_to_scalar() { + match self.literal.try_to_scalar() { Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance) { GlobalAlloc::Static(def_id) => { assert!(!tcx.is_thread_local_static(def_id)); From 8092b90cb2f8d5a70a0e0a61bd439143133e1dc7 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 9 Feb 2022 11:43:47 +0100 Subject: [PATCH 3/7] bless tests --- ..._allocation.main.ConstProp.after.64bit.mir | 50 +------------------ ...allocation2.main.ConstProp.after.64bit.mir | 48 +----------------- ...allocation3.main.ConstProp.after.64bit.mir | 38 -------------- ...d[0].SimplifyCfg-elaborate-drops.after.mir | 7 --- ...motion_extern_static.BAR.PromoteTemps.diff | 8 +-- ...d[0].SimplifyCfg-elaborate-drops.after.mir | 9 +--- ...motion_extern_static.FOO.PromoteTemps.diff | 10 ++-- ...table_variable_no_prop.main.ConstProp.diff | 7 --- .../read_immutable_static.main.ConstProp.diff | 10 ---- 9 files changed, 8 insertions(+), 179 deletions(-) diff --git a/src/test/mir-opt/const_allocation.main.ConstProp.after.64bit.mir b/src/test/mir-opt/const_allocation.main.ConstProp.after.64bit.mir index 88cdbda2fae36..9add329521298 100644 --- a/src/test/mir-opt/const_allocation.main.ConstProp.after.64bit.mir +++ b/src/test/mir-opt/const_allocation.main.ConstProp.after.64bit.mir @@ -9,12 +9,9 @@ fn main() -> () { StorageLive(_1); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8 StorageLive(_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8 _2 = const {alloc1: &&[(Option, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:8:5: 8:8 - // ty::Const - // + ty: &&[(std::option::Option, &[&str])] - // + val: Value(Scalar(alloc1)) // mir::Constant // + span: $DIR/const_allocation.rs:8:5: 8:8 - // + literal: Const { ty: &&[(std::option::Option, &[&str])], val: Value(Scalar(alloc1)) } + // + literal: Const { ty: &&[(Option, &[&str])], val: Value(Scalar(alloc1)) } _1 = (*_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8 StorageDead(_2); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9 StorageDead(_1); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9 @@ -22,48 +19,3 @@ fn main() -> () { return; // scope 0 at $DIR/const_allocation.rs:9:2: 9:2 } } - -alloc1 (static: FOO, size: 16, align: 8) { - ╾───────alloc18───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ -} - -alloc18 (size: 72, align: 8) { - 0x00 │ 00 00 00 00 __ __ __ __ ╾───────alloc5────────╼ │ ....░░░░╾──────╼ - 0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░ - 0x20 │ ╾───────alloc9────────╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........ - 0x30 │ 01 00 00 00 2a 00 00 00 ╾───────alloc14───────╼ │ ....*...╾──────╼ - 0x40 │ 03 00 00 00 00 00 00 00 │ ........ -} - -alloc5 (size: 0, align: 8) {} - -alloc9 (size: 32, align: 8) { - 0x00 │ ╾───────alloc8────────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ - 0x10 │ ╾───────alloc10───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ -} - -alloc8 (size: 3, align: 1) { - 66 6f 6f │ foo -} - -alloc10 (size: 3, align: 1) { - 62 61 72 │ bar -} - -alloc14 (size: 48, align: 8) { - 0x00 │ ╾───────alloc13───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ - 0x10 │ ╾───────alloc15───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ - 0x20 │ ╾───────alloc16───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ -} - -alloc13 (size: 3, align: 1) { - 6d 65 68 │ meh -} - -alloc15 (size: 3, align: 1) { - 6d 6f 70 │ mop -} - -alloc16 (size: 4, align: 1) { - 6d c3 b6 70 │ m..p -} diff --git a/src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir b/src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir index d6a97917d6226..ef2618e457df6 100644 --- a/src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir +++ b/src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir @@ -9,12 +9,9 @@ fn main() -> () { StorageLive(_1); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8 StorageLive(_2); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8 _2 = const {alloc1: &&[(Option, &[&u8])]}; // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8 - // ty::Const - // + ty: &&[(std::option::Option, &[&u8])] - // + val: Value(Scalar(alloc1)) // mir::Constant // + span: $DIR/const_allocation2.rs:5:5: 5:8 - // + literal: Const { ty: &&[(std::option::Option, &[&u8])], val: Value(Scalar(alloc1)) } + // + literal: Const { ty: &&[(Option, &[&u8])], val: Value(Scalar(alloc1)) } _1 = (*_2); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8 StorageDead(_2); // scope 0 at $DIR/const_allocation2.rs:5:8: 5:9 StorageDead(_1); // scope 0 at $DIR/const_allocation2.rs:5:8: 5:9 @@ -22,46 +19,3 @@ fn main() -> () { return; // scope 0 at $DIR/const_allocation2.rs:6:2: 6:2 } } - -alloc1 (static: FOO, size: 16, align: 8) { - ╾───────alloc28───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ -} - -alloc28 (size: 72, align: 8) { - 0x00 │ 00 00 00 00 __ __ __ __ ╾───────alloc13───────╼ │ ....░░░░╾──────╼ - 0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░ - 0x20 │ ╾───────alloc18───────╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........ - 0x30 │ 01 00 00 00 2a 00 00 00 ╾───────alloc26───────╼ │ ....*...╾──────╼ - 0x40 │ 03 00 00 00 00 00 00 00 │ ........ -} - -alloc13 (size: 0, align: 8) {} - -alloc18 (size: 16, align: 8) { - ╾───────alloc16───────╼ ╾───────alloc17───────╼ │ ╾──────╼╾──────╼ -} - -alloc16 (size: 1, align: 1) { - 05 │ . -} - -alloc17 (size: 1, align: 1) { - 06 │ . -} - -alloc26 (size: 24, align: 8) { - 0x00 │ ╾─────alloc22+0x3─────╼ ╾───────alloc23───────╼ │ ╾──────╼╾──────╼ - 0x10 │ ╾─────alloc25+0x2─────╼ │ ╾──────╼ -} - -alloc22 (size: 4, align: 1) { - 2a 45 15 6f │ *E.o -} - -alloc23 (size: 1, align: 1) { - 2a │ * -} - -alloc25 (size: 4, align: 1) { - 2a 45 15 6f │ *E.o -} diff --git a/src/test/mir-opt/const_allocation3.main.ConstProp.after.64bit.mir b/src/test/mir-opt/const_allocation3.main.ConstProp.after.64bit.mir index cac882333aeb9..f3cbfcaca266c 100644 --- a/src/test/mir-opt/const_allocation3.main.ConstProp.after.64bit.mir +++ b/src/test/mir-opt/const_allocation3.main.ConstProp.after.64bit.mir @@ -9,9 +9,6 @@ fn main() -> () { StorageLive(_1); // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8 StorageLive(_2); // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8 _2 = const {alloc1: &&Packed}; // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8 - // ty::Const - // + ty: &&Packed - // + val: Value(Scalar(alloc1)) // mir::Constant // + span: $DIR/const_allocation3.rs:5:5: 5:8 // + literal: Const { ty: &&Packed, val: Value(Scalar(alloc1)) } @@ -22,38 +19,3 @@ fn main() -> () { return; // scope 0 at $DIR/const_allocation3.rs:6:2: 6:2 } } - -alloc1 (static: FOO, size: 8, align: 8) { - ╾───────alloc11───────╼ │ ╾──────╼ -} - -alloc11 (size: 180, align: 1) { - 0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................ - 0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾──alloc6── │ ............╾─── - 0x20 │ ──────────╼ 01 ef cd ab 00 00 00 00 00 00 00 00 │ ───╼............ - 0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x60 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x70 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x80 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ╾──── │ ..............╾─ - 0x90 │ ─────alloc8─────╼ 00 00 ╾─────alloc9+0x63─────╼ │ ─────╼..╾──────╼ - 0xa0 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0xb0 │ 00 00 00 00 │ .... -} - -alloc6 (size: 4, align: 4) { - 2a 00 00 00 │ *... -} - -alloc8 (fn: main) - -alloc9 (size: 100, align: 1) { - 0x00 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x20 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ - 0x60 │ 00 00 00 00 │ .... -} diff --git a/src/test/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-elaborate-drops.after.mir b/src/test/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-elaborate-drops.after.mir index bec0fa9c04957..c6b15174e4691 100644 --- a/src/test/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-elaborate-drops.after.mir +++ b/src/test/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-elaborate-drops.after.mir @@ -8,9 +8,6 @@ promoted[0] in BAR: &[&i32; 1] = { bb0: { _3 = const {alloc1: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34 - // ty::Const - // + ty: &i32 - // + val: Value(Scalar(alloc1)) // mir::Constant // + span: $DIR/const-promotion-extern-static.rs:9:33: 9:34 // + literal: Const { ty: &i32, val: Value(Scalar(alloc1)) } @@ -20,7 +17,3 @@ promoted[0] in BAR: &[&i32; 1] = { return; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44 } } - -alloc1 (static: Y, size: 4, align: 4) { - 2a 00 00 00 │ *... -} diff --git a/src/test/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff b/src/test/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff index b480b25782564..84db15e1c8a87 100644 --- a/src/test/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff +++ b/src/test/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff @@ -18,9 +18,7 @@ - StorageLive(_5); // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34 - _5 = const {alloc1: &i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:9:33: 9:34 + _6 = const BAR::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44 - // ty::Const -- // + ty: &i32 -- // + val: Value(Scalar(alloc1)) ++ // ty::Const + // + ty: &[&i32; 1] + // + val: Unevaluated(BAR, [], Some(promoted[0])) // mir::Constant @@ -51,9 +49,5 @@ bb2 (cleanup): { resume; // scope 0 at $DIR/const-promotion-extern-static.rs:9:1: 9:45 } -- } -- -- alloc1 (static: Y, size: 4, align: 4) { -- 2a 00 00 00 │ *... } diff --git a/src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-elaborate-drops.after.mir b/src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-elaborate-drops.after.mir index c01b31525b6a6..8bfdf1acaa9f3 100644 --- a/src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-elaborate-drops.after.mir +++ b/src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-elaborate-drops.after.mir @@ -7,18 +7,13 @@ promoted[0] in FOO: &[&i32; 1] = { let mut _3: *const i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 bb0: { - _3 = const {alloc3: *const i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 - // ty::Const - // + ty: *const i32 - // + val: Value(Scalar(alloc3)) + _3 = const {alloc2: *const i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 // mir::Constant // + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43 - // + literal: Const { ty: *const i32, val: Value(Scalar(alloc3)) } + // + literal: Const { ty: *const i32, val: Value(Scalar(alloc2)) } _2 = &(*_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:41: 13:43 _1 = [move _2]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46 _0 = &_1; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55 return; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55 } } - -alloc3 (extern static: X) diff --git a/src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff b/src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff index ed48f5dc9dcbc..3f34c4982b2f9 100644 --- a/src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff +++ b/src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff @@ -18,16 +18,14 @@ - StorageLive(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46 - StorageLive(_4); // scope 0 at $DIR/const-promotion-extern-static.rs:13:32: 13:45 - StorageLive(_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 -- _5 = const {alloc3: *const i32}; // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 +- _5 = const {alloc2: *const i32}; // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 + _6 = const FOO::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55 - // ty::Const -- // + ty: *const i32 -- // + val: Value(Scalar(alloc3)) ++ // ty::Const + // + ty: &[&i32; 1] + // + val: Unevaluated(FOO, [], Some(promoted[0])) // mir::Constant - // + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43 -- // + literal: Const { ty: *const i32, val: Value(Scalar(alloc3)) } +- // + literal: Const { ty: *const i32, val: Value(Scalar(alloc2)) } - _4 = &(*_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:41: 13:43 - _3 = [move _4]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46 - _2 = &_3; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55 @@ -54,6 +52,4 @@ resume; // scope 0 at $DIR/const-promotion-extern-static.rs:13:1: 13:56 } } -- -- alloc3 (extern static: X) diff --git a/src/test/mir-opt/const_prop/mutable_variable_no_prop.main.ConstProp.diff b/src/test/mir-opt/const_prop/mutable_variable_no_prop.main.ConstProp.diff index afc621784633e..cee2d4c0c0991 100644 --- a/src/test/mir-opt/const_prop/mutable_variable_no_prop.main.ConstProp.diff +++ b/src/test/mir-opt/const_prop/mutable_variable_no_prop.main.ConstProp.diff @@ -24,9 +24,6 @@ StorageLive(_3); // scope 2 at $DIR/mutable_variable_no_prop.rs:9:13: 9:19 StorageLive(_4); // scope 2 at $DIR/mutable_variable_no_prop.rs:9:13: 9:19 _4 = const {alloc1: *mut u32}; // scope 2 at $DIR/mutable_variable_no_prop.rs:9:13: 9:19 - // ty::Const - // + ty: *mut u32 - // + val: Value(Scalar(alloc1)) // mir::Constant // + span: $DIR/mutable_variable_no_prop.rs:9:13: 9:19 // + literal: Const { ty: *mut u32, val: Value(Scalar(alloc1)) } @@ -45,7 +42,3 @@ } } - alloc1 (static: STATIC, size: 4, align: 4) { - 2a 00 00 00 │ *... - } - diff --git a/src/test/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff b/src/test/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff index 13ef30d89a076..7246b3bdb67bd 100644 --- a/src/test/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff +++ b/src/test/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff @@ -17,9 +17,6 @@ StorageLive(_2); // scope 0 at $DIR/read_immutable_static.rs:7:13: 7:16 StorageLive(_3); // scope 0 at $DIR/read_immutable_static.rs:7:13: 7:16 _3 = const {alloc1: &u8}; // scope 0 at $DIR/read_immutable_static.rs:7:13: 7:16 - // ty::Const - // + ty: &u8 - // + val: Value(Scalar(alloc1)) // mir::Constant // + span: $DIR/read_immutable_static.rs:7:13: 7:16 // + literal: Const { ty: &u8, val: Value(Scalar(alloc1)) } @@ -28,9 +25,6 @@ StorageLive(_4); // scope 0 at $DIR/read_immutable_static.rs:7:19: 7:22 StorageLive(_5); // scope 0 at $DIR/read_immutable_static.rs:7:19: 7:22 _5 = const {alloc1: &u8}; // scope 0 at $DIR/read_immutable_static.rs:7:19: 7:22 - // ty::Const - // + ty: &u8 - // + val: Value(Scalar(alloc1)) // mir::Constant // + span: $DIR/read_immutable_static.rs:7:19: 7:22 // + literal: Const { ty: &u8, val: Value(Scalar(alloc1)) } @@ -48,7 +42,3 @@ } } - alloc1 (static: FOO, size: 1, align: 1) { - 02 │ . - } - From 22d6204db8c5919e8ef5801dd1049603999f9cfa Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 9 Feb 2022 14:21:25 +0100 Subject: [PATCH 4/7] use mir::Visitor when collecting alloc_ids in pretty printing --- compiler/rustc_middle/src/mir/pretty.rs | 26 ++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index 4f29ef7a6402c..c263eb6f321cf 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -17,9 +17,8 @@ use rustc_middle::mir::interpret::{ use rustc_middle::mir::visit::Visitor; use rustc_middle::mir::MirSource; use rustc_middle::mir::*; -use rustc_middle::ty::{self, TyCtxt, TypeFoldable, TypeVisitor}; +use rustc_middle::ty::{self, TyCtxt}; use rustc_target::abi::Size; -use std::ops::ControlFlow; const INDENT: &str = " "; /// Alignment for lining up comments following MIR statements @@ -669,6 +668,7 @@ pub fn write_allocations<'tcx>( fn alloc_ids_from_alloc(alloc: &Allocation) -> impl DoubleEndedIterator + '_ { alloc.relocations().values().map(|id| *id) } + fn alloc_ids_from_const(val: ConstValue<'_>) -> impl Iterator + '_ { match val { ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _size)) => { @@ -682,17 +682,29 @@ pub fn write_allocations<'tcx>( } } } + struct CollectAllocIds(BTreeSet); - impl<'tcx> TypeVisitor<'tcx> for CollectAllocIds { - fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow { - if let ty::ConstKind::Value(val) = c.val() { + + impl<'tcx> Visitor<'tcx> for CollectAllocIds { + fn visit_const(&mut self, c: &&'tcx ty::Const<'tcx>, _loc: Location) { + if let ty::ConstKind::Value(val) = c.val { self.0.extend(alloc_ids_from_const(val)); } - c.super_visit_with(self) + } + + fn visit_constant(&mut self, c: &Constant<'tcx>, loc: Location) { + match c.literal { + ConstantKind::Ty(c) => self.visit_const(&c, loc), + ConstantKind::Val(val, _) => { + self.0.extend(alloc_ids_from_const(val)); + } + } } } + let mut visitor = CollectAllocIds(Default::default()); - body.visit_with(&mut visitor); + visitor.visit_body(body); + // `seen` contains all seen allocations, including the ones we have *not* printed yet. // The protocol is to first `insert` into `seen`, and only if that returns `true` // then push to `todo`. From 54ff25e4467ad84f3f2e158b6969e5f71e72a287 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 9 Feb 2022 14:21:49 +0100 Subject: [PATCH 5/7] bless mir-opt tests --- ..._allocation.main.ConstProp.after.64bit.mir | 45 +++++++++++++++++++ ...allocation2.main.ConstProp.after.64bit.mir | 43 ++++++++++++++++++ ...allocation3.main.ConstProp.after.64bit.mir | 35 +++++++++++++++ ...d[0].SimplifyCfg-elaborate-drops.after.mir | 4 ++ ...motion_extern_static.BAR.PromoteTemps.diff | 4 ++ ...d[0].SimplifyCfg-elaborate-drops.after.mir | 6 ++- ...motion_extern_static.FOO.PromoteTemps.diff | 6 ++- ...table_variable_no_prop.main.ConstProp.diff | 4 ++ .../read_immutable_static.main.ConstProp.diff | 4 ++ 9 files changed, 147 insertions(+), 4 deletions(-) diff --git a/src/test/mir-opt/const_allocation.main.ConstProp.after.64bit.mir b/src/test/mir-opt/const_allocation.main.ConstProp.after.64bit.mir index 9add329521298..1f1d857425e5b 100644 --- a/src/test/mir-opt/const_allocation.main.ConstProp.after.64bit.mir +++ b/src/test/mir-opt/const_allocation.main.ConstProp.after.64bit.mir @@ -19,3 +19,48 @@ fn main() -> () { return; // scope 0 at $DIR/const_allocation.rs:9:2: 9:2 } } + +alloc1 (static: FOO, size: 16, align: 8) { + ╾───────alloc18───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ +} + +alloc18 (size: 72, align: 8) { + 0x00 │ 00 00 00 00 __ __ __ __ ╾───────alloc5────────╼ │ ....░░░░╾──────╼ + 0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░ + 0x20 │ ╾───────alloc9────────╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........ + 0x30 │ 01 00 00 00 2a 00 00 00 ╾───────alloc14───────╼ │ ....*...╾──────╼ + 0x40 │ 03 00 00 00 00 00 00 00 │ ........ +} + +alloc5 (size: 0, align: 8) {} + +alloc9 (size: 32, align: 8) { + 0x00 │ ╾───────alloc8────────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ + 0x10 │ ╾───────alloc10───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ +} + +alloc8 (size: 3, align: 1) { + 66 6f 6f │ foo +} + +alloc10 (size: 3, align: 1) { + 62 61 72 │ bar +} + +alloc14 (size: 48, align: 8) { + 0x00 │ ╾───────alloc13───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ + 0x10 │ ╾───────alloc15───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ + 0x20 │ ╾───────alloc16───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ +} + +alloc13 (size: 3, align: 1) { + 6d 65 68 │ meh +} + +alloc15 (size: 3, align: 1) { + 6d 6f 70 │ mop +} + +alloc16 (size: 4, align: 1) { + 6d c3 b6 70 │ m..p +} diff --git a/src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir b/src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir index ef2618e457df6..ef651f01c9b98 100644 --- a/src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir +++ b/src/test/mir-opt/const_allocation2.main.ConstProp.after.64bit.mir @@ -19,3 +19,46 @@ fn main() -> () { return; // scope 0 at $DIR/const_allocation2.rs:6:2: 6:2 } } + +alloc1 (static: FOO, size: 16, align: 8) { + ╾───────alloc28───────╼ 03 00 00 00 00 00 00 00 │ ╾──────╼........ +} + +alloc28 (size: 72, align: 8) { + 0x00 │ 00 00 00 00 __ __ __ __ ╾───────alloc13───────╼ │ ....░░░░╾──────╼ + 0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 __ __ __ __ │ ............░░░░ + 0x20 │ ╾───────alloc18───────╼ 02 00 00 00 00 00 00 00 │ ╾──────╼........ + 0x30 │ 01 00 00 00 2a 00 00 00 ╾───────alloc26───────╼ │ ....*...╾──────╼ + 0x40 │ 03 00 00 00 00 00 00 00 │ ........ +} + +alloc13 (size: 0, align: 8) {} + +alloc18 (size: 16, align: 8) { + ╾───────alloc16───────╼ ╾───────alloc17───────╼ │ ╾──────╼╾──────╼ +} + +alloc16 (size: 1, align: 1) { + 05 │ . +} + +alloc17 (size: 1, align: 1) { + 06 │ . +} + +alloc26 (size: 24, align: 8) { + 0x00 │ ╾─────alloc22+0x3─────╼ ╾───────alloc23───────╼ │ ╾──────╼╾──────╼ + 0x10 │ ╾─────alloc25+0x2─────╼ │ ╾──────╼ +} + +alloc22 (size: 4, align: 1) { + 2a 45 15 6f │ *E.o +} + +alloc23 (size: 1, align: 1) { + 2a │ * +} + +alloc25 (size: 4, align: 1) { + 2a 45 15 6f │ *E.o +} diff --git a/src/test/mir-opt/const_allocation3.main.ConstProp.after.64bit.mir b/src/test/mir-opt/const_allocation3.main.ConstProp.after.64bit.mir index f3cbfcaca266c..fb481697aa811 100644 --- a/src/test/mir-opt/const_allocation3.main.ConstProp.after.64bit.mir +++ b/src/test/mir-opt/const_allocation3.main.ConstProp.after.64bit.mir @@ -19,3 +19,38 @@ fn main() -> () { return; // scope 0 at $DIR/const_allocation3.rs:6:2: 6:2 } } + +alloc1 (static: FOO, size: 8, align: 8) { + ╾───────alloc11───────╼ │ ╾──────╼ +} + +alloc11 (size: 180, align: 1) { + 0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................ + 0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾──alloc6── │ ............╾─── + 0x20 │ ──────────╼ 01 ef cd ab 00 00 00 00 00 00 00 00 │ ───╼............ + 0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x60 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x70 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x80 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ╾──── │ ..............╾─ + 0x90 │ ─────alloc8─────╼ 00 00 ╾─────alloc9+0x63─────╼ │ ─────╼..╾──────╼ + 0xa0 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0xb0 │ 00 00 00 00 │ .... +} + +alloc6 (size: 4, align: 4) { + 2a 00 00 00 │ *... +} + +alloc8 (fn: main) + +alloc9 (size: 100, align: 1) { + 0x00 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x20 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................ + 0x60 │ 00 00 00 00 │ .... +} diff --git a/src/test/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-elaborate-drops.after.mir b/src/test/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-elaborate-drops.after.mir index c6b15174e4691..666b805e822c1 100644 --- a/src/test/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-elaborate-drops.after.mir +++ b/src/test/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-elaborate-drops.after.mir @@ -17,3 +17,7 @@ promoted[0] in BAR: &[&i32; 1] = { return; // scope 0 at $DIR/const-promotion-extern-static.rs:9:31: 9:44 } } + +alloc1 (static: Y, size: 4, align: 4) { + 2a 00 00 00 │ *... +} diff --git a/src/test/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff b/src/test/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff index 84db15e1c8a87..ad83e9c276e66 100644 --- a/src/test/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff +++ b/src/test/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff @@ -49,5 +49,9 @@ bb2 (cleanup): { resume; // scope 0 at $DIR/const-promotion-extern-static.rs:9:1: 9:45 } +- } +- +- alloc1 (static: Y, size: 4, align: 4) { +- 2a 00 00 00 │ *... } diff --git a/src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-elaborate-drops.after.mir b/src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-elaborate-drops.after.mir index 8bfdf1acaa9f3..785c8386e88b5 100644 --- a/src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-elaborate-drops.after.mir +++ b/src/test/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-elaborate-drops.after.mir @@ -7,13 +7,15 @@ promoted[0] in FOO: &[&i32; 1] = { let mut _3: *const i32; // in scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 bb0: { - _3 = const {alloc2: *const i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 + _3 = const {alloc3: *const i32}; // scope 0 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 // mir::Constant // + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43 - // + literal: Const { ty: *const i32, val: Value(Scalar(alloc2)) } + // + literal: Const { ty: *const i32, val: Value(Scalar(alloc3)) } _2 = &(*_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:41: 13:43 _1 = [move _2]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46 _0 = &_1; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55 return; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55 } } + +alloc3 (extern static: X) diff --git a/src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff b/src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff index 3f34c4982b2f9..a9cf3ca976797 100644 --- a/src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff +++ b/src/test/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff @@ -18,14 +18,14 @@ - StorageLive(_3); // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46 - StorageLive(_4); // scope 0 at $DIR/const-promotion-extern-static.rs:13:32: 13:45 - StorageLive(_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 -- _5 = const {alloc2: *const i32}; // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 +- _5 = const {alloc3: *const i32}; // scope 1 at $DIR/const-promotion-extern-static.rs:13:42: 13:43 + _6 = const FOO::promoted[0]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55 + // ty::Const + // + ty: &[&i32; 1] + // + val: Unevaluated(FOO, [], Some(promoted[0])) // mir::Constant - // + span: $DIR/const-promotion-extern-static.rs:13:42: 13:43 -- // + literal: Const { ty: *const i32, val: Value(Scalar(alloc2)) } +- // + literal: Const { ty: *const i32, val: Value(Scalar(alloc3)) } - _4 = &(*_5); // scope 1 at $DIR/const-promotion-extern-static.rs:13:41: 13:43 - _3 = [move _4]; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:46 - _2 = &_3; // scope 0 at $DIR/const-promotion-extern-static.rs:13:31: 13:55 @@ -52,4 +52,6 @@ resume; // scope 0 at $DIR/const-promotion-extern-static.rs:13:1: 13:56 } } +- +- alloc3 (extern static: X) diff --git a/src/test/mir-opt/const_prop/mutable_variable_no_prop.main.ConstProp.diff b/src/test/mir-opt/const_prop/mutable_variable_no_prop.main.ConstProp.diff index cee2d4c0c0991..1fd92ddd46190 100644 --- a/src/test/mir-opt/const_prop/mutable_variable_no_prop.main.ConstProp.diff +++ b/src/test/mir-opt/const_prop/mutable_variable_no_prop.main.ConstProp.diff @@ -42,3 +42,7 @@ } } + alloc1 (static: STATIC, size: 4, align: 4) { + 2a 00 00 00 │ *... + } + diff --git a/src/test/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff b/src/test/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff index 7246b3bdb67bd..15b3e07664258 100644 --- a/src/test/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff +++ b/src/test/mir-opt/const_prop/read_immutable_static.main.ConstProp.diff @@ -42,3 +42,7 @@ } } + alloc1 (static: FOO, size: 1, align: 1) { + 02 │ . + } + From fff06e5edcb3c296164149b358566e63ed90b9d4 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 9 Feb 2022 16:18:57 +0100 Subject: [PATCH 6/7] use AllocId and Ty in ExprKind::StaticRef and delay ConstValue construction --- compiler/rustc_middle/src/thir.rs | 10 ++++++++-- compiler/rustc_middle/src/thir/visit.rs | 2 +- .../rustc_mir_build/src/build/expr/as_constant.rs | 10 +++++----- compiler/rustc_mir_build/src/thir/cx/expr.rs | 12 ++---------- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index 40dce281c82bf..a4e2a7c3e9169 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -17,6 +17,7 @@ use rustc_index::newtype_index; use rustc_index::vec::IndexVec; use rustc_middle::infer::canonical::Canonical; use rustc_middle::middle::region; +use rustc_middle::mir::interpret::AllocId; use rustc_middle::mir::{ BinOp, BorrowKind, FakeReadCause, Field, Mutability, UnOp, UserTypeProjection, }; @@ -419,7 +420,8 @@ pub enum ExprKind<'tcx> { /// This is only distinguished from `Literal` so that we can register some /// info for diagnostics. StaticRef { - literal: Const<'tcx>, + alloc_id: AllocId, + ty: Ty<'tcx>, def_id: DefId, }, /// Inline assembly, i.e. `asm!()`. @@ -715,7 +717,11 @@ impl<'tcx> fmt::Display for Pat<'tcx> { Some(&adt_def.variants[variant_index]) } _ => self.ty.ty_adt_def().and_then(|adt| { - if !adt.is_enum() { Some(adt.non_enum_variant()) } else { None } + if !adt.is_enum() { + Some(adt.non_enum_variant()) + } else { + None + } }), }; diff --git a/compiler/rustc_middle/src/thir/visit.rs b/compiler/rustc_middle/src/thir/visit.rs index 95489ac3ab2c6..b3e2cb132a273 100644 --- a/compiler/rustc_middle/src/thir/visit.rs +++ b/compiler/rustc_middle/src/thir/visit.rs @@ -123,7 +123,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp } Closure { closure_id: _, substs: _, upvars: _, movability: _, fake_reads: _ } => {} Literal { literal, user_ty: _, const_id: _ } => visitor.visit_const(literal), - StaticRef { literal, def_id: _ } => visitor.visit_const(literal), + StaticRef { .. } => {} InlineAsm { ref operands, template: _, options: _, line_spans: _ } => { for op in &**operands { use InlineAsmOperand::*; diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index 1136844377ab0..0c0b0f2bd05af 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -1,6 +1,7 @@ //! See docs in build/expr/mod.rs use crate::build::Builder; +use rustc_middle::mir::interpret::{ConstValue, Scalar}; use rustc_middle::mir::*; use rustc_middle::thir::*; use rustc_middle::ty::CanonicalUserTypeAnnotation; @@ -26,11 +27,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { assert_eq!(literal.ty(), ty); Constant { span, user_ty, literal: literal.into() } } - ExprKind::StaticRef { literal, .. } => { - let const_val = literal.val.try_to_value().unwrap_or_else(|| { - bug!("expected `ConstKind::Value`, but found {:?}", literal.val) - }); - let literal = ConstantKind::Val(const_val, literal.ty); + ExprKind::StaticRef { alloc_id, ty, .. } => { + let const_val = + ConstValue::Scalar(Scalar::from_pointer(alloc_id.into(), &this.tcx)); + let literal = ConstantKind::Val(const_val, ty); Constant { span, user_ty: None, literal } } diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 29216776de163..829dec74803dd 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -8,7 +8,6 @@ use rustc_middle::hir::place::Place as HirPlace; use rustc_middle::hir::place::PlaceBase as HirPlaceBase; use rustc_middle::hir::place::ProjectionKind as HirProjectionKind; use rustc_middle::middle::region; -use rustc_middle::mir::interpret::Scalar; use rustc_middle::mir::{BinOp, BorrowKind, Field, UnOp}; use rustc_middle::thir::*; use rustc_middle::ty::adjustment::{ @@ -943,15 +942,8 @@ impl<'tcx> Cx<'tcx> { let kind = if self.tcx.is_thread_local_static(id) { ExprKind::ThreadLocalRef(id) } else { - let ptr = self.tcx.create_static_alloc(id); - ExprKind::StaticRef { - literal: ty::Const::from_scalar( - self.tcx, - Scalar::from_pointer(ptr.into(), &self.tcx), - ty, - ), - def_id: id, - } + let alloc_id = self.tcx.create_static_alloc(id); + ExprKind::StaticRef { alloc_id, ty, def_id: id } }; ExprKind::Deref { arg: self.thir.exprs.push(Expr { ty, temp_lifetime, span: expr.span, kind }), From db019f2160495b27f4bb6856c91eb9aab67cb4da Mon Sep 17 00:00:00 2001 From: b-naber Date: Mon, 14 Feb 2022 16:48:05 +0100 Subject: [PATCH 7/7] try to bless 32bit mir tests manually --- Cargo.lock | 39 +++++++++++++++++-- compiler/rustc_middle/src/mir/pretty.rs | 6 +-- compiler/rustc_middle/src/thir.rs | 6 +-- ..._allocation.main.ConstProp.after.32bit.mir | 5 +-- ...allocation2.main.ConstProp.after.32bit.mir | 5 +-- ...allocation3.main.ConstProp.after.32bit.mir | 3 -- 6 files changed, 42 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe265a05146fb..b330155d14483 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -369,7 +369,7 @@ dependencies = [ "tar", "tempfile", "termcolor", - "toml", + "toml_edit", "unicode-width", "unicode-xid", "url 2.2.2", @@ -455,7 +455,7 @@ dependencies = [ "serde_json", "tar", "termcolor", - "toml", + "toml_edit", "url 2.2.2", ] @@ -710,6 +710,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "combine" +version = "4.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50b727aacc797f9fc28e355d21f34709ac4fc9adecfe470ad07b8f4464f53062" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "commoncrypto" version = "0.2.0" @@ -1939,6 +1949,15 @@ dependencies = [ "unicase", ] +[[package]] +name = "kstring" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b310ccceade8121d7d77fee406160e457c2f4e7c7982d589da3499bc7ea4526" +dependencies = [ + "serde", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -3146,13 +3165,14 @@ dependencies = [ "tokio-stream", "tokio-util", "toml", + "toml_edit", "url 2.2.2", "walkdir", ] [[package]] name = "rls-analysis" -version = "0.18.2" +version = "0.18.3" dependencies = [ "derive-new", "env_logger 0.9.0", @@ -5151,6 +5171,19 @@ dependencies = [ "serde", ] +[[package]] +name = "toml_edit" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "744e9ed5b352340aa47ce033716991b5589e23781acb97cad37d4ea70560f55b" +dependencies = [ + "combine", + "indexmap", + "itertools 0.10.1", + "kstring", + "serde", +] + [[package]] name = "topological-sort" version = "0.1.0" diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index c263eb6f321cf..784babffeff42 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -686,15 +686,15 @@ pub fn write_allocations<'tcx>( struct CollectAllocIds(BTreeSet); impl<'tcx> Visitor<'tcx> for CollectAllocIds { - fn visit_const(&mut self, c: &&'tcx ty::Const<'tcx>, _loc: Location) { - if let ty::ConstKind::Value(val) = c.val { + fn visit_const(&mut self, c: ty::Const<'tcx>, _loc: Location) { + if let ty::ConstKind::Value(val) = c.val() { self.0.extend(alloc_ids_from_const(val)); } } fn visit_constant(&mut self, c: &Constant<'tcx>, loc: Location) { match c.literal { - ConstantKind::Ty(c) => self.visit_const(&c, loc), + ConstantKind::Ty(c) => self.visit_const(c, loc), ConstantKind::Val(val, _) => { self.0.extend(alloc_ids_from_const(val)); } diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index a4e2a7c3e9169..04bc0c8b52114 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -717,11 +717,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> { Some(&adt_def.variants[variant_index]) } _ => self.ty.ty_adt_def().and_then(|adt| { - if !adt.is_enum() { - Some(adt.non_enum_variant()) - } else { - None - } + if !adt.is_enum() { Some(adt.non_enum_variant()) } else { None } }), }; diff --git a/src/test/mir-opt/const_allocation.main.ConstProp.after.32bit.mir b/src/test/mir-opt/const_allocation.main.ConstProp.after.32bit.mir index 8e02dca4fb8b6..f8a8afa92e0e6 100644 --- a/src/test/mir-opt/const_allocation.main.ConstProp.after.32bit.mir +++ b/src/test/mir-opt/const_allocation.main.ConstProp.after.32bit.mir @@ -9,12 +9,9 @@ fn main() -> () { StorageLive(_1); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8 StorageLive(_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8 _2 = const {alloc1: &&[(Option, &[&str])]}; // scope 0 at $DIR/const_allocation.rs:8:5: 8:8 - // ty::Const - // + ty: &&[(std::option::Option, &[&str])] - // + val: Value(Scalar(alloc1)) // mir::Constant // + span: $DIR/const_allocation.rs:8:5: 8:8 - // + literal: Const { ty: &&[(std::option::Option, &[&str])], val: Value(Scalar(alloc1)) } + // + literal: Const { ty: &&[(Option, &[&str])], val: Value(Scalar(alloc1)) } _1 = (*_2); // scope 0 at $DIR/const_allocation.rs:8:5: 8:8 StorageDead(_2); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9 StorageDead(_1); // scope 0 at $DIR/const_allocation.rs:8:8: 8:9 diff --git a/src/test/mir-opt/const_allocation2.main.ConstProp.after.32bit.mir b/src/test/mir-opt/const_allocation2.main.ConstProp.after.32bit.mir index 059b721f5bb63..8b5ad40c9f92c 100644 --- a/src/test/mir-opt/const_allocation2.main.ConstProp.after.32bit.mir +++ b/src/test/mir-opt/const_allocation2.main.ConstProp.after.32bit.mir @@ -9,12 +9,9 @@ fn main() -> () { StorageLive(_1); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8 StorageLive(_2); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8 _2 = const {alloc1: &&[(Option, &[&u8])]}; // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8 - // ty::Const - // + ty: &&[(std::option::Option, &[&u8])] - // + val: Value(Scalar(alloc1)) // mir::Constant // + span: $DIR/const_allocation2.rs:5:5: 5:8 - // + literal: Const { ty: &&[(std::option::Option, &[&u8])], val: Value(Scalar(alloc1)) } + // + literal: Const { ty: &&[(Option, &[&u8])], val: Value(Scalar(alloc1)) } _1 = (*_2); // scope 0 at $DIR/const_allocation2.rs:5:5: 5:8 StorageDead(_2); // scope 0 at $DIR/const_allocation2.rs:5:8: 5:9 StorageDead(_1); // scope 0 at $DIR/const_allocation2.rs:5:8: 5:9 diff --git a/src/test/mir-opt/const_allocation3.main.ConstProp.after.32bit.mir b/src/test/mir-opt/const_allocation3.main.ConstProp.after.32bit.mir index 32000340dcebf..991cf40d1b7ce 100644 --- a/src/test/mir-opt/const_allocation3.main.ConstProp.after.32bit.mir +++ b/src/test/mir-opt/const_allocation3.main.ConstProp.after.32bit.mir @@ -9,9 +9,6 @@ fn main() -> () { StorageLive(_1); // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8 StorageLive(_2); // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8 _2 = const {alloc1: &&Packed}; // scope 0 at $DIR/const_allocation3.rs:5:5: 5:8 - // ty::Const - // + ty: &&Packed - // + val: Value(Scalar(alloc1)) // mir::Constant // + span: $DIR/const_allocation3.rs:5:5: 5:8 // + literal: Const { ty: &&Packed, val: Value(Scalar(alloc1)) }