Skip to content

Commit 70c10f1

Browse files
committed
Introduce an edge from a const eval to the MIR of all statics it depends on
1 parent 3615093 commit 70c10f1

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/librustc_mir/interpret/const_eval.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc::mir;
55
use rustc::ty::{self, TyCtxt, Ty, Instance};
66
use rustc::ty::layout::{self, LayoutOf};
77
use rustc::ty::subst::Subst;
8+
use rustc::util::nodemap::FxHashSet;
89

910
use syntax::ast::Mutability;
1011
use syntax::codemap::Span;
@@ -504,7 +505,13 @@ pub fn const_eval_provider<'a, 'tcx>(
504505
};
505506

506507
let (res, ecx) = eval_body_and_ecx(tcx, cid, None, key.param_env);
507-
res.map(|(miri_value, _, miri_ty)| {
508+
res.map(|(miri_value, ptr, miri_ty)| {
509+
if tcx.is_static(def_id).is_some() {
510+
if let Ok(ptr) = ptr.primval.to_ptr() {
511+
let mut seen = FxHashSet::default();
512+
create_depgraph_edges(tcx, ptr.alloc_id, &mut seen);
513+
}
514+
}
508515
tcx.mk_const(ty::Const {
509516
val: ConstVal::Value(miri_value),
510517
ty: miri_ty,
@@ -521,3 +528,24 @@ pub fn const_eval_provider<'a, 'tcx>(
521528
}
522529
})
523530
}
531+
532+
fn create_depgraph_edges<'a, 'tcx>(
533+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
534+
alloc_id: AllocId,
535+
seen: &mut FxHashSet<AllocId>,
536+
) {
537+
trace!("create_depgraph_edges: {:?}, {:?}", alloc_id, seen);
538+
if seen.insert(alloc_id) {
539+
trace!("seen: {:?}, {:?}", alloc_id, seen);
540+
if let Some(alloc) = tcx.interpret_interner.get_alloc(alloc_id) {
541+
trace!("get_alloc: {:?}, {:?}, {:?}", alloc_id, seen, alloc);
542+
for (_, &reloc) in &alloc.relocations {
543+
if let Some(did) = tcx.interpret_interner.get_corresponding_static_def_id(reloc) {
544+
trace!("get_corresponding: {:?}, {:?}, {:?}, {:?}, {:?}", alloc_id, seen, alloc, did, reloc);
545+
let _ = tcx.maybe_optimized_mir(did);
546+
}
547+
create_depgraph_edges(tcx, reloc, seen);
548+
}
549+
}
550+
}
551+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// revisions:rpass1 rpass2
12+
13+
#[cfg(rpass1)]
14+
pub static A: i32 = 42;
15+
#[cfg(rpass2)]
16+
pub static A: i32 = 43;
17+
18+
pub static B: &i32 = &A;
19+
20+
fn main() {}

0 commit comments

Comments
 (0)