Skip to content

Commit

Permalink
Rollup merge of #67621 - matthewjasper:correct-static-type, r=oli-obk
Browse files Browse the repository at this point in the history
Use the correct type for static qualifs

Closes #67609
  • Loading branch information
oli-obk committed Dec 27, 2019
2 parents 98de504 + 5d19d4d commit b88ce0e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/librustc_mir/transform/check_consts/qualifs.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/consts/const-eval/promote-static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// regression test for #67609.

// check-pass

static NONE: Option<String> = None;

static NONE_REF_REF: &&Option<String> = {
let x = &&NONE;
x
};

fn main() {
println!("{:?}", NONE_REF_REF);
}

0 comments on commit b88ce0e

Please sign in to comment.