Skip to content
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

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

Closed
fmease opened this issue Feb 24, 2022 · 1 comment · Fixed by #95189
Closed
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fmease
Copy link
Member

fmease commented Feb 24, 2022

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

@fmease fmease added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 24, 2022
@rustbot rustbot added C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. labels Feb 24, 2022
@fmease
Copy link
Member Author

fmease commented Mar 21, 2022

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-incorrect Diagnostics: A diagnostic that is giving misleading or incorrect information. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants