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

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

Closed
azteca1998 opened this issue Nov 29, 2023 · 4 comments · Fixed by #118533
Assignees
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-confusing Diagnostics: Confusing error or lint that should be reworked. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@azteca1998
Copy link

azteca1998 commented Nov 29, 2023

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

@azteca1998 azteca1998 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 Nov 29, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 29, 2023
@Jules-Bertholet
Copy link
Contributor

Jules-Bertholet commented Nov 29, 2023

@rustbot label D-verbose D-confusing A-attributes A-macros

Regressed between 1.37 and 1.38: https://rust.godbolt.org/z/eoczvTcd7

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-confusing Diagnostics: Confusing error or lint that should be reworked. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. labels Nov 29, 2023
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 30, 2023
@chenyukang
Copy link
Member

maybe related #110082

@chenyukang chenyukang self-assigned this Dec 1, 2023
estebank added a commit to estebank/rust that referenced this issue Dec 1, 2023
When we encounter code like

```rust
#![non_existent]

#[derive(Clone)]
struct S;

fn main() {
    S.clone();
}
```

we avoid emitting errors about the `derive` (which isn't early resolved
as normal, but *has* a later resolution) and stop the compiler from
advancing to later stages, to avoid knock down errors from the `derive`
not being evaluated. Recovering these would be possible, but would
produce incorrect errors in the cases where had the outer attribute been
present it would have modified the behavior/evaluation of the `derive`
attributes.

Fix rust-lang#118455.
@estebank
Copy link
Contributor

estebank commented Dec 1, 2023

@chenyukang I took a stab at a minimally invasive attempt (by using FatalError.raise() in this specific case): #118511, but if you want to see if there's an alternative method that doesn't rely on "quitting early", please go ahead :)

@chenyukang
Copy link
Member

oops, I didn't know you created a PR,
I just created another one: #118533

estebank added a commit to estebank/rust that referenced this issue Dec 4, 2023
When we encounter code like

```rust

struct S;

fn main() {
    S.clone();
}
```

we avoid emitting errors about the `derive` (which isn't early resolved
as normal, but *has* a later resolution) and stop the compiler from
advancing to later stages, to avoid knock down errors from the `derive`
not being evaluated. Recovering these would be possible, but would
produce incorrect errors in the cases where had the outer attribute been
present it would have modified the behavior/evaluation of the `derive`
attributes.

Fix rust-lang#118455.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 28, 2024
…trochenkov

Suppress unhelpful diagnostics for unresolved top level attributes

Fixes rust-lang#118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them.

Also fixes rust-lang#89566, fixes rust-lang#67107.

r? `@petrochenkov`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 28, 2024
…trochenkov

Suppress unhelpful diagnostics for unresolved top level attributes

Fixes rust-lang#118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them.

Also fixes rust-lang#89566, fixes rust-lang#67107.

r? ``@petrochenkov``
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 28, 2024
…trochenkov

Suppress unhelpful diagnostics for unresolved top level attributes

Fixes rust-lang#118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them.

Also fixes rust-lang#89566, fixes rust-lang#67107.

r? ```@petrochenkov```
bors added a commit to rust-lang-ci/rust that referenced this issue Jan 30, 2024
…ochenkov

Suppress unhelpful diagnostics for unresolved top level attributes

Fixes rust-lang#118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them.

Also fixes rust-lang#89566, fixes rust-lang#67107.

r? `@petrochenkov`
@bors bors closed this as completed in ee2e9e1 Jan 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 30, 2024
Rollup merge of rust-lang#118533 - chenyukang:yukang-fix-118455, r=petrochenkov

Suppress unhelpful diagnostics for unresolved top level attributes

Fixes rust-lang#118455, unresolved top level attribute error didn't imported prelude and already have emitted an error, report builtin macro and attributes error by the way, so `check_invalid_crate_level_attr` in can ignore them.

Also fixes rust-lang#89566, fixes rust-lang#67107.

r? `@petrochenkov`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) D-confusing Diagnostics: Confusing error or lint that should be reworked. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
6 participants