From e0106d99d699bbb427f4ebb156a343cc26159e11 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Mon, 29 Oct 2018 14:50:30 +0100 Subject: [PATCH 1/3] Assert that promoteds don't fail to be evaluated for being too generic --- src/librustc_mir/const_eval.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/librustc_mir/const_eval.rs b/src/librustc_mir/const_eval.rs index 7db9c3f110272..38ab21b7c8c85 100644 --- a/src/librustc_mir/const_eval.rs +++ b/src/librustc_mir/const_eval.rs @@ -576,7 +576,11 @@ pub fn const_eval_provider<'a, 'tcx>( key.param_env.reveal = Reveal::UserFacing; match tcx.const_eval(key) { // try again with reveal all as requested - Err(ErrorHandled::TooGeneric) => {}, + Err(ErrorHandled::TooGeneric) => { + // Promoteds should never be "too generic" when getting evaluated. + // They either don't get evaluated, or we are in a monomorphic context + assert!(key.value.promoted.is_none()); + }, // dedupliate calls other => return other, } From 3ad154f4842e9a1f09b5da37a76a333691f31784 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Mon, 29 Oct 2018 15:22:47 +0100 Subject: [PATCH 2/3] Fix wrong validation clasisfication of `Option<&T>::Some` values --- src/librustc_mir/interpret/validity.rs | 2 +- src/test/ui/consts/const-validation-fail-55455.rs | 9 +++++++++ src/test/ui/consts/promoted-validation-55454.rs | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/consts/const-validation-fail-55455.rs create mode 100644 src/test/ui/consts/promoted-validation-55454.rs diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index 226717538a294..982f3dab1080d 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -303,7 +303,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> let (lo, hi) = layout.valid_range.clone().into_inner(); let max_hi = u128::max_value() >> (128 - size.bits()); // as big as the size fits assert!(hi <= max_hi); - if lo == 0 && hi == max_hi { + if (lo == 0 && hi == max_hi) || (hi + 1 == lo) { // Nothing to check return Ok(()); } diff --git a/src/test/ui/consts/const-validation-fail-55455.rs b/src/test/ui/consts/const-validation-fail-55455.rs new file mode 100644 index 0000000000000..def4062339f96 --- /dev/null +++ b/src/test/ui/consts/const-validation-fail-55455.rs @@ -0,0 +1,9 @@ +// https://github.com/rust-lang/rust/issues/55454 +// compile-pass + +struct This(T); + +const C: This> = This(Some(&1)); + +fn main() { +} diff --git a/src/test/ui/consts/promoted-validation-55454.rs b/src/test/ui/consts/promoted-validation-55454.rs new file mode 100644 index 0000000000000..5e193b1b7de0b --- /dev/null +++ b/src/test/ui/consts/promoted-validation-55454.rs @@ -0,0 +1,9 @@ +// https://github.com/rust-lang/rust/issues/55454 +// compile-pass + +#[derive(PartialEq)] +struct This(T); + +fn main() { + This(Some(&1)) == This(Some(&1)); +} From 3e9d7e8aa8b73ea3d73940a573523fd498181b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20S=CC=B6c=CC=B6h=CC=B6n=CC=B6e=CC=B6i=CC=B6d=CC=B6?= =?UTF-8?q?e=CC=B6r=20Scherer?= Date: Mon, 29 Oct 2018 21:31:22 +0100 Subject: [PATCH 3/3] Add a comment explaining the two checks --- src/librustc_mir/interpret/validity.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index 982f3dab1080d..9d86e737dd5b6 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -303,6 +303,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> let (lo, hi) = layout.valid_range.clone().into_inner(); let max_hi = u128::max_value() >> (128 - size.bits()); // as big as the size fits assert!(hi <= max_hi); + // We could also write `(hi + 1) % (max_hi + 1) == lo` but `max_hi + 1` overflows for `u128` if (lo == 0 && hi == max_hi) || (hi + 1 == lo) { // Nothing to check return Ok(());