Skip to content

Commit d668c53

Browse files
Recover non-list repr attr with a span for later attr validation
1 parent 3c95364 commit d668c53

File tree

4 files changed

+123
-87
lines changed

4 files changed

+123
-87
lines changed

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ impl<S: Stage> CombineAttributeParser<S> for ReprParser {
3232
cx: &'c mut AcceptContext<'_, '_, S>,
3333
args: &'c ArgParser<'_>,
3434
) -> impl IntoIterator<Item = Self::Item> + 'c {
35-
let mut reprs = Vec::new();
36-
3735
let Some(list) = args.list() else {
3836
cx.expected_list(cx.attr_span);
39-
return reprs;
37+
// Recover this as `ReprEmpty` so that we have a span to point to.
38+
// This span is used for, e.g., complaining about crate-level `#![repr]`.
39+
return vec![(ReprAttr::ReprEmpty, cx.attr_span)];
4040
};
4141

42+
let mut reprs = Vec::new();
4243
if list.is_empty() {
4344
// this is so validation can emit a lint
4445
reprs.push((ReprAttr::ReprEmpty, cx.attr_span));

compiler/rustc_passes/src/check_attr.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,12 +2052,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
20522052
if item.is_some() {
20532053
match target {
20542054
Target::Struct | Target::Union | Target::Enum => continue,
2055-
Target::Fn | Target::Method(_) => {
2056-
self.dcx().emit_err(errors::ReprAlignShouldBeAlign {
2057-
span: *repr_span,
2058-
item: target.name(),
2059-
});
2060-
}
20612055
_ => {
20622056
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
20632057
hint_span: *repr_span,

tests/ui/attributes/malformed-attrs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
#![windows_subsystem]
3030
//~^ ERROR malformed
3131

32+
#![repr]
33+
//~^ ERROR malformed
34+
//~| ERROR `repr` attribute cannot be used at crate level
35+
3236
#[unsafe(export_name)]
3337
//~^ ERROR malformed
3438
#[rustc_allow_const_fn_unstable]
@@ -47,6 +51,7 @@
4751
//~^ ERROR malformed
4852
#[repr]
4953
//~^ ERROR malformed
54+
//~| ERROR attribute should be applied to a struct, enum, or union
5055
#[rustc_as_ptr = 5]
5156
//~^ ERROR malformed
5257
#[inline = 5]

0 commit comments

Comments
 (0)