Skip to content

Commit

Permalink
rustc_const_eval: demand that the MIR qualify_consts ran on each eval…
Browse files Browse the repository at this point in the history
…uated body.
  • Loading branch information
eddyb committed Feb 25, 2017
1 parent 2029c71 commit afc00b0
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 112 deletions.
5 changes: 5 additions & 0 deletions src/librustc/ty/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ define_maps! { <'tcx>
/// (in the `RefCell` sense) to prevent accidental mutation.
pub mir: Mir(DefId) -> &'tcx RefCell<mir::Mir<'tcx>>,

/// Maps DefId's that have an associated Mir to the result
/// of the MIR qualify_consts pass. The actual meaning of
/// the value isn't known except to the pass itself.
pub mir_const_qualif: Mir(DefId) -> u8,

/// Records the type of each closure. The def ID is the ID of the
/// expression defining the closure.
pub closure_kind: ItemSignature(DefId) -> ty::ClosureKind,
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_const_eval/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use rustc::util::nodemap::DefIdMap;
use graphviz::IntoCow;
use syntax::ast;
use rustc::hir::{self, Expr};
use syntax_pos::Span;
use syntax_pos::{Span, DUMMY_SP};

use std::borrow::Cow;
use std::cmp::Ordering;
Expand Down Expand Up @@ -228,6 +228,7 @@ pub struct ConstContext<'a, 'tcx: 'a> {
impl<'a, 'tcx> ConstContext<'a, 'tcx> {
pub fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, body: hir::BodyId) -> Self {
let def_id = tcx.hir.body_owner_def_id(body);
ty::queries::mir_const_qualif::get(tcx, DUMMY_SP, def_id);
ConstContext::with_tables(tcx, tcx.item_tables(def_id))
}

Expand Down
5 changes: 2 additions & 3 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
let index = stability::Index::new(&hir_map);

let mut local_providers = ty::maps::Providers::default();
mir::mir_map::provide(&mut local_providers);
mir::provide(&mut local_providers);
typeck::provide(&mut local_providers);

let mut extern_providers = ty::maps::Providers::default();
Expand Down Expand Up @@ -958,8 +958,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
// in stage 4 below.
passes.push_hook(box mir::transform::dump_mir::DumpMir);
passes.push_pass(box mir::transform::simplify::SimplifyCfg::new("initial"));
passes.push_pass(
box mir::transform::qualify_consts::QualifyAndPromoteConstants::default());
passes.push_pass(box mir::transform::qualify_consts::QualifyAndPromoteConstants);
passes.push_pass(box mir::transform::type_check::TypeckMir);
passes.push_pass(
box mir::transform::simplify_branches::SimplifyBranches::new("initial"));
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ provide! { <'tcx> tcx, def_id, cdata

mir
}
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }
typeck_tables => { cdata.item_body_tables(def_id.index, tcx) }
closure_kind => { cdata.closure_kind(def_id.index) }
closure_type => { cdata.closure_ty(def_id.index, tcx) }
Expand Down
17 changes: 14 additions & 3 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ impl<'a, 'tcx> MetadataBlob {
impl<'tcx> EntryKind<'tcx> {
fn to_def(&self, did: DefId) -> Option<Def> {
Some(match *self {
EntryKind::Const => Def::Const(did),
EntryKind::AssociatedConst(_) => Def::AssociatedConst(did),
EntryKind::Const(_) => Def::Const(did),
EntryKind::AssociatedConst(..) => Def::AssociatedConst(did),
EntryKind::ImmStatic |
EntryKind::ForeignImmStatic => Def::Static(did, false),
EntryKind::MutStatic |
Expand Down Expand Up @@ -825,14 +825,25 @@ impl<'a, 'tcx> CrateMetadata {
}
}

pub fn mir_const_qualif(&self, id: DefIndex) -> u8 {
match self.entry(id).kind {
EntryKind::Const(qualif) |
EntryKind::AssociatedConst(AssociatedContainer::ImplDefault, qualif) |
EntryKind::AssociatedConst(AssociatedContainer::ImplFinal, qualif) => {
qualif
}
_ => bug!(),
}
}

pub fn get_associated_item(&self, id: DefIndex) -> ty::AssociatedItem {
let item = self.entry(id);
let def_key = self.def_key(id);
let parent = self.local_def_id(def_key.parent.unwrap());
let name = def_key.disambiguated_data.data.get_opt_name().unwrap();

let (kind, container, has_self) = match item.kind {
EntryKind::AssociatedConst(container) => {
EntryKind::AssociatedConst(container, _) => {
(ty::AssociatedKind::Const, container, false)
}
EntryKind::Method(data) => {
Expand Down
13 changes: 10 additions & 3 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
};

let kind = match trait_item.kind {
ty::AssociatedKind::Const => EntryKind::AssociatedConst(container),
ty::AssociatedKind::Const => {
EntryKind::AssociatedConst(container, 0)
}
ty::AssociatedKind::Method => {
let fn_data = if let hir::TraitItemKind::Method(_, ref m) = ast_item.node {
let arg_names = match *m {
Expand Down Expand Up @@ -533,7 +535,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
};

let kind = match impl_item.kind {
ty::AssociatedKind::Const => EntryKind::AssociatedConst(container),
ty::AssociatedKind::Const => {
EntryKind::AssociatedConst(container,
ty::queries::mir_const_qualif::get(self.tcx, ast_item.span, def_id))
}
ty::AssociatedKind::Method => {
let fn_data = if let hir::ImplItemKind::Method(ref sig, body) = ast_item.node {
FnData {
Expand Down Expand Up @@ -637,7 +642,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let kind = match item.node {
hir::ItemStatic(_, hir::MutMutable, _) => EntryKind::MutStatic,
hir::ItemStatic(_, hir::MutImmutable, _) => EntryKind::ImmStatic,
hir::ItemConst(..) => EntryKind::Const,
hir::ItemConst(..) => {
EntryKind::Const(ty::queries::mir_const_qualif::get(tcx, item.span, def_id))
}
hir::ItemFn(_, _, constness, .., body) => {
let data = FnData {
constness: constness,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ pub struct Entry<'tcx> {

#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]
pub enum EntryKind<'tcx> {
Const,
Const(u8),
ImmStatic,
MutStatic,
ForeignImmStatic,
Expand All @@ -243,7 +243,7 @@ pub enum EntryKind<'tcx> {
DefaultImpl(Lazy<ImplData<'tcx>>),
Method(Lazy<MethodData>),
AssociatedType(AssociatedContainer),
AssociatedConst(AssociatedContainer),
AssociatedConst(AssociatedContainer, u8),
}

#[derive(RustcEncodable, RustcDecodable)]
Expand Down
6 changes: 6 additions & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,9 @@ pub mod mir_map;
pub mod pretty;
pub mod transform;

use rustc::ty::maps::Providers;

pub fn provide(providers: &mut Providers) {
mir_map::provide(providers);
transform::qualify_consts::provide(providers);
}
Loading

0 comments on commit afc00b0

Please sign in to comment.