Skip to content

A nonexistent inner attribute in lib.rs or main.rs breaks every procedural macro used within the entire crate #118455

Closed
@azteca1998

Description

@azteca1998

Code

#![some_nonexistent_attribute]

#[derive(Debug)]
pub struct SomeUserCode;

Current output

error: cannot find attribute `unsafe_code` in this scope
 --> src/lib.rs:1:4
  |
1 | #![some_nonexistent_attribute]
  |    ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: cannot determine resolution for the attribute macro `derive`
 --> src/lib.rs:3:3
  |
3 | #[derive(Debug)]
  |   ^^^^^^
  |
  = note: import resolution is stuck, try simplifying macro imports

error: could not compile `bug-replica` (lib) due to 2 previous errors

Desired output

error: cannot find attribute `unsafe_code` in this scope
 --> src/lib.rs:1:4
  |
1 | #![some_nonexistent_attribute]
  |    ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: could not compile `bug-replica` (lib) due to previous error

Rationale and extra context

Having an inner nonexistent attribute in lib.rs or main.rs shouldn't break every procedural macro in the entire crate.

Usually, one would detect this error just after writing, but when contained within #[cfg_attr(not(feature = "some-feature", invalid_attribute))] it can remain undetected for a while due to rust-analyzer not reporting errors for every feature combination, and causing the developer to have to deal with potentially several hundred errors without any clear lead on what caused it.

When this bug is triggered while having and using, for example, a struct deriving #[derive(Clone, Eq, PartialEq)] or any other trait, it'll make the compiler emit an error for every attempt to use them. For moderately large projects this means suddenly having hundreds to thousands of errors not related to the actual change from one commit to the next one.

Other cases

No response

Anything else?

The bug exists at least in versions 1.72.1 (stable) and 1.76.0-nightly (a1a37735c 2023-11-23).
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=45f3021f3d7420b1d8e76d3295d5e283

Metadata

Metadata

Assignees

Labels

A-attributesArea: Attributes (`#[…]`, `#![…]`)A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)D-confusingDiagnostics: Confusing error or lint that should be reworked.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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