From 2ecce7ccc5d74eb3d6b0ff400b2a19944e2ae51a Mon Sep 17 00:00:00 2001 From: David Wood Date: Thu, 11 Oct 2018 19:36:51 +0200 Subject: Extend lang items to assert correct target. This commit extends the existing lang items functionality to assert that the `#[lang_item]` attribute is only found on the appropriate item for any given lang item. That is, language items representing traits must only ever have their corresponding attribute placed on a trait, for example. --- src/test/ui/error-codes/E0152.rs | 2 +- src/test/ui/error-codes/E0152.stderr | 4 ++-- src/test/ui/error-codes/E0718.rs | 17 +++++++++++++++++ src/test/ui/error-codes/E0718.stderr | 9 +++++++++ .../ui/panic-handler/panic-handler-wrong-location.rs | 18 ++++++++++++++++++ .../panic-handler/panic-handler-wrong-location.stderr | 11 +++++++++++ 6 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/error-codes/E0718.rs create mode 100644 src/test/ui/error-codes/E0718.stderr create mode 100644 src/test/ui/panic-handler/panic-handler-wrong-location.rs create mode 100644 src/test/ui/panic-handler/panic-handler-wrong-location.stderr (limited to 'src/test') diff --git a/src/test/ui/error-codes/E0152.rs b/src/test/ui/error-codes/E0152.rs index 8fbad7b3ff3..96a4d51bd24 100644 --- a/src/test/ui/error-codes/E0152.rs +++ b/src/test/ui/error-codes/E0152.rs @@ -10,7 +10,7 @@ #![feature(lang_items)] -#[lang = "panic_impl"] +#[lang = "arc"] struct Foo; //~ ERROR E0152 fn main() { diff --git a/src/test/ui/error-codes/E0152.stderr b/src/test/ui/error-codes/E0152.stderr index c7f5f362efb..a0530f24de6 100644 --- a/src/test/ui/error-codes/E0152.stderr +++ b/src/test/ui/error-codes/E0152.stderr @@ -1,10 +1,10 @@ -error[E0152]: duplicate lang item found: `panic_impl`. +error[E0152]: duplicate lang item found: `arc`. --> $DIR/E0152.rs:14:1 | LL | struct Foo; //~ ERROR E0152 | ^^^^^^^^^^^ | - = note: first defined in crate `std`. + = note: first defined in crate `alloc`. error: aborting due to previous error diff --git a/src/test/ui/error-codes/E0718.rs b/src/test/ui/error-codes/E0718.rs new file mode 100644 index 00000000000..ce74e35ac6b --- /dev/null +++ b/src/test/ui/error-codes/E0718.rs @@ -0,0 +1,17 @@ +// Copyright 2013 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. + +#![feature(lang_items)] + +// Arc is expected to be a struct, so this will error. +#[lang = "arc"] +static X: u32 = 42; + +fn main() {} diff --git a/src/test/ui/error-codes/E0718.stderr b/src/test/ui/error-codes/E0718.stderr new file mode 100644 index 00000000000..8ce721d30a1 --- /dev/null +++ b/src/test/ui/error-codes/E0718.stderr @@ -0,0 +1,9 @@ +error[E0718]: `arc` language item must be applied to a struct + --> $DIR/E0718.rs:14:1 + | +LL | #[lang = "arc"] + | ^^^^^^^^^^^^^^^ attribute should be applied to a struct, not a static item + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0718`. diff --git a/src/test/ui/panic-handler/panic-handler-wrong-location.rs b/src/test/ui/panic-handler/panic-handler-wrong-location.rs new file mode 100644 index 00000000000..04e02682bc1 --- /dev/null +++ b/src/test/ui/panic-handler/panic-handler-wrong-location.rs @@ -0,0 +1,18 @@ +// Copyright 2018 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:-C panic=abort + +#![no_std] +#![no_main] + +#[panic_handler] +#[no_mangle] +static X: u32 = 42; diff --git a/src/test/ui/panic-handler/panic-handler-wrong-location.stderr b/src/test/ui/panic-handler/panic-handler-wrong-location.stderr new file mode 100644 index 00000000000..f761e26b86e --- /dev/null +++ b/src/test/ui/panic-handler/panic-handler-wrong-location.stderr @@ -0,0 +1,11 @@ +error[E0718]: `panic_impl` language item must be applied to a function + --> $DIR/panic-handler-wrong-location.rs:16:1 + | +LL | #[panic_handler] + | ^^^^^^^^^^^^^^^^ attribute should be applied to a function, not a static item + +error: `#[panic_handler]` function required, but not found + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0718`. -- cgit 1.4.1-3-g733a5 From 146fbc6a96b50a4eccd13b8908f36d7ce2f6af9f Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 12 Oct 2018 12:42:33 -0400 Subject: Add test for issue #54966 Closes #54966 --- src/test/ui/issues/issue-54966.rs | 6 ++++++ src/test/ui/issues/issue-54966.stderr | 9 +++++++++ 2 files changed, 15 insertions(+) create mode 100644 src/test/ui/issues/issue-54966.rs create mode 100644 src/test/ui/issues/issue-54966.stderr (limited to 'src/test') diff --git a/src/test/ui/issues/issue-54966.rs b/src/test/ui/issues/issue-54966.rs new file mode 100644 index 00000000000..0ed3c4b3ca9 --- /dev/null +++ b/src/test/ui/issues/issue-54966.rs @@ -0,0 +1,6 @@ +// issue-54966: ICE returning an unknown type with impl FnMut + +fn generate_duration() -> Oper {} +//~^ ERROR cannot find type `Oper` in this scope + +fn main() {} diff --git a/src/test/ui/issues/issue-54966.stderr b/src/test/ui/issues/issue-54966.stderr new file mode 100644 index 00000000000..aa9a61cb592 --- /dev/null +++ b/src/test/ui/issues/issue-54966.stderr @@ -0,0 +1,9 @@ +error[E0412]: cannot find type `Oper` in this scope + --> $DIR/issue-54966.rs:3:27 + | +LL | fn generate_duration() -> Oper {} + | ^^^^ not found in this scope + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0412`. -- cgit 1.4.1-3-g733a5 From 0e268d9e898f76c16fd82d13ef47eca521374789 Mon Sep 17 00:00:00 2001 From: Andy Russell Date: Fri, 12 Oct 2018 13:07:39 -0400 Subject: add test for #24421 Fixes #24421. --- src/test/ui/closure-expected-type/README.md | 1 - src/test/ui/closure-expected-type/issue-24421.rs | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) delete mode 100644 src/test/ui/closure-expected-type/README.md create mode 100644 src/test/ui/closure-expected-type/issue-24421.rs (limited to 'src/test') diff --git a/src/test/ui/closure-expected-type/README.md b/src/test/ui/closure-expected-type/README.md deleted file mode 100644 index 9995b00a9a7..00000000000 --- a/src/test/ui/closure-expected-type/README.md +++ /dev/null @@ -1 +0,0 @@ -See `src/test/run-pass/closure-expected-type`. diff --git a/src/test/ui/closure-expected-type/issue-24421.rs b/src/test/ui/closure-expected-type/issue-24421.rs new file mode 100644 index 00000000000..b3c50a74a32 --- /dev/null +++ b/src/test/ui/closure-expected-type/issue-24421.rs @@ -0,0 +1,20 @@ +// Copyright 2018 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-pass + +fn test(f: F) {} + +fn main() { + test(|x, y | {}); + test(|x:&u64, y:&u64| {}); + test(|x:&u64, y | {}); + test(|x, y:&u64| {}); +} -- cgit 1.4.1-3-g733a5 From b041a79f267d61b01bd339f6f01e48bcdf2a56ab Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 12 Oct 2018 12:43:13 -0400 Subject: Add test for issue #52240 Closes #52240 --- src/test/ui/issues/issue-52240.rs | 16 ++++++++++++++++ src/test/ui/issues/issue-52240.stderr | 9 +++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/test/ui/issues/issue-52240.rs create mode 100644 src/test/ui/issues/issue-52240.stderr (limited to 'src/test') diff --git a/src/test/ui/issues/issue-52240.rs b/src/test/ui/issues/issue-52240.rs new file mode 100644 index 00000000000..9ac7e9905da --- /dev/null +++ b/src/test/ui/issues/issue-52240.rs @@ -0,0 +1,16 @@ +// issue-52240: Can turn immutable into mut with `ref mut` + +enum Foo { + Bar(i32), +} + +fn main() { + let arr = vec!(Foo::Bar(0)); + if let (Some(Foo::Bar(ref mut val)), _) = (&arr.get(0), 0) { + //~^ ERROR cannot borrow field of immutable binding as mutable + *val = 9001; + } + match arr[0] { + Foo::Bar(ref s) => println!("{}", s) + } +} diff --git a/src/test/ui/issues/issue-52240.stderr b/src/test/ui/issues/issue-52240.stderr new file mode 100644 index 00000000000..c2c2524816d --- /dev/null +++ b/src/test/ui/issues/issue-52240.stderr @@ -0,0 +1,9 @@ +error[E0596]: cannot borrow field of immutable binding as mutable + --> $DIR/issue-52240.rs:9:27 + | +LL | if let (Some(Foo::Bar(ref mut val)), _) = (&arr.get(0), 0) { + | ^^^^^^^^^^^ cannot mutably borrow field of immutable binding + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0596`. -- cgit 1.4.1-3-g733a5 From ba446d72be0231ffcd4e4b0d0fc645078480fd19 Mon Sep 17 00:00:00 2001 From: memoryruins Date: Fri, 12 Oct 2018 14:33:38 -0400 Subject: Add nll stderr to #52240 --- src/test/ui/issues/issue-52240.nll.stderr | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/test/ui/issues/issue-52240.nll.stderr (limited to 'src/test') diff --git a/src/test/ui/issues/issue-52240.nll.stderr b/src/test/ui/issues/issue-52240.nll.stderr new file mode 100644 index 00000000000..69b663b17d3 --- /dev/null +++ b/src/test/ui/issues/issue-52240.nll.stderr @@ -0,0 +1,9 @@ +error[E0596]: cannot borrow data in a `&` reference as mutable + --> $DIR/issue-52240.rs:9:27 + | +LL | if let (Some(Foo::Bar(ref mut val)), _) = (&arr.get(0), 0) { + | ^^^^^^^^^^^ cannot borrow as mutable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0596`. -- cgit 1.4.1-3-g733a5 From ab91a6b4df04a60cf19682b1f016848d3e4784ad Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Thu, 11 Oct 2018 23:10:37 -0700 Subject: `#[must_use]` for associated functions is supposed to actually work MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the comments of (closed, defunct) pull request #54884, Mazdak "Centril" Farrokhzad noted that must-use annotations didn't work on an associated function (what other communities might call a "static method"). Subsequent logging revealed that in this case we have a `Def::Method`, whereas the lint pass was only matching on `Def::Fn`. (One could argue that those def-names are thereby misleading—must-use for self-ful methods have always worked—but documenting or reworking that can be left to another day.) --- src/liballoc/rc.rs | 4 ++-- src/liballoc/sync.rs | 4 ++-- src/librustc_lint/unused.rs | 9 +++++---- src/test/run-pass/resolve-pseudo-shadowing.rs | 2 +- src/test/ui/fn_must_use.rs | 8 ++++++++ src/test/ui/fn_must_use.stderr | 20 +++++++++++++------- 6 files changed, 31 insertions(+), 16 deletions(-) (limited to 'src/test') diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 915b8e7787e..40bb2faa362 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -867,7 +867,7 @@ impl Clone for Rc { /// /// let five = Rc::new(5); /// - /// Rc::clone(&five); + /// let _ = Rc::clone(&five); /// ``` #[inline] fn clone(&self) -> Rc { @@ -1304,7 +1304,7 @@ impl Clone for Weak { /// /// let weak_five = Rc::downgrade(&Rc::new(5)); /// - /// Weak::clone(&weak_five); + /// let _ = Weak::clone(&weak_five); /// ``` #[inline] fn clone(&self) -> Weak { diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 9e245fbd7bb..35935861fb1 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -713,7 +713,7 @@ impl Clone for Arc { /// /// let five = Arc::new(5); /// - /// Arc::clone(&five); + /// let _ = Arc::clone(&five); /// ``` #[inline] fn clone(&self) -> Arc { @@ -1135,7 +1135,7 @@ impl Clone for Weak { /// /// let weak_five = Arc::downgrade(&Arc::new(5)); /// - /// Weak::clone(&weak_five); + /// let _ = Weak::clone(&weak_five); /// ``` #[inline] fn clone(&self) -> Weak { diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index ec3c310c63c..7d178d20967 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -80,10 +80,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults { match callee.node { hir::ExprKind::Path(ref qpath) => { let def = cx.tables.qpath_def(qpath, callee.hir_id); - if let Def::Fn(_) = def { - Some(def) - } else { // `Def::Local` if it was a closure, for which we - None // do not currently support must-use linting + match def { + Def::Fn(_) | Def::Method(_) => Some(def), + // `Def::Local` if it was a closure, for which we + // do not currently support must-use linting + _ => None } }, _ => None diff --git a/src/test/run-pass/resolve-pseudo-shadowing.rs b/src/test/run-pass/resolve-pseudo-shadowing.rs index 071279ae7d8..bf6e686bb7b 100644 --- a/src/test/run-pass/resolve-pseudo-shadowing.rs +++ b/src/test/run-pass/resolve-pseudo-shadowing.rs @@ -12,7 +12,7 @@ fn check(_c: Clone) { fn check2() { - <() as std::clone::Clone>::clone(&()); + let _ = <() as std::clone::Clone>::clone(&()); } check2(); } diff --git a/src/test/ui/fn_must_use.rs b/src/test/ui/fn_must_use.rs index def23046db2..e3e20bc89b4 100644 --- a/src/test/ui/fn_must_use.rs +++ b/src/test/ui/fn_must_use.rs @@ -22,6 +22,11 @@ impl MyStruct { fn need_to_use_this_method_value(&self) -> usize { self.n } + + #[must_use] + fn need_to_use_this_associated_function_value() -> isize { + -1 + } } trait EvenNature { @@ -66,6 +71,9 @@ fn main() { m.is_even(); // trait method! //~^ WARN unused return value + MyStruct::need_to_use_this_associated_function_value(); + //~^ WARN unused return value + m.replace(3); // won't warn (annotation needs to be in trait definition) // comparison methods are `must_use` diff --git a/src/test/ui/fn_must_use.stderr b/src/test/ui/fn_must_use.stderr index b5bad22f3dc..1bce8abbbf0 100644 --- a/src/test/ui/fn_must_use.stderr +++ b/src/test/ui/fn_must_use.stderr @@ -1,5 +1,5 @@ warning: unused return value of `need_to_use_this_value` which must be used - --> $DIR/fn_must_use.rs:60:5 + --> $DIR/fn_must_use.rs:65:5 | LL | need_to_use_this_value(); //~ WARN unused return value | ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -12,39 +12,45 @@ LL | #![warn(unused_must_use)] = note: it's important warning: unused return value of `MyStruct::need_to_use_this_method_value` which must be used - --> $DIR/fn_must_use.rs:65:5 + --> $DIR/fn_must_use.rs:70:5 | LL | m.need_to_use_this_method_value(); //~ WARN unused return value | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ warning: unused return value of `EvenNature::is_even` which must be used - --> $DIR/fn_must_use.rs:66:5 + --> $DIR/fn_must_use.rs:71:5 | LL | m.is_even(); // trait method! | ^^^^^^^^^^^^ | = note: no side effects +warning: unused return value of `MyStruct::need_to_use_this_associated_function_value` which must be used + --> $DIR/fn_must_use.rs:74:5 + | +LL | MyStruct::need_to_use_this_associated_function_value(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + warning: unused return value of `std::cmp::PartialEq::eq` which must be used - --> $DIR/fn_must_use.rs:72:5 + --> $DIR/fn_must_use.rs:80:5 | LL | 2.eq(&3); //~ WARN unused return value | ^^^^^^^^^ warning: unused return value of `std::cmp::PartialEq::eq` which must be used - --> $DIR/fn_must_use.rs:73:5 + --> $DIR/fn_must_use.rs:81:5 | LL | m.eq(&n); //~ WARN unused return value | ^^^^^^^^^ warning: unused comparison which must be used - --> $DIR/fn_must_use.rs:76:5 + --> $DIR/fn_must_use.rs:84:5 | LL | 2 == 3; //~ WARN unused comparison | ^^^^^^ warning: unused comparison which must be used - --> $DIR/fn_must_use.rs:77:5 + --> $DIR/fn_must_use.rs:85:5 | LL | m == n; //~ WARN unused comparison | ^^^^^^ -- cgit 1.4.1-3-g733a5 From 406cbf1a39c99ef7b7f329a44f1d1c7af7a8ecda Mon Sep 17 00:00:00 2001 From: Donato Sciarra Date: Fri, 12 Oct 2018 00:34:26 +0200 Subject: Support underscore as constant name Issue: 54912 --- src/libsyntax/feature_gate.rs | 10 +++++ src/libsyntax/parse/parser.rs | 8 +++- src/test/ui/feature-gate-underscore_const_names.rs | 24 ++++++++++++ .../ui/feature-gate-underscore_const_names.stderr | 16 ++++++++ src/test/ui/underscore_const_names.rs | 43 ++++++++++++++++++++++ 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/feature-gate-underscore_const_names.rs create mode 100644 src/test/ui/feature-gate-underscore_const_names.stderr create mode 100644 src/test/ui/underscore_const_names.rs (limited to 'src/test') diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index b86b92fb29e..84122688c83 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -499,6 +499,9 @@ declare_features! ( // #[cfg_attr(predicate, multiple, attributes, here)] (active, cfg_attr_multi, "1.31.0", Some(54881), None), + + // Allows `const _: TYPE = VALUE` + (active, underscore_const_names, "1.31.0", Some(54912), None), ); declare_features! ( @@ -1583,6 +1586,13 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { } } + ast::ItemKind::Const(_,_) => { + if i.ident.name == "_" { + gate_feature_post!(&self, underscore_const_names, i.span, + "naming constants with `_` is unstable"); + } + } + ast::ItemKind::ForeignMod(ref foreign_module) => { self.check_abi(foreign_module.abi, i.span); } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index b5896f37c00..c7089a295fc 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -6346,7 +6346,13 @@ impl<'a> Parser<'a> { } fn parse_item_const(&mut self, m: Option) -> PResult<'a, ItemInfo> { - let id = self.parse_ident()?; + let id = match self.token { + token::Ident(ident, false) if ident.name == keywords::Underscore.name() => { + self.bump(); // `_` + ident.gensym() + }, + _ => self.parse_ident()?, + }; self.expect(&token::Colon)?; let ty = self.parse_ty()?; self.expect(&token::Eq)?; diff --git a/src/test/ui/feature-gate-underscore_const_names.rs b/src/test/ui/feature-gate-underscore_const_names.rs new file mode 100644 index 00000000000..b283e286514 --- /dev/null +++ b/src/test/ui/feature-gate-underscore_const_names.rs @@ -0,0 +1,24 @@ +// Copyright 2012-2018 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. +#![feature(const_let)] + +trait Trt {} +struct Str {} + +impl Trt for Str {} + +const _ : () = { + use std::marker::PhantomData; + struct ImplementsTrait(PhantomData); + let _ = ImplementsTrait::(PhantomData); + () +}; + +fn main() {} diff --git a/src/test/ui/feature-gate-underscore_const_names.stderr b/src/test/ui/feature-gate-underscore_const_names.stderr new file mode 100644 index 00000000000..ab90ef8f11f --- /dev/null +++ b/src/test/ui/feature-gate-underscore_const_names.stderr @@ -0,0 +1,16 @@ +error[E0658]: naming constants with `_` is unstable (see issue #54912) + --> $DIR/feature-gate-underscore_const_names.rs:17:1 + | +LL | / const _ : () = { +LL | | use std::marker::PhantomData; +LL | | struct ImplementsTrait(PhantomData); +LL | | let _ = ImplementsTrait::(PhantomData); +LL | | () +LL | | }; + | |__^ + | + = help: add #![feature(underscore_const_names)] to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. diff --git a/src/test/ui/underscore_const_names.rs b/src/test/ui/underscore_const_names.rs new file mode 100644 index 00000000000..8d31fd0b1e9 --- /dev/null +++ b/src/test/ui/underscore_const_names.rs @@ -0,0 +1,43 @@ +// Copyright 2012-2018 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-pass + +#![feature(const_let)] +#![feature(underscore_const_names)] + +trait Trt {} +struct Str {} +impl Trt for Str {} + +macro_rules! check_impl { + ($struct:ident,$trait:ident) => { + const _ : () = { + use std::marker::PhantomData; + struct ImplementsTrait(PhantomData); + let _ = ImplementsTrait::<$struct>(PhantomData); + () + }; + } +} + +#[deny(unused)] +const _ : () = (); + +const _ : i32 = 42; +const _ : Str = Str{}; + +check_impl!(Str, Trt); +check_impl!(Str, Trt); + +fn main() { + check_impl!(Str, Trt); + check_impl!(Str, Trt); +} -- cgit 1.4.1-3-g733a5 From 69eaa11633b1279d4f2edb734e541f545d583b0a Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Thu, 11 Oct 2018 21:28:20 -0400 Subject: [mir-inlining] Don't inline virtual calls Prior to this change, the test case would output `1` instead of `2` like it should. --- src/librustc_mir/transform/inline.rs | 23 ++++++++++++++++------- src/test/mir-opt/inline-trait-method.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 src/test/mir-opt/inline-trait-method.rs (limited to 'src/test') diff --git a/src/librustc_mir/transform/inline.rs b/src/librustc_mir/transform/inline.rs index 040ee35632c..5963f1a481c 100644 --- a/src/librustc_mir/transform/inline.rs +++ b/src/librustc_mir/transform/inline.rs @@ -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; @@ -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 + }); + } } } } diff --git a/src/test/mir-opt/inline-trait-method.rs b/src/test/mir-opt/inline-trait-method.rs new file mode 100644 index 00000000000..0f79f43ee2d --- /dev/null +++ b/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 -- cgit 1.4.1-3-g733a5