Skip to content

Commit 9d304e8

Browse files
authored
Rollup merge of #157580 - rperier:duplicate_importing_suggestion, r=folkertdev
Importing suggestion reported twice when reporting privacy error cc #157454 This is a first fix proposal
2 parents 753665a + 579efc5 commit 9d304e8

3 files changed

Lines changed: 49 additions & 4 deletions

File tree

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,7 +2307,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23072307

23082308
self.mention_default_field_values(source, ident, &mut err);
23092309

2310-
if let Some((this_res, outer_ident)) = outermost_res {
2310+
let shown_candidates = if let Some((this_res, outer_ident)) = outermost_res {
23112311
let mut import_suggestions = self.lookup_import_candidates(
23122312
outer_ident,
23132313
this_res.ns().unwrap_or(Namespace::TypeNS),
@@ -2338,7 +2338,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23382338
};
23392339
err.subdiagnostic(label);
23402340
}
2341-
}
2341+
!point_to_def
2342+
} else {
2343+
false
2344+
};
23422345

23432346
let mut non_exhaustive = None;
23442347
// If an ADT is foreign and marked as `non_exhaustive`, then that's
@@ -2476,8 +2479,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24762479
// 2) the use isn't nested, otherwise `dedup_span` is one ident in `{...}`.
24772480
//
24782481
// See issue #156060.
2479-
let can_replace_use =
2480-
!single_nested && !outermost_res.is_some_and(|(_, outer)| outer.span != ident.span);
2482+
let can_replace_use = !shown_candidates
2483+
&& !single_nested
2484+
&& !outermost_res.is_some_and(|(_, outer)| outer.span != ident.span);
24812485
if can_replace_use {
24822486
// We prioritize shorter paths, non-core imports and direct imports over the
24832487
// alternatives.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
mod A {
2+
pub struct A;
3+
//~^ NOTE ...and refers to the unit struct `A` which is defined here
4+
//~| NOTE you could import this directly
5+
}
6+
7+
mod B {
8+
use crate::A::A;
9+
//~^ NOTE the unit struct import `A` is defined here...
10+
}
11+
12+
fn main() {
13+
B::A;
14+
//~^ ERROR unit struct import `A` is private [E0603]
15+
//~| NOTE private unit struct import
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0603]: unit struct import `A` is private
2+
--> $DIR/import-suggestion-duplicated-importing-same-item.rs:13:8
3+
|
4+
LL | B::A;
5+
| ^ private unit struct import
6+
|
7+
note: the unit struct import `A` is defined here...
8+
--> $DIR/import-suggestion-duplicated-importing-same-item.rs:8:9
9+
|
10+
LL | use crate::A::A;
11+
| ^^^^^^^^^^^
12+
note: ...and refers to the unit struct `A` which is defined here
13+
--> $DIR/import-suggestion-duplicated-importing-same-item.rs:2:5
14+
|
15+
LL | pub struct A;
16+
| ^^^^^^^^^^^^^ you could import this directly
17+
help: consider importing this unit struct instead
18+
|
19+
LL - B::A;
20+
LL + A::A;
21+
|
22+
23+
error: aborting due to 1 previous error
24+
25+
For more information about this error, try `rustc --explain E0603`.

0 commit comments

Comments
 (0)