Skip to content

Commit 91a43f0

Browse files
committed
Only suggest 1-tuple if expected and found types match
1 parent 18cea90 commit 91a43f0

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

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

+28-20
Original file line numberDiff line numberDiff line change
@@ -2044,26 +2044,34 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
20442044
// If a tuple of length one was expected and the found expression has
20452045
// parentheses around it, perhaps the user meant to write `(expr,)` to
20462046
// build a tuple (issue #86100)
2047-
(ty::Tuple(_), _) if expected.tuple_fields().count() == 1 => {
2048-
if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) {
2049-
if code.starts_with('(') && code.ends_with(')') {
2050-
let before_close = span.hi() - BytePos::from_u32(1);
2051-
2052-
err.span_suggestion(
2053-
span.with_hi(before_close).shrink_to_hi(),
2054-
"use a trailing comma to create a tuple with one element",
2055-
",".into(),
2056-
Applicability::MaybeIncorrect,
2057-
);
2058-
} else {
2059-
err.multipart_suggestion(
2060-
"use a trailing comma to create a tuple with one element",
2061-
vec![
2062-
(span.shrink_to_lo(), "(".into()),
2063-
(span.shrink_to_hi(), ",)".into()),
2064-
],
2065-
Applicability::MaybeIncorrect,
2066-
);
2047+
(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+
}
20672075
}
20682076
}
20692077
}

src/test/ui/typeck/issue-84768.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ LL | <F as FnOnce(&mut u8)>::call_once(f, 1)
1212
|
1313
= note: expected tuple `(&mut u8,)`
1414
found type `{integer}`
15-
help: use a trailing comma to create a tuple with one element
16-
|
17-
LL | <F as FnOnce(&mut u8)>::call_once(f, (1,))
18-
| + ++
1915

2016
error: aborting due to 2 previous errors
2117

0 commit comments

Comments
 (0)