Skip to content

Commit 71b0e95

Browse files
authored
Rollup merge of #99846 - TaKO8Ki:refactor-UnresolvedImportError, r=davidtwco
Refactor `UnresolvedImportError` This patch changes the type of `note` field in `UnresolvedImportError` to `Option<String>`.
2 parents 3a37c9a + 2ce42eb commit 71b0e95

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

compiler/rustc_parse/src/parser/item.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,6 @@ impl<'a> Parser<'a> {
16771677
}
16781678

16791679
/// Is this a possibly malformed start of a `macro_rules! foo` item definition?
1680-
16811680
fn is_macro_rules_item(&mut self) -> IsMacroRulesItem {
16821681
if self.check_keyword(kw::MacroRules) {
16831682
let macro_rules_span = self.token.span;

compiler/rustc_resolve/src/diagnostics.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
20232023
span: Span,
20242024
mut path: Vec<Segment>,
20252025
parent_scope: &ParentScope<'b>,
2026-
) -> Option<(Vec<Segment>, Vec<String>)> {
2026+
) -> Option<(Vec<Segment>, Option<String>)> {
20272027
debug!("make_path_suggestion: span={:?} path={:?}", span, path);
20282028

20292029
match (path.get(0), path.get(1)) {
@@ -2058,12 +2058,12 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
20582058
&mut self,
20592059
mut path: Vec<Segment>,
20602060
parent_scope: &ParentScope<'b>,
2061-
) -> Option<(Vec<Segment>, Vec<String>)> {
2061+
) -> Option<(Vec<Segment>, Option<String>)> {
20622062
// Replace first ident with `self` and check if that is valid.
20632063
path[0].ident.name = kw::SelfLower;
20642064
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
20652065
debug!("make_missing_self_suggestion: path={:?} result={:?}", path, result);
2066-
if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None }
2066+
if let PathResult::Module(..) = result { Some((path, None)) } else { None }
20672067
}
20682068

20692069
/// Suggests a missing `crate::` if that resolves to an correct module.
@@ -2077,20 +2077,20 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
20772077
&mut self,
20782078
mut path: Vec<Segment>,
20792079
parent_scope: &ParentScope<'b>,
2080-
) -> Option<(Vec<Segment>, Vec<String>)> {
2080+
) -> Option<(Vec<Segment>, Option<String>)> {
20812081
// Replace first ident with `crate` and check if that is valid.
20822082
path[0].ident.name = kw::Crate;
20832083
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
20842084
debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result);
20852085
if let PathResult::Module(..) = result {
20862086
Some((
20872087
path,
2088-
vec![
2088+
Some(
20892089
"`use` statements changed in Rust 2018; read more at \
20902090
<https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-\
20912091
clarity.html>"
20922092
.to_string(),
2093-
],
2093+
),
20942094
))
20952095
} else {
20962096
None
@@ -2108,12 +2108,12 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
21082108
&mut self,
21092109
mut path: Vec<Segment>,
21102110
parent_scope: &ParentScope<'b>,
2111-
) -> Option<(Vec<Segment>, Vec<String>)> {
2111+
) -> Option<(Vec<Segment>, Option<String>)> {
21122112
// Replace first ident with `crate` and check if that is valid.
21132113
path[0].ident.name = kw::Super;
21142114
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
21152115
debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result);
2116-
if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None }
2116+
if let PathResult::Module(..) = result { Some((path, None)) } else { None }
21172117
}
21182118

21192119
/// Suggests a missing external crate name if that resolves to an correct module.
@@ -2130,7 +2130,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
21302130
&mut self,
21312131
mut path: Vec<Segment>,
21322132
parent_scope: &ParentScope<'b>,
2133-
) -> Option<(Vec<Segment>, Vec<String>)> {
2133+
) -> Option<(Vec<Segment>, Option<String>)> {
21342134
if path[1].ident.span.rust_2015() {
21352135
return None;
21362136
}
@@ -2151,7 +2151,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
21512151
name, path, result
21522152
);
21532153
if let PathResult::Module(..) = result {
2154-
return Some((path, Vec::new()));
2154+
return Some((path, None));
21552155
}
21562156
}
21572157

@@ -2175,7 +2175,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
21752175
import: &'b Import<'b>,
21762176
module: ModuleOrUniformRoot<'b>,
21772177
ident: Ident,
2178-
) -> Option<(Option<Suggestion>, Vec<String>)> {
2178+
) -> Option<(Option<Suggestion>, Option<String>)> {
21792179
let ModuleOrUniformRoot::Module(mut crate_module) = module else {
21802180
return None;
21812181
};
@@ -2287,12 +2287,9 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
22872287
String::from("a macro with this name exists at the root of the crate"),
22882288
Applicability::MaybeIncorrect,
22892289
));
2290-
let note = vec![
2291-
"this could be because a macro annotated with `#[macro_export]` will be exported \
2292-
at the root of the crate instead of the module where it is defined"
2293-
.to_string(),
2294-
];
2295-
Some((suggestion, note))
2290+
Some((suggestion, Some("this could be because a macro annotated with `#[macro_export]` will be exported \
2291+
at the root of the crate instead of the module where it is defined"
2292+
.to_string())))
22962293
} else {
22972294
None
22982295
}

compiler/rustc_resolve/src/imports.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl<'a> Resolver<'a> {
336336
struct UnresolvedImportError {
337337
span: Span,
338338
label: Option<String>,
339-
note: Vec<String>,
339+
note: Option<String>,
340340
suggestion: Option<Suggestion>,
341341
}
342342

@@ -427,7 +427,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
427427
let err = UnresolvedImportError {
428428
span: import.span,
429429
label: None,
430-
note: Vec::new(),
430+
note: None,
431431
suggestion: None,
432432
};
433433
if path.contains("::") {
@@ -463,10 +463,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
463463

464464
let mut diag = struct_span_err!(self.r.session, span, E0432, "{}", &msg);
465465

466-
if let Some((_, UnresolvedImportError { note, .. })) = errors.iter().last() {
467-
for message in note {
468-
diag.note(message);
469-
}
466+
if let Some((_, UnresolvedImportError { note: Some(note), .. })) = errors.iter().last() {
467+
diag.note(note);
470468
}
471469

472470
for (_, err) in errors.into_iter().take(MAX_LABEL_COUNT) {
@@ -644,7 +642,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
644642
None => UnresolvedImportError {
645643
span,
646644
label: Some(label),
647-
note: Vec::new(),
645+
note: None,
648646
suggestion,
649647
},
650648
};
@@ -686,7 +684,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
686684
return Some(UnresolvedImportError {
687685
span: import.span,
688686
label: Some(String::from("cannot glob-import a module into itself")),
689-
note: Vec::new(),
687+
note: None,
690688
suggestion: None,
691689
});
692690
}
@@ -830,7 +828,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
830828
let (suggestion, note) =
831829
match self.check_for_module_export_macro(import, module, ident) {
832830
Some((suggestion, note)) => (suggestion.or(lev_suggestion), note),
833-
_ => (lev_suggestion, Vec::new()),
831+
_ => (lev_suggestion, None),
834832
};
835833

836834
let label = match module {

0 commit comments

Comments
 (0)