diff --git a/src/librustc_mir/transform/inline.rs b/src/librustc_mir/transform/inline.rs index 3f8070fb3aa31..47c8fd02330f7 100644 --- a/src/librustc_mir/transform/inline.rs +++ b/src/librustc_mir/transform/inline.rs @@ -88,12 +88,14 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> { if let TerminatorKind::Call { func: Operand::Constant(ref f), .. } = terminator.kind { if let ty::TyFnDef(callee_def_id, substs) = f.ty.sty { - callsites.push_back(CallSite { - callee: callee_def_id, - substs, - bb, - location: terminator.source_info - }); + if self.tcx.trait_of_item(callee_def_id).is_none() { + callsites.push_back(CallSite { + callee: callee_def_id, + substs, + bb, + location: terminator.source_info + }); + } } } } diff --git a/src/test/run-pass/mir-inlining/no-trait-method-issue-40473.rs b/src/test/run-pass/mir-inlining/no-trait-method-issue-40473.rs new file mode 100644 index 0000000000000..11a29d9741755 --- /dev/null +++ b/src/test/run-pass/mir-inlining/no-trait-method-issue-40473.rs @@ -0,0 +1,25 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags:-Zmir-opt-level=2 +pub trait Foo { + fn bar(&self) -> usize { 2 } +} + +impl Foo for () { + fn bar(&self) -> usize { 3 } +} + +// Test a case where MIR would inline the default trait method +// instead of bailing out. Issue #40473. +fn main() { + let result = ().bar(); + assert_eq!(result, 3); +}