-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
generate MIR for constants and statics #33106
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,34 +60,28 @@ struct OuterDump<'a, 'tcx: 'a> { | |
} | ||
|
||
impl<'a, 'tcx> OuterDump<'a, 'tcx> { | ||
fn visit_mir<OP>(&mut self, attributes: &'a [ast::Attribute], mut walk_op: OP) | ||
fn visit_mir<OP>(&mut self, mut walk_op: OP) | ||
where OP: for<'m> FnMut(&mut InnerDump<'a, 'm, 'tcx>) | ||
{ | ||
let mut closure_dump = InnerDump { | ||
tcx: self.tcx, | ||
attr: None, | ||
map: &mut *self.map, | ||
}; | ||
for attr in attributes { | ||
if attr.check_name("rustc_mir") { | ||
closure_dump.attr = Some(attr); | ||
} | ||
} | ||
walk_op(&mut closure_dump); | ||
} | ||
} | ||
|
||
|
||
impl<'a, 'tcx> Visitor<'tcx> for OuterDump<'a, 'tcx> { | ||
fn visit_item(&mut self, item: &'tcx hir::Item) { | ||
self.visit_mir(&item.attrs, |c| intravisit::walk_item(c, item)); | ||
self.visit_mir(|c| c.visit_item(item)); | ||
intravisit::walk_item(self, item); | ||
} | ||
|
||
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) { | ||
match trait_item.node { | ||
hir::MethodTraitItem(_, Some(_)) => { | ||
self.visit_mir(&trait_item.attrs, |c| intravisit::walk_trait_item(c, trait_item)); | ||
self.visit_mir(|c| intravisit::walk_trait_item(c, trait_item)); | ||
} | ||
hir::MethodTraitItem(_, None) | | ||
hir::ConstTraitItem(..) | | ||
|
@@ -99,7 +93,7 @@ impl<'a, 'tcx> Visitor<'tcx> for OuterDump<'a, 'tcx> { | |
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) { | ||
match impl_item.node { | ||
hir::ImplItemKind::Method(..) => { | ||
self.visit_mir(&impl_item.attrs, |c| intravisit::walk_impl_item(c, impl_item)); | ||
self.visit_mir(|c| intravisit::walk_impl_item(c, impl_item)); | ||
} | ||
hir::ImplItemKind::Const(..) | hir::ImplItemKind::Type(..) => {} | ||
} | ||
|
@@ -113,7 +107,6 @@ impl<'a, 'tcx> Visitor<'tcx> for OuterDump<'a, 'tcx> { | |
struct InnerDump<'a, 'm, 'tcx: 'a + 'm> { | ||
tcx: &'a TyCtxt<'tcx>, | ||
map: &'m mut MirMap<'tcx>, | ||
attr: Option<&'a ast::Attribute>, | ||
} | ||
|
||
impl<'a, 'm, 'tcx> Visitor<'tcx> for InnerDump<'a,'m,'tcx> { | ||
|
@@ -150,6 +143,32 @@ impl<'a, 'm, 'tcx> Visitor<'tcx> for InnerDump<'a,'m,'tcx> { | |
|
||
intravisit::walk_fn(self, fk, decl, body, span); | ||
} | ||
|
||
fn visit_item(&mut self, i: &'tcx hir::Item) { | ||
match i.node { | ||
hir::ItemConst(_, ref expr) | | ||
hir::ItemStatic(_, _, ref expr) => { | ||
let param_env = ty::ParameterEnvironment::for_item(self.tcx, i.id); | ||
let infcx = infer::new_infer_ctxt(self.tcx, | ||
&self.tcx.tables, | ||
Some(param_env), | ||
ProjectionMode::AnyFinal); | ||
let (mir, scope_auxiliary) = build::construct_expr(Cx::new(&infcx), | ||
i.id, | ||
i.span, | ||
expr); | ||
pretty::dump_mir(self.tcx, | ||
"mir_map", | ||
&0, | ||
i.id, | ||
&mir, | ||
Some(&scope_auxiliary)); | ||
assert!(self.map.map.insert(i.id, mir).is_none()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it makes sense to generate a I realise we do not have a constant propagator for MIR yet, but a FIXME or a TODO in that direction would be nice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh... the reason I did this is so we can use miri to evaluate them. This entire PR is just so we can evaluate them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, a PR like this is a prerequisite for experimenting with replacing the current constant evaluator with Miri. Currently Miri is stumped by statics and non-trivial constants since they are given by HIR that it can't interpret and there is no compiler-plugin-accessible way to lower HIR to MIR last time I checked. |
||
}, | ||
_ => {}, | ||
} | ||
intravisit::walk_item(self, i); | ||
} | ||
} | ||
|
||
fn build_mir<'a,'tcx:'a>(cx: Cx<'a,'tcx>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// This test does nothing useful yet, it just generates MIR for constants | ||
|
||
#![feature(rustc_attrs)] | ||
|
||
#[rustc_mir] | ||
const C: i32 = 5 + 6; | ||
|
||
#[rustc_mir] | ||
const D: i32 = C - 3; | ||
|
||
fn main() { | ||
assert_eq!(D, 8); | ||
assert_eq!(C, 11); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should the extent be for the item or the expression?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For constants and statics, item. For arbitrary expressions the extent of the binding to which the expression value is bound to.