Skip to content

Commit

Permalink
use subdiagnostic for sugesting add let
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Nov 8, 2022
1 parent 667b15b commit 9e7d228
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_error_messages/locales/en-US/infer.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,4 @@ infer_msl_introduces_static = introduces a `'static` lifetime requirement
infer_msl_unmet_req = because this has an unmet lifetime requirement
infer_msl_trait_note = this has an implicit `'static` lifetime requirement
infer_msl_trait_sugg = consider relaxing the implicit `'static` requirement
infer_suggest_add_let_for_letchains = consider adding `let`
12 changes: 12 additions & 0 deletions compiler/rustc_infer/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ pub enum SourceKindMultiSuggestion<'a> {
},
}

#[derive(Subdiagnostic)]
#[suggestion(
infer_suggest_add_let_for_letchains,
style = "verbose",
applicability = "machine-applicable",
code = "let "
)]
pub(crate) struct SuggAddLetForLetChains {
#[primary_span]
pub span: Span,
}

impl<'a> SourceKindMultiSuggestion<'a> {
pub fn new_fully_qualified(
span: Span,
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use crate::traits::{
StatementAsExpression,
};

use crate::errors::SuggAddLetForLetChains;
use hir::intravisit::{walk_expr, walk_stmt};
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed, IntoDiagnosticArg};
Expand Down Expand Up @@ -2421,12 +2422,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let mut visitor = IfVisitor { err_span: span, found_if: false, result: false };
visitor.visit_body(&body);
if visitor.result {
err.span_suggestion_verbose(
span.shrink_to_lo(),
"consider adding `let`",
"let ".to_string(),
Applicability::MachineApplicable,
);
err.subdiagnostic(SuggAddLetForLetChains{span: span.shrink_to_lo()});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ pub(crate) struct ExpectedExpressionFoundLet {
pub(crate) struct ExpectedEqForLetExpr {
#[primary_span]
pub span: Span,
#[suggestion_verbose(applicability = "maybe-incorrect", code = "=")]
#[suggestion(applicability = "maybe-incorrect", code = "=", style = "verbose")]
pub sugg_span: Span,
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/did_you_mean/issue-103909.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ error[E0308]: mismatched types
|
LL | if Err(err) = File::open("hello.txt") {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`
|
help: consider adding `let`
|
LL | if let Err(err) = File::open("hello.txt") {
| +++

error: aborting due to 2 previous errors

Expand Down

0 comments on commit 9e7d228

Please sign in to comment.