Rust and rustc intro
Daniel Cumming
1
Runtime Verification Inc.
Overview
2
What is Rust?
3
What is Rust
4
“A language empowering everyone to build reliable and efficient software.” - rust-lang.org
What is Rust
5
“A language empowering everyone to build reliable and efficient software.” - rust-lang.org
What is Rust
6
“A language empowering everyone to build reliable and efficient software.” - rust-lang.org
What is Rust
7
“A language empowering everyone to build reliable and efficient software.” - rust-lang.org
What is Rust
8
“A language empowering everyone to build reliable and efficient software.” - rust-lang.org
Assuming no inappropriate use of unsafe keyword
What is Rust
9
“A language empowering everyone to build reliable and efficient software.” - rust-lang.org
Safely possible with appropriate use of unsafe keyword
Assuming no inappropriate use of unsafe keyword
What is rustc?
10
What is rustc?
11
Stages of the Rust compiler
12
Rust Source Code
13
fn main() {
println!("Hello, world!");
}
Rust IR: Abstract Syntax Tree
14
An abstract syntax tree (AST) is a data structure used in computer science to represent the structure of a program or code snippet. It is a tree representation of the abstract syntactic structure of text (often source code) written in a formal language. Each node of the tree denotes a construct occurring in the text. - Wikipedia (AST)
Rust IR: Abstract Syntax Tree
15
An abstract syntax tree (AST) is a data structure used in computer science to represent the structure of a program or code snippet. It is a tree representation of the abstract syntactic structure of text (often source code) written in a formal language. Each node of the tree denotes a construct occurring in the text. - Wikipedia (AST)
Rust IR: High IR
16
The HIR – "High-Level Intermediate Representation" – is the primary IR used in most of rustc. It is a compiler-friendly representation of the abstract syntax tree (AST)... Many parts of HIR resemble Rust surface syntax quite closely, with the exception that some of Rust's expression forms have been desugared away. - rustc-dev-guide
Rust IR: High IR
17
The HIR – "High-Level Intermediate Representation" – is the primary IR used in most of rustc. It is a compiler-friendly representation of the abstract syntax tree (AST)... Many parts of HIR resemble Rust surface syntax quite closely, with the exception that some of Rust's expression forms have been desugared away. - rustc-dev-guide
Rust IR: Typed-High IR
18
THIR is a lowered version of the HIR where all the types have been filled in, which is possible after type checking has completed. But it has some other interesting features that distinguish it from the HIR… - rustc-dev-guide
Rust IR: Typed-High IR
19
THIR is a lowered version of the HIR where all the types have been filled in, which is possible after type checking has completed. But it has some other interesting features that distinguish it from the HIR… - rustc-dev-guide
Rust IR: Middle IR
20
[MIR] is a radically simplified form of Rust that is used for certain flow-sensitive safety checks – notably the borrow checker! – and also for optimization and code generation. - rustc-dev-guide
Rust IR: Middle IR
21
[MIR] is a radically simplified form of Rust that is used for certain flow-sensitive safety checks – notably the borrow checker! – and also for optimization and code generation. - rustc-dev-guide
Code Generation
22
Code generation (or "codegen") is the part of the compiler that actually generates an executable binary. Usually, rustc uses LLVM for code generation… - rustc-dev-guide
Code Generation
23
Code generation (or "codegen") is the part of the compiler that actually generates an executable binary. Usually, rustc uses LLVM for code generation… - rustc-dev-guide
Target / Binary
24
Callbacks
25
rustc_driver module
26
Callbacks
27
CodegenBackend
28
CodegenBackend
29
Building rustc
30
31
Questions?
https://runtimeverification.com/
@rv_inc
https://discord.com/invite/CurfmXNtbN
contact@runtimeverification.com
32
Lexing takes strings and turns them into streams of tokens. … This is implemented in rustc_lexer. - rustc-dev-guide
33
fn main() {
println!("Hello, world!");
}
Lexing takes strings and turns them into streams of tokens. … This is implemented in rustc_lexer. - rustc-dev-guide
fn | main | ( | ) |
{ | println! | “ | Hello, world! |
” | ) | ; | } |
34
Parsing takes streams of tokens and turns them into a structured form which is easier for the compiler to work with, usually called an Abstract Syntax Tree (AST) . - rustc-dev-guide
35
Parsing takes streams of tokens and turns them into a structured form which is easier for the compiler to work with, usually called an Abstract Syntax Tree (AST) . - rustc-dev-guide
fn | main | ( | ) |
{ | println! | “ | Hello, world! |
” | ) | ; | } |
pub struct Stmt {
pub id: NodeId,
pub kind: StmtKind,
pub span: Span,
}
36
Given a raw source code for a crate, the compiler will produce a massive AST with all macros expanded, all modules inlined, etc. - rustc-dev-guide
37
Given a raw source code for a crate, the compiler will produce a massive AST with all macros expanded, all modules inlined, etc. - rustc-dev-guide
println!("Hello, world!");
std::io::_print(
std::format_args_nl!("Hello, world!")
);
38
The name resolution in Rust is a two-phase process. In the first phase, which runs during macro expansion, we build a tree of modules and resolve imports. Macro expansion and name resolution communicate with each other via the ResolverAstLoweringExt trait.
The input to the second phase is the syntax tree, produced by parsing input files and expanding macros. This phase produces links from all the names in the source to relevant places where the name was introduced. It also generates helpful error messages, like typo suggestions, traits to import or lints about unused items.
39
The AST lowering step converts AST to HIR. This means many structures are removed if they are irrelevant for type analysis or similar syntax agnostic analyses. - rustc-dev-guide
40
The AST lowering step converts AST to HIR. This means many structures are removed if they are irrelevant for type analysis or similar syntax agnostic analyses. - rustc-dev-guide
for _i in 0..10 { continue; }
match #[lang = "into_iter"](#[lang = "Range"]
{ start: 0, end: 10 }
mut iter => loop {
match #[lang = "next"](&mut iter) {
#[lang = "None"] {} => break,
#[lang = "Some"] { 0: _i } => { continue; }
}
}
};
41
Type inference is the process of automatic detection of the type of an expression. It is what allows Rust to work with fewer or no type annotations, making things easier for users… - rustc-dev-guide
42
Type inference is the process of automatic detection of the type of an expression. It is what allows Rust to work with fewer or no type annotations, making things easier for users… - rustc-dev-guide
fn main() {
let mut things = vec![]; // things: Vec<&str>
things.push("thing");
}
43
TODO - rustc-dev-guide
44
Type "collection" is the process of converting the types found in the HIR (hir::Ty), which represent the syntactic things that the user wrote, into the internal representation used by the compiler (Ty<'tcx>) - rustc-dev-guide
The process of verifying and enforcing the constraints of types - Wikipedia (Type Checking)