From eb55cdce4bc1c03e1ce805af633dac611a948d43 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 25 Apr 2022 22:53:09 +0200 Subject: [PATCH 01/10] use `ParseSess` instead of `Session` in `into_diagnostic` --- .../rustc_macros/src/session_diagnostic.rs | 6 ++++-- compiler/rustc_session/src/parse.rs | 18 +++++++++++++++++- compiler/rustc_session/src/session.rs | 14 +++++++++----- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_macros/src/session_diagnostic.rs b/compiler/rustc_macros/src/session_diagnostic.rs index ff7506979fc34..5661451f52071 100644 --- a/compiler/rustc_macros/src/session_diagnostic.rs +++ b/compiler/rustc_macros/src/session_diagnostic.rs @@ -119,7 +119,9 @@ fn span_err(span: impl proc_macro::MultiSpan, msg: &str) -> proc_macro::Diagnost /// Emit a diagnostic on span `$span` with msg `$msg` (optionally performing additional decoration /// using the `FnOnce` passed in `diag`) and return `Err(ErrorHandled)`. macro_rules! throw_span_err { - ($span:expr, $msg:expr) => {{ throw_span_err!($span, $msg, |diag| diag) }}; + ($span:expr, $msg:expr) => {{ + throw_span_err!($span, $msg, |diag| diag) + }}; ($span:expr, $msg:expr, $f:expr) => {{ return Err(_throw_span_err($span, $msg, $f)); }}; @@ -308,7 +310,7 @@ impl<'a> SessionDiagnosticDerive<'a> { { fn into_diagnostic( self, - #sess: &'__session_diagnostic_sess rustc_session::Session + #sess: &'__session_diagnostic_sess rustc_session::parse::ParseSess ) -> rustc_errors::DiagnosticBuilder<'__session_diagnostic_sess, #param_ty> { use rustc_errors::IntoDiagnosticArg; #implementation diff --git a/compiler/rustc_session/src/parse.rs b/compiler/rustc_session/src/parse.rs index 6a36ae63c6834..dd7682ebacef5 100644 --- a/compiler/rustc_session/src/parse.rs +++ b/compiler/rustc_session/src/parse.rs @@ -3,13 +3,14 @@ use crate::config::CheckCfg; use crate::lint::{BufferedEarlyLint, BuiltinLintDiagnostics, Lint, LintId}; +use crate::SessionDiagnostic; use rustc_ast::node_id::NodeId; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::sync::{Lock, Lrc}; use rustc_errors::{emitter::SilentEmitter, ColorConfig, Handler}; use rustc_errors::{ error_code, fallback_fluent_bundle, Applicability, Diagnostic, DiagnosticBuilder, - ErrorGuaranteed, MultiSpan, + DiagnosticMessage, ErrorGuaranteed, MultiSpan, }; use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures}; use rustc_span::edition::Edition; @@ -287,4 +288,19 @@ impl ParseSess { pub fn proc_macro_quoted_spans(&self) -> Vec { self.proc_macro_quoted_spans.lock().clone() } + + pub fn emit_err<'a>(&'a self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed { + err.into_diagnostic(self).emit() + } + + pub fn emit_warning<'a>(&'a self, warning: impl SessionDiagnostic<'a, ()>) { + warning.into_diagnostic(self).emit() + } + + pub fn struct_err( + &self, + msg: impl Into, + ) -> DiagnosticBuilder<'_, ErrorGuaranteed> { + self.span_diagnostic.struct_err(msg) + } } diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index b4548129689b1..2bc0c5f1228e0 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -212,7 +212,7 @@ pub struct PerfStats { pub trait SessionDiagnostic<'a, T: EmissionGuarantee = ErrorGuaranteed> { /// Write out as a diagnostic out of `sess`. #[must_use] - fn into_diagnostic(self, sess: &'a Session) -> DiagnosticBuilder<'a, T>; + fn into_diagnostic(self, sess: &'a ParseSess) -> DiagnosticBuilder<'a, T>; } impl Session { @@ -334,7 +334,7 @@ impl Session { &self, msg: impl Into, ) -> DiagnosticBuilder<'_, ErrorGuaranteed> { - self.diagnostic().struct_err(msg) + self.parse_sess.struct_err(msg) } pub fn struct_err_with_code( &self, @@ -414,10 +414,10 @@ impl Session { self.diagnostic().err(msg) } pub fn emit_err<'a>(&'a self, err: impl SessionDiagnostic<'a>) -> ErrorGuaranteed { - err.into_diagnostic(self).emit() + self.parse_sess.emit_err(err) } pub fn emit_warning<'a>(&'a self, warning: impl SessionDiagnostic<'a, ()>) { - warning.into_diagnostic(self).emit() + self.parse_sess.emit_warning(warning) } #[inline] pub fn err_count(&self) -> usize { @@ -783,7 +783,11 @@ impl Session { Path::new(&rustlib_path), Path::new("bin"), ]); - if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] } + if self_contained { + vec![p.clone(), p.join("self-contained")] + } else { + vec![p] + } } pub fn init_incr_comp_session( From 519dd8e9de1a85e18b3033a007a3bf01c6e79560 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 25 Apr 2022 22:55:15 +0200 Subject: [PATCH 02/10] migrate ambiguous plus diagnostic --- .../locales/en-US/parser.ftl | 3 ++ compiler/rustc_parse/Cargo.toml | 1 + .../rustc_parse/src/parser/diagnostics.rs | 32 +++++++++++++------ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/parser.ftl b/compiler/rustc_error_messages/locales/en-US/parser.ftl index 24e59a93cea6c..fd7daadb1ab42 100644 --- a/compiler/rustc_error_messages/locales/en-US/parser.ftl +++ b/compiler/rustc_error_messages/locales/en-US/parser.ftl @@ -1,3 +1,6 @@ parser-struct-literal-body-without-path = struct literal body without path .suggestion = you might have forgotten to add the struct literal inside the block +parser-maybe-report-ambiguous-plus = + ambiguous `+` in a type + .suggestion = use parentheses to disambiguate diff --git a/compiler/rustc_parse/Cargo.toml b/compiler/rustc_parse/Cargo.toml index a823607ab0ec2..c6ca260e9831e 100644 --- a/compiler/rustc_parse/Cargo.toml +++ b/compiler/rustc_parse/Cargo.toml @@ -13,6 +13,7 @@ rustc_ast_pretty = { path = "../rustc_ast_pretty" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_feature = { path = "../rustc_feature" } rustc_lexer = { path = "../rustc_lexer" } +rustc_macros = { path = "../rustc_macros" } rustc_errors = { path = "../rustc_errors" } rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index ed2640451705b..d270f3606d760 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(); } } From d6da5fb3532bd44f3baa6eff0011bd20c6323e6b Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 25 Apr 2022 22:55:24 +0200 Subject: [PATCH 03/10] update lockfile --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 30175ae3561ab..567cef4c51eba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4181,6 +4181,7 @@ dependencies = [ "rustc_errors", "rustc_feature", "rustc_lexer", + "rustc_macros", "rustc_session", "rustc_span", "tracing", From 5874b09806e40e4791ab8d50d956fedcca9df91d Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 25 Apr 2022 23:17:32 +0200 Subject: [PATCH 04/10] fix formatting --- compiler/rustc_macros/src/session_diagnostic.rs | 4 +--- compiler/rustc_session/src/session.rs | 6 +----- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_macros/src/session_diagnostic.rs b/compiler/rustc_macros/src/session_diagnostic.rs index 5661451f52071..9466d0f34bc07 100644 --- a/compiler/rustc_macros/src/session_diagnostic.rs +++ b/compiler/rustc_macros/src/session_diagnostic.rs @@ -119,9 +119,7 @@ fn span_err(span: impl proc_macro::MultiSpan, msg: &str) -> proc_macro::Diagnost /// Emit a diagnostic on span `$span` with msg `$msg` (optionally performing additional decoration /// using the `FnOnce` passed in `diag`) and return `Err(ErrorHandled)`. macro_rules! throw_span_err { - ($span:expr, $msg:expr) => {{ - throw_span_err!($span, $msg, |diag| diag) - }}; + ($span:expr, $msg:expr) => {{ throw_span_err!($span, $msg, |diag| diag) }}; ($span:expr, $msg:expr, $f:expr) => {{ return Err(_throw_span_err($span, $msg, $f)); }}; diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 2bc0c5f1228e0..e8279f6fed24f 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -783,11 +783,7 @@ impl Session { Path::new(&rustlib_path), Path::new("bin"), ]); - if self_contained { - vec![p.clone(), p.join("self-contained")] - } else { - vec![p] - } + if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] } } pub fn init_incr_comp_session( From 530f4dce291371e0b1d567b47a1888aa8c806410 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 25 Apr 2022 23:26:52 +0200 Subject: [PATCH 05/10] remove old code --- compiler/rustc_parse/src/parser/diagnostics.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index d270f3606d760..23c4d67ebd797 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(); } } From 2e261a82f3ba99e6d11a35ead9da95007530187a Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Mon, 25 Apr 2022 23:49:53 +0200 Subject: [PATCH 06/10] add `struct_warn` method --- compiler/rustc_session/src/parse.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/rustc_session/src/parse.rs b/compiler/rustc_session/src/parse.rs index dd7682ebacef5..e933fe1cb2412 100644 --- a/compiler/rustc_session/src/parse.rs +++ b/compiler/rustc_session/src/parse.rs @@ -303,4 +303,8 @@ impl ParseSess { ) -> DiagnosticBuilder<'_, ErrorGuaranteed> { self.span_diagnostic.struct_err(msg) } + + pub fn struct_warn(&self, msg: impl Into) -> DiagnosticBuilder<'_, ()> { + self.span_diagnostic.struct_warn(msg) + } } From 35b42cb9ecc61bb36a23e53ff4913865d3ab1e80 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 26 Apr 2022 11:11:23 +0200 Subject: [PATCH 07/10] avoid `format!` --- compiler/rustc_parse/src/parser/diagnostics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 23c4d67ebd797..f0a053d88b5db 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, }); } From 6c3e793fb3303878790a6e872e8fa8bc1e4ef4e9 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 26 Apr 2022 11:12:48 +0200 Subject: [PATCH 08/10] move `AmbigousPlus` outside --- .../rustc_parse/src/parser/diagnostics.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index f0a053d88b5db..e8dc0b3a68436 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), From 1e35bab1047a9969e363d55159c466f965e7c726 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 26 Apr 2022 11:13:23 +0200 Subject: [PATCH 09/10] separate messages by a newline --- compiler/rustc_error_messages/locales/en-US/parser.ftl | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/rustc_error_messages/locales/en-US/parser.ftl b/compiler/rustc_error_messages/locales/en-US/parser.ftl index fd7daadb1ab42..3143b81b6098f 100644 --- a/compiler/rustc_error_messages/locales/en-US/parser.ftl +++ b/compiler/rustc_error_messages/locales/en-US/parser.ftl @@ -1,6 +1,7 @@ parser-struct-literal-body-without-path = struct literal body without path .suggestion = you might have forgotten to add the struct literal inside the block + parser-maybe-report-ambiguous-plus = ambiguous `+` in a type .suggestion = use parentheses to disambiguate From e7ae9eb3f272a96c2c73363539ca31986e5d5e88 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Wed, 27 Apr 2022 12:03:16 +0200 Subject: [PATCH 10/10] rename `sum_with_parens` --- compiler/rustc_parse/src/parser/diagnostics.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index e8dc0b3a68436..4edc9b39efcc7 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 }); } }