Skip to content

Commit 344ea6e

Browse files
committed
Factor out emit_tuple_wrap_err, improve Applicability
1 parent 72d3b45 commit 344ea6e

File tree

1 file changed

+39
-29
lines changed
  • compiler/rustc_infer/src/infer/error_reporting

1 file changed

+39
-29
lines changed

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

+39-29
Original file line numberDiff line numberDiff line change
@@ -2045,35 +2045,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20452045
// parentheses around it, perhaps the user meant to write `(expr,)` to
20462046
// build a tuple (issue #86100)
20472047
(ty::Tuple(_), _) => {
2048-
if let [expected_tup_elem] =
2049-
expected.tuple_fields().collect::<Vec<_>>()[..]
2050-
{
2051-
if same_type_modulo_infer(expected_tup_elem, found) {
2052-
if let Ok(code) =
2053-
self.tcx.sess().source_map().span_to_snippet(span)
2054-
{
2055-
if code.starts_with('(') && code.ends_with(')') {
2056-
let before_close = span.hi() - BytePos::from_u32(1);
2057-
2058-
err.span_suggestion(
2059-
span.with_hi(before_close).shrink_to_hi(),
2060-
"use a trailing comma to create a tuple with one element",
2061-
",".into(),
2062-
Applicability::MaybeIncorrect,
2063-
);
2064-
} else {
2065-
err.multipart_suggestion(
2066-
"use a trailing comma to create a tuple with one element",
2067-
vec![
2068-
(span.shrink_to_lo(), "(".into()),
2069-
(span.shrink_to_hi(), ",)".into()),
2070-
],
2071-
Applicability::MaybeIncorrect,
2072-
);
2073-
}
2074-
}
2075-
}
2076-
}
2048+
self.emit_tuple_wrap_err(&mut err, span, found, expected)
20772049
}
20782050
// If a character was expected and the found expression is a string literal
20792051
// containing a single character, perhaps the user meant to write `'c'` to
@@ -2136,6 +2108,44 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
21362108
diag
21372109
}
21382110

2111+
fn emit_tuple_wrap_err(
2112+
&self,
2113+
err: &mut DiagnosticBuilder<'tcx>,
2114+
span: Span,
2115+
found: Ty<'tcx>,
2116+
expected: Ty<'tcx>,
2117+
) {
2118+
let [expected_tup_elem] = &expected.tuple_fields().collect::<Vec<_>>()[..]
2119+
else { return };
2120+
2121+
if !same_type_modulo_infer(expected_tup_elem, found) {
2122+
return;
2123+
}
2124+
2125+
let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
2126+
else { return };
2127+
2128+
if code.starts_with('(') && code.ends_with(')') {
2129+
let before_close = span.hi() - BytePos::from_u32(1);
2130+
2131+
err.span_suggestion(
2132+
span.with_hi(before_close).shrink_to_hi(),
2133+
"use a trailing comma to create a tuple with one element",
2134+
",".into(),
2135+
Applicability::MachineApplicable,
2136+
);
2137+
} else {
2138+
err.multipart_suggestion(
2139+
"use a trailing comma to create a tuple with one element",
2140+
vec![
2141+
(span.shrink_to_lo(), "(".into()),
2142+
(span.shrink_to_hi(), ",)".into()),
2143+
],
2144+
Applicability::MachineApplicable,
2145+
);
2146+
}
2147+
}
2148+
21392149
fn values_str(
21402150
&self,
21412151
values: ValuePairs<'tcx>,

0 commit comments

Comments
 (0)