From 706452eba74026c51e8d0fa30aee2497c69eafc0 Mon Sep 17 00:00:00 2001 From: Luis Cardoso <61982523+LuisCardosoOliveira@users.noreply.github.com> Date: Fri, 19 Aug 2022 15:34:13 +0200 Subject: [PATCH 1/3] translations(rustc_session): migrate the file cgu_reuse_tracker This commit migrates the errors that indicates an incorrect CGU type and the fatal error that indicates that a CGU has not been correctly recorded --- .../locales/en-US/session.ftl | 5 +++ compiler/rustc_error_messages/src/lib.rs | 1 + .../rustc_session/src/cgu_reuse_tracker.rs | 34 ++++++++++++++++--- compiler/rustc_session/src/errors.rs | 22 ++++++++++++ compiler/rustc_session/src/lib.rs | 3 ++ 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 compiler/rustc_error_messages/locales/en-US/session.ftl create mode 100644 compiler/rustc_session/src/errors.rs diff --git a/compiler/rustc_error_messages/locales/en-US/session.ftl b/compiler/rustc_error_messages/locales/en-US/session.ftl new file mode 100644 index 00000000000..71d3abc5e6b --- /dev/null +++ b/compiler/rustc_error_messages/locales/en-US/session.ftl @@ -0,0 +1,5 @@ +incorrect_cgu_reuse_type = + CGU-reuse for `{$cgu_user_name}` is `{$actual_reuse}` but should be `{$at_least}``${expected_reuse}` + +cgu_not_recorded = + CGU-reuse for `{$cgu_user_name}` is (mangled: `{$cgu_name}`) was not recorded` \ No newline at end of file diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 2d001d445be..2fde301d19d 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -37,6 +37,7 @@ fluent_messages! { builtin_macros => "../locales/en-US/builtin_macros.ftl", const_eval => "../locales/en-US/const_eval.ftl", expand => "../locales/en-US/expand.ftl", + session => "../locales/en-US/session.ftl", interface => "../locales/en-US/interface.ftl", lint => "../locales/en-US/lint.ftl", parser => "../locales/en-US/parser.ftl", diff --git a/compiler/rustc_session/src/cgu_reuse_tracker.rs b/compiler/rustc_session/src/cgu_reuse_tracker.rs index dd64e8ab71e..88fcbf7c113 100644 --- a/compiler/rustc_session/src/cgu_reuse_tracker.rs +++ b/compiler/rustc_session/src/cgu_reuse_tracker.rs @@ -2,8 +2,13 @@ //! compilation. This is used for incremental compilation tests and debug //! output. +use crate::errors::IncorrectCguReuseType; +// use crate::errors::{CguNotRecorded, IncorrectCguReuseType}; use rustc_data_structures::fx::FxHashMap; +use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg}; use rustc_span::{Span, Symbol}; +use std::borrow::Cow; +use std::fmt::{self}; use std::sync::{Arc, Mutex}; use tracing::debug; @@ -14,6 +19,22 @@ pub enum CguReuse { PostLto, } +impl fmt::Display for CguReuse { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + CguReuse::No => write!(f, "No"), + CguReuse::PreLto => write!(f, "PreLto "), + CguReuse::PostLto => write!(f, "PostLto "), + } + } +} + +impl IntoDiagnosticArg for CguReuse { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Str(Cow::Owned(self.to_string())) + } +} + #[derive(Copy, Clone, Debug, PartialEq)] pub enum ComparisonKind { Exact, @@ -99,11 +120,13 @@ impl CguReuseTracker { if error { let at_least = if at_least { "at least " } else { "" }; - let msg = format!( - "CGU-reuse for `{cgu_user_name}` is `{actual_reuse:?}` but \ - should be {at_least}`{expected_reuse:?}`" - ); - diag.span_err(error_span.0, &msg); + IncorrectCguReuseType { + span: error_span.0, + cgu_user_name: &cgu_user_name, + actual_reuse, + expected_reuse, + at_least, + }; } } else { let msg = format!( @@ -111,6 +134,7 @@ impl CguReuseTracker { not recorded" ); diag.span_fatal(error_span.0, &msg) + // CguNotRecorded { cgu_user_name, cgu_name }; } } } diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs new file mode 100644 index 00000000000..ef78d1c9836 --- /dev/null +++ b/compiler/rustc_session/src/errors.rs @@ -0,0 +1,22 @@ +use crate as rustc_session; +use crate::cgu_reuse_tracker::CguReuse; +use rustc_macros::SessionDiagnostic; +use rustc_span::Span; + +#[derive(SessionDiagnostic)] +#[error(session::incorrect_cgu_reuse_type)] +pub struct IncorrectCguReuseType<'a> { + #[primary_span] + pub span: Span, + pub cgu_user_name: &'a str, + pub actual_reuse: CguReuse, + pub expected_reuse: CguReuse, + pub at_least: &'a str, +} + +// #[derive(SessionDiagnostic)] +// #[fatal(session::cgu_not_recorded)] +// pub struct CguNotRecorded<'a> { +// pub cgu_user_name: &'a str, +// pub cgu_name: &'a str, +// } diff --git a/compiler/rustc_session/src/lib.rs b/compiler/rustc_session/src/lib.rs index 0617bd5fae7..113bd85135e 100644 --- a/compiler/rustc_session/src/lib.rs +++ b/compiler/rustc_session/src/lib.rs @@ -8,9 +8,12 @@ #![feature(map_many_mut)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] +#![deny(rustc::untranslatable_diagnostic)] +#![deny(rustc::diagnostic_outside_of_impl)] #[macro_use] extern crate rustc_macros; +pub mod errors; pub mod cgu_reuse_tracker; pub mod utils; From d5262a945246a624113faaadbbdae4ec5013c862 Mon Sep 17 00:00:00 2001 From: Luis Cardoso <61982523+LuisCardosoOliveira@users.noreply.github.com> Date: Mon, 22 Aug 2022 08:28:50 +0200 Subject: [PATCH 2/3] translations(rustc_session): migrate 80% of the file parse.rs This commit migrates around 80% of the parse file to use SsessionDiagnostic We still have to migrate struct_err and struct_warn. --- .../locales/en-US/session.ftl | 14 ++++++++-- .../rustc_session/src/cgu_reuse_tracker.rs | 4 ++- compiler/rustc_session/src/errors.rs | 28 +++++++++++++++++-- compiler/rustc_session/src/lib.rs | 2 -- compiler/rustc_session/src/parse.rs | 21 ++++++++++---- 5 files changed, 55 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/session.ftl b/compiler/rustc_error_messages/locales/en-US/session.ftl index 71d3abc5e6b..e94a7b2c1c8 100644 --- a/compiler/rustc_error_messages/locales/en-US/session.ftl +++ b/compiler/rustc_error_messages/locales/en-US/session.ftl @@ -1,5 +1,13 @@ -incorrect_cgu_reuse_type = +session_incorrect_cgu_reuse_type = CGU-reuse for `{$cgu_user_name}` is `{$actual_reuse}` but should be `{$at_least}``${expected_reuse}` -cgu_not_recorded = - CGU-reuse for `{$cgu_user_name}` is (mangled: `{$cgu_name}`) was not recorded` \ No newline at end of file +session_cgu_not_recorded = + CGU-reuse for `{$cgu_user_name}` is (mangled: `{$cgu_name}`) was not recorded` + +session_feature_gate_error = {$explain} + +session_feature_diagnostic_for_issue = + see issue #{$n} for more information + +session_feature_diagnostic_help = + add `#![feature({$feature})]` to the crate attributes to enable diff --git a/compiler/rustc_session/src/cgu_reuse_tracker.rs b/compiler/rustc_session/src/cgu_reuse_tracker.rs index 88fcbf7c113..0b75a89c4d8 100644 --- a/compiler/rustc_session/src/cgu_reuse_tracker.rs +++ b/compiler/rustc_session/src/cgu_reuse_tracker.rs @@ -3,7 +3,6 @@ //! output. use crate::errors::IncorrectCguReuseType; -// use crate::errors::{CguNotRecorded, IncorrectCguReuseType}; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg}; use rustc_span::{Span, Symbol}; @@ -129,11 +128,14 @@ impl CguReuseTracker { }; } } else { + //FIXME: Remove this once PR #100694 that implements `[fatal(..)]` is merged let msg = format!( "CGU-reuse for `{cgu_user_name}` (mangled: `{cgu_name}`) was \ not recorded" ); diag.span_fatal(error_span.0, &msg) + + //FIXME: Uncomment this once PR #100694 that implements `[fatal(..)]` is merged // CguNotRecorded { cgu_user_name, cgu_name }; } } diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index ef78d1c9836..54e5fe82f5c 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -1,10 +1,13 @@ +use std::num::NonZeroU32; + use crate as rustc_session; use crate::cgu_reuse_tracker::CguReuse; +use rustc_errors::MultiSpan; use rustc_macros::SessionDiagnostic; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; #[derive(SessionDiagnostic)] -#[error(session::incorrect_cgu_reuse_type)] +#[diag(session::incorrect_cgu_reuse_type)] pub struct IncorrectCguReuseType<'a> { #[primary_span] pub span: Span, @@ -14,9 +17,30 @@ pub struct IncorrectCguReuseType<'a> { pub at_least: &'a str, } +//FIXME: Uncomment this once PR #100694 that implements `[fatal(..)]` is merged // #[derive(SessionDiagnostic)] // #[fatal(session::cgu_not_recorded)] // pub struct CguNotRecorded<'a> { // pub cgu_user_name: &'a str, // pub cgu_name: &'a str, // } + +#[derive(SessionDiagnostic)] +#[diag(session::feature_gate_error, code = "E0658")] +pub struct FeatureGateError<'a> { + #[primary_span] + pub span: MultiSpan, + pub explain: &'a str, +} + +#[derive(SessionSubdiagnostic)] +#[note(session::feature_diagnostic_for_issue)] +pub struct FeatureDiagnosticForIssue { + pub n: NonZeroU32, +} + +#[derive(SessionSubdiagnostic)] +#[help(session::feature_diagnostic_help)] +pub struct FeatureDiagnosticHelp { + pub feature: Symbol, +} diff --git a/compiler/rustc_session/src/lib.rs b/compiler/rustc_session/src/lib.rs index 113bd85135e..3f098078173 100644 --- a/compiler/rustc_session/src/lib.rs +++ b/compiler/rustc_session/src/lib.rs @@ -8,8 +8,6 @@ #![feature(map_many_mut)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] -#![deny(rustc::untranslatable_diagnostic)] -#![deny(rustc::diagnostic_outside_of_impl)] #[macro_use] extern crate rustc_macros; diff --git a/compiler/rustc_session/src/parse.rs b/compiler/rustc_session/src/parse.rs index 17866dc6bdd..ebec754dcff 100644 --- a/compiler/rustc_session/src/parse.rs +++ b/compiler/rustc_session/src/parse.rs @@ -2,6 +2,7 @@ //! It also serves as an input to the parser itself. use crate::config::CheckCfg; +use crate::errors::{FeatureDiagnosticForIssue, FeatureDiagnosticHelp, FeatureGateError}; use crate::lint::{ builtin::UNSTABLE_SYNTAX_PRE_EXPANSION, BufferedEarlyLint, BuiltinLintDiagnostics, Lint, LintId, }; @@ -11,7 +12,7 @@ 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, DiagnosticId, + fallback_fluent_bundle, Applicability, Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage, EmissionGuarantee, ErrorGuaranteed, MultiSpan, StashKey, }; use rustc_feature::{find_feature_issue, GateIssue, UnstableFeatures}; @@ -112,7 +113,7 @@ pub fn feature_err_issue<'a>( .map(|err| err.cancel()); } - let mut err = sess.span_diagnostic.struct_span_err_with_code(span, explain, error_code!(E0658)); + let mut err = sess.create_err(FeatureGateError { span, explain }); add_feature_diagnostics_for_issue(&mut err, sess, feature, issue); err } @@ -130,6 +131,8 @@ pub fn feature_warn<'a>(sess: &'a ParseSess, feature: Symbol, span: Span, explai /// /// This variant allows you to control whether it is a library or language feature. /// Almost always, you want to use this for a language feature. If so, prefer `feature_warn`. +#[allow(rustc::diagnostic_outside_of_impl)] +#[allow(rustc::untranslatable_diagnostic)] pub fn feature_warn_issue<'a>( sess: &'a ParseSess, feature: Symbol, @@ -172,14 +175,12 @@ pub fn add_feature_diagnostics_for_issue<'a>( issue: GateIssue, ) { if let Some(n) = find_feature_issue(feature, issue) { - err.note(&format!( - "see issue #{n} for more information" - )); + err.subdiagnostic(FeatureDiagnosticForIssue { n }); } // #23973: do not suggest `#![feature(...)]` if we are in beta/stable if sess.unstable_features.is_nightly_build() { - err.help(&format!("add `#![feature({feature})]` to the crate attributes to enable")); + err.subdiagnostic(FeatureDiagnosticHelp { feature }); } } @@ -372,6 +373,8 @@ impl ParseSess { } #[rustc_lint_diagnostics] + #[allow(rustc::diagnostic_outside_of_impl)] + #[allow(rustc::untranslatable_diagnostic)] pub fn struct_err( &self, msg: impl Into, @@ -380,16 +383,22 @@ impl ParseSess { } #[rustc_lint_diagnostics] + #[allow(rustc::diagnostic_outside_of_impl)] + #[allow(rustc::untranslatable_diagnostic)] pub fn struct_warn(&self, msg: impl Into) -> DiagnosticBuilder<'_, ()> { self.span_diagnostic.struct_warn(msg) } #[rustc_lint_diagnostics] + #[allow(rustc::diagnostic_outside_of_impl)] + #[allow(rustc::untranslatable_diagnostic)] pub fn struct_fatal(&self, msg: impl Into) -> DiagnosticBuilder<'_, !> { self.span_diagnostic.struct_fatal(msg) } #[rustc_lint_diagnostics] + #[allow(rustc::diagnostic_outside_of_impl)] + #[allow(rustc::untranslatable_diagnostic)] pub fn struct_diagnostic( &self, msg: impl Into, From 2c77f3e9c5804edc0bb520c7b4774424cea6beb0 Mon Sep 17 00:00:00 2001 From: Luis Cardoso <61982523+LuisCardosoOliveira@users.noreply.github.com> Date: Wed, 24 Aug 2022 17:15:08 +0200 Subject: [PATCH 3/3] translations(rustc_session): migrate check_expected_reuse This commit migrates the errors in the function check_expected_reuse to use the new SessionDiagnostic. It also does some small refactor for the IncorrectCguReuseType to include the 'at least' word in the fluent translation file --- compiler/rustc_codegen_ssa/src/back/write.rs | 2 +- .../locales/en-US/session.ftl | 5 ++++- compiler/rustc_session/src/cgu_reuse_tracker.rs | 17 +++++------------ compiler/rustc_session/src/errors.rs | 15 +++++++-------- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/write.rs b/compiler/rustc_codegen_ssa/src/back/write.rs index 2930d09d71f..68f3b19b715 100644 --- a/compiler/rustc_codegen_ssa/src/back/write.rs +++ b/compiler/rustc_codegen_ssa/src/back/write.rs @@ -1892,7 +1892,7 @@ impl OngoingCodegen { } }); - sess.cgu_reuse_tracker.check_expected_reuse(sess.diagnostic()); + sess.cgu_reuse_tracker.check_expected_reuse(sess); sess.abort_if_errors(); diff --git a/compiler/rustc_error_messages/locales/en-US/session.ftl b/compiler/rustc_error_messages/locales/en-US/session.ftl index e94a7b2c1c8..983e5cee823 100644 --- a/compiler/rustc_error_messages/locales/en-US/session.ftl +++ b/compiler/rustc_error_messages/locales/en-US/session.ftl @@ -1,5 +1,8 @@ session_incorrect_cgu_reuse_type = - CGU-reuse for `{$cgu_user_name}` is `{$actual_reuse}` but should be `{$at_least}``${expected_reuse}` + CGU-reuse for `{$cgu_user_name}` is `{$actual_reuse}` but should be {$at_least -> + [one] {"at least "} + *[other] {""} + }`{$expected_reuse}` session_cgu_not_recorded = CGU-reuse for `{$cgu_user_name}` is (mangled: `{$cgu_name}`) was not recorded` diff --git a/compiler/rustc_session/src/cgu_reuse_tracker.rs b/compiler/rustc_session/src/cgu_reuse_tracker.rs index 0b75a89c4d8..2a4a772f610 100644 --- a/compiler/rustc_session/src/cgu_reuse_tracker.rs +++ b/compiler/rustc_session/src/cgu_reuse_tracker.rs @@ -2,7 +2,8 @@ //! compilation. This is used for incremental compilation tests and debug //! output. -use crate::errors::IncorrectCguReuseType; +use crate::errors::{CguNotRecorded, IncorrectCguReuseType}; +use crate::Session; use rustc_data_structures::fx::FxHashMap; use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg}; use rustc_span::{Span, Symbol}; @@ -104,7 +105,7 @@ impl CguReuseTracker { } } - pub fn check_expected_reuse(&self, diag: &rustc_errors::Handler) { + pub fn check_expected_reuse(&self, sess: &Session) { if let Some(ref data) = self.data { let data = data.lock().unwrap(); @@ -118,7 +119,7 @@ impl CguReuseTracker { }; if error { - let at_least = if at_least { "at least " } else { "" }; + let at_least = if at_least { 1 } else { 0 }; IncorrectCguReuseType { span: error_span.0, cgu_user_name: &cgu_user_name, @@ -128,15 +129,7 @@ impl CguReuseTracker { }; } } else { - //FIXME: Remove this once PR #100694 that implements `[fatal(..)]` is merged - let msg = format!( - "CGU-reuse for `{cgu_user_name}` (mangled: `{cgu_name}`) was \ - not recorded" - ); - diag.span_fatal(error_span.0, &msg) - - //FIXME: Uncomment this once PR #100694 that implements `[fatal(..)]` is merged - // CguNotRecorded { cgu_user_name, cgu_name }; + sess.emit_fatal(CguNotRecorded { cgu_user_name, cgu_name }); } } } diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index 54e5fe82f5c..7252f1799da 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -14,16 +14,15 @@ pub struct IncorrectCguReuseType<'a> { pub cgu_user_name: &'a str, pub actual_reuse: CguReuse, pub expected_reuse: CguReuse, - pub at_least: &'a str, + pub at_least: u8, } -//FIXME: Uncomment this once PR #100694 that implements `[fatal(..)]` is merged -// #[derive(SessionDiagnostic)] -// #[fatal(session::cgu_not_recorded)] -// pub struct CguNotRecorded<'a> { -// pub cgu_user_name: &'a str, -// pub cgu_name: &'a str, -// } +#[derive(SessionDiagnostic)] +#[diag(session::cgu_not_recorded)] +pub struct CguNotRecorded<'a> { + pub cgu_user_name: &'a str, + pub cgu_name: &'a str, +} #[derive(SessionDiagnostic)] #[diag(session::feature_gate_error, code = "E0658")]