Skip to content

Commit 3acb6aa

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 3675520 + 3959ca3 commit 3acb6aa

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

clean/inline.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ pub fn build_impl(cx: &DocContext,
337337
let type_scheme = tcx.lookup_item_type(did);
338338
let default = if assoc_const.has_value {
339339
Some(const_eval::lookup_const_by_id(tcx, did, None, None)
340-
.unwrap().span.to_src(cx))
340+
.unwrap().0.span.to_src(cx))
341341
} else {
342342
None
343343
};
@@ -479,15 +479,15 @@ fn build_const(cx: &DocContext, tcx: &TyCtxt,
479479
use rustc::middle::const_eval;
480480
use rustc_front::print::pprust;
481481

482-
let expr = const_eval::lookup_const_by_id(tcx, did, None, None).unwrap_or_else(|| {
482+
let (expr, ty) = const_eval::lookup_const_by_id(tcx, did, None, None).unwrap_or_else(|| {
483483
panic!("expected lookup_const_by_id to succeed for {:?}", did);
484484
});
485485
debug!("converting constant expr {:?} to snippet", expr);
486486
let sn = pprust::expr_to_string(expr);
487487
debug!("got snippet {}", sn);
488488

489489
clean::Constant {
490-
type_: tcx.lookup_item_type(did).ty.clean(cx),
490+
type_: ty.map(|t| t.clean(cx)).unwrap_or_else(|| tcx.lookup_item_type(did).ty.clean(cx)),
491491
expr: sn
492492
}
493493
}

0 commit comments

Comments
 (0)