avoid string dispatch in fluent

This commit is contained in:
Nathan Stocks 2022-10-07 14:39:09 -06:00
parent 57eba4f535
commit 31c269ae75
3 changed files with 86 additions and 39 deletions

View File

@ -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

View File

@ -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 @@ fn check_repr(
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 @@ fn check_repr(
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 @@ fn check_repr(
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 @@ fn check_repr(
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 @@ fn check_repr(
| 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 @@ fn check_repr(
continue;
}
};
self.tcx.sess.emit_err(AttributeShouldBeAppliedTo {
hint_span: hint.span(),
span,
what,
});
}
// Just point at all repr hints if there are any incompatibilities.

View File

@ -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)]