Skip to content

Commit

Permalink
Rollup merge of #99846 - TaKO8Ki:refactor-UnresolvedImportError, r=da…
Browse files Browse the repository at this point in the history
…vidtwco

Refactor `UnresolvedImportError`

This patch changes the type of `note` field in `UnresolvedImportError` to `Option<String>`.
  • Loading branch information
Dylan-DPC authored Jul 28, 2022
2 parents 3a37c9a + 2ce42eb commit 71b0e95
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
1 change: 0 additions & 1 deletion compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,6 @@ impl<'a> Parser<'a> {
}

/// Is this a possibly malformed start of a `macro_rules! foo` item definition?

fn is_macro_rules_item(&mut self) -> IsMacroRulesItem {
if self.check_keyword(kw::MacroRules) {
let macro_rules_span = self.token.span;
Expand Down
31 changes: 14 additions & 17 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
span: Span,
mut path: Vec<Segment>,
parent_scope: &ParentScope<'b>,
) -> Option<(Vec<Segment>, Vec<String>)> {
) -> Option<(Vec<Segment>, Option<String>)> {
debug!("make_path_suggestion: span={:?} path={:?}", span, path);

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

/// Suggests a missing `crate::` if that resolves to an correct module.
Expand All @@ -2077,20 +2077,20 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
&mut self,
mut path: Vec<Segment>,
parent_scope: &ParentScope<'b>,
) -> Option<(Vec<Segment>, Vec<String>)> {
) -> Option<(Vec<Segment>, Option<String>)> {
// Replace first ident with `crate` and check if that is valid.
path[0].ident.name = kw::Crate;
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
debug!("make_missing_crate_suggestion: path={:?} result={:?}", path, result);
if let PathResult::Module(..) = result {
Some((
path,
vec![
Some(
"`use` statements changed in Rust 2018; read more at \
<https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-\
clarity.html>"
.to_string(),
],
),
))
} else {
None
Expand All @@ -2108,12 +2108,12 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
&mut self,
mut path: Vec<Segment>,
parent_scope: &ParentScope<'b>,
) -> Option<(Vec<Segment>, Vec<String>)> {
) -> Option<(Vec<Segment>, Option<String>)> {
// Replace first ident with `crate` and check if that is valid.
path[0].ident.name = kw::Super;
let result = self.r.maybe_resolve_path(&path, None, parent_scope);
debug!("make_missing_super_suggestion: path={:?} result={:?}", path, result);
if let PathResult::Module(..) = result { Some((path, Vec::new())) } else { None }
if let PathResult::Module(..) = result { Some((path, None)) } else { None }
}

/// Suggests a missing external crate name if that resolves to an correct module.
Expand All @@ -2130,7 +2130,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
&mut self,
mut path: Vec<Segment>,
parent_scope: &ParentScope<'b>,
) -> Option<(Vec<Segment>, Vec<String>)> {
) -> Option<(Vec<Segment>, Option<String>)> {
if path[1].ident.span.rust_2015() {
return None;
}
Expand All @@ -2151,7 +2151,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
name, path, result
);
if let PathResult::Module(..) = result {
return Some((path, Vec::new()));
return Some((path, None));
}
}

Expand All @@ -2175,7 +2175,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
import: &'b Import<'b>,
module: ModuleOrUniformRoot<'b>,
ident: Ident,
) -> Option<(Option<Suggestion>, Vec<String>)> {
) -> Option<(Option<Suggestion>, Option<String>)> {
let ModuleOrUniformRoot::Module(mut crate_module) = module else {
return None;
};
Expand Down Expand Up @@ -2287,12 +2287,9 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
String::from("a macro with this name exists at the root of the crate"),
Applicability::MaybeIncorrect,
));
let note = vec![
"this could be because a macro annotated with `#[macro_export]` will be exported \
at the root of the crate instead of the module where it is defined"
.to_string(),
];
Some((suggestion, note))
Some((suggestion, Some("this could be because a macro annotated with `#[macro_export]` will be exported \
at the root of the crate instead of the module where it is defined"
.to_string())))
} else {
None
}
Expand Down
16 changes: 7 additions & 9 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl<'a> Resolver<'a> {
struct UnresolvedImportError {
span: Span,
label: Option<String>,
note: Vec<String>,
note: Option<String>,
suggestion: Option<Suggestion>,
}

Expand Down Expand Up @@ -427,7 +427,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
let err = UnresolvedImportError {
span: import.span,
label: None,
note: Vec::new(),
note: None,
suggestion: None,
};
if path.contains("::") {
Expand Down Expand Up @@ -463,10 +463,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {

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

if let Some((_, UnresolvedImportError { note, .. })) = errors.iter().last() {
for message in note {
diag.note(message);
}
if let Some((_, UnresolvedImportError { note: Some(note), .. })) = errors.iter().last() {
diag.note(note);
}

for (_, err) in errors.into_iter().take(MAX_LABEL_COUNT) {
Expand Down Expand Up @@ -644,7 +642,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
None => UnresolvedImportError {
span,
label: Some(label),
note: Vec::new(),
note: None,
suggestion,
},
};
Expand Down Expand Up @@ -686,7 +684,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
return Some(UnresolvedImportError {
span: import.span,
label: Some(String::from("cannot glob-import a module into itself")),
note: Vec::new(),
note: None,
suggestion: None,
});
}
Expand Down Expand Up @@ -830,7 +828,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
let (suggestion, note) =
match self.check_for_module_export_macro(import, module, ident) {
Some((suggestion, note)) => (suggestion.or(lev_suggestion), note),
_ => (lev_suggestion, Vec::new()),
_ => (lev_suggestion, None),
};

let label = match module {
Expand Down

0 comments on commit 71b0e95

Please sign in to comment.