Skip to content

Commit 3da517d

Browse files
authored
Unrolled build for rust-lang#121190
Rollup merge of rust-lang#121190 - bvanjoi:fix-114884, r=petrochenkov avoid overlapping privacy suggestion for single nested imports Fixes rust-lang#114884 This PR aims to avoid confusion inside braces for import suggestions. r? ``@petrochenkov``
2 parents bfe762e + 7303014 commit 3da517d

File tree

6 files changed

+81
-25
lines changed

6 files changed

+81
-25
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+39-23
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17361736
}
17371737

17381738
fn report_privacy_error(&mut self, privacy_error: &PrivacyError<'a>) {
1739-
let PrivacyError { ident, binding, outermost_res, parent_scope, dedup_span } =
1739+
let PrivacyError { ident, binding, outermost_res, parent_scope, single_nested, dedup_span } =
17401740
*privacy_error;
17411741

17421742
let res = binding.res();
@@ -1775,7 +1775,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
17751775
&import_suggestions,
17761776
Instead::Yes,
17771777
FoundUse::Yes,
1778-
DiagMode::Import,
1778+
DiagMode::Import { append: single_nested },
17791779
vec![],
17801780
"",
17811781
);
@@ -2701,7 +2701,11 @@ pub(crate) enum DiagMode {
27012701
/// The binding is part of a pattern
27022702
Pattern,
27032703
/// The binding is part of a use statement
2704-
Import,
2704+
Import {
2705+
/// `true` mean add the tips afterward for case `use a::{b,c}`,
2706+
/// rather than replacing within.
2707+
append: bool,
2708+
},
27052709
}
27062710

27072711
pub(crate) fn import_candidates(
@@ -2726,6 +2730,8 @@ pub(crate) fn import_candidates(
27262730
);
27272731
}
27282732

2733+
type PathString<'a> = (String, &'a str, Option<DefId>, &'a Option<String>, bool);
2734+
27292735
/// When an entity with a given name is not available in scope, we search for
27302736
/// entities with that name in all crates. This method allows outputting the
27312737
/// results of this search in a programmer-friendly way. If any entities are
@@ -2746,10 +2752,8 @@ fn show_candidates(
27462752
return false;
27472753
}
27482754

2749-
let mut accessible_path_strings: Vec<(String, &str, Option<DefId>, &Option<String>, bool)> =
2750-
Vec::new();
2751-
let mut inaccessible_path_strings: Vec<(String, &str, Option<DefId>, &Option<String>, bool)> =
2752-
Vec::new();
2755+
let mut accessible_path_strings: Vec<PathString<'_>> = Vec::new();
2756+
let mut inaccessible_path_strings: Vec<PathString<'_>> = Vec::new();
27532757

27542758
candidates.iter().for_each(|c| {
27552759
if c.accessible {
@@ -2811,6 +2815,15 @@ fn show_candidates(
28112815
err.note(note.clone());
28122816
}
28132817

2818+
let append_candidates = |msg: &mut String, accessible_path_strings: Vec<PathString<'_>>| {
2819+
msg.push(':');
2820+
2821+
for candidate in accessible_path_strings {
2822+
msg.push('\n');
2823+
msg.push_str(&candidate.0);
2824+
}
2825+
};
2826+
28142827
if let Some(span) = use_placement_span {
28152828
let (add_use, trailing) = match mode {
28162829
DiagMode::Pattern => {
@@ -2822,7 +2835,7 @@ fn show_candidates(
28222835
);
28232836
return true;
28242837
}
2825-
DiagMode::Import => ("", ""),
2838+
DiagMode::Import { .. } => ("", ""),
28262839
DiagMode::Normal => ("use ", ";\n"),
28272840
};
28282841
for candidate in &mut accessible_path_strings {
@@ -2839,13 +2852,22 @@ fn show_candidates(
28392852
format!("{add_use}{}{append}{trailing}{additional_newline}", &candidate.0);
28402853
}
28412854

2842-
err.span_suggestions_with_style(
2843-
span,
2844-
msg,
2845-
accessible_path_strings.into_iter().map(|a| a.0),
2846-
Applicability::MaybeIncorrect,
2847-
SuggestionStyle::ShowAlways,
2848-
);
2855+
match mode {
2856+
DiagMode::Import { append: true, .. } => {
2857+
append_candidates(&mut msg, accessible_path_strings);
2858+
err.span_help(span, msg);
2859+
}
2860+
_ => {
2861+
err.span_suggestions_with_style(
2862+
span,
2863+
msg,
2864+
accessible_path_strings.into_iter().map(|a| a.0),
2865+
Applicability::MaybeIncorrect,
2866+
SuggestionStyle::ShowAlways,
2867+
);
2868+
}
2869+
}
2870+
28492871
if let [first, .., last] = &path[..] {
28502872
let sp = first.ident.span.until(last.ident.span);
28512873
// Our suggestion is empty, so make sure the span is not empty (or we'd ICE).
@@ -2860,17 +2882,11 @@ fn show_candidates(
28602882
}
28612883
}
28622884
} else {
2863-
msg.push(':');
2864-
2865-
for candidate in accessible_path_strings {
2866-
msg.push('\n');
2867-
msg.push_str(&candidate.0);
2868-
}
2869-
2885+
append_candidates(&mut msg, accessible_path_strings);
28702886
err.help(msg);
28712887
}
28722888
true
2873-
} else if !(inaccessible_path_strings.is_empty() || matches!(mode, DiagMode::Import)) {
2889+
} else if !(inaccessible_path_strings.is_empty() || matches!(mode, DiagMode::Import { .. })) {
28742890
let prefix =
28752891
if let DiagMode::Pattern = mode { "you might have meant to match on " } else { "" };
28762892
if let [(name, descr, def_id, note, _)] = &inaccessible_path_strings[..] {

compiler/rustc_resolve/src/ident.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
868868
.into_iter()
869869
.find_map(|binding| if binding == ignore_binding { None } else { binding });
870870

871-
if let Some(Finalize { path_span, report_private, used, .. }) = finalize {
871+
if let Some(Finalize { path_span, report_private, used, root_span, .. }) = finalize {
872872
let Some(binding) = binding else {
873873
return Err((Determined, Weak::No));
874874
};
@@ -881,6 +881,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
881881
dedup_span: path_span,
882882
outermost_res: None,
883883
parent_scope: *parent_scope,
884+
single_nested: path_span != root_span,
884885
});
885886
} else {
886887
return Err((Determined, Weak::No));

compiler/rustc_resolve/src/imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
715715
&mut diag,
716716
Some(err.span),
717717
candidates,
718-
DiagMode::Import,
718+
DiagMode::Import { append: false },
719719
(source != target)
720720
.then(|| format!(" as {target}"))
721721
.as_deref()

compiler/rustc_resolve/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@ struct PrivacyError<'a> {
729729
dedup_span: Span,
730730
outermost_res: Option<(Res, Ident)>,
731731
parent_scope: ParentScope<'a>,
732+
/// Is the format `use a::{b,c}`?
733+
single_nested: bool,
732734
}
733735

734736
#[derive(Debug)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// https://github.com/rust-lang/rust/issues/114884
2+
3+
mod mod1 {
4+
pub trait TraitA {}
5+
}
6+
7+
mod mod2 {
8+
mod sub_mod {
9+
use super::super::mod1::TraitA;
10+
}
11+
}
12+
13+
use mod2::{sub_mod::TraitA};
14+
//~^ ERROR: module `sub_mod` is private
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0603]: module `sub_mod` is private
2+
--> $DIR/append-import-suggestion.rs:13:12
3+
|
4+
LL | use mod2::{sub_mod::TraitA};
5+
| ^^^^^^^ private module
6+
|
7+
help: consider importing this trait instead:
8+
mod1::TraitA
9+
--> $DIR/append-import-suggestion.rs:13:12
10+
|
11+
LL | use mod2::{sub_mod::TraitA};
12+
| ^^^^^^^^^^^^^^^
13+
note: the module `sub_mod` is defined here
14+
--> $DIR/append-import-suggestion.rs:8:5
15+
|
16+
LL | mod sub_mod {
17+
| ^^^^^^^^^^^
18+
19+
error: aborting due to 1 previous error
20+
21+
For more information about this error, try `rustc --explain E0603`.

0 commit comments

Comments
 (0)