Skip to content

Commit 0111892

Browse files
committed
Auto merge of #30587 - oli-obk:eager_const_eval2, r=nikomatsakis
typestrong const integers ~~It would be great if someone could run crater on this PR, as this has a high danger of breaking valid code~~ Crater ran. Good to go. ---- So this PR does a few things: 1. ~~const eval array values when const evaluating an array expression~~ 2. ~~const eval repeat value when const evaluating a repeat expression~~ 3. ~~const eval all struct and tuple fields when evaluating a struct/tuple expression~~ 4. remove the `ConstVal::Int` and `ConstVal::Uint` variants and replace them with a single enum (`ConstInt`) which has variants for all integral types * `usize`/`isize` are also enums with variants for 32 and 64 bit. At creation and various usage steps there are assertions in place checking if the target bitwidth matches with the chosen enum variant 5. enum discriminants (`ty::Disr`) are now `ConstInt` 6. trans has its own `Disr` type now (newtype around `u64`) This obviously can't be done without breaking changes (the ones that are noticable in stable) We could probably write lints that find those situations and error on it for a cycle or two. But then again, those situations are rare and really bugs imo anyway: ```rust let v10 = 10 as i8; let v4 = 4 as isize; assert_eq!(v10 << v4 as usize, 160 as i8); ``` stops compiling because 160 is not a valid i8 ```rust struct S<T, S> { a: T, b: u8, c: S } let s = S { a: 0xff_ff_ff_ffu32, b: 1, c: 0xaa_aa_aa_aa as i32 }; ``` stops compiling because `0xaa_aa_aa_aa` is not a valid i32 ---- cc @eddyb @pnkfelix related: rust-lang/rfcs#1071
2 parents d19f1b6 + f665c39 commit 0111892

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1823
-1041
lines changed

mk/crates.mk

+10-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ TARGET_CRATES := libc std term \
5353
getopts collections test rand \
5454
core alloc \
5555
rustc_unicode rustc_bitflags \
56-
alloc_system alloc_jemalloc
56+
alloc_system alloc_jemalloc rustc_const_eval
5757
RUSTC_CRATES := rustc rustc_typeck rustc_mir rustc_borrowck rustc_resolve rustc_driver \
5858
rustc_trans rustc_back rustc_llvm rustc_privacy rustc_lint \
5959
rustc_data_structures rustc_front rustc_platform_intrinsics \
@@ -91,8 +91,11 @@ DEPS_test := std getopts term native:rust_test_helpers
9191
DEPS_syntax := std term serialize log arena libc rustc_bitflags rustc_unicode
9292
DEPS_syntax_ext := syntax fmt_macros
9393

94+
DEPS_rustc_const_eval := std syntax
95+
9496
DEPS_rustc := syntax fmt_macros flate arena serialize getopts rbml rustc_front\
95-
log graphviz rustc_llvm rustc_back rustc_data_structures
97+
log graphviz rustc_llvm rustc_back rustc_data_structures\
98+
rustc_const_eval
9699
DEPS_rustc_back := std syntax rustc_llvm rustc_front flate log libc
97100
DEPS_rustc_borrowck := rustc rustc_front log graphviz syntax
98101
DEPS_rustc_data_structures := std log serialize
@@ -103,16 +106,17 @@ DEPS_rustc_driver := arena flate getopts graphviz libc rustc rustc_back rustc_bo
103106
DEPS_rustc_front := std syntax log serialize
104107
DEPS_rustc_lint := rustc log syntax
105108
DEPS_rustc_llvm := native:rustllvm libc std rustc_bitflags
106-
DEPS_rustc_metadata := rustc rustc_front syntax rbml
109+
DEPS_rustc_metadata := rustc rustc_front syntax rbml rustc_const_eval
107110
DEPS_rustc_passes := syntax rustc core rustc_front
108-
DEPS_rustc_mir := rustc rustc_front syntax
111+
DEPS_rustc_mir := rustc rustc_front syntax rustc_const_eval
109112
DEPS_rustc_resolve := arena rustc rustc_front log syntax
110113
DEPS_rustc_platform_intrinsics := rustc rustc_llvm
111114
DEPS_rustc_plugin := rustc rustc_metadata syntax rustc_mir
112115
DEPS_rustc_privacy := rustc rustc_front log syntax
113116
DEPS_rustc_trans := arena flate getopts graphviz libc rustc rustc_back rustc_mir \
114-
log syntax serialize rustc_llvm rustc_front rustc_platform_intrinsics
115-
DEPS_rustc_typeck := rustc syntax rustc_front rustc_platform_intrinsics
117+
log syntax serialize rustc_llvm rustc_front rustc_platform_intrinsics \
118+
rustc_const_eval
119+
DEPS_rustc_typeck := rustc syntax rustc_front rustc_platform_intrinsics rustc_const_eval
116120

117121
DEPS_rustdoc := rustc rustc_driver native:hoedown serialize getopts \
118122
test rustc_lint rustc_front

src/librustc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ log = { path = "../liblog" }
1818
rbml = { path = "../librbml" }
1919
rustc_back = { path = "../librustc_back" }
2020
rustc_bitflags = { path = "../librustc_bitflags" }
21+
rustc_const_eval = { path = "../librustc_const_eval" }
2122
rustc_data_structures = { path = "../librustc_data_structures" }
2223
rustc_front = { path = "../librustc_front" }
2324
rustc_llvm = { path = "../librustc_llvm" }

src/librustc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ extern crate rustc_front;
5555
extern crate rustc_data_structures;
5656
extern crate serialize;
5757
extern crate collections;
58+
extern crate rustc_const_eval;
5859
#[macro_use] extern crate log;
5960
#[macro_use] extern crate syntax;
6061
#[macro_use] #[no_link] extern crate rustc_bitflags;

src/librustc/middle/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl<'a, 'tcx> Folder for StaticInliner<'a, 'tcx> {
476476
Some(Def::AssociatedConst(did)) |
477477
Some(Def::Const(did)) => match lookup_const_by_id(self.tcx, did,
478478
Some(pat.id), None) {
479-
Some(const_expr) => {
479+
Some((const_expr, _const_ty)) => {
480480
const_expr_to_pat(self.tcx, const_expr, pat.span).map(|new_pat| {
481481

482482
if let Some(ref mut renaming_map) = self.renaming_map {

0 commit comments

Comments
 (0)