rustc
Mark Mansi
Outline
Key Takeaways
Very Fast Rust Primer
Rust Primer
pub fn main () {
// Variable declarations...
let foo = 30;
let mut bar = 50;
// if-else
if foo == bar {
bar += 30;
}
// Standard data structures
// and type inference
let mut v = Vec::new();
v.push(bar);
v.push(foo);
// Loops
for item in v.iter() {
println!(“Hello World {}”, item);
}
}
enum Color {
Red,
Green,
Blue,
Grey(u8),
}
struct Dog {
name: String,
color: Color,
}
trait Pet {
fn greet(&self) -> String;
}
impl Pet for Dog {
fn greet(&self) -> String {
// Last expr is return value
format!(
“Good dog, {}”,
self.name
)
}
}
let mut v = Vec::new()
… // Add elements to v
for item in v.iter() {
v.push(5);
}
pub fn push(&mut self, new: T)
pub fn iter(&self) -> Iter<'_, T>
error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable
|
4 | for item in v.iter() {
| --------
| |
| immutable borrow occurs here
| immutable borrow later used here
5 | v.push(5);
| ^^^^^^^^^ mutable borrow occurs here
Rust Primer: More resources
The Rust Programming Language Book: https://doc.rust-lang.org/book/
Help Forum: https://users.rust-lang.org
rustc Architecture
rustc Architecture: Overview
Rust
LLVM
Queries instead of Stages!
type_check_crate()
list_of_all_hir_items()
hir(foo)
type_of(foo)
type_check_item(foo)
hir(bar)
type_of(bar)
type_check_item(bar)
Queries instead of Stages!
Why? Incremental Compilation!
rustc Task List
trait Foo { … }
fn baz<T: Foo>(t: T) { … }
baz(3);
baz(“hello”);
let x = 5;
match x {
0 => { … }
1 => { … }
2 => { … }
}
std::vec::Vec<T>
Vec<u64>
Vec<String>
rustc Task List
AST
rustc Task List
AST
HIR
rustc Task List
AST
HIR
THIR
rustc Task List
AST
HIR
MIR
THIR
rustc Task List
AST
HIR
MIR
THIR
LLVM IR
PL design -> Compiler performance
PL design -> Compiler design
Non-textbook Choices
Other Considerations For Compiler Builders
Besides Correctness: Development and Debugging
Besides Correctness: Developer Environment
Besides Correctness: Compiler Performance
Engineering: Compatibility and Stability Guarantees
Compiling playground v0.0.1 (/playground)
warning: cannot borrow `v` as mutable because it is also borrowed as immutable
|
3 | let shared = &v;
| -- immutable borrow occurs here
...
6 | v.push(shared.len());
| ^ ------ immutable borrow later used here
| |
| mutable borrow occurs here
|
= note: `#[warn(mutable_borrow_reservation_conflict)]` on by default
= warning: this borrowing pattern was not meant to be accepted, and may become a hard error in future
= note: for more information, see issue #59159 <https://github.com/rust-lang/rust/issues/59159>
Engineering: Testing and Dogfooding
Engineering: Bootstrapping
Engineering: Bootstrapping
Engineering: Bootstrapping
YO DAWG I HEARD YOU LIKE THE RUST COMPILER
SO I PUT SOME RUST IN YOUR RUST COMPILER SO YOU CAN COMPILE THE RUST COMPILER WHEN YOU COMPILE THE RUST COMPILER
Engineering: Bootstrapping Challenges
Engineering: Bootstrapping
Idea: 2-stage bootstrapping
Pros
Cons
Engineering: Bootstrapping
Engineering: Developer Considerations
Logistics
Other resources