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

Improve -Z crate-attr diagnostics #138336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jyn514
Copy link
Member

@jyn514 jyn514 commented Mar 11, 2025

  • Show the #![ ... ] in the span (to make it clear that it should not
    be included in the CLI argument)
  • Show more detailed errors when the crate has valid token trees but
    invalid syntax.
    Previously, crate-attr=feature(foo),feature(bar) would just say
    "invalid crate attribute" and point at the comma. Now, it explicitly
    says that the comma was unexpected, which is useful when using
    --error-format=short. It also fixes the column to show the correct
    span.
  • Recover from parse errors. Previously we would abort immediately on
    syntax errors; now we go on to try and type-check the rest of the
    crate.

The new diagnostic code also happens to be slightly shorter.

r? diagnostics

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 11, 2025
@jyn514 jyn514 force-pushed the crate-attr-diagnostics branch from 5e4599d to 2d05a27 Compare March 11, 2025 04:39
@rustbot rustbot removed the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Mar 11, 2025
@jyn514 jyn514 changed the title Crate attr diagnostics Improve -Z crate-attr diagnostics Mar 11, 2025
@jyn514 jyn514 force-pushed the crate-attr-diagnostics branch 2 times, most recently from 7fc52b8 to 74e6e2d Compare March 11, 2025 04:42
@rust-log-analyzer

This comment has been minimized.

@jyn514 jyn514 force-pushed the crate-attr-diagnostics branch from 74e6e2d to 4b25b06 Compare March 11, 2025 12:22
@petrochenkov
Copy link
Contributor

petrochenkov commented Mar 11, 2025

Move crate-attr parsing from rustc_builtin_macros to
rustc_parse. This is not a built-in macro.

All kind of code injected by rustc consistently lives in rustc_builtin_macros - prelude injection, test harness, etc (search for ::inject(), I suggest keeping it in the current location.
(The logic is that attribute/prelude/etc injection is not used to parse anything, so it's not in rustc_parse.)

@rust-log-analyzer

This comment has been minimized.

@jyn514 jyn514 force-pushed the crate-attr-diagnostics branch from 4b25b06 to 86072ff Compare March 12, 2025 02:31
@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Mar 12, 2025
@jyn514
Copy link
Member Author

jyn514 commented Mar 12, 2025

All kind of code injected by rustc consistently lives in rustc_builtin_macros - prelude injection, test harness, etc (search for ::inject(), I suggest keeping it in the current location.
(The logic is that attribute/prelude/etc injection is not used to parse anything, so it's not in rustc_parse.)

ok, done. fortunately parse_attribute was public (i guess another motivation of not having this in rustc_parse is so that we can't use parser internals).

- Show the `#![ ... ]` in the span (to make it clear that it should not
  be included in the CLI argument)
- Show more detailed errors when the crate has valid token trees but
  invalid syntax.
  Previously, `crate-attr=feature(foo),feature(bar)` would just say
  "invalid crate attribute" and point at the comma. Now, it explicitly
  says that the comma was unexpected, which is useful when using
  `--error-format=short`. It also fixes the column to show the correct
  span.
- Recover from parse errors. Previously we would abort immediately on
  syntax errors; now we go on to try and type-check the rest of the
  crate.

The new diagnostic code also happens to be slightly shorter.
@jyn514 jyn514 force-pushed the crate-attr-diagnostics branch from 86072ff to 42b890b Compare March 12, 2025 02:32
@jyn514 jyn514 force-pushed the crate-attr-diagnostics branch from 42b890b to 4a301c7 Compare March 12, 2025 02:33
@jieyouxu jieyouxu added the -Zcrate-attr Unstable option: -Zcrate-attr label Mar 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-Zcrate-attr Unstable option: -Zcrate-attr S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants