Skip to content

Commit c21730a

Browse files
TbkhiNoratrieb
authored andcommitted
improve rustc-driver.md
minor addition and adding links Update rustc-driver.md removing parens for functions reorganized directory reorganized directory minor edits
1 parent 5317005 commit c21730a

File tree

5 files changed

+55
-50
lines changed

5 files changed

+55
-50
lines changed

src/SUMMARY.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@
9090

9191
- [Prologue](./part-3-intro.md)
9292
- [Command-line arguments](./cli.md)
93-
- [rustc_driver and rustc_interface](./rustc-driver.md)
94-
- [Example: Type checking](./rustc-driver-interacting-with-the-ast.md)
95-
- [Example: Getting diagnostics](./rustc-driver-getting-diagnostics.md)
93+
- [rustc_driver and rustc_interface](./rustc-driver/intro.md)
94+
- [Example: Type checking](./rustc-driver/interacting-with-the-ast.md)
95+
- [Example: Getting diagnostics](./rustc-driver/getting-diagnostics.md)
9696
- [Syntax and the AST](./syntax-intro.md)
9797
- [Lexing and Parsing](./the-parser.md)
9898
- [Macro expansion](./macro-expansion.md)

src/rustc-driver.md

-45
This file was deleted.

src/rustc-driver-getting-diagnostics.md renamed to src/rustc-driver/getting-diagnostics.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ otherwise be printed to stderr.
77

88
To get diagnostics from the compiler,
99
configure [`rustc_interface::Config`] to output diagnostic to a buffer,
10-
and run [`TyCtxt.analysis`]. The following was tested
11-
with <!-- date-check: september 2024 --> `nightly-2024-09-16`:
10+
and run [`TyCtxt.analysis`].
11+
The following was tested with <!-- date-check: september 2024 --> `nightly-2024-09-16`:
1212

1313
```rust
1414
{{#include ../examples/rustc-driver-getting-diagnostics.rs}}

src/rustc-driver/intro.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# `rustc_driver` and `rustc_interface`
2+
3+
The [`rustc_driver`] is essentially `rustc`'s `main` function.
4+
It acts as the glue for running the various phases of the compiler in the correct order,
5+
using the interface defined in the [`rustc_interface`] crate.
6+
7+
Generally the [`rustc_interface`] crate provides external users with an (unstable) API
8+
for running code at particular times during the compilation process, allowing
9+
third parties to effectively use `rustc`'s internals as a library for
10+
analyzing a crate or for ad hoc emulating of the compiler (i.e. `rustdoc`
11+
compiling code and serving output).
12+
13+
More specifically the [`rustc_interface::run_compiler`][i_rc] function is the
14+
main entrypoint for using [`nightly-rustc`] as a library.
15+
Initially [`run_compiler`][i_rc] takes a configuration variable for the compiler
16+
and a `closure` taking a yet unresolved [`Compiler`].
17+
Operationally [`run_compiler`][i_rc] creates a `Compiler` from the configuration and passes
18+
it to the `closure`. Inside the `closure` you can use the `Compiler` to drive
19+
queries to compile a crate and get the results.
20+
Providing results about the internal state of the compiler what the [`rustc_driver`] does too.
21+
You can see a minimal example of how to use [`rustc_interface`] [here][example].
22+
23+
You can see what queries are currently available in the [`Compiler`] rustdocs.
24+
You can see an example of how to use the queries by looking at the `rustc_driver` implementation,
25+
specifically [`rustc_driver::run_compiler`][rd_rc]
26+
(not to be confused with [`rustc_interface::run_compiler`][i_rc]).
27+
Generally [`rustc_driver::run_compiler`][i_rc] takes a bunch of command-line args
28+
and some other configurations and drives the compilation to completion.
29+
30+
Finally [`rustc_driver::run_compiler`][rd_rc] also takes a [`Callbacks`][cb],
31+
which is a `trait` that allows for custom compiler configuration,
32+
as well as allowing custom code to run after different phases of the compilation.
33+
34+
> **Warning:** By its very nature, the internal compiler APIs are always going
35+
> to be unstable. That said, we do try not to break things unnecessarily.
36+
37+
38+
[`Compiler`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/struct.Compiler.html
39+
[`rustc_driver`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/
40+
[`rustc_interface`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html
41+
[`Session`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/struct.Session.html
42+
[`SourceMap`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/source_map/struct.SourceMap.html
43+
[`TyCtxt`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html
44+
[Appendix A]: appendix/stupid-stats.html
45+
[cb]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/trait.Callbacks.html
46+
[example]: https://github.com/rust-lang/rustc-dev-guide/blob/master/examples/rustc-driver-example.rs
47+
[i_rc]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/fn.run_compiler.html
48+
[rd_rc]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver_impl/fn.run_compiler.html
49+
[stupid-stats]: https://github.com/nrc/stupid-stats
50+
[`nightly-rustc`]: https://doc.rust-lang.org/nightly/nightly-rustc/

0 commit comments

Comments
 (0)