Skip to content

Commit e908047

Browse files
committed
Note potential but private items in show_candidates
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent 9bad8ac commit e908047

12 files changed

+163
-11
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -1325,9 +1325,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13251325
})
13261326
}
13271327

1328-
// If only some candidates are accessible, take just them
1328+
// If only some candidates are accessible, add note to make it public
13291329
if !candidates.iter().all(|v: &ImportSuggestion| !v.accessible) {
1330-
candidates.retain(|x| x.accessible)
1330+
candidates.iter_mut().for_each(|v| {
1331+
if !v.accessible {
1332+
v.note = Some(format!(
1333+
"maybe you should make the path `{}` public with `pub`",
1334+
path_names_to_string(&v.path)
1335+
));
1336+
}
1337+
});
13311338
}
13321339

13331340
candidates
@@ -1794,7 +1801,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17941801
&import_suggestions,
17951802
Instead::Yes,
17961803
FoundUse::Yes,
1797-
DiagMode::Import { append: single_nested },
1804+
DiagMode::Import { append: single_nested, unresolved_import: false },
17981805
vec![],
17991806
"",
18001807
);
@@ -2751,6 +2758,8 @@ pub(crate) enum DiagMode {
27512758
Pattern,
27522759
/// The binding is part of a use statement
27532760
Import {
2761+
/// `true` means diagnostics is for unresolved import
2762+
unresolved_import: bool,
27542763
/// `true` mean add the tips afterward for case `use a::{b,c}`,
27552764
/// rather than replacing within.
27562765
append: bool,
@@ -2801,6 +2810,7 @@ fn show_candidates(
28012810
return false;
28022811
}
28032812

2813+
let mut showed = false;
28042814
let mut accessible_path_strings: Vec<PathString<'_>> = Vec::new();
28052815
let mut inaccessible_path_strings: Vec<PathString<'_>> = Vec::new();
28062816

@@ -2959,8 +2969,11 @@ fn show_candidates(
29592969
append_candidates(&mut msg, accessible_path_strings);
29602970
err.help(msg);
29612971
}
2962-
true
2963-
} else if !(inaccessible_path_strings.is_empty() || matches!(mode, DiagMode::Import { .. })) {
2972+
showed = true;
2973+
}
2974+
if !inaccessible_path_strings.is_empty()
2975+
&& (!matches!(mode, DiagMode::Import { unresolved_import: false, .. }))
2976+
{
29642977
let prefix =
29652978
if let DiagMode::Pattern = mode { "you might have meant to match on " } else { "" };
29662979
if let [(name, descr, source_span, note, _)] = &inaccessible_path_strings[..] {
@@ -3023,10 +3036,9 @@ fn show_candidates(
30233036

30243037
err.span_note(multi_span, msg);
30253038
}
3026-
true
3027-
} else {
3028-
false
3039+
showed = true;
30293040
}
3041+
showed
30303042
}
30313043

30323044
#[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

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ 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
66+
= note: maybe you should make the path `bar::import` public with `pub`
6167
help: consider importing this function
6268
|
6369
LL + use other::import;

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

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ 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
24+
= note: maybe you should make the path `m1::foo` public with `pub`
1925
help: consider importing this function instead
2026
|
2127
LL + use foo::foo;

tests/ui/imports/issue-4366.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ 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
12+
= note: maybe you should make the path `m1::foo` public with `pub`
713
help: consider importing this function
814
|
915
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,21 @@
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+
= note: maybe you should make the path `two::foo::bar::Foo` public with `pub`
13+
help: consider importing this struct through its public re-export instead
14+
|
15+
LL - pub use crate::two::foo::Foo;
16+
LL + pub use one::Foo;
17+
|
18+
19+
error: aborting due to 1 previous error
20+
21+
For more information about this error, try `rustc --explain E0432`.

tests/ui/privacy/privacy-ns1.stderr

+30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ LL | pub struct Baz;
77
LL | Bar();
88
| ^^^
99
|
10+
= note: maybe you should make the path `foo1::Bar` public with `pub`
11+
= note: maybe you should make the path `foo3::Bar` public with `pub`
12+
note: these functions exist but are inaccessible
13+
--> $DIR/privacy-ns1.rs:14:5
14+
|
15+
LL | fn Bar() { }
16+
| ^^^^^^^^ `foo1::Bar`: not accessible
17+
...
18+
LL | fn Bar() { }
19+
| ^^^^^^^^ `foo3::Bar`: not accessible
1020
help: a unit struct with a similar name exists
1121
|
1222
LL - Bar();
@@ -26,6 +36,16 @@ LL | pub struct Baz;
2636
LL | Bar();
2737
| ^^^
2838
|
39+
= note: maybe you should make the path `foo1::Bar` public with `pub`
40+
= note: maybe you should make the path `foo3::Bar` public with `pub`
41+
note: these functions exist but are inaccessible
42+
--> $DIR/privacy-ns1.rs:14:5
43+
|
44+
LL | fn Bar() { }
45+
| ^^^^^^^^ `foo1::Bar`: not accessible
46+
...
47+
LL | fn Bar() { }
48+
| ^^^^^^^^ `foo3::Bar`: not accessible
2949
help: a unit struct with a similar name exists
3050
|
3151
LL - Bar();
@@ -45,6 +65,16 @@ LL | pub struct Baz;
4565
LL | let _x: Box<Bar>;
4666
| ^^^
4767
|
68+
= note: maybe you should make the path `foo2::Bar` public with `pub`
69+
= note: maybe you should make the path `foo3::Bar` public with `pub`
70+
note: these traits exist but are inaccessible
71+
--> $DIR/privacy-ns1.rs:25:5
72+
|
73+
LL | trait Bar {
74+
| ^^^^^^^^^ `foo2::Bar`: not accessible
75+
...
76+
LL | trait Bar {
77+
| ^^^^^^^^^ `foo3::Bar`: not accessible
4878
help: a struct with a similar name exists
4979
|
5080
LL - let _x: Box<Bar>;

tests/ui/privacy/privacy-ns2.stderr

+30
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ 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: maybe you should make the path `foo1::Bar` public with `pub`
8+
= note: maybe you should make the path `foo3::Bar` public with `pub`
9+
note: these functions exist but are inaccessible
10+
--> $DIR/privacy-ns2.rs:14:5
11+
|
12+
LL | fn Bar() { }
13+
| ^^^^^^^^ `foo1::Bar`: not accessible
14+
...
15+
LL | fn Bar() { }
16+
| ^^^^^^^^ `foo3::Bar`: not accessible
717
help: consider importing this function instead
818
|
919
LL + use foo2::Bar;
@@ -18,6 +28,16 @@ LL | pub struct Baz;
1828
LL | Bar();
1929
| ^^^
2030
|
31+
= note: maybe you should make the path `foo1::Bar` public with `pub`
32+
= note: maybe you should make the path `foo3::Bar` public with `pub`
33+
note: these functions exist but are inaccessible
34+
--> $DIR/privacy-ns2.rs:14:5
35+
|
36+
LL | fn Bar() { }
37+
| ^^^^^^^^ `foo1::Bar`: not accessible
38+
...
39+
LL | fn Bar() { }
40+
| ^^^^^^^^ `foo3::Bar`: not accessible
2141
help: a unit struct with a similar name exists
2242
|
2343
LL - Bar();
@@ -34,6 +54,16 @@ error[E0573]: expected type, found function `Bar`
3454
LL | let _x : Bar();
3555
| ^^^^^ not a type
3656
|
57+
= note: maybe you should make the path `foo2::Bar` public with `pub`
58+
= note: maybe you should make the path `foo3::Bar` public with `pub`
59+
note: these traits exist but are inaccessible
60+
--> $DIR/privacy-ns2.rs:31:5
61+
|
62+
LL | trait Bar {
63+
| ^^^^^^^^^ `foo2::Bar`: not accessible
64+
...
65+
LL | trait Bar {
66+
| ^^^^^^^^^ `foo3::Bar`: not accessible
3767
help: use `=` if you meant to assign
3868
|
3969
LL - let _x : Bar();

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

+14
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ error[E0412]: cannot find type `Mul` in this scope
1919
LL | fn getMul() -> Mul {
2020
| ^^^ not found in this scope
2121
|
22+
= note: maybe you should make the path `mul3::Mul` public with `pub`
23+
= note: maybe you should make the path `mul4::Mul` public with `pub`
24+
= note: maybe you should make the path `mul5::Mul` public with `pub`
25+
note: these items exist but are inaccessible
26+
--> $DIR/issue-21221-1.rs:10:5
27+
|
28+
LL | enum Mul {
29+
| ^^^^^^^^ `mul3::Mul`: not accessible
30+
...
31+
LL | type Mul = String;
32+
| ^^^^^^^^^^^^^^^^^^ `mul4::Mul`: not accessible
33+
...
34+
LL | struct Mul{
35+
| ^^^^^^^^^^ `mul5::Mul`: not accessible
2236
help: consider importing one of these traits
2337
|
2438
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)