-
Notifications
You must be signed in to change notification settings - Fork 549
Fix links #288
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
Merged
Merged
Fix links #288
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,25 @@ | ||
# The Rustc Driver | ||
# The Rustc Driver and Interface | ||
|
||
The [`rustc_driver`] is essentially `rustc`'s `main()` function. It acts as | ||
the glue for running the various phases of the compiler in the correct order, | ||
managing state such as the [`SourceMap`] \(maps AST nodes to source code), | ||
[`Session`] \(general build context and error messaging) and the [`TyCtxt`] | ||
\(the "typing context", allowing you to query the type system and other cool | ||
stuff). The `rustc_driver` crate also provides external users with a method | ||
using the interface defined in the [`rustc_interface`] crate. | ||
|
||
The `rustc_interface` crate provides external users with an (unstable) API | ||
for running code at particular times during the compilation process, allowing | ||
third parties to effectively use `rustc`'s internals as a library for | ||
analysing a crate or emulating the compiler in-process (e.g. the RLS). | ||
|
||
For those using `rustc` as a library, the `run_compiler()` function is the main | ||
entrypoint to the compiler. Its main parameters are a list of command-line | ||
arguments and a reference to something which implements the `CompilerCalls` | ||
trait. A `CompilerCalls` creates the overall `CompileController`, letting it | ||
govern which compiler passes are run and attach callbacks to be fired at the end | ||
of each phase. | ||
|
||
From `rustc_driver`'s perspective, the main phases of the compiler are: | ||
|
||
1. *Parse Input:* Initial crate parsing | ||
2. *Configure and Expand:* Resolve `#[cfg]` attributes, name resolution, and | ||
expand macros | ||
3. *Run Analysis Passes:* Run trait resolution, typechecking, region checking | ||
and other miscellaneous analysis passes on the crate | ||
4. *Translate to LLVM:* Translate to the in-memory form of LLVM IR and turn it | ||
into an executable/object files | ||
analysing a crate or emulating the compiler in-process (e.g. the RLS or rustdoc). | ||
|
||
The `CompileController` then gives users the ability to inspect the ongoing | ||
compilation process | ||
For those using `rustc` as a library, the `interface::run_compiler()` function is the main | ||
entrypoint to the compiler. It takes a configuration for the compiler and a closure that | ||
takes a [`Compiler`]. `run_compiler` creates a `Compiler` from the configuration and passes | ||
it to the closure. Inside the closure, you can use the `Compiler` to drive queries to compile | ||
a crate and get the results. This is what the `rustc_driver` does too. | ||
|
||
- after parsing | ||
- after AST expansion | ||
- after HIR lowering | ||
- after analysis, and | ||
- when compilation is done | ||
|
||
The `CompileState`'s various `state_after_*()` constructors can be inspected to | ||
determine what bits of information are available to which callback. | ||
|
||
For a more detailed explanation on using `rustc_driver`, check out the | ||
[stupid-stats] guide by `@nrc` (attached as [Appendix A]). | ||
You can see what queries are currently available through the rustdocs for [`Compiler`]. | ||
You can see an example of how to use them by looking at the `rustc_driver` implementation, | ||
specifically the [`rustc_driver::run_compiler` function][rd_rc] (not to be confused with | ||
`interface::run_compiler`). The `rustc_driver::run_compiler` function takes a bunch of | ||
command-line args and some other configurations and drives the compilation to completion. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe mention that |
||
|
||
> **Warning:** By its very nature, the internal compiler APIs are always going | ||
> to be unstable. That said, we do try not to break things unnecessarily. | ||
|
@@ -54,21 +33,15 @@ manifests itself in the way people can plug into the compiler, preferring a | |
"push"-style API (callbacks) instead of the more Rust-ic "pull" style (think | ||
the `Iterator` trait). | ||
|
||
For example the [`CompileState`], the state passed to callbacks after each | ||
phase, is essentially just a box of optional references to pieces inside the | ||
compiler. The lifetime bound on the `CompilerCalls` trait then helps to ensure | ||
compiler internals don't "escape" the compiler (e.g. if you tried to keep a | ||
reference to the AST after the compiler is finished), while still letting users | ||
record *some* state for use after the `run_compiler()` function finishes. | ||
|
||
Thread-local storage and interning are used a lot through the compiler to reduce | ||
duplication while also preventing a lot of the ergonomic issues due to many | ||
pervasive lifetimes. The `rustc::ty::tls` module is used to access these | ||
thread-locals, although you should rarely need to touch it. | ||
|
||
|
||
[rd_rc]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/fn.run_compiler.html | ||
[`rustc_interface`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/index.html | ||
[`rustc_driver`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/ | ||
[`CompileState`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/driver/struct.CompileState.html | ||
[`Compiler`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_interface/interface/struct.Compiler.html | ||
[`Session`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/session/struct.Session.html | ||
[`TyCtxt`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.TyCtxt.html | ||
[`SourceMap`]: https://doc.rust-lang.org/nightly/nightly-rustc/syntax/source_map/struct.SourceMap.html | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not exactly true as
rustc_driver
still exposes a similar interface withrun_compiler
+Callbacks
.