From b610ce72cec5e450410c50418117a608c5d2ef6a Mon Sep 17 00:00:00 2001 From: IQuant Date: Fri, 3 Mar 2023 22:03:12 +0300 Subject: [PATCH] Migrate TupleTrailingCommaSuggestion --- compiler/rustc_infer/messages.ftl | 2 ++ compiler/rustc_infer/src/errors/mod.rs | 16 ++++++++++++ .../src/infer/error_reporting/mod.rs | 25 ++++++++----------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_infer/messages.ftl b/compiler/rustc_infer/messages.ftl index cde0f8e35d0..0f33660e50c 100644 --- a/compiler/rustc_infer/messages.ftl +++ b/compiler/rustc_infer/messages.ftl @@ -368,3 +368,5 @@ infer_sbfrit_box_return_expr = if you change the return type to expect trait obj infer_stp_wrap_one = try wrapping the pattern in `{$variant}` infer_stp_wrap_many = try wrapping the pattern in a variant of `{$path}` + +infer_tuple_trailing_comma = use a trailing comma to create a tuple with one element diff --git a/compiler/rustc_infer/src/errors/mod.rs b/compiler/rustc_infer/src/errors/mod.rs index 439d8a381e4..6432ae7d3f8 100644 --- a/compiler/rustc_infer/src/errors/mod.rs +++ b/compiler/rustc_infer/src/errors/mod.rs @@ -1371,3 +1371,19 @@ impl AddToDiagnostic for SuggestTuplePatternMany { ); } } + +#[derive(Subdiagnostic)] +pub enum TupleTrailingCommaSuggestion { + #[suggestion(infer_tuple_trailing_comma, code = ",", applicability = "machine-applicable")] + OnlyComma { + #[primary_span] + span: Span, + }, + #[multipart_suggestion(infer_tuple_trailing_comma, applicability = "machine-applicable")] + AlsoParentheses { + #[suggestion_part(code = "(")] + span_low: Span, + #[suggestion_part(code = ",)")] + span_high: Span, + }, +} diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index c9956b60a56..a4345d3ab21 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -50,6 +50,7 @@ use super::region_constraints::GenericKind; use super::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TypeTrace, ValuePairs}; use crate::errors; +use crate::errors::TupleTrailingCommaSuggestion; use crate::infer; use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type; use crate::infer::ExpectedFound; @@ -2110,22 +2111,18 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) else { return }; - let msg = "use a trailing comma to create a tuple with one element"; - if code.starts_with('(') && code.ends_with(')') { + let sugg = 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(), - msg, - ",", - Applicability::MachineApplicable, - ); + TupleTrailingCommaSuggestion::OnlyComma { + span: span.with_hi(before_close).shrink_to_hi(), + } } else { - err.multipart_suggestion( - msg, - vec![(span.shrink_to_lo(), "(".into()), (span.shrink_to_hi(), ",)".into())], - Applicability::MachineApplicable, - ); - } + TupleTrailingCommaSuggestion::AlsoParentheses { + span_low: span.shrink_to_lo(), + span_high: span.shrink_to_hi(), + } + }; + err.subdiagnostic(sugg); } fn values_str(