From 86f8c4e8e3136ee1831ffaa9d6fbdac7267d35ea Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Sat, 27 Aug 2022 00:24:13 -0400 Subject: [PATCH 1/6] ADD - InvalidSymbolName to migrate symbol-name({}) error to new diagnostics infraestructure ADD - dependencies needed to port a module to new Diagnostics infra (rustc_macros, rustc_errors, errors file, and fluent file) --- Cargo.lock | 2 ++ .../locales/en-US/symbol_mangling.ftl | 1 + compiler/rustc_error_messages/src/lib.rs | 1 + compiler/rustc_symbol_mangling/Cargo.toml | 2 ++ compiler/rustc_symbol_mangling/src/errors.rs | 12 ++++++++++++ compiler/rustc_symbol_mangling/src/lib.rs | 1 + compiler/rustc_symbol_mangling/src/test.rs | 6 +++++- 7 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl create mode 100644 compiler/rustc_symbol_mangling/src/errors.rs diff --git a/Cargo.lock b/Cargo.lock index 21d522cb9ff42..6cddbe9d2cd5f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4155,7 +4155,9 @@ dependencies = [ "punycode", "rustc-demangle", "rustc_data_structures", + "rustc_errors", "rustc_hir", + "rustc_macros", "rustc_middle", "rustc_session", "rustc_span", diff --git a/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl b/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl new file mode 100644 index 0000000000000..6c8166beab859 --- /dev/null +++ b/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl @@ -0,0 +1 @@ +symbol_mangling_invalid_symbol_name = symbol-name({$mangled_formatted}) diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index d1ac326a72c6d..eabb9192029d5 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -52,6 +52,7 @@ fluent_messages! { ty_utils => "../locales/en-US/ty_utils.ftl", typeck => "../locales/en-US/typeck.ftl", mir_dataflow => "../locales/en-US/mir_dataflow.ftl", + symbol_mangling => "../locales/en-US/symbol_mangling.ftl", } pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES}; diff --git a/compiler/rustc_symbol_mangling/Cargo.toml b/compiler/rustc_symbol_mangling/Cargo.toml index b104a40c23115..3db05225722e7 100644 --- a/compiler/rustc_symbol_mangling/Cargo.toml +++ b/compiler/rustc_symbol_mangling/Cargo.toml @@ -18,3 +18,5 @@ rustc_hir = { path = "../rustc_hir" } rustc_target = { path = "../rustc_target" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_session = { path = "../rustc_session" } +rustc_macros = { path = "../rustc_macros" } +rustc_errors = { path = "../rustc_errors" } diff --git a/compiler/rustc_symbol_mangling/src/errors.rs b/compiler/rustc_symbol_mangling/src/errors.rs new file mode 100644 index 0000000000000..38aa4345f0c9c --- /dev/null +++ b/compiler/rustc_symbol_mangling/src/errors.rs @@ -0,0 +1,12 @@ +//! Errors emitted by symbol_mangling. + +use rustc_macros::SessionDiagnostic; +use rustc_span::Span; + +#[derive(SessionDiagnostic)] +#[error(symbol_mangling::invalid_symbol_name)] +pub struct InvalidSymbolName<'a> { + #[primary_span] + pub span: Span, + pub mangled_formatted: &'a str, +} diff --git a/compiler/rustc_symbol_mangling/src/lib.rs b/compiler/rustc_symbol_mangling/src/lib.rs index 5fc992023caa0..252726691a760 100644 --- a/compiler/rustc_symbol_mangling/src/lib.rs +++ b/compiler/rustc_symbol_mangling/src/lib.rs @@ -110,6 +110,7 @@ use tracing::debug; mod legacy; mod v0; +pub mod errors; pub mod test; pub mod typeid; diff --git a/compiler/rustc_symbol_mangling/src/test.rs b/compiler/rustc_symbol_mangling/src/test.rs index 7249ce04c155e..9c1d5d4292ca2 100644 --- a/compiler/rustc_symbol_mangling/src/test.rs +++ b/compiler/rustc_symbol_mangling/src/test.rs @@ -4,6 +4,7 @@ //! def-path. This is used for unit testing the code that generates //! paths etc in all kinds of annoying scenarios. +use crate::errors::InvalidSymbolName; use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{subst::InternalSubsts, Instance, TyCtxt}; @@ -59,7 +60,10 @@ impl SymbolNamesTest<'_> { tcx.erase_regions(InternalSubsts::identity_for_item(tcx, def_id)), ); let mangled = tcx.symbol_name(instance); - tcx.sess.span_err(attr.span, &format!("symbol-name({})", mangled)); + tcx.sess.emit_err(InvalidSymbolName { + span: attr.span, + mangled_formatted: &format!("{mangled}"), + }); if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) { tcx.sess.span_err(attr.span, &format!("demangling({})", demangling)); tcx.sess.span_err(attr.span, &format!("demangling-alt({:#})", demangling)); From 359002bbebb5a3879af4d957001b6526ae4c550e Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Sun, 21 Aug 2022 00:38:23 -0400 Subject: [PATCH 2/6] ADD - migrate InvalidTraitItem and AltInvalidTraitItem errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thought of doing this by having a struct and an enum with Default and Alt cases, but not sure if we wanted to have the text in code instead of having “demangling()” and “demangling-alt()” in the ftl file. Don’t like the current way of having structs representing the same-ish and using long names to distinguish their expectations, instead of putting this in an enum and handling the different cases inside the type. I am fine with whichever option the team prefers; also understand having them as separate structs keeps it simple. --- .../locales/en-US/symbol_mangling.ftl | 4 ++++ compiler/rustc_symbol_mangling/src/errors.rs | 16 ++++++++++++++++ compiler/rustc_symbol_mangling/src/test.rs | 12 +++++++++--- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl b/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl index 6c8166beab859..644c8f84c28ef 100644 --- a/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl +++ b/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl @@ -1 +1,5 @@ symbol_mangling_invalid_symbol_name = symbol-name({$mangled_formatted}) + +symbol_mangling_invalid_trait_item = demangling({$demangling_formatted}) + +symbol_mangling_alt_invalid_trait_item = demangling-alt({$alt_demangling_formatted}) diff --git a/compiler/rustc_symbol_mangling/src/errors.rs b/compiler/rustc_symbol_mangling/src/errors.rs index 38aa4345f0c9c..db8b3159a6ff5 100644 --- a/compiler/rustc_symbol_mangling/src/errors.rs +++ b/compiler/rustc_symbol_mangling/src/errors.rs @@ -10,3 +10,19 @@ pub struct InvalidSymbolName<'a> { pub span: Span, pub mangled_formatted: &'a str, } + +#[derive(SessionDiagnostic)] +#[error(symbol_mangling::invalid_trait_item)] +pub struct InvalidTraitItem<'a> { + #[primary_span] + pub span: Span, + pub demangling_formatted: &'a str, +} + +#[derive(SessionDiagnostic)] +#[error(symbol_mangling::alt_invalid_trait_item)] +pub struct AltInvalidTraitItem<'a> { + #[primary_span] + pub span: Span, + pub alt_demangling_formatted: &'a str, +} diff --git a/compiler/rustc_symbol_mangling/src/test.rs b/compiler/rustc_symbol_mangling/src/test.rs index 9c1d5d4292ca2..06efefb726ca7 100644 --- a/compiler/rustc_symbol_mangling/src/test.rs +++ b/compiler/rustc_symbol_mangling/src/test.rs @@ -4,7 +4,7 @@ //! def-path. This is used for unit testing the code that generates //! paths etc in all kinds of annoying scenarios. -use crate::errors::InvalidSymbolName; +use crate::errors::{AltInvalidTraitItem, InvalidSymbolName, InvalidTraitItem}; use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{subst::InternalSubsts, Instance, TyCtxt}; @@ -65,8 +65,14 @@ impl SymbolNamesTest<'_> { mangled_formatted: &format!("{mangled}"), }); if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) { - tcx.sess.span_err(attr.span, &format!("demangling({})", demangling)); - tcx.sess.span_err(attr.span, &format!("demangling-alt({:#})", demangling)); + tcx.sess.emit_err(InvalidTraitItem { + span: attr.span, + demangling_formatted: &format!("{demangling}"), + }); + tcx.sess.emit_err(AltInvalidTraitItem { + span: attr.span, + alt_demangling_formatted: &format!("{:#}", demangling), + }); } } From bd83bbc93adce97e34c1c4d5d8d74f7b19068326 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Sun, 21 Aug 2022 00:42:59 -0400 Subject: [PATCH 3/6] UPDATE - accept String instead of unused 'str --- compiler/rustc_symbol_mangling/src/errors.rs | 12 ++++++------ compiler/rustc_symbol_mangling/src/test.rs | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_symbol_mangling/src/errors.rs b/compiler/rustc_symbol_mangling/src/errors.rs index db8b3159a6ff5..c0e49e07bfc59 100644 --- a/compiler/rustc_symbol_mangling/src/errors.rs +++ b/compiler/rustc_symbol_mangling/src/errors.rs @@ -5,24 +5,24 @@ use rustc_span::Span; #[derive(SessionDiagnostic)] #[error(symbol_mangling::invalid_symbol_name)] -pub struct InvalidSymbolName<'a> { +pub struct InvalidSymbolName { #[primary_span] pub span: Span, - pub mangled_formatted: &'a str, + pub mangled_formatted: String, } #[derive(SessionDiagnostic)] #[error(symbol_mangling::invalid_trait_item)] -pub struct InvalidTraitItem<'a> { +pub struct InvalidTraitItem { #[primary_span] pub span: Span, - pub demangling_formatted: &'a str, + pub demangling_formatted: String, } #[derive(SessionDiagnostic)] #[error(symbol_mangling::alt_invalid_trait_item)] -pub struct AltInvalidTraitItem<'a> { +pub struct AltInvalidTraitItem { #[primary_span] pub span: Span, - pub alt_demangling_formatted: &'a str, + pub alt_demangling_formatted: String, } diff --git a/compiler/rustc_symbol_mangling/src/test.rs b/compiler/rustc_symbol_mangling/src/test.rs index 06efefb726ca7..2ed1dea357dd5 100644 --- a/compiler/rustc_symbol_mangling/src/test.rs +++ b/compiler/rustc_symbol_mangling/src/test.rs @@ -62,16 +62,16 @@ impl SymbolNamesTest<'_> { let mangled = tcx.symbol_name(instance); tcx.sess.emit_err(InvalidSymbolName { span: attr.span, - mangled_formatted: &format!("{mangled}"), + mangled_formatted: format!("{mangled}"), }); if let Ok(demangling) = rustc_demangle::try_demangle(mangled.name) { tcx.sess.emit_err(InvalidTraitItem { span: attr.span, - demangling_formatted: &format!("{demangling}"), + demangling_formatted: format!("{demangling}"), }); tcx.sess.emit_err(AltInvalidTraitItem { span: attr.span, - alt_demangling_formatted: &format!("{:#}", demangling), + alt_demangling_formatted: format!("{:#}", demangling), }); } } From 8f5fada86df26aaccbf4f5e5851ac040b5ea5852 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Sun, 21 Aug 2022 00:51:33 -0400 Subject: [PATCH 4/6] ADD - migrate InvalidDefPath to new diagnostics infra --- .../locales/en-US/symbol_mangling.ftl | 2 ++ compiler/rustc_symbol_mangling/src/errors.rs | 8 ++++++++ compiler/rustc_symbol_mangling/src/test.rs | 8 +++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl b/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl index 644c8f84c28ef..55d6fbbf86f33 100644 --- a/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl +++ b/compiler/rustc_error_messages/locales/en-US/symbol_mangling.ftl @@ -3,3 +3,5 @@ symbol_mangling_invalid_symbol_name = symbol-name({$mangled_formatted}) symbol_mangling_invalid_trait_item = demangling({$demangling_formatted}) symbol_mangling_alt_invalid_trait_item = demangling-alt({$alt_demangling_formatted}) + +symbol_mangling_invalid_def_path = def-path({$def_path}) diff --git a/compiler/rustc_symbol_mangling/src/errors.rs b/compiler/rustc_symbol_mangling/src/errors.rs index c0e49e07bfc59..4872dfa2653a6 100644 --- a/compiler/rustc_symbol_mangling/src/errors.rs +++ b/compiler/rustc_symbol_mangling/src/errors.rs @@ -26,3 +26,11 @@ pub struct AltInvalidTraitItem { pub span: Span, pub alt_demangling_formatted: String, } + +#[derive(SessionDiagnostic)] +#[error(symbol_mangling::invalid_def_path)] +pub struct InvalidDefPath { + #[primary_span] + pub span: Span, + pub def_path: String, +} diff --git a/compiler/rustc_symbol_mangling/src/test.rs b/compiler/rustc_symbol_mangling/src/test.rs index 2ed1dea357dd5..b1c4cab11eb8e 100644 --- a/compiler/rustc_symbol_mangling/src/test.rs +++ b/compiler/rustc_symbol_mangling/src/test.rs @@ -4,7 +4,7 @@ //! def-path. This is used for unit testing the code that generates //! paths etc in all kinds of annoying scenarios. -use crate::errors::{AltInvalidTraitItem, InvalidSymbolName, InvalidTraitItem}; +use crate::errors::{AltInvalidTraitItem, InvalidDefPath, InvalidSymbolName, InvalidTraitItem}; use rustc_hir::def_id::LocalDefId; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::{subst::InternalSubsts, Instance, TyCtxt}; @@ -77,8 +77,10 @@ impl SymbolNamesTest<'_> { } for attr in tcx.get_attrs(def_id.to_def_id(), DEF_PATH) { - let path = with_no_trimmed_paths!(tcx.def_path_str(def_id.to_def_id())); - tcx.sess.span_err(attr.span, &format!("def-path({})", path)); + tcx.sess.emit_err(InvalidDefPath { + span: attr.span, + def_path: with_no_trimmed_paths!(tcx.def_path_str(def_id.to_def_id())), + }); } } } From ef2f6ab55eb1862fbf5a700d034ea4fd2fb23e80 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Sun, 21 Aug 2022 00:57:52 -0400 Subject: [PATCH 5/6] ADD - diagnostics lints to symbol_mangling module --- compiler/rustc_symbol_mangling/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_symbol_mangling/src/lib.rs b/compiler/rustc_symbol_mangling/src/lib.rs index 252726691a760..0c6489acb3483 100644 --- a/compiler/rustc_symbol_mangling/src/lib.rs +++ b/compiler/rustc_symbol_mangling/src/lib.rs @@ -91,6 +91,8 @@ #![feature(never_type)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] +#![deny(rustc::untranslatable_diagnostic)] +#![deny(rustc::diagnostic_outside_of_impl)] #[macro_use] extern crate rustc_middle; From 3ee69463164ac33af6eb483ccd2f64b06730c220 Mon Sep 17 00:00:00 2001 From: Jhonny Bill Mena Date: Sun, 21 Aug 2022 11:09:00 -0400 Subject: [PATCH 6/6] UPDATE - to support diag introduced in PR #100765 --- compiler/rustc_symbol_mangling/src/errors.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_symbol_mangling/src/errors.rs b/compiler/rustc_symbol_mangling/src/errors.rs index 4872dfa2653a6..242997365a892 100644 --- a/compiler/rustc_symbol_mangling/src/errors.rs +++ b/compiler/rustc_symbol_mangling/src/errors.rs @@ -4,7 +4,7 @@ use rustc_macros::SessionDiagnostic; use rustc_span::Span; #[derive(SessionDiagnostic)] -#[error(symbol_mangling::invalid_symbol_name)] +#[diag(symbol_mangling::invalid_symbol_name)] pub struct InvalidSymbolName { #[primary_span] pub span: Span, @@ -12,7 +12,7 @@ pub struct InvalidSymbolName { } #[derive(SessionDiagnostic)] -#[error(symbol_mangling::invalid_trait_item)] +#[diag(symbol_mangling::invalid_trait_item)] pub struct InvalidTraitItem { #[primary_span] pub span: Span, @@ -20,7 +20,7 @@ pub struct InvalidTraitItem { } #[derive(SessionDiagnostic)] -#[error(symbol_mangling::alt_invalid_trait_item)] +#[diag(symbol_mangling::alt_invalid_trait_item)] pub struct AltInvalidTraitItem { #[primary_span] pub span: Span, @@ -28,7 +28,7 @@ pub struct AltInvalidTraitItem { } #[derive(SessionDiagnostic)] -#[error(symbol_mangling::invalid_def_path)] +#[diag(symbol_mangling::invalid_def_path)] pub struct InvalidDefPath { #[primary_span] pub span: Span,