From 5d19d4d894031c337e8b6d13802ebda622d30d23 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Wed, 25 Dec 2019 21:09:51 +0000 Subject: [PATCH] Use the correct type for static qualifs --- .../transform/check_consts/qualifs.rs | 17 ++++++++--------- src/test/ui/consts/const-eval/promote-static.rs | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 src/test/ui/consts/const-eval/promote-static.rs diff --git a/src/librustc_mir/transform/check_consts/qualifs.rs b/src/librustc_mir/transform/check_consts/qualifs.rs index 746d83b05c6..bd895171e35 100644 --- a/src/librustc_mir/transform/check_consts/qualifs.rs +++ b/src/librustc_mir/transform/check_consts/qualifs.rs @@ -1,6 +1,5 @@ //! A copy of the `Qualif` trait in `qualify_consts.rs` that is suitable for the new validator. -use rustc::hir::def_id::DefId; use rustc::mir::*; use rustc::ty::{self, Ty}; use syntax_pos::DUMMY_SP; @@ -33,12 +32,6 @@ pub trait Qualif { /// of the type. fn in_any_value_of_ty(_cx: &ConstCx<'_, 'tcx>, _ty: Ty<'tcx>) -> bool; - fn in_static(cx: &ConstCx<'_, 'tcx>, def_id: DefId) -> bool { - // `mir_const_qualif` does return the qualifs in the final value of a `static`, so we could - // use value-based qualification here, but we shouldn't do this without a good reason. - Self::in_any_value_of_ty(cx, cx.tcx.type_of(def_id)) - } - fn in_projection_structurally( cx: &ConstCx<'_, 'tcx>, per_local: &impl Fn(Local) -> bool, @@ -101,8 +94,14 @@ pub trait Qualif { } Operand::Constant(ref constant) => { - if let Some(static_) = constant.check_static_ptr(cx.tcx) { - Self::in_static(cx, static_) + if constant.check_static_ptr(cx.tcx).is_some() { + // `mir_const_qualif` does return the qualifs in the final value of a `static`, + // so we could use value-based qualification here, but we shouldn't do this + // without a good reason. + // + // Note: this uses `constant.literal.ty` which is a reference or pointer to the + // type of the actual `static` item. + Self::in_any_value_of_ty(cx, constant.literal.ty) } else if let ty::ConstKind::Unevaluated(def_id, _) = constant.literal.val { // Don't peek inside trait associated constants. if cx.tcx.trait_of_item(def_id).is_some() { diff --git a/src/test/ui/consts/const-eval/promote-static.rs b/src/test/ui/consts/const-eval/promote-static.rs new file mode 100644 index 00000000000..d3c663c53e9 --- /dev/null +++ b/src/test/ui/consts/const-eval/promote-static.rs @@ -0,0 +1,14 @@ +// regression test for #67609. + +// check-pass + +static NONE: Option = None; + +static NONE_REF_REF: &&Option = { + let x = &&NONE; + x +}; + +fn main() { + println!("{:?}", NONE_REF_REF); +}