Skip to content

Commit

Permalink
[mir-inlining] Don't inline virtual calls
Browse files Browse the repository at this point in the history
Prior to this change, the test case would output `1` instead of `2` like
it should.
  • Loading branch information
wesleywiser committed Oct 14, 2018
1 parent 24faa97 commit 69eaa11
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/librustc_mir/transform/inline.rs
Expand Up @@ -19,7 +19,7 @@ use rustc_data_structures::indexed_vec::{Idx, IndexVec};

use rustc::mir::*;
use rustc::mir::visit::*;
use rustc::ty::{self, Instance, Ty, TyCtxt};
use rustc::ty::{self, Instance, InstanceDef, Ty, TyCtxt};
use rustc::ty::subst::{Subst,Substs};

use std::collections::VecDeque;
Expand Down Expand Up @@ -100,12 +100,21 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
param_env,
callee_def_id,
substs) {
callsites.push_back(CallSite {
callee: instance.def_id(),
substs: instance.substs,
bb,
location: terminator.source_info
});
let is_virtual =
if let InstanceDef::Virtual(..) = instance.def {
true
} else {
false
};

if !is_virtual {
callsites.push_back(CallSite {
callee: instance.def_id(),
substs: instance.substs,
bb,
location: terminator.source_info
});
}
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/mir-opt/inline-trait-method.rs
@@ -0,0 +1,31 @@
// compile-flags: -Z span_free_formats

fn main() {
println!("{}", test(&()));
}

fn test(x: &dyn X) -> u32 {
x.y()
}

trait X {
fn y(&self) -> u32 {
1
}
}

impl X for () {
fn y(&self) -> u32 {
2
}
}

// END RUST SOURCE
// START rustc.test.Inline.after.mir
// ...
// bb0: {
// ...
// _0 = const X::y(move _2) -> bb1;
// }
// ...
// END rustc.test.Inline.after.mir

0 comments on commit 69eaa11

Please sign in to comment.