From 519dd8e9de1a85e18b3033a007a3bf01c6e79560 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 25 Apr 2022 22:55:15 +0200 Subject: migrate ambiguous plus diagnostic --- compiler/rustc_parse/src/parser/diagnostics.rs | 32 ++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index ed264045170..d270f3606d7 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -21,6 +21,7 @@ use rustc_errors::{pluralize, struct_span_err, Diagnostic, EmissionGuarantee, Er use rustc_errors::{ Applicability, DiagnosticBuilder, DiagnosticMessage, Handler, MultiSpan, PResult, }; +use rustc_macros::SessionDiagnostic; use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, Ident}; use rustc_span::{Span, SpanSnippetError, DUMMY_SP}; @@ -1170,16 +1171,29 @@ impl<'a> Parser<'a> { impl_dyn_multi: bool, ty: &Ty, ) { + #[derive(SessionDiagnostic)] + #[error(slug = "parser-maybe-report-ambiguous-plus")] + struct AmbiguousPlus { + pub sum_with_parens: String, + #[primary_span] + #[suggestion(code = "{sum_with_parens}")] + pub span: Span, + } + if matches!(allow_plus, AllowPlus::No) && impl_dyn_multi { - let sum_with_parens = format!("({})", pprust::ty_to_string(&ty)); - self.struct_span_err(ty.span, "ambiguous `+` in a type") - .span_suggestion( - ty.span, - "use parentheses to disambiguate", - sum_with_parens, - Applicability::MachineApplicable, - ) - .emit(); + self.sess.emit_err(AmbiguousPlus { + sum_with_parens: format!("({})", pprust::ty_to_string(&ty)), + span: ty.span, + }); + // let sum_with_parens = format!("({})", pprust::ty_to_string(&ty)); + // self.struct_span_err(ty.span, "ambiguous `+` in a type") + // .span_suggestion( + // ty.span, + // "use parentheses to disambiguate", + // sum_with_parens, + // Applicability::MachineApplicable, + // ) + // .emit(); } } -- cgit 1.4.1-3-g733a5 From 530f4dce291371e0b1d567b47a1888aa8c806410 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 25 Apr 2022 23:26:52 +0200 Subject: remove old code --- compiler/rustc_parse/src/parser/diagnostics.rs | 9 --------- 1 file changed, 9 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index d270f3606d7..23c4d67ebd7 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1185,15 +1185,6 @@ impl<'a> Parser<'a> { sum_with_parens: format!("({})", pprust::ty_to_string(&ty)), span: ty.span, }); - // let sum_with_parens = format!("({})", pprust::ty_to_string(&ty)); - // self.struct_span_err(ty.span, "ambiguous `+` in a type") - // .span_suggestion( - // ty.span, - // "use parentheses to disambiguate", - // sum_with_parens, - // Applicability::MachineApplicable, - // ) - // .emit(); } } -- cgit 1.4.1-3-g733a5 From 35b42cb9ecc61bb36a23e53ff4913865d3ab1e80 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 26 Apr 2022 11:11:23 +0200 Subject: avoid `format!` --- compiler/rustc_parse/src/parser/diagnostics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 23c4d67ebd7..f0a053d88b5 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1176,13 +1176,13 @@ impl<'a> Parser<'a> { struct AmbiguousPlus { pub sum_with_parens: String, #[primary_span] - #[suggestion(code = "{sum_with_parens}")] + #[suggestion(code = "({sum_with_parens})")] pub span: Span, } if matches!(allow_plus, AllowPlus::No) && impl_dyn_multi { self.sess.emit_err(AmbiguousPlus { - sum_with_parens: format!("({})", pprust::ty_to_string(&ty)), + sum_with_parens: pprust::ty_to_string(&ty), span: ty.span, }); } -- cgit 1.4.1-3-g733a5 From 6c3e793fb3303878790a6e872e8fa8bc1e4ef4e9 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 26 Apr 2022 11:12:48 +0200 Subject: move `AmbigousPlus` outside --- compiler/rustc_parse/src/parser/diagnostics.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index f0a053d88b5..e8dc0b3a684 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -242,6 +242,16 @@ impl MultiSugg { err.multipart_suggestions(msg, suggestions.map(|s| s.patches), applicability); } } + +#[derive(SessionDiagnostic)] +#[error(slug = "parser-maybe-report-ambiguous-plus")] +struct AmbiguousPlus { + pub sum_with_parens: String, + #[primary_span] + #[suggestion(code = "({sum_with_parens})")] + pub span: Span, +} + // SnapshotParser is used to create a snapshot of the parser // without causing duplicate errors being emitted when the `Parser` // is dropped. @@ -1171,15 +1181,6 @@ impl<'a> Parser<'a> { impl_dyn_multi: bool, ty: &Ty, ) { - #[derive(SessionDiagnostic)] - #[error(slug = "parser-maybe-report-ambiguous-plus")] - struct AmbiguousPlus { - pub sum_with_parens: String, - #[primary_span] - #[suggestion(code = "({sum_with_parens})")] - pub span: Span, - } - if matches!(allow_plus, AllowPlus::No) && impl_dyn_multi { self.sess.emit_err(AmbiguousPlus { sum_with_parens: pprust::ty_to_string(&ty), -- cgit 1.4.1-3-g733a5 From e7ae9eb3f272a96c2c73363539ca31986e5d5e88 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 27 Apr 2022 12:03:16 +0200 Subject: rename `sum_with_parens` --- compiler/rustc_parse/src/parser/diagnostics.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'compiler/rustc_parse/src/parser') diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index e8dc0b3a684..4edc9b39efc 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -246,9 +246,9 @@ impl MultiSugg { #[derive(SessionDiagnostic)] #[error(slug = "parser-maybe-report-ambiguous-plus")] struct AmbiguousPlus { - pub sum_with_parens: String, + pub sum_ty: String, #[primary_span] - #[suggestion(code = "({sum_with_parens})")] + #[suggestion(code = "({sum_ty})")] pub span: Span, } @@ -1182,10 +1182,7 @@ impl<'a> Parser<'a> { ty: &Ty, ) { if matches!(allow_plus, AllowPlus::No) && impl_dyn_multi { - self.sess.emit_err(AmbiguousPlus { - sum_with_parens: pprust::ty_to_string(&ty), - span: ty.span, - }); + self.sess.emit_err(AmbiguousPlus { sum_ty: pprust::ty_to_string(&ty), span: ty.span }); } } -- cgit 1.4.1-3-g733a5