From aab37fe52c0f142d116fee67d6eddedc7825dcc7 Mon Sep 17 00:00:00 2001 From: Dan Aloni Date: Sat, 27 Jun 2020 21:08:32 +0300 Subject: [PATCH 1/3] Add test for issue #56175 --- .../ui/issues/auxiliary/reexported-trait.rs | 17 ++++++++++++ src/test/ui/issues/issue-56175.rs | 9 +++++++ src/test/ui/issues/issue-56175.stderr | 27 +++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 src/test/ui/issues/auxiliary/reexported-trait.rs create mode 100644 src/test/ui/issues/issue-56175.rs create mode 100644 src/test/ui/issues/issue-56175.stderr diff --git a/src/test/ui/issues/auxiliary/reexported-trait.rs b/src/test/ui/issues/auxiliary/reexported-trait.rs new file mode 100644 index 0000000000000..51a991bef5959 --- /dev/null +++ b/src/test/ui/issues/auxiliary/reexported-trait.rs @@ -0,0 +1,17 @@ +mod private { + pub trait Trait { + fn trait_method(&self) { + } + } + pub trait TraitB { + fn trait_method_b(&self) { + } + } +} + +pub struct FooStruct; +pub use crate::private::Trait; +impl crate::private::Trait for FooStruct {} + +pub use crate::private::TraitB as TraitBRename; +impl crate::private::TraitB for FooStruct {} diff --git a/src/test/ui/issues/issue-56175.rs b/src/test/ui/issues/issue-56175.rs new file mode 100644 index 0000000000000..ca1d0d4310ae9 --- /dev/null +++ b/src/test/ui/issues/issue-56175.rs @@ -0,0 +1,9 @@ +// edition:2018 +// aux-crate:reexported_trait=reexported-trait.rs + +fn main() { + reexported_trait::FooStruct.trait_method(); + //~^ ERROR + reexported_trait::FooStruct.trait_method_b(); + //~^ ERROR +} diff --git a/src/test/ui/issues/issue-56175.stderr b/src/test/ui/issues/issue-56175.stderr new file mode 100644 index 0000000000000..dc5beb4271df7 --- /dev/null +++ b/src/test/ui/issues/issue-56175.stderr @@ -0,0 +1,27 @@ +error[E0599]: no method named `trait_method` found for struct `reexported_trait::FooStruct` in the current scope + --> $DIR/issue-56175.rs:5:33 + | +LL | reexported_trait::FooStruct.trait_method(); + | ^^^^^^^^^^^^ method not found in `reexported_trait::FooStruct` + | + = help: items from traits can only be used if the trait is in scope +help: the following trait is implemented but not in scope; perhaps add a `use` for it: + | +LL | use reexported_trait::private::Trait; + | + +error[E0599]: no method named `trait_method_b` found for struct `reexported_trait::FooStruct` in the current scope + --> $DIR/issue-56175.rs:7:33 + | +LL | reexported_trait::FooStruct.trait_method_b(); + | ^^^^^^^^^^^^^^ method not found in `reexported_trait::FooStruct` + | + = help: items from traits can only be used if the trait is in scope +help: the following trait is implemented but not in scope; perhaps add a `use` for it: + | +LL | use reexported_trait::private::TraitB; + | + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0599`. From 5427d3bf7c38df0c37597272bf34c6710cab3c52 Mon Sep 17 00:00:00 2001 From: Dan Aloni Date: Mon, 29 Jun 2020 21:51:32 +0300 Subject: [PATCH 2/3] Fix try_print_visible_def_path for Rust 2018 The recursive check of `try_print_visible_def_path` did not properly handle the Rust 2018 case of crate-paths without 'extern crate'. Instead, it returned a "not found" via (false, self). This fixes issue #56175. --- src/librustc_middle/ty/print/pretty.rs | 35 +++++++++++++------------- src/test/ui/issues/issue-56175.stderr | 4 +-- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index 061214249713d..bbf9f550c03f4 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -282,26 +282,27 @@ pub trait PrettyPrinter<'tcx>: // where there is no explicit `extern crate`, we just prepend // the crate name. match self.tcx().extern_crate(def_id) { - Some(&ExternCrate { - src: ExternCrateSource::Extern(def_id), - dependency_of: LOCAL_CRATE, - span, - .. - }) => { - debug!("try_print_visible_def_path: def_id={:?}", def_id); - return Ok(( - if !span.is_dummy() { - self.print_def_path(def_id, &[])? - } else { - self.path_crate(cnum)? - }, - true, - )); - } + Some(&ExternCrate { src, dependency_of, span, .. }) => match (src, dependency_of) { + (ExternCrateSource::Extern(def_id), LOCAL_CRATE) => { + debug!("try_print_visible_def_path: def_id={:?}", def_id); + return Ok(( + if !span.is_dummy() { + self.print_def_path(def_id, &[])? + } else { + self.path_crate(cnum)? + }, + true, + )); + } + (ExternCrateSource::Path, LOCAL_CRATE) if self.tcx().sess.rust_2018() => { + debug!("try_print_visible_def_path: def_id={:?}", def_id); + return Ok((self.path_crate(cnum)?, true)); + } + _ => {} + }, None => { return Ok((self.path_crate(cnum)?, true)); } - _ => {} } } diff --git a/src/test/ui/issues/issue-56175.stderr b/src/test/ui/issues/issue-56175.stderr index dc5beb4271df7..c0799db7c1286 100644 --- a/src/test/ui/issues/issue-56175.stderr +++ b/src/test/ui/issues/issue-56175.stderr @@ -7,7 +7,7 @@ LL | reexported_trait::FooStruct.trait_method(); = help: items from traits can only be used if the trait is in scope help: the following trait is implemented but not in scope; perhaps add a `use` for it: | -LL | use reexported_trait::private::Trait; +LL | use reexported_trait::Trait; | error[E0599]: no method named `trait_method_b` found for struct `reexported_trait::FooStruct` in the current scope @@ -19,7 +19,7 @@ LL | reexported_trait::FooStruct.trait_method_b(); = help: items from traits can only be used if the trait is in scope help: the following trait is implemented but not in scope; perhaps add a `use` for it: | -LL | use reexported_trait::private::TraitB; +LL | use reexported_trait::TraitBRename; | error: aborting due to 2 previous errors From f77b6fe5711e42321b3e2b8b438e00e682f5411b Mon Sep 17 00:00:00 2001 From: Dan Aloni Date: Wed, 1 Jul 2020 20:05:51 +0300 Subject: [PATCH 3/3] Review fix --- src/librustc_middle/ty/print/pretty.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_middle/ty/print/pretty.rs b/src/librustc_middle/ty/print/pretty.rs index bbf9f550c03f4..f94a51feca3b8 100644 --- a/src/librustc_middle/ty/print/pretty.rs +++ b/src/librustc_middle/ty/print/pretty.rs @@ -294,7 +294,7 @@ pub trait PrettyPrinter<'tcx>: true, )); } - (ExternCrateSource::Path, LOCAL_CRATE) if self.tcx().sess.rust_2018() => { + (ExternCrateSource::Path, LOCAL_CRATE) => { debug!("try_print_visible_def_path: def_id={:?}", def_id); return Ok((self.path_crate(cnum)?, true)); }