Skip to content

repr attributes can be applied at module level #64734

Closed
@gnzlbg

Description

@gnzlbg
Contributor

Spot what's wrong with this code:

#![repr(C)] struct Foo(i32);

extern "C" {
    fn foo(x: Foo);
}

fn main() {}

(Playground)

For some reason, top-level #![repr(..)] attributes are allowed, so Foo isn't repr(C). Sadly,
@rust-lang/underhanded isn't a thing.

Activity

gnzlbg

gnzlbg commented on Sep 24, 2019

@gnzlbg
ContributorAuthor

cc @Centril ^^^ I don't know who is familiar with libsyntax or the library that handles this.

added
A-attributesArea: Attributes (`#[…]`, `#![…]`)
C-bugCategory: This is a bug.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Sep 24, 2019
jonas-schievink

jonas-schievink commented on Sep 24, 2019

@jonas-schievink
Contributor

I don't think attributes that affect codegen are checked for validity at all. The same thing happens for #[cold], #[inline], etc.

hanna-kruppe

hanna-kruppe commented on Sep 24, 2019

@hanna-kruppe
Contributor

Some attributes including #[repr] and #[inline] are checked, it appears it's just not visiting the root module. Nested modules do get checked.

Centril

Centril commented on Sep 24, 2019

@Centril
Contributor

In case it turns out that many people rely on this, we could give reasonable (and arguably desirable) interpretation to #![inline] and #![repr(C)]: apply these recursively in the module to e.g. structs and fns (unless there is conflict, in which case the most-local attribute is chosen).

gnzlbg

gnzlbg commented on Sep 24, 2019

@gnzlbg
ContributorAuthor

If people rely on this compiling, but doing nothing, changing the repr of all types silently feels like a footgun.

carbotaniuman

carbotaniuman commented on Sep 25, 2019

@carbotaniuman
Contributor

I feel the standard warn by default, deny by default, hard error approach is the easiest way to handle this.

Maybe even skip to deny by default.

varkor

varkor commented on Sep 26, 2019

@varkor
Member

Given that occurrences of #![repr(C)] currently do nothing, and it's easy to search for in a codebase (and thus remove or audit), I don't see a strong reason not to start emitting errors, rather than just linting, on these, as long as we can determine this won't break downstream code if there's a crate making this mistake.

varkor

varkor commented on Sep 26, 2019

@varkor
Member

I don't think attributes that affect codegen are checked for validity at all. The same thing happens for #[cold], #[inline], etc.

We should fix this too, in the same manner as @rkruppe pointed out. (Either as part of this issue, or in a new issue.) Lack of validity checking can cause concrete problems, as in #64768.

added a commit that references this issue on Oct 5, 2020

Rollup merge of rust-lang#76329 - GuillaumeGomez:doc-alias-crate-leve…

e6e7ccc
ehuss

ehuss commented on May 14, 2021

@ehuss
Contributor

Closing, as this is now a hard error via #76329.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ehuss@Centril@gnzlbg@jonas-schievink@hanna-kruppe

        Issue actions

          repr attributes can be applied at module level · Issue #64734 · rust-lang/rust