more code refactor on smart_resolve_report_errors
This commit is contained in:
parent
7adfb44b95
commit
fdda7e0a33
@ -131,13 +131,13 @@ pub(super) enum LifetimeElisionCandidate {
|
||||
}
|
||||
|
||||
/// Only used for diagnostics.
|
||||
struct BaseError<'a> {
|
||||
struct BaseError {
|
||||
msg: String,
|
||||
fallback_label: String,
|
||||
span: Span,
|
||||
span_label: Option<(Span, &'a str)>,
|
||||
span_label: Option<(Span, &'static str)>,
|
||||
could_be_expr: bool,
|
||||
suggestion: Option<(Span, &'a str, String)>,
|
||||
suggestion: Option<(Span, &'static str, String)>,
|
||||
}
|
||||
|
||||
impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
@ -154,7 +154,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
span: Span,
|
||||
source: PathSource<'_>,
|
||||
res: Option<Res>,
|
||||
) -> BaseError<'static> {
|
||||
) -> BaseError {
|
||||
// Make the base error.
|
||||
let mut expected = source.descr_expected();
|
||||
let path_str = Segment::names_to_string(path);
|
||||
@ -290,7 +290,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
|
||||
self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span);
|
||||
|
||||
if let Some((span, label)) = base_error.span_label.clone() {
|
||||
if let Some((span, label)) = base_error.span_label {
|
||||
err.span_label(span, label);
|
||||
}
|
||||
|
||||
@ -303,17 +303,29 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
|
||||
self.suggest_self_or_self_ref(&mut err, path, span);
|
||||
self.detect_assoct_type_constraint_meant_as_path(&mut err, &base_error);
|
||||
if self.suggest_self_ty_or_self_value(&mut err, source, path, span) {
|
||||
if self.suggest_self_ty(&mut err, source, path, span)
|
||||
|| self.suggest_self_value(&mut err, source, path, span)
|
||||
{
|
||||
return (err, Vec::new());
|
||||
}
|
||||
|
||||
let (found, candidates) =
|
||||
self.try_lookup_name_with_relex_fashion(&mut err, source, path, span, res, &base_error);
|
||||
self.try_lookup_name_relaxed(&mut err, source, path, span, res, &base_error);
|
||||
if found {
|
||||
return (err, candidates);
|
||||
}
|
||||
|
||||
self.suggest_type_ascription(&mut err, source, path, res, span, &base_error);
|
||||
if !self.type_ascription_suggestion(&mut err, base_error.span) {
|
||||
let mut fallback =
|
||||
self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
|
||||
if self.suggest_typo(&mut err, source, path, span, &base_error) {
|
||||
fallback = true;
|
||||
}
|
||||
if fallback {
|
||||
// Fallback label.
|
||||
err.span_label(base_error.span, &base_error.fallback_label);
|
||||
}
|
||||
}
|
||||
self.err_code_special_cases(&mut err, source, path, span);
|
||||
|
||||
(err, candidates)
|
||||
@ -322,7 +334,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
fn detect_assoct_type_constraint_meant_as_path(
|
||||
&self,
|
||||
err: &mut Diagnostic,
|
||||
base_error: &BaseError<'static>,
|
||||
base_error: &BaseError,
|
||||
) {
|
||||
let Some(ty) = self.diagnostic_metadata.current_type_path else { return; };
|
||||
let TyKind::Path(_, path) = &ty.kind else { return; };
|
||||
@ -355,7 +367,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
|
||||
fn suggest_self_or_self_ref(&mut self, err: &mut Diagnostic, path: &[Segment], span: Span) {
|
||||
let is_assoc_fn = self.self_type_is_available();
|
||||
let item_str = path.last().unwrap().ident;
|
||||
let Some(path_last_segment) = path.last() else { return };
|
||||
let item_str = path_last_segment.ident;
|
||||
// Emit help message for fake-self from other languages (e.g., `this` in Javascript).
|
||||
if ["this", "my"].contains(&item_str.as_str()) && is_assoc_fn {
|
||||
err.span_suggestion_short(
|
||||
@ -392,14 +405,14 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
}
|
||||
}
|
||||
|
||||
fn try_lookup_name_with_relex_fashion(
|
||||
fn try_lookup_name_relaxed(
|
||||
&mut self,
|
||||
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
span: Span,
|
||||
res: Option<Res>,
|
||||
base_error: &BaseError<'static>,
|
||||
base_error: &BaseError,
|
||||
) -> (bool, Vec<ImportSuggestion>) {
|
||||
// Try to lookup name in more relaxed fashion for better error reporting.
|
||||
let ident = path.last().unwrap().ident;
|
||||
@ -563,19 +576,18 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
return (false, candidates);
|
||||
}
|
||||
|
||||
fn suggest_type_ascription(
|
||||
fn suggest_trait_and_bounds(
|
||||
&mut self,
|
||||
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
res: Option<Res>,
|
||||
span: Span,
|
||||
base_error: &BaseError<'static>,
|
||||
) {
|
||||
base_error: &BaseError,
|
||||
) -> bool {
|
||||
let is_macro =
|
||||
base_error.span.from_expansion() && base_error.span.desugaring_kind().is_none();
|
||||
if !self.type_ascription_suggestion(err, base_error.span) {
|
||||
let mut fallback = false;
|
||||
|
||||
if let (
|
||||
PathSource::Trait(AliasPossibility::Maybe),
|
||||
Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)),
|
||||
@ -608,8 +620,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
};
|
||||
multi_span.push_span_label(sp, msg);
|
||||
}
|
||||
multi_span
|
||||
.push_span_label(base_error.span, "expected this type to be a trait...");
|
||||
multi_span.push_span_label(base_error.span, "expected this type to be a trait...");
|
||||
err.span_help(
|
||||
multi_span,
|
||||
"`+` is used to constrain a \"trait object\" type with lifetimes or \
|
||||
@ -636,11 +647,22 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
}
|
||||
}
|
||||
|
||||
fallback |= self.restrict_assoc_type_in_where_clause(span, err);
|
||||
fallback
|
||||
}
|
||||
|
||||
fn suggest_typo(
|
||||
&mut self,
|
||||
err: &mut DiagnosticBuilder<'_, ErrorGuaranteed>,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
span: Span,
|
||||
base_error: &BaseError,
|
||||
) -> bool {
|
||||
let is_expected = &|res| source.is_expected(res);
|
||||
let ident_span = path.last().map_or(span, |ident| ident.ident.span);
|
||||
fallback |= self.restrict_assoc_type_in_where_clause(span, err);
|
||||
|
||||
let typo_sugg = self.lookup_typo_candidate(path, source.namespace(), is_expected);
|
||||
let mut fallback = false;
|
||||
if !self.r.add_typo_suggestion(err, typo_sugg, ident_span) {
|
||||
fallback = true;
|
||||
match self.diagnostic_metadata.current_let_binding {
|
||||
@ -661,11 +683,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
let suggestion = self.get_single_associated_item(&path, &source, is_expected);
|
||||
self.r.add_typo_suggestion(err, suggestion, ident_span);
|
||||
}
|
||||
if fallback {
|
||||
// Fallback label.
|
||||
err.span_label(base_error.span, &base_error.fallback_label);
|
||||
}
|
||||
}
|
||||
fallback
|
||||
}
|
||||
|
||||
fn err_code_special_cases(
|
||||
@ -713,14 +731,16 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
}
|
||||
|
||||
/// Emit special messages for unresolved `Self` and `self`.
|
||||
fn suggest_self_ty_or_self_value(
|
||||
fn suggest_self_ty(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
span: Span,
|
||||
) -> bool {
|
||||
if is_self_type(path, source.namespace()) {
|
||||
if !is_self_type(path, source.namespace()) {
|
||||
return false;
|
||||
}
|
||||
err.code(rustc_errors::error_code!(E0411));
|
||||
err.span_label(
|
||||
span,
|
||||
@ -736,10 +756,20 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
),
|
||||
);
|
||||
}
|
||||
return true;
|
||||
true
|
||||
}
|
||||
|
||||
fn suggest_self_value(
|
||||
&mut self,
|
||||
err: &mut Diagnostic,
|
||||
source: PathSource<'_>,
|
||||
path: &[Segment],
|
||||
span: Span,
|
||||
) -> bool {
|
||||
if !is_self_value(path, source.namespace()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if is_self_value(path, source.namespace()) {
|
||||
debug!("smart_resolve_path_fragment: E0424, source={:?}", source);
|
||||
err.code(rustc_errors::error_code!(E0424));
|
||||
err.span_label(
|
||||
@ -807,9 +837,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
),
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
false
|
||||
true
|
||||
}
|
||||
|
||||
fn suggest_swapping_misplaced_self_ty_and_trait(
|
||||
@ -861,7 +889,11 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||
span: Span,
|
||||
) {
|
||||
if let PathSource::Expr(_) = source &&
|
||||
let Some(Expr { span: expr_span, kind: ExprKind::Assign(lhs, _, _), .. } ) = self.diagnostic_metadata.in_if_condition {
|
||||
let Some(Expr {
|
||||
span: expr_span,
|
||||
kind: ExprKind::Assign(lhs, _, _),
|
||||
..
|
||||
}) = self.diagnostic_metadata.in_if_condition {
|
||||
// Icky heuristic so we don't suggest:
|
||||
// `if (i + 2) = 2` => `if let (i + 2) = 2` (approximately pattern)
|
||||
// `if 2 = i` => `if let 2 = i` (lhs needs to contain error span)
|
||||
|
Loading…
x
Reference in New Issue
Block a user