diff options
| author | Rob Pilling <robpilling@gmail.com> | 2022-01-28 23:17:47 +0000 |
|---|---|---|
| committer | Rob Pilling <robpilling@gmail.com> | 2022-01-28 23:43:47 +0000 |
| commit | 91a43f04237dc9fe839fe8466d459e4f215d02fc (patch) | |
| tree | e27a4b2c217d52ff55e8354dbb422e16426431f7 | |
| parent | 18cea90d4a01cbb3a1285f633b1058fd39931956 (diff) | |
| download | rust-91a43f04237dc9fe839fe8466d459e4f215d02fc.tar.gz rust-91a43f04237dc9fe839fe8466d459e4f215d02fc.zip | |
Only suggest 1-tuple if expected and found types match
| -rw-r--r-- | compiler/rustc_infer/src/infer/error_reporting/mod.rs | 48 | ||||
| -rw-r--r-- | src/test/ui/typeck/issue-84768.stderr | 4 |
2 files changed, 28 insertions, 24 deletions
diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 71a81d12839..c16181aab04 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2044,26 +2044,34 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { // If a tuple of length one was expected and the found expression has // parentheses around it, perhaps the user meant to write `(expr,)` to // build a tuple (issue #86100) - (ty::Tuple(_), _) if expected.tuple_fields().count() == 1 => { - if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) { - if code.starts_with('(') && code.ends_with(')') { - let before_close = span.hi() - BytePos::from_u32(1); - - err.span_suggestion( - span.with_hi(before_close).shrink_to_hi(), - "use a trailing comma to create a tuple with one element", - ",".into(), - Applicability::MaybeIncorrect, - ); - } else { - err.multipart_suggestion( - "use a trailing comma to create a tuple with one element", - vec![ - (span.shrink_to_lo(), "(".into()), - (span.shrink_to_hi(), ",)".into()), - ], - Applicability::MaybeIncorrect, - ); + (ty::Tuple(_), _) => { + if let [expected_tup_elem] = + expected.tuple_fields().collect::<Vec<_>>()[..] + { + if same_type_modulo_infer(expected_tup_elem, found) { + if let Ok(code) = + self.tcx.sess().source_map().span_to_snippet(span) + { + if code.starts_with('(') && code.ends_with(')') { + let before_close = span.hi() - BytePos::from_u32(1); + + err.span_suggestion( + span.with_hi(before_close).shrink_to_hi(), + "use a trailing comma to create a tuple with one element", + ",".into(), + Applicability::MaybeIncorrect, + ); + } else { + err.multipart_suggestion( + "use a trailing comma to create a tuple with one element", + vec![ + (span.shrink_to_lo(), "(".into()), + (span.shrink_to_hi(), ",)".into()), + ], + Applicability::MaybeIncorrect, + ); + } + } } } } diff --git a/src/test/ui/typeck/issue-84768.stderr b/src/test/ui/typeck/issue-84768.stderr index 331aa25c7a3..0a79d539ea9 100644 --- a/src/test/ui/typeck/issue-84768.stderr +++ b/src/test/ui/typeck/issue-84768.stderr @@ -12,10 +12,6 @@ LL | <F as FnOnce(&mut u8)>::call_once(f, 1) | = note: expected tuple `(&mut u8,)` found type `{integer}` -help: use a trailing comma to create a tuple with one element - | -LL | <F as FnOnce(&mut u8)>::call_once(f, (1,)) - | + ++ error: aborting due to 2 previous errors |
