Skip to content

Commit 31c269a

Browse files
committed
avoid string dispatch in fluent
1 parent 57eba4f commit 31c269a

File tree

3 files changed

+86
-39
lines changed

3 files changed

+86
-39
lines changed

compiler/rustc_error_messages/locales/en-US/passes.ftl

+19-17
Original file line numberDiff line numberDiff line change
@@ -596,23 +596,25 @@ passes_unrecognized_repr_hint =
596596
unrecognized representation hint
597597
.help = valid reprs are `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`
598598
599-
passes_attribute_should_be_applied_to =
600-
attribute should be applied to {$what ->
601-
[enum] an enum
602-
[struct] a struct
603-
[struct-union] a struct or union
604-
[struct-enum-union] a struct, enum, or union
605-
[struct-enum-function-union] a struct, enum, function, or union
606-
*[unspecified] (unspecified--this is a compiler bug)
607-
}
608-
.label = not {$what ->
609-
[enum] an enum
610-
[struct] a struct
611-
[struct-union] a struct or union
612-
[struct-enum-union] a struct, enum, or union
613-
[struct-enum-function-union] a struct, enum, function, or union
614-
*[unspecified] (unspecified--this is a compiler bug)
615-
}
599+
passes_attr_application_enum =
600+
attribute should be applied to an enum
601+
.label = not an enum
602+
603+
passes_attr_application_struct =
604+
attribute should be applied to a struct
605+
.label = not a struct
606+
607+
passes_attr_application_struct_union =
608+
attribute should be applied to a struct or union
609+
.label = not a struct or union
610+
611+
passes_attr_application_struct_enum_union =
612+
attribute should be applied to a struct, enum, or union
613+
.label = not a struct, enum, or union
614+
615+
passes_attr_application_struct_enum_function_union =
616+
attribute should be applied to a struct, enum, function, or union
617+
.label = not a struct, enum, function, or union
616618
617619
passes_transparent_incompatible =
618620
transparent {$target} cannot have other repr hints

compiler/rustc_passes/src/check_attr.rs

+31-15
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//! item.
66
77
use crate::errors::{
8-
self, AttributeShouldBeAppliedTo, DebugVisualizerUnreadable, InvalidAttrAtCrateLevel,
9-
ObjectLifetimeErr, OnlyHasEffectOn, TransparentIncompatible, UnrecognizedReprHint,
8+
self, AttrApplication, DebugVisualizerUnreadable, InvalidAttrAtCrateLevel, ObjectLifetimeErr,
9+
OnlyHasEffectOn, TransparentIncompatible, UnrecognizedReprHint,
1010
};
1111
use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
1212
use rustc_data_structures::fx::FxHashMap;
@@ -1594,12 +1594,17 @@ impl CheckAttrVisitor<'_> {
15941594
continue;
15951595
}
15961596

1597-
let what = match hint.name_or_empty() {
1597+
match hint.name_or_empty() {
15981598
sym::C => {
15991599
is_c = true;
16001600
match target {
16011601
Target::Struct | Target::Union | Target::Enum => continue,
1602-
_ => "struct-enum-union",
1602+
_ => {
1603+
self.tcx.sess.emit_err(AttrApplication::StructEnumUnion {
1604+
hint_span: hint.span(),
1605+
span,
1606+
});
1607+
}
16031608
}
16041609
}
16051610
sym::align => {
@@ -1615,20 +1620,30 @@ impl CheckAttrVisitor<'_> {
16151620

16161621
match target {
16171622
Target::Struct | Target::Union | Target::Enum | Target::Fn => continue,
1618-
_ => "struct-enum-function-union",
1623+
_ => {
1624+
self.tcx.sess.emit_err(AttrApplication::StructEnumFunctionUnion {
1625+
hint_span: hint.span(),
1626+
span,
1627+
});
1628+
}
16191629
}
16201630
}
16211631
sym::packed => {
16221632
if target != Target::Struct && target != Target::Union {
1623-
"struct-union"
1633+
self.tcx.sess.emit_err(AttrApplication::StructUnion {
1634+
hint_span: hint.span(),
1635+
span,
1636+
});
16241637
} else {
16251638
continue;
16261639
}
16271640
}
16281641
sym::simd => {
16291642
is_simd = true;
16301643
if target != Target::Struct {
1631-
"struct"
1644+
self.tcx
1645+
.sess
1646+
.emit_err(AttrApplication::Struct { hint_span: hint.span(), span });
16321647
} else {
16331648
continue;
16341649
}
@@ -1637,7 +1652,12 @@ impl CheckAttrVisitor<'_> {
16371652
is_transparent = true;
16381653
match target {
16391654
Target::Struct | Target::Union | Target::Enum => continue,
1640-
_ => "struct-enum-union",
1655+
_ => {
1656+
self.tcx.sess.emit_err(AttrApplication::StructEnumUnion {
1657+
hint_span: hint.span(),
1658+
span,
1659+
});
1660+
}
16411661
}
16421662
}
16431663
sym::i8
@@ -1654,7 +1674,9 @@ impl CheckAttrVisitor<'_> {
16541674
| sym::usize => {
16551675
int_reprs += 1;
16561676
if target != Target::Enum {
1657-
"enum"
1677+
self.tcx
1678+
.sess
1679+
.emit_err(AttrApplication::Enum { hint_span: hint.span(), span });
16581680
} else {
16591681
continue;
16601682
}
@@ -1664,12 +1686,6 @@ impl CheckAttrVisitor<'_> {
16641686
continue;
16651687
}
16661688
};
1667-
1668-
self.tcx.sess.emit_err(AttributeShouldBeAppliedTo {
1669-
hint_span: hint.span(),
1670-
span,
1671-
what,
1672-
});
16731689
}
16741690

16751691
// Just point at all repr hints if there are any incompatibilities.

compiler/rustc_passes/src/errors.rs

+36-7
Original file line numberDiff line numberDiff line change
@@ -1288,13 +1288,42 @@ pub struct UnrecognizedReprHint {
12881288
}
12891289

12901290
#[derive(Diagnostic)]
1291-
#[diag(passes::attribute_should_be_applied_to, code = "E0517")]
1292-
pub struct AttributeShouldBeAppliedTo<'a> {
1293-
#[primary_span]
1294-
pub hint_span: Span,
1295-
#[label]
1296-
pub span: Span,
1297-
pub what: &'a str,
1291+
pub enum AttrApplication {
1292+
#[diag(passes::attr_application_enum, code = "E0517")]
1293+
Enum {
1294+
#[primary_span]
1295+
hint_span: Span,
1296+
#[label]
1297+
span: Span,
1298+
},
1299+
#[diag(passes::attr_application_struct, code = "E0517")]
1300+
Struct {
1301+
#[primary_span]
1302+
hint_span: Span,
1303+
#[label]
1304+
span: Span,
1305+
},
1306+
#[diag(passes::attr_application_struct_union, code = "E0517")]
1307+
StructUnion {
1308+
#[primary_span]
1309+
hint_span: Span,
1310+
#[label]
1311+
span: Span,
1312+
},
1313+
#[diag(passes::attr_application_struct_enum_union, code = "E0517")]
1314+
StructEnumUnion {
1315+
#[primary_span]
1316+
hint_span: Span,
1317+
#[label]
1318+
span: Span,
1319+
},
1320+
#[diag(passes::attr_application_struct_enum_function_union, code = "E0517")]
1321+
StructEnumFunctionUnion {
1322+
#[primary_span]
1323+
hint_span: Span,
1324+
#[label]
1325+
span: Span,
1326+
},
12981327
}
12991328

13001329
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)