Skip to content

Commit 9097332

Browse files
committed
Auto merge of #44383 - qmx:gh/40473/no-inline-trait-method, r=nikomatsakis
MIR: should not inline trait method Fixes #40473. The idea here is bailing out of inlining if we're talking about a trait method.
2 parents e6bce95 + 10a5b53 commit 9097332

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/librustc_mir/transform/inline.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
8888
if let TerminatorKind::Call {
8989
func: Operand::Constant(ref f), .. } = terminator.kind {
9090
if let ty::TyFnDef(callee_def_id, substs) = f.ty.sty {
91-
callsites.push_back(CallSite {
92-
callee: callee_def_id,
93-
substs,
94-
bb,
95-
location: terminator.source_info
96-
});
91+
if self.tcx.trait_of_item(callee_def_id).is_none() {
92+
callsites.push_back(CallSite {
93+
callee: callee_def_id,
94+
substs,
95+
bb,
96+
location: terminator.source_info
97+
});
98+
}
9799
}
98100
}
99101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 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+
// compile-flags:-Zmir-opt-level=2
12+
pub trait Foo {
13+
fn bar(&self) -> usize { 2 }
14+
}
15+
16+
impl Foo for () {
17+
fn bar(&self) -> usize { 3 }
18+
}
19+
20+
// Test a case where MIR would inline the default trait method
21+
// instead of bailing out. Issue #40473.
22+
fn main() {
23+
let result = ().bar();
24+
assert_eq!(result, 3);
25+
}

0 commit comments

Comments
 (0)