Skip to content

Commit b610ce7

Browse files
committed
Migrate TupleTrailingCommaSuggestion
1 parent d18adb7 commit b610ce7

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

compiler/rustc_infer/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,5 @@ infer_sbfrit_box_return_expr = if you change the return type to expect trait obj
368368
369369
infer_stp_wrap_one = try wrapping the pattern in `{$variant}`
370370
infer_stp_wrap_many = try wrapping the pattern in a variant of `{$path}`
371+
372+
infer_tuple_trailing_comma = use a trailing comma to create a tuple with one element

compiler/rustc_infer/src/errors/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,3 +1371,19 @@ impl AddToDiagnostic for SuggestTuplePatternMany {
13711371
);
13721372
}
13731373
}
1374+
1375+
#[derive(Subdiagnostic)]
1376+
pub enum TupleTrailingCommaSuggestion {
1377+
#[suggestion(infer_tuple_trailing_comma, code = ",", applicability = "machine-applicable")]
1378+
OnlyComma {
1379+
#[primary_span]
1380+
span: Span,
1381+
},
1382+
#[multipart_suggestion(infer_tuple_trailing_comma, applicability = "machine-applicable")]
1383+
AlsoParentheses {
1384+
#[suggestion_part(code = "(")]
1385+
span_low: Span,
1386+
#[suggestion_part(code = ",)")]
1387+
span_high: Span,
1388+
},
1389+
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use super::region_constraints::GenericKind;
5050
use super::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TypeTrace, ValuePairs};
5151

5252
use crate::errors;
53+
use crate::errors::TupleTrailingCommaSuggestion;
5354
use crate::infer;
5455
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
5556
use crate::infer::ExpectedFound;
@@ -2110,22 +2111,18 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
21102111
let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
21112112
else { return };
21122113

2113-
let msg = "use a trailing comma to create a tuple with one element";
2114-
if code.starts_with('(') && code.ends_with(')') {
2114+
let sugg = if code.starts_with('(') && code.ends_with(')') {
21152115
let before_close = span.hi() - BytePos::from_u32(1);
2116-
err.span_suggestion(
2117-
span.with_hi(before_close).shrink_to_hi(),
2118-
msg,
2119-
",",
2120-
Applicability::MachineApplicable,
2121-
);
2116+
TupleTrailingCommaSuggestion::OnlyComma {
2117+
span: span.with_hi(before_close).shrink_to_hi(),
2118+
}
21222119
} else {
2123-
err.multipart_suggestion(
2124-
msg,
2125-
vec![(span.shrink_to_lo(), "(".into()), (span.shrink_to_hi(), ",)".into())],
2126-
Applicability::MachineApplicable,
2127-
);
2128-
}
2120+
TupleTrailingCommaSuggestion::AlsoParentheses {
2121+
span_low: span.shrink_to_lo(),
2122+
span_high: span.shrink_to_hi(),
2123+
}
2124+
};
2125+
err.subdiagnostic(sugg);
21292126
}
21302127

21312128
fn values_str(

0 commit comments

Comments
 (0)