Skip to content

Commit 068594e

Browse files
authored
Rollup merge of #138790 - xizheyin:issue-138626, r=compiler-errors
Note potential but private items in show_candidates Closes #138626 . We should add potential private items to give ample hints. And for the other seemingly false positive ` pub use crate::one::Foo;` should be kept because we don't know if the user wants to import other module's items or not, and therefore should be given the full option to do so. r? compiler
2 parents cdd69d6 + 26cfa6f commit 068594e

12 files changed

+135
-14
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1325,11 +1325,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13251325
})
13261326
}
13271327

1328-
// If only some candidates are accessible, take just them
1329-
if !candidates.iter().all(|v: &ImportSuggestion| !v.accessible) {
1330-
candidates.retain(|x| x.accessible)
1331-
}
1332-
13331328
candidates
13341329
}
13351330

@@ -1793,7 +1788,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17931788
&import_suggestions,
17941789
Instead::Yes,
17951790
FoundUse::Yes,
1796-
DiagMode::Import { append: single_nested },
1791+
DiagMode::Import { append: single_nested, unresolved_import: false },
17971792
vec![],
17981793
"",
17991794
);
@@ -2750,6 +2745,8 @@ pub(crate) enum DiagMode {
27502745
Pattern,
27512746
/// The binding is part of a use statement
27522747
Import {
2748+
/// `true` means diagnostics is for unresolved import
2749+
unresolved_import: bool,
27532750
/// `true` mean add the tips afterward for case `use a::{b,c}`,
27542751
/// rather than replacing within.
27552752
append: bool,
@@ -2800,6 +2797,7 @@ fn show_candidates(
28002797
return false;
28012798
}
28022799

2800+
let mut showed = false;
28032801
let mut accessible_path_strings: Vec<PathString<'_>> = Vec::new();
28042802
let mut inaccessible_path_strings: Vec<PathString<'_>> = Vec::new();
28052803

@@ -2958,8 +2956,11 @@ fn show_candidates(
29582956
append_candidates(&mut msg, accessible_path_strings);
29592957
err.help(msg);
29602958
}
2961-
true
2962-
} else if !(inaccessible_path_strings.is_empty() || matches!(mode, DiagMode::Import { .. })) {
2959+
showed = true;
2960+
}
2961+
if !inaccessible_path_strings.is_empty()
2962+
&& (!matches!(mode, DiagMode::Import { unresolved_import: false, .. }))
2963+
{
29632964
let prefix =
29642965
if let DiagMode::Pattern = mode { "you might have meant to match on " } else { "" };
29652966
if let [(name, descr, source_span, note, _)] = &inaccessible_path_strings[..] {
@@ -3022,10 +3023,9 @@ fn show_candidates(
30223023

30233024
err.span_note(multi_span, msg);
30243025
}
3025-
true
3026-
} else {
3027-
false
3026+
showed = true;
30283027
}
3028+
showed
30293029
}
30303030

30313031
#[derive(Debug)]

compiler/rustc_resolve/src/imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
734734
&mut diag,
735735
Some(err.span),
736736
candidates,
737-
DiagMode::Import { append: false },
737+
DiagMode::Import { append: false, unresolved_import: true },
738738
(source != target)
739739
.then(|| format!(" as {target}"))
740740
.as_deref()

tests/ui/imports/glob-resolve1.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ error[E0425]: cannot find function `import` in this scope
5858
LL | import();
5959
| ^^^^^^ not found in this scope
6060
|
61+
note: function `bar::import` exists but is inaccessible
62+
--> $DIR/glob-resolve1.rs:7:5
63+
|
64+
LL | fn fpriv() {}
65+
| ^^^^^^^^^^ not accessible
6166
help: consider importing this function
6267
|
6368
LL + use other::import;

tests/ui/imports/issue-4366-2.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ error[E0423]: expected function, found module `foo`
1616
LL | foo();
1717
| ^^^ not a function
1818
|
19+
note: function `m1::foo` exists but is inaccessible
20+
--> $DIR/issue-4366-2.rs:21:5
21+
|
22+
LL | fn foo() {}
23+
| ^^^^^^^^ not accessible
1924
help: consider importing this function instead
2025
|
2126
LL + use foo::foo;

tests/ui/imports/issue-4366.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ error[E0425]: cannot find function `foo` in this scope
44
LL | fn sub() -> isize { foo(); 1 }
55
| ^^^ not found in this scope
66
|
7+
note: function `m1::foo` exists but is inaccessible
8+
--> $DIR/issue-4366.rs:23:5
9+
|
10+
LL | fn foo() {}
11+
| ^^^^^^^^ not accessible
712
help: consider importing this function
813
|
914
LL + use foo::foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub mod one {
2+
mod foo {
3+
pub struct Foo;
4+
}
5+
6+
pub use self::foo::Foo;
7+
}
8+
9+
pub mod two {
10+
mod foo {
11+
mod bar {
12+
pub struct Foo;
13+
}
14+
}
15+
16+
pub use crate::two::foo::Foo; //~ ERROR unresolved import `crate::two::foo::Foo` [E0432]
17+
}
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0432]: unresolved import `crate::two::foo::Foo`
2+
--> $DIR/show-private-items-issue-138626.rs:16:13
3+
|
4+
LL | pub use crate::two::foo::Foo;
5+
| ^^^^^^^^^^^^^^^^^^^^ no `Foo` in `two::foo`
6+
|
7+
note: struct `two::foo::bar::Foo` exists but is inaccessible
8+
--> $DIR/show-private-items-issue-138626.rs:12:13
9+
|
10+
LL | pub struct Foo;
11+
| ^^^^^^^^^^^^^^^ not accessible
12+
help: consider importing this struct through its public re-export instead
13+
|
14+
LL - pub use crate::two::foo::Foo;
15+
LL + pub use one::Foo;
16+
|
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0432`.

tests/ui/privacy/privacy-ns1.stderr

+24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ LL | pub struct Baz;
77
LL | Bar();
88
| ^^^
99
|
10+
note: these functions exist but are inaccessible
11+
--> $DIR/privacy-ns1.rs:14:5
12+
|
13+
LL | fn Bar() { }
14+
| ^^^^^^^^ `foo1::Bar`: not accessible
15+
...
16+
LL | fn Bar() { }
17+
| ^^^^^^^^ `foo3::Bar`: not accessible
1018
help: a unit struct with a similar name exists
1119
|
1220
LL - Bar();
@@ -26,6 +34,14 @@ LL | pub struct Baz;
2634
LL | Bar();
2735
| ^^^
2836
|
37+
note: these functions exist but are inaccessible
38+
--> $DIR/privacy-ns1.rs:14:5
39+
|
40+
LL | fn Bar() { }
41+
| ^^^^^^^^ `foo1::Bar`: not accessible
42+
...
43+
LL | fn Bar() { }
44+
| ^^^^^^^^ `foo3::Bar`: not accessible
2945
help: a unit struct with a similar name exists
3046
|
3147
LL - Bar();
@@ -45,6 +61,14 @@ LL | pub struct Baz;
4561
LL | let _x: Box<Bar>;
4662
| ^^^
4763
|
64+
note: these traits exist but are inaccessible
65+
--> $DIR/privacy-ns1.rs:25:5
66+
|
67+
LL | trait Bar {
68+
| ^^^^^^^^^ `foo2::Bar`: not accessible
69+
...
70+
LL | trait Bar {
71+
| ^^^^^^^^^ `foo3::Bar`: not accessible
4872
help: a struct with a similar name exists
4973
|
5074
LL - let _x: Box<Bar>;

tests/ui/privacy/privacy-ns2.stderr

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ error[E0423]: expected function, tuple struct or tuple variant, found trait `Bar
44
LL | Bar();
55
| ^^^ not a function, tuple struct or tuple variant
66
|
7+
note: these functions exist but are inaccessible
8+
--> $DIR/privacy-ns2.rs:14:5
9+
|
10+
LL | fn Bar() { }
11+
| ^^^^^^^^ `foo1::Bar`: not accessible
12+
...
13+
LL | fn Bar() { }
14+
| ^^^^^^^^ `foo3::Bar`: not accessible
715
help: consider importing this function instead
816
|
917
LL + use foo2::Bar;
@@ -18,6 +26,14 @@ LL | pub struct Baz;
1826
LL | Bar();
1927
| ^^^
2028
|
29+
note: these functions exist but are inaccessible
30+
--> $DIR/privacy-ns2.rs:14:5
31+
|
32+
LL | fn Bar() { }
33+
| ^^^^^^^^ `foo1::Bar`: not accessible
34+
...
35+
LL | fn Bar() { }
36+
| ^^^^^^^^ `foo3::Bar`: not accessible
2137
help: a unit struct with a similar name exists
2238
|
2339
LL - Bar();
@@ -34,6 +50,14 @@ error[E0573]: expected type, found function `Bar`
3450
LL | let _x : Bar();
3551
| ^^^^^ not a type
3652
|
53+
note: these traits exist but are inaccessible
54+
--> $DIR/privacy-ns2.rs:31:5
55+
|
56+
LL | trait Bar {
57+
| ^^^^^^^^^ `foo2::Bar`: not accessible
58+
...
59+
LL | trait Bar {
60+
| ^^^^^^^^^ `foo3::Bar`: not accessible
3761
help: use `=` if you meant to assign
3862
|
3963
LL - let _x : Bar();

tests/ui/resolve/issue-21221-1.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ error[E0412]: cannot find type `Mul` in this scope
1919
LL | fn getMul() -> Mul {
2020
| ^^^ not found in this scope
2121
|
22+
note: these items exist but are inaccessible
23+
--> $DIR/issue-21221-1.rs:10:5
24+
|
25+
LL | enum Mul {
26+
| ^^^^^^^^ `mul3::Mul`: not accessible
27+
...
28+
LL | type Mul = String;
29+
| ^^^^^^^^^^^^^^^^^^ `mul4::Mul`: not accessible
30+
...
31+
LL | struct Mul{
32+
| ^^^^^^^^^^ `mul5::Mul`: not accessible
2233
help: consider importing one of these traits
2334
|
2435
LL + use std::ops::Mul;

tests/ui/unresolved/unresolved-import.rs

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ mod food {
3131

3232
mod zug {
3333
pub mod baz {
34+
//~^ NOTE module `food::zug::baz` exists but is inaccessible
35+
//~| NOTE not accessible
3436
pub struct Foobar;
3537
}
3638
}

tests/ui/unresolved/unresolved-import.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ LL | use food::baz;
2626
| | |
2727
| | help: a similar name exists in the module: `bag`
2828
| no `baz` in `food`
29+
|
30+
note: module `food::zug::baz` exists but is inaccessible
31+
--> $DIR/unresolved-import.rs:33:9
32+
|
33+
LL | pub mod baz {
34+
| ^^^^^^^^^^^ not accessible
2935

3036
error[E0432]: unresolved import `food::beens`
3137
--> $DIR/unresolved-import.rs:19:12
@@ -37,13 +43,13 @@ LL | use food::{beens as Foo};
3743
| help: a similar name exists in the module: `beans`
3844

3945
error[E0432]: unresolved import `MyEnum`
40-
--> $DIR/unresolved-import.rs:44:9
46+
--> $DIR/unresolved-import.rs:46:9
4147
|
4248
LL | use MyEnum::*;
4349
| ^^^^^^ help: a similar path exists: `self::MyEnum`
4450

4551
error[E0432]: unresolved import `Enum`
46-
--> $DIR/unresolved-import.rs:55:9
52+
--> $DIR/unresolved-import.rs:57:9
4753
|
4854
LL | use Enum::*;
4955
| ^^^^ help: a similar path exists: `self::Enum`

0 commit comments

Comments
 (0)