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

Incorrect outer #![derive(Debug)] emits too many errors #67107

Closed
estebank opened this issue Dec 6, 2019 · 1 comment · Fixed by #118533
Closed

Incorrect outer #![derive(Debug)] emits too many errors #67107

estebank opened this issue Dec 6, 2019 · 1 comment · Fixed by #118533
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Dec 6, 2019

The following incorrect code:

#![derive(Debug)]
struct Coordinate(i32, i32);

fn main() {
    println!("{:?}", Coordinate(4, 2));
}

generates the following output:

error: `derive` may only be applied to structs, enums and unions
 --> src/main.rs:1:1
  |
1 | #![derive(Debug)]
  | ^^^^^^^^^^^^^^^^^ help: try an outer attribute: `#[derive(Debug)]`

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

error[E0277]: `Coordinate` doesn't implement `std::fmt::Debug`
 --> src/main.rs:5:22
  |
5 |     println!("{:?}", Coordinate(4, 2));
  |                      ^^^^^^^^^^^^^^^^ `Coordinate` cannot be formatted using `{:?}`
  |
  = help: the trait `std::fmt::Debug` is not implemented for `Coordinate`
  = note: add `#[derive(Debug)]` or manually implement `std::fmt::Debug`
  = note: required by `std::fmt::Debug::fmt`

We should not emit the second value, given it could detect that the following element is an ADT it could internally treat it as #[derive(Debug)] so that no knock-down errors will happen.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints P-low Low priority A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. labels Dec 6, 2019
@JohnTitor JohnTitor added the C-bug Category: This is a bug. label Dec 8, 2019
@jDomantas
Copy link
Contributor

A slightly bigger problem with this issue is that compiler emits an error for every custom attribute in the crate if the bad attribute ends up being on the root of the crate:

#![forbif(unused)]
// ^^^^^^ note the typo

use serde::Serialize;

#[derive(Serialize)]
pub struct Foo {
    #[serde(rename = "bar")]
    foo: i32,
    #[serde(rename = "quux")]
    baz: i32,
}

For this example compiler emits a total of 4 errors - first the one about the typo, and then one of each of derive and serde attributes. Only the first error is meaningful, with the other 3 seeming completely unrelated to the first one.

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-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. P-low Low priority 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.

3 participants