Skip to content

Commit 26cfa6f

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

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

@@ -1794,7 +1789,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17941789
&import_suggestions,
17951790
Instead::Yes,
17961791
FoundUse::Yes,
1797-
DiagMode::Import { append: single_nested },
1792+
DiagMode::Import { append: single_nested, unresolved_import: false },
17981793
vec![],
17991794
"",
18001795
);
@@ -2751,6 +2746,8 @@ pub(crate) enum DiagMode {
27512746
Pattern,
27522747
/// The binding is part of a use statement
27532748
Import {
2749+
/// `true` means diagnostics is for unresolved import
2750+
unresolved_import: bool,
27542751
/// `true` mean add the tips afterward for case `use a::{b,c}`,
27552752
/// rather than replacing within.
27562753
append: bool,
@@ -2801,6 +2798,7 @@ fn show_candidates(
28012798
return false;
28022799
}
28032800

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

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

30243025
err.span_note(multi_span, msg);
30253026
}
3026-
true
3027-
} else {
3028-
false
3027+
showed = true;
30293028
}
3029+
showed
30303030
}
30313031

30323032
#[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)