From b413596da6b17537dd8c8b626fbad97f921a2759 Mon Sep 17 00:00:00 2001 From: Douglas Campos Date: Wed, 6 Sep 2017 22:06:50 -0400 Subject: [PATCH 1/4] add broken test --- .../no-trait-method-issue-40473.rs | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/test/run-pass/mir-inlining/no-trait-method-issue-40473.rs 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 00000000000..3a18fcfd288 --- /dev/null +++ b/src/test/run-pass/mir-inlining/no-trait-method-issue-40473.rs @@ -0,0 +1,23 @@ +// 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 } +} + +fn main() { + let result = ().bar(); + assert_eq!(result, 3); +} From 784e4316ebf24c516fb956b02c661ad51031b4cb Mon Sep 17 00:00:00 2001 From: Douglas Campos Date: Wed, 6 Sep 2017 23:03:41 -0400 Subject: [PATCH 2/4] ugly, but works! --- src/librustc_mir/transform/inline.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/librustc_mir/transform/inline.rs b/src/librustc_mir/transform/inline.rs index 3f8070fb3aa..4477496f231 100644 --- a/src/librustc_mir/transform/inline.rs +++ b/src/librustc_mir/transform/inline.rs @@ -88,12 +88,22 @@ 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 - }); + let should_inline = match self.tcx.opt_associated_item(callee_def_id) { + Some(item) => match item.container { + ty::AssociatedItemContainer::ImplContainer(_) => true, + ty::AssociatedItemContainer::TraitContainer(_) => false, + }, + None => true + }; + + if should_inline { + callsites.push_back(CallSite { + callee: callee_def_id, + substs, + bb, + location: terminator.source_info + }); + } } } } From 72c92b3233472c7aaf756a4fd02678f065d2f633 Mon Sep 17 00:00:00 2001 From: Douglas Campos Date: Wed, 6 Sep 2017 23:10:08 -0400 Subject: [PATCH 3/4] better test documentation --- src/test/run-pass/mir-inlining/no-trait-method-issue-40473.rs | 2 ++ 1 file changed, 2 insertions(+) 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 index 3a18fcfd288..11a29d97417 100644 --- 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 @@ -17,6 +17,8 @@ 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); From 10a5b539c7f9db4930f0cadad3805fd66464ecf9 Mon Sep 17 00:00:00 2001 From: Douglas Campos Date: Fri, 8 Sep 2017 11:06:28 -0400 Subject: [PATCH 4/4] simplify --- src/librustc_mir/transform/inline.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/librustc_mir/transform/inline.rs b/src/librustc_mir/transform/inline.rs index 4477496f231..47c8fd02330 100644 --- a/src/librustc_mir/transform/inline.rs +++ b/src/librustc_mir/transform/inline.rs @@ -88,15 +88,7 @@ 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 { - let should_inline = match self.tcx.opt_associated_item(callee_def_id) { - Some(item) => match item.container { - ty::AssociatedItemContainer::ImplContainer(_) => true, - ty::AssociatedItemContainer::TraitContainer(_) => false, - }, - None => true - }; - - if should_inline { + if self.tcx.trait_of_item(callee_def_id).is_none() { callsites.push_back(CallSite { callee: callee_def_id, substs,