Skip to content

Attempting to include! inner attributes leads to incorrect and confusing diagnostics #94340

Closed
@fmease

Description

@fmease

First of all, trying to include! inner attributes should be an error, I think, reading between the lines of std::include's documentation and the Reference on modules. Correct me if I am wrong. I initially did not know this but std::include may only include expressions and items (according to the documentation) and inner attributes are not items (according to the Reference).

Now, given the following code in file it.rs:

include!("attrs.rs");

And the code below in file attrs.rs:

#![deny(rust_2018_idioms)]
#![deny(unused_must_use)]

The current output of rustc it.rs --crate-type=lib is:

error: an inner attribute is not permitted in this context
 --> attrs.rs:1:1
  |
1 | #![deny(rust_2018_idioms)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
  = note: outer attributes, like `#[test]`, annotate the item following them

error: an inner attribute is not permitted following an outer attribute
 --> attrs.rs:2:1
  |
1 | #![deny(rust_2018_idioms)]
  | -------------------------- previous outer attribute
2 | #![deny(unused_must_use)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^ not permitted following an outer attribute
  |
  = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
  = note: outer attributes, like `#[test]`, annotate the item following them

error: expected item after attributes
 --> attrs.rs:2:1
  |
1 | #![deny(rust_2018_idioms)]
  | -------------------------- other attributes here
2 | #![deny(unused_must_use)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

  • error: an inner attribute is not permitted following an outer attribute  and  previous outer attribute
    • the compiler incorrectly labels the inner attribute as an outer attribute
    • this diagnostic should not be emitted at all
  • error: an inner attribute is not permitted in this context
    • this diagnostic is generally fine
    • however, I'd like to see an explanation or additional context (heh) like a note saying that they can't be include!d
  • note: inner attributes, like #![no_std], annotate the item enclosing them, and are usually found at the beginning of source files  and 
    note: outer attributes, like #[test], annotate the item following them
    • irrelevant advice, does not help here
    • the found at the beginning of source files part is confusing since both inner attributes are in fact at the beginning of the source file attrs.rs
  • error: expected item after attributes
    • this confusing/incorrect: those are inner attributes, no item needs to follow
    • this should not be emitted at all
    • update: This diagnostic is fine, I just read it the wrong way. It just means that an item was expected but not found and along the way, some (outer or inner) attributes were found. It doesn't necessarily mean that those attributes need an item / need to be ascribed to an item
    • update 2: still, it shouldn't be emitted at all if there are only inner attributes since it's allowed to include an empty file as a declaration and our case is basically 2 invalid inner attributes which should be ignored (because errors are going to reported from some other part of the code base) following by <eof> which in this case should be allowed

rustc 1.60.0-nightly (5d8767c 2022-02-12)

@rustbot modify labels: +C-bug +D-incorrect +D-confusing

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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

Issue actions