Skip to content

Disentangle AST crates and error crates#155237

Open
nnethercote wants to merge 4 commits intorust-lang:mainfrom
nnethercote:ast-errors-rejig
Open

Disentangle AST crates and error crates#155237
nnethercote wants to merge 4 commits intorust-lang:mainfrom
nnethercote:ast-errors-rejig

Conversation

@nnethercote
Copy link
Copy Markdown
Contributor

@nnethercote nnethercote commented Apr 13, 2026

Currently the rustc_ast* crates and the rustc_error* crates (and rustc_lint_defs) are quite intertwined. This PR disentangles them. Details in individual commits.

r? @davidtwco

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 13, 2026

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

rustc_error_messages was changed

cc @TaKO8Ki

@rustbot rustbot added A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 13, 2026
@nnethercote
Copy link
Copy Markdown
Contributor Author

nnethercote commented Apr 13, 2026

The path from rustc_hir to rustc_span, before:
image

And after:
image

I think the latter is a lot nicer -- much less linear, with rustc_hir_id gone and rustc_error* fully separated from rustc_ast*.

@rust-log-analyzer

This comment has been minimized.

@nnethercote nnethercote marked this pull request as draft April 13, 2026 12:43
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 13, 2026
@nik-rev
Copy link
Copy Markdown
Contributor

nik-rev commented Apr 13, 2026

@nnethercote that crate graph visualization looks nice, what tool did you use to generate it?

@nnethercote
Copy link
Copy Markdown
Contributor Author

@nik-rev:

cargo +nightly depgraph --all-deps --dedup-transitive-deps --workspace-only --root=rustc-main,rustc_codegen_llvm ~/graph.dot
dot -Tsvg ~/graph.dot > ~/graph.svg

Requires installing cargo-depgraph.

`rustc_error_messages` currently depends on
`rustc_ast`/`rustc_ast_pretty`. This is odd, because
`rustc_error_messages` feels like a very low-level module but
`rustc_ast`/`rustc_ast_pretty` do not.

The reason is that a few AST types impl `IntoDiagArg` via
pretty-printing. `rustc_error_messages` can define `IntoDiagArg` and
then impl it for the AST types. But if we invert the dependency we hit
a problem with the orphan rule: `rustc_ast` must impl `IntoDiagArg`
for the AST types, but that requires calling pretty-printing code which
is in `rustc_ast_pretty`, a downstream crate.

This commit avoids this problem by just removing the `IntoDiagArg` impls
for these AST types. There aren't that many of them, and we can just use
`String` in the relevant error structs and use the pretty printer in the
downstream crates that construct the error structs. There are plenty of
existing examples where `String` is used in error structs.

There is now no dependency between `rustc_ast*` and
`rustc_error_messages`.
It currently only depends on two things:
- `rustc_ast::AttrId`: this is just a re-export of `rustc_span::AttrId`,
  so we can import the original instead.
- `rustc_ast::AttributeExt`: needed only for the `name` and `id`
  methods. We can instead pass in the `name` and `id` directly.
@nnethercote nnethercote changed the title Adjust rustc_ast* dependencies Distentangle AST crates and error crates Apr 14, 2026
@nnethercote nnethercote changed the title Distentangle AST crates and error crates Disentangle AST crates and error crates Apr 14, 2026
@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Apr 14, 2026
@rust-log-analyzer

This comment has been minimized.

`rustc_hir_id` is a very small crate containing some basic HIR types
(e.g. `HirId`) so that other crates can use them without depending on
`rustc_hir` (which is a much bigger crate).

However, `rustc_span` already has a module called `def_id` which also
contains some basic HIR types (e.g. `DefId`). So there's not much point
also have `rustc_hir_id`. This commit moves the contents of
`rustc_hir_id` into that module.
This puts it alongside `rustc_span::def_id`, which serves a similar
purpose.

This lets us remove `rustc_errors`'s dependency on `rustc_ast`,
completing the disentangling of the AST crates and the error crates.
@nnethercote nnethercote marked this pull request as ready for review April 14, 2026 10:25
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 14, 2026
@nnethercote
Copy link
Copy Markdown
Contributor Author

@davidtwco: this is now ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic 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-clippy Relevant to the Clippy team. 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.

5 participants