From 31c269ae756506c00018ac750d945a4e1a92eaa2 Mon Sep 17 00:00:00 2001 From: Nathan Stocks Date: Fri, 7 Oct 2022 14:39:09 -0600 Subject: [PATCH] avoid string dispatch in fluent --- .../locales/en-US/passes.ftl | 36 ++++++++------- compiler/rustc_passes/src/check_attr.rs | 46 +++++++++++++------ compiler/rustc_passes/src/errors.rs | 43 ++++++++++++++--- 3 files changed, 86 insertions(+), 39 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/passes.ftl b/compiler/rustc_error_messages/locales/en-US/passes.ftl index 68f4097bbb5..1f1c9c29d66 100644 --- a/compiler/rustc_error_messages/locales/en-US/passes.ftl +++ b/compiler/rustc_error_messages/locales/en-US/passes.ftl @@ -596,23 +596,25 @@ passes_unrecognized_repr_hint = unrecognized representation hint .help = valid reprs are `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize` -passes_attribute_should_be_applied_to = - attribute should be applied to {$what -> - [enum] an enum - [struct] a struct - [struct-union] a struct or union - [struct-enum-union] a struct, enum, or union - [struct-enum-function-union] a struct, enum, function, or union - *[unspecified] (unspecified--this is a compiler bug) - } - .label = not {$what -> - [enum] an enum - [struct] a struct - [struct-union] a struct or union - [struct-enum-union] a struct, enum, or union - [struct-enum-function-union] a struct, enum, function, or union - *[unspecified] (unspecified--this is a compiler bug) - } +passes_attr_application_enum = + attribute should be applied to an enum + .label = not an enum + +passes_attr_application_struct = + attribute should be applied to a struct + .label = not a struct + +passes_attr_application_struct_union = + attribute should be applied to a struct or union + .label = not a struct or union + +passes_attr_application_struct_enum_union = + attribute should be applied to a struct, enum, or union + .label = not a struct, enum, or union + +passes_attr_application_struct_enum_function_union = + attribute should be applied to a struct, enum, function, or union + .label = not a struct, enum, function, or union passes_transparent_incompatible = transparent {$target} cannot have other repr hints diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 33eb57fe769..54c1718a656 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -5,8 +5,8 @@ //! item. use crate::errors::{ - self, AttributeShouldBeAppliedTo, DebugVisualizerUnreadable, InvalidAttrAtCrateLevel, - ObjectLifetimeErr, OnlyHasEffectOn, TransparentIncompatible, UnrecognizedReprHint, + self, AttrApplication, DebugVisualizerUnreadable, InvalidAttrAtCrateLevel, ObjectLifetimeErr, + OnlyHasEffectOn, TransparentIncompatible, UnrecognizedReprHint, }; use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem}; use rustc_data_structures::fx::FxHashMap; @@ -1594,12 +1594,17 @@ impl CheckAttrVisitor<'_> { continue; } - let what = match hint.name_or_empty() { + match hint.name_or_empty() { sym::C => { is_c = true; match target { Target::Struct | Target::Union | Target::Enum => continue, - _ => "struct-enum-union", + _ => { + self.tcx.sess.emit_err(AttrApplication::StructEnumUnion { + hint_span: hint.span(), + span, + }); + } } } sym::align => { @@ -1615,12 +1620,20 @@ impl CheckAttrVisitor<'_> { match target { Target::Struct | Target::Union | Target::Enum | Target::Fn => continue, - _ => "struct-enum-function-union", + _ => { + self.tcx.sess.emit_err(AttrApplication::StructEnumFunctionUnion { + hint_span: hint.span(), + span, + }); + } } } sym::packed => { if target != Target::Struct && target != Target::Union { - "struct-union" + self.tcx.sess.emit_err(AttrApplication::StructUnion { + hint_span: hint.span(), + span, + }); } else { continue; } @@ -1628,7 +1641,9 @@ impl CheckAttrVisitor<'_> { sym::simd => { is_simd = true; if target != Target::Struct { - "struct" + self.tcx + .sess + .emit_err(AttrApplication::Struct { hint_span: hint.span(), span }); } else { continue; } @@ -1637,7 +1652,12 @@ impl CheckAttrVisitor<'_> { is_transparent = true; match target { Target::Struct | Target::Union | Target::Enum => continue, - _ => "struct-enum-union", + _ => { + self.tcx.sess.emit_err(AttrApplication::StructEnumUnion { + hint_span: hint.span(), + span, + }); + } } } sym::i8 @@ -1654,7 +1674,9 @@ impl CheckAttrVisitor<'_> { | sym::usize => { int_reprs += 1; if target != Target::Enum { - "enum" + self.tcx + .sess + .emit_err(AttrApplication::Enum { hint_span: hint.span(), span }); } else { continue; } @@ -1664,12 +1686,6 @@ impl CheckAttrVisitor<'_> { continue; } }; - - self.tcx.sess.emit_err(AttributeShouldBeAppliedTo { - hint_span: hint.span(), - span, - what, - }); } // Just point at all repr hints if there are any incompatibilities. diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index db16a690d62..1cc81a9ab98 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -1288,13 +1288,42 @@ pub struct UnrecognizedReprHint { } #[derive(Diagnostic)] -#[diag(passes::attribute_should_be_applied_to, code = "E0517")] -pub struct AttributeShouldBeAppliedTo<'a> { - #[primary_span] - pub hint_span: Span, - #[label] - pub span: Span, - pub what: &'a str, +pub enum AttrApplication { + #[diag(passes::attr_application_enum, code = "E0517")] + Enum { + #[primary_span] + hint_span: Span, + #[label] + span: Span, + }, + #[diag(passes::attr_application_struct, code = "E0517")] + Struct { + #[primary_span] + hint_span: Span, + #[label] + span: Span, + }, + #[diag(passes::attr_application_struct_union, code = "E0517")] + StructUnion { + #[primary_span] + hint_span: Span, + #[label] + span: Span, + }, + #[diag(passes::attr_application_struct_enum_union, code = "E0517")] + StructEnumUnion { + #[primary_span] + hint_span: Span, + #[label] + span: Span, + }, + #[diag(passes::attr_application_struct_enum_function_union, code = "E0517")] + StructEnumFunctionUnion { + #[primary_span] + hint_span: Span, + #[label] + span: Span, + }, } #[derive(Diagnostic)]