Skip to content

Commit 10406a2

Browse files
authored
Rollup merge of #164167 - estebank:issue-76566, r=oli-obk
Account for `use foo as bar;` on resolution error suggestions Do not suggest ``` help: consider importing this module | LL + use a; | ``` Suggest renamed re-exports: ``` error[E0603]: module `i` is private --> $DIR/privacy1.rs:172:20 | LL | use self::foo::i::A; | ^ private module | note: the module `i` is defined here --> $DIR/privacy1.rs:177:9 | LL | mod i { | ^^^^^ help: consider importing this struct through its public re-export instead | LL - use self::foo::i::A; LL + use mytest::foo::Z; | ``` Suggest renamed imports: ``` error[E0404]: expected trait, found struct `Debug` --> $DIR/suggest-re-exported-name.rs:7:22 | LL | pub fn foo() -> impl Debug { | ^^^^^ not a trait | help: consider using this trait through its public re-export instead | LL | pub fn foo() -> impl OtherDebug { | +++++ ``` Fix #76566.
2 parents 4385065 + e8b09fb commit 10406a2

13 files changed

Lines changed: 228 additions & 45 deletions

‎compiler/rustc_resolve/src/diagnostics/impls.rs‎

Lines changed: 103 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,15 +1744,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17441744
let child_doc_visible = doc_visible
17451745
&& did.is_none_or(|did| did.is_local() || !this.tcx.is_doc_hidden(did));
17461746

1747-
// collect results based on the filter function
1748-
// avoid suggesting anything from the same module in which we are resolving
1749-
// avoid suggesting anything with a hygienic name
1750-
if ident.name == lookup_ident.name
1751-
&& ns == namespace
1752-
&& in_module != parent_scope.module
1753-
&& ident.ctxt.is_root()
1754-
&& filter_fn(res)
1755-
{
1747+
let mk_path = |target_name| {
17561748
// create the path
17571749
let mut segms = if lookup_ident.span.at_least_rust_2018() {
17581750
// crate-local absolute paths start with `crate::` in edition 2018
@@ -1763,8 +1755,49 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17631755
};
17641756
segms.append(&mut path_segments.clone());
17651757

1766-
segms.push(ast::PathSegment::from_ident(ident.orig(orig_ident_span)));
1767-
let path = Path { span: name_binding.span, segments: segms };
1758+
segms.push(ast::PathSegment::from_ident(Ident::new(
1759+
target_name,
1760+
orig_ident_span,
1761+
)));
1762+
Path { span: name_binding.span, segments: segms }
1763+
};
1764+
if let DeclKind::Import { import, source_decl, .. } = name_binding.kind
1765+
&& let ImportKind::Single { source, target, .. } = import.kind
1766+
&& source != target
1767+
&& source.name == lookup_ident.name
1768+
&& ns == namespace
1769+
&& ident.ctxt.is_root()
1770+
&& filter_fn(res)
1771+
&& this.is_accessible_from(source_decl.vis(), parent_scope.module)
1772+
&& this.is_accessible_from(name_binding.vis(), parent_scope.module)
1773+
&& !matches!(res, Res::Err | Res::SelfTyAlias { .. } | Res::SelfTyParam { .. })
1774+
&& lookup_ident.name != kw::SelfLower
1775+
&& lookup_ident.name != kw::SelfUpper
1776+
{
1777+
// Detect we've already imported the item with a different name.
1778+
candidates.push(ImportSuggestion {
1779+
did,
1780+
descr: res.descr(),
1781+
path: mk_path(target.name),
1782+
accessible: child_accessible,
1783+
doc_visible: child_doc_visible,
1784+
note: None,
1785+
via_import,
1786+
is_stable,
1787+
});
1788+
}
1789+
1790+
// collect results based on the filter function
1791+
// avoid suggesting anything from the same module in which we are resolving
1792+
// avoid suggesting anything with a hygienic name
1793+
if ident.name == lookup_ident.name
1794+
&& ns == namespace
1795+
&& in_module != parent_scope.module
1796+
&& ident.ctxt.is_root()
1797+
&& filter_fn(res)
1798+
&& !matches!(res, Res::Err | Res::SelfTyAlias { .. } | Res::SelfTyParam { .. })
1799+
{
1800+
let path = mk_path(ident.name);
17681801

17691802
if child_accessible
17701803
// Remove invisible match if exists
@@ -4105,19 +4138,34 @@ fn show_candidates(
41054138

41064139
let mut showed = false;
41074140
let mut accessible_path_strings: Vec<PathString<'_>> = Vec::new();
4141+
let mut accessible_renames: Vec<PathString<'_>> = Vec::new();
41084142
let mut inaccessible_path_strings: Vec<PathString<'_>> = Vec::new();
41094143

41104144
candidates.iter().for_each(|c| {
41114145
if c.accessible {
41124146
// Don't suggest `#[doc(hidden)]` items from other crates
41134147
if c.doc_visible {
4114-
accessible_path_strings.push((
4115-
pprust::path_to_string(&c.path),
4116-
c.descr,
4117-
c.did.and_then(|did| Some(tcx.source_span(did.as_local()?))),
4118-
&c.note,
4119-
c.via_import,
4120-
))
4148+
if let [first, .., last] | [first @ last] = &path
4149+
&& !first.ident.span.in_external_macro(tcx.sess.source_map())
4150+
&& first.ident.span.eq_ctxt(last.ident.span)
4151+
&& c.path.segments.len() == 1
4152+
{
4153+
accessible_renames.push((
4154+
pprust::path_to_string(&c.path),
4155+
c.descr,
4156+
c.did.and_then(|did| Some(tcx.source_span(did.as_local()?))),
4157+
&c.note,
4158+
c.via_import,
4159+
));
4160+
} else {
4161+
accessible_path_strings.push((
4162+
pprust::path_to_string(&c.path),
4163+
c.descr,
4164+
c.did.and_then(|did| Some(tcx.source_span(did.as_local()?))),
4165+
&c.note,
4166+
c.via_import,
4167+
));
4168+
}
41214169
}
41224170
} else {
41234171
inaccessible_path_strings.push((
@@ -4126,13 +4174,15 @@ fn show_candidates(
41264174
c.did.and_then(|did| Some(tcx.source_span(did.as_local()?))),
41274175
&c.note,
41284176
c.via_import,
4129-
))
4177+
));
41304178
}
41314179
});
41324180

41334181
// we want consistent results across executions, but candidates are produced
41344182
// by iterating through a hash map, so make sure they are ordered:
4135-
for path_strings in [&mut accessible_path_strings, &mut inaccessible_path_strings] {
4183+
for path_strings in
4184+
[&mut accessible_path_strings, &mut accessible_renames, &mut inaccessible_path_strings]
4185+
{
41364186
path_strings.sort_by(|a, b| a.0.cmp(&b.0));
41374187
path_strings.dedup_by(|a, b| a.0 == b.0);
41384188
let core_path_strings =
@@ -4155,6 +4205,39 @@ fn show_candidates(
41554205
path_strings.extend(foreign_crate_path_strings);
41564206
}
41574207

4208+
if !accessible_renames.is_empty() {
4209+
let (determiner, kind, s, through) =
4210+
if let [(_, descr, _, _, via_import)] = &accessible_renames[..] {
4211+
("this", *descr, "", if *via_import { " through its public re-export" } else { "" })
4212+
} else {
4213+
// Get the unique item kinds and if there's only one, we use the right kind name
4214+
// instead of the more generic "items".
4215+
let kinds = accessible_path_strings
4216+
.iter()
4217+
.map(|(_, descr, _, _, _)| *descr)
4218+
.collect::<UnordSet<&str>>();
4219+
let kind = if let Some(kind) = kinds.get_only() { kind } else { "item" };
4220+
let s = if kind.ends_with('s') { "es" } else { "s" };
4221+
4222+
("one of these", kind, s, "")
4223+
};
4224+
4225+
let instead = if let Instead::Yes = instead { " instead" } else { "" };
4226+
let msg = format!("consider using {determiner} {kind}{s}{through}{instead}");
4227+
4228+
for note in accessible_path_strings.iter().flat_map(|cand| cand.3.as_ref()) {
4229+
err.note(note.clone());
4230+
}
4231+
4232+
err.span_suggestions_with_style(
4233+
path[0].ident.span.to(path[path.len() - 1].ident.span),
4234+
msg,
4235+
accessible_renames.into_iter().map(|a| a.0),
4236+
Applicability::MaybeIncorrect,
4237+
SuggestionStyle::ShowAlways,
4238+
);
4239+
showed = true;
4240+
}
41584241
if !accessible_path_strings.is_empty() {
41594242
let (determiner, kind, s, name, through) =
41604243
if let [(name, descr, _, _, via_import)] = &accessible_path_strings[..] {

‎tests/ui/impl-restriction/restriction_resolution_errors.stderr‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ help: there is a crate or module with a similar name
2121
LL - pub impl(in a::b) trait T1 {}
2222
LL + pub impl(in c::b) trait T1 {}
2323
|
24-
help: consider importing this module
25-
|
26-
LL + use a;
27-
|
2824

2925
error[E0433]: cannot find module `c` in the crate root
3026
--> $DIR/restriction_resolution_errors.rs:22:28

‎tests/ui/mut-restriction/restriction-resolution-errors.stderr‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ help: there is a crate or module with a similar name
2121
LL - pub mut(in a::b) i1: i32,
2222
LL + pub mut(in c::b) i1: i32,
2323
|
24-
help: consider importing this module
25-
|
26-
LL + use a;
27-
|
2824

2925
error[E0433]: cannot find module `c` in the crate root
3026
--> $DIR/restriction-resolution-errors.rs:13:31

‎tests/ui/offset-of/offset-of-self.stderr‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ error[E0425]: cannot find type `S` in module `self`
1010
LL | offset_of!(self::S, v);
1111
| ^ not found in `self`
1212
|
13-
help: consider importing this struct
14-
|
15-
LL + use S;
16-
|
17-
help: if you import `S`, refer to it directly
13+
help: consider using this struct
1814
|
1915
LL - offset_of!(self::S, v);
2016
LL + offset_of!(S, v);

‎tests/ui/privacy/privacy1.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub mod mytest {
172172
use self::foo::i::A; //~ ERROR: module `i` is private
173173

174174
pub mod foo {
175-
pub use self::i::A as foo;
175+
pub use self::i::A as Z;
176176

177177
mod i {
178178
pub struct A;

‎tests/ui/privacy/privacy1.stderr‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,18 @@ error[E0603]: module `i` is private
3838
--> $DIR/privacy1.rs:172:20
3939
|
4040
LL | use self::foo::i::A;
41-
| ^ - struct `A` is not publicly re-exported
42-
| |
43-
| private module
41+
| ^ private module
4442
|
4543
note: the module `i` is defined here
4644
--> $DIR/privacy1.rs:177:9
4745
|
4846
LL | mod i {
4947
| ^^^^^
48+
help: consider importing this struct through its public re-export instead
49+
|
50+
LL - use self::foo::i::A;
51+
LL + use mytest::foo::Z;
52+
|
5053

5154
error[E0603]: module `baz` is private
5255
--> $DIR/privacy1.rs:148:18
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
2+
--> $DIR/attributes-on-modules-fail.rs:22:1
3+
|
4+
LL | #[derive(Copy)]
5+
| ^^^^^^^^^^^^^^^ not applicable here
6+
LL | mod n {}
7+
| -------- not a `struct`, `enum` or `union`
8+
9+
error[E0425]: cannot find type `Y` in this scope
10+
--> $DIR/attributes-on-modules-fail.rs:14:14
11+
|
12+
LL | type A = Y;
13+
| ^ not found in this scope
14+
15+
error[E0425]: cannot find type `X` in this scope
16+
--> $DIR/attributes-on-modules-fail.rs:20:10
17+
|
18+
LL | type A = X;
19+
| ^ not found in this scope
20+
|
21+
help: consider importing this struct
22+
|
23+
LL + use m::X;
24+
|
25+
26+
error: aborting due to 3 previous errors
27+
28+
Some errors have detailed explanations: E0425, E0774.
29+
For more information about an error, try `rustc --explain E0425`.

tests/ui/proc-macro/attributes-on-modules-fail.stderr renamed to tests/ui/proc-macro/attributes-on-modules-fail.ed2024.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error[E0774]: `derive` may only be applied to `struct`s, `enum`s and `union`s
2-
--> $DIR/attributes-on-modules-fail.rs:17:1
2+
--> $DIR/attributes-on-modules-fail.rs:22:1
33
|
44
LL | #[derive(Copy)]
55
| ^^^^^^^^^^^^^^^ not applicable here
66
LL | mod n {}
77
| -------- not a `struct`, `enum` or `union`
88

99
error[E0425]: cannot find type `Y` in this scope
10-
--> $DIR/attributes-on-modules-fail.rs:11:14
10+
--> $DIR/attributes-on-modules-fail.rs:14:14
1111
|
1212
LL | type A = Y;
1313
| ^ not found in this scope
1414
|
1515
help: consider importing this struct
1616
|
17-
LL + use Y;
17+
LL + use crate::Y;
1818
|
1919

2020
error[E0425]: cannot find type `X` in this scope
21-
--> $DIR/attributes-on-modules-fail.rs:15:10
21+
--> $DIR/attributes-on-modules-fail.rs:20:10
2222
|
2323
LL | type A = X;
2424
| ^ not found in this scope
2525
|
2626
help: consider importing this struct
2727
|
28-
LL + use m::X;
28+
LL + use crate::m::X;
2929
|
3030

3131
error: aborting due to 3 previous errors

‎tests/ui/proc-macro/attributes-on-modules-fail.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
//@ edition:2015
1+
//@ revisions: ed2015 ed2024
2+
//@ [ed2015] edition:2015
3+
//@ [ed2024] edition:2024
24
//@ proc-macro: test-macros.rs
35

6+
47
#[macro_use]
58
extern crate test_macros;
69

@@ -9,6 +12,8 @@ mod m {
912
pub struct X;
1013

1114
type A = Y; //~ ERROR cannot find type `Y` in this scope
15+
// In edition 2015 we'd suggest referring to `crate::Y` as `Y`, which is not a good suggestion,
16+
// but in other editions we suggest the appropriate `crate::Y`.
1217
}
1318

1419
struct Y;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@ run-rustfix
2+
use std::fmt::Debug as OtherDebug;
3+
4+
pub struct Debug;
5+
6+
pub fn foo() -> impl OtherDebug { //~ ERROR: expected trait, found struct `Debug`
7+
""
8+
}
9+
10+
pub fn bar() -> impl OtherDebug { //~ ERROR: expected trait, found struct `crate::Debug`
11+
""
12+
}
13+
14+
pub fn baz<T: OtherDebug>() {} //~ ERROR: expected trait, found struct `Debug`
15+
16+
fn main() {}

0 commit comments

Comments
 (0)