Skip to content

Recover non-list repr attr with a span for later attr validation #143533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions compiler/rustc_attr_parsing/src/attributes/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ impl<S: Stage> CombineAttributeParser<S> for ReprParser {
cx: &'c mut AcceptContext<'_, '_, S>,
args: &'c ArgParser<'_>,
) -> impl IntoIterator<Item = Self::Item> + 'c {
let mut reprs = Vec::new();

let Some(list) = args.list() else {
cx.expected_list(cx.attr_span);
return reprs;
// Recover this as `ReprEmpty` so that we have a span to point to.
// This span is used for, e.g., complaining about crate-level `#![repr]`.
return vec![(ReprAttr::ReprEmpty, cx.attr_span)];
};

let mut reprs = Vec::new();
if list.is_empty() {
// this is so validation can emit a lint
reprs.push((ReprAttr::ReprEmpty, cx.attr_span));
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2052,12 +2052,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
if item.is_some() {
match target {
Target::Struct | Target::Union | Target::Enum => continue,
Target::Fn | Target::Method(_) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was telling us that #[repr] was actually #[repr(align(..))] which is wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this fix is not included in #143522, we should make sure this does not get lost

self.dcx().emit_err(errors::ReprAlignShouldBeAlign {
span: *repr_span,
item: target.name(),
});
}
_ => {
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
hint_span: *repr_span,
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/attributes/malformed-attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#![windows_subsystem]
//~^ ERROR malformed

#![repr]
//~^ ERROR malformed
//~| ERROR `repr` attribute cannot be used at crate level

#[unsafe(export_name)]
//~^ ERROR malformed
#[rustc_allow_const_fn_unstable]
Expand All @@ -47,6 +51,7 @@
//~^ ERROR malformed
#[repr]
//~^ ERROR malformed
//~| ERROR attribute should be applied to a struct, enum, or union
#[rustc_as_ptr = 5]
//~^ ERROR malformed
#[inline = 5]
Expand Down
Loading
Loading