Skip to content

Commit 3e78eac

Browse files
authored
Rollup merge of #73871 - da-x:private-types-2018-no-extern, r=petrochenkov
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 #56175.
2 parents fed2013 + f77b6fe commit 3e78eac

File tree

4 files changed

+71
-17
lines changed

4 files changed

+71
-17
lines changed

src/librustc_middle/ty/print/pretty.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -282,26 +282,27 @@ pub trait PrettyPrinter<'tcx>:
282282
// where there is no explicit `extern crate`, we just prepend
283283
// the crate name.
284284
match self.tcx().extern_crate(def_id) {
285-
Some(&ExternCrate {
286-
src: ExternCrateSource::Extern(def_id),
287-
dependency_of: LOCAL_CRATE,
288-
span,
289-
..
290-
}) => {
291-
debug!("try_print_visible_def_path: def_id={:?}", def_id);
292-
return Ok((
293-
if !span.is_dummy() {
294-
self.print_def_path(def_id, &[])?
295-
} else {
296-
self.path_crate(cnum)?
297-
},
298-
true,
299-
));
300-
}
285+
Some(&ExternCrate { src, dependency_of, span, .. }) => match (src, dependency_of) {
286+
(ExternCrateSource::Extern(def_id), LOCAL_CRATE) => {
287+
debug!("try_print_visible_def_path: def_id={:?}", def_id);
288+
return Ok((
289+
if !span.is_dummy() {
290+
self.print_def_path(def_id, &[])?
291+
} else {
292+
self.path_crate(cnum)?
293+
},
294+
true,
295+
));
296+
}
297+
(ExternCrateSource::Path, LOCAL_CRATE) => {
298+
debug!("try_print_visible_def_path: def_id={:?}", def_id);
299+
return Ok((self.path_crate(cnum)?, true));
300+
}
301+
_ => {}
302+
},
301303
None => {
302304
return Ok((self.path_crate(cnum)?, true));
303305
}
304-
_ => {}
305306
}
306307
}
307308

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
mod private {
2+
pub trait Trait {
3+
fn trait_method(&self) {
4+
}
5+
}
6+
pub trait TraitB {
7+
fn trait_method_b(&self) {
8+
}
9+
}
10+
}
11+
12+
pub struct FooStruct;
13+
pub use crate::private::Trait;
14+
impl crate::private::Trait for FooStruct {}
15+
16+
pub use crate::private::TraitB as TraitBRename;
17+
impl crate::private::TraitB for FooStruct {}

src/test/ui/issues/issue-56175.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// edition:2018
2+
// aux-crate:reexported_trait=reexported-trait.rs
3+
4+
fn main() {
5+
reexported_trait::FooStruct.trait_method();
6+
//~^ ERROR
7+
reexported_trait::FooStruct.trait_method_b();
8+
//~^ ERROR
9+
}

src/test/ui/issues/issue-56175.stderr

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0599]: no method named `trait_method` found for struct `reexported_trait::FooStruct` in the current scope
2+
--> $DIR/issue-56175.rs:5:33
3+
|
4+
LL | reexported_trait::FooStruct.trait_method();
5+
| ^^^^^^^^^^^^ method not found in `reexported_trait::FooStruct`
6+
|
7+
= help: items from traits can only be used if the trait is in scope
8+
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
9+
|
10+
LL | use reexported_trait::Trait;
11+
|
12+
13+
error[E0599]: no method named `trait_method_b` found for struct `reexported_trait::FooStruct` in the current scope
14+
--> $DIR/issue-56175.rs:7:33
15+
|
16+
LL | reexported_trait::FooStruct.trait_method_b();
17+
| ^^^^^^^^^^^^^^ method not found in `reexported_trait::FooStruct`
18+
|
19+
= help: items from traits can only be used if the trait is in scope
20+
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
21+
|
22+
LL | use reexported_trait::TraitBRename;
23+
|
24+
25+
error: aborting due to 2 previous errors
26+
27+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)