# 2022-01-06
## Today's plan: Pernosco!
[Pernosco link](https://is.gd/PVheub)
[Link at visit_item call](https://is.gd/OTxgnr)
Example program:
```rust
fn f() {
g(); // Since there are no local variables in scope yet, this resolves to the item.
let g = || {}; // "the local variable"
fn g() {} // "the item"
g(); // This resolves to the local variable `g` since it shadows the item.
}
fn main() {}
```
Command line:
```bash
> RUSTC_LOG=rustc_resolve rustc +rust-0-stage1 ~/tmp/example.rs
```
Compiler `config.toml`:
```shell=
# Includes one of the default files in src/bootstrap/defaults
profile = "compiler"
changelog-seen = 2
[rust]
debug = true
debuginfo-level-rustc = 2
```
### AST for the example
* fn f -- https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/index.html
* block
* list of statements -- [statementkind](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/enum.StatementKind.html)
* statementkind::expr -- `g()`
* statementkind::local -- `let g = `
* statementkind::item -- g
* statementkind::expr -- g()
## Pattern for this time
* Pernosco walk through: 15 minutes
* Read through the `visit_item` of `f`, which resolves the body
* Pass 1: Examine first `g()`
*
* Pass 2: Examine second `g()`
* 10 minutes reading
* 5 minutes **take notes**
* 20 minutes of chat
## Pernosco notes
* See the log in upper left
* Go to log and hit Ctrl-F and then type "resolving item"
* "Occurrences of regex" window will pop up, you can jump to points in the log
* This will find the point that the body of each item is resolved
* Go over to the stack, scroll to the bottom, and then scroll back UP until you see some code like `resolve_item`
* You should see:
```
fn resolve_item(&mut self, item: &'ast Item) {
let name = item.ident.name;
debug!("(resolving item) resolving {} ({:?})", name, item.kind);
```
Other useful things:
* If you go to the "Toolbox" you can add views like...
* Local variables-- see the values of local variables
* Callees-- see which functions are called
## Note-taking
While you read, take notes however you prefer. You can use this hackmd but you can also use paper or your brain.
At the end, we will take 5 minutes to leave some notes (in a section tagged with your handle) **in this hackmd** for others to persue/read. Note that while I don't plan to publish these notes, the hackmd is public and reachable by anyone who knows the url, so be polite.
We'll read them over and then I'll highlight particular people to expand on what they wrote.
### Your name here
### nikomatsakis
* Links
* [visit_fn](https://is.gd/j3ZqJU) -- walking the function body
* place where `g` is resolved the first time: [link](https://pernos.co/debug/7LJwklruo9BcakpR9BmhaA/index.html#f{m[BNk,AeQ_,t[Ag,Ebpi_,f{e[BNk,AdI_,s{afv4NqlAA,bUQ,uEgi5aQ,oEgrIHw___,v[{wz00,v[{f'list',q'stdouterr',p{_,xAS4y_,{f'container',q'stack',p{_,xAYag___,{wt1I,v[{f'source',q'source',p{_,xAUZU_,{f'list',q'callees',p{_,xa/s_,{f'container',q'locals',p{_,x2Lo____/)
* Observation
* a great place to start is to find the logs
Comparing first and second call to `resolve_path`:
* Second call terminates [here](https://pernos.co/debug/7LJwklruo9BcakpR9BmhaA/index.html#f{m[BQc,Akc_,t[Ag,Ebpi_,f{e[BQc,Af0_,s{afv4NqlAA,bUQ,uEgi5aQ,oEgjZSA___,v[{wz00,v[{f'list',q'stdouterr',p{_,xqmI_,{f'container',q'stack',p{_,x3D0_,{f'list',q'explainer',p{'explainFocus'{'data'{'producer'{'subrange'{'producer'{'memory'{'addressSpace'{'execs'Ag,'task'{'serial'AQ,'tid'Ebpb__,'ranges'[{'end'fv4ABP65,'start'fv4ABP6U____,'subrange'{'start'Aw,'end'BA___,'renderer''utf8'_,'frame'.,'moment'{'event'BQQ,'instr'AQ_,'node'AQ,'tuid'{'serial'Ag,'tid'Ebpi__,'focusSelector''output'_,xAYag___,{wt1I,v[{f'source',q'source',p{_,xw8w_,{f'list',q'callees',p{_,xQMk_,{f'container',q'locals',p{_,xggk____/)
* `resolve_ident_in_lexical_scope`
* eventually records resolution [here](https://pernos.co/debug/7LJwklruo9BcakpR9BmhaA/index.html#f{m[BQs,Afg_,t[Ag,Ebpi_,f{e[BQc,Af0_,s{afv4NqlAA,bUQ,uEgi5aQ,oEgjZSA___,v[{wz00,v[{f'list',q'stdouterr',p{_,xqmI_,{f'container',q'stack',p{_,x3D0_,{f'list',q'explainer',p{'explainFocus'{'data'{'producer'{'subrange'{'producer'{'memory'{'addressSpace'{'execs'Ag,'task'{'serial'AQ,'tid'Ebpb__,'ranges'[{'end'fv4ABP65,'start'fv4ABP6U____,'subrange'{'start'Aw,'end'BA___,'renderer''utf8'_,'frame'.,'moment'{'event'BQQ,'instr'AQ_,'node'AQ,'tuid'{'serial'Ag,'tid'Ebpi__,'focusSelector''output'_,xAYag___,{wt1I,v[{f'source',q'source',p{_,xw8w_,{f'list',q'callees',p{_,xQMk_,{f'container',q'locals',p{_,xggk____/)
* first call kind of does the same thing ([link](https://pernos.co/debug/7LJwklruo9BcakpR9BmhaA/index.html#f{m[BN8,BZw_,t[Ag,Ebpi_,f{e[BN0,Af0_,s{afv4NqlAA,bUQ,uEgi5aQ,oEgjZSA___,v[{wz00,v[{f'list',q'stdouterr',p{_,xqmI_,{f'container',q'stack',p{_,x3D0_,{f'list',q'explainer',p{'explainFocus'{'data'{'producer'{'subrange'{'producer'{'memory'{'addressSpace'{'execs'Ag,'task'{'serial'AQ,'tid'Ebpb__,'ranges'[{'end'fv4ABP65,'start'fv4ABP6U____,'subrange'{'start'BQ,'end'Bg___,'renderer''utf8'_,'frame'.,'moment'{'event'BNo,'instr'AQ_,'node'AQ,'tuid'{'serial'Ag,'tid'Ebpi__,'focusSelector''output'_,xAYag___,{wt1I,v[{f'source',q'source',p{_,xw8w_,{f'list',q'callees',p{_,xQMk_,{f'container',q'locals',p{_,xggk_,{f'list',q'file-search',p{'file''Stdouterr','regex''resolve_path.*ident:3A%20g'_,xAYag____/)), but it winds up returning an item
* so the difference is all in the way the ribs are setup
Pernosco dataflow view did not help me :)
[Ribs created here](https://pernos.co/debug/7LJwklruo9BcakpR9BmhaA/index.html#f{m[BPk,AcU_,t[Ag,Ebpi_,f{e[BPc,A0Y_,s{afv4NqlAA,bUQ,uEWBgHA,oEWCNHg___,v[{wz00,v[{f'list',q'stdouterr',p{_,xVTE_,{f'container',q'stack',p{_,xbh4___,{wt1I,v[{f'source',q'source',p{_,xw80_,{f'container',q'locals',p{_,xggk____/)
[Block map and anonymous module seemed interesting](https://is.gd/m8H83Q)
### Sean Chen
There's something akin to JavaScript hoisting going on in the example code.
The nested `fn g() {}` declaration is hoisted to the top of the function body. The first `g()` call
resolves to this hoisted nested function declaration. The `let g = || {}` closure definition then
shadows the `fn g()` declaration. The second `g()` call resolves then to the closure definition.
### doc
- Just need more time with the tool.
- I'm getting a bit of a feel by experimenting, but reaching the goal is unlikely.
- I'm not sure I even understand the goal.
- I need the remedial version of this session.
- My head hurts.
### metajack
[Link to walk](https://pernos.co/debug/7LJwklruo9BcakpR9BmhaA/index.html#f{m[BM8,BiQ_,t[Ag,Ebpi_,f{e[BM8,Bhs_,s{afv4NqlAA,bUQ,uEcZcaw,oEcoDXg___/)
- resolve_block()
- resolve_expr()
- ExprKind::Call
- smart_resolve_path
- why does smart_resolve_path happen? late.rs:2334 is next line executed? not highlighted (walk_expr)
- what does the parse tree look like?
- PathResult or a Binding. first g resolves to PathResult(PathResult::NonModule(partial_res))
- stuck again on line 2335
- the path is resolved by smart_resolve_path. expr is empty after that so nothing to do
- record_partial_res is where it records the answer
### selectiveduplicate
- `walk_item` function walks through `Item` nodes
- `Item` kinds: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.ItemKind.html
- calls the corresponding `visit` method (for example, for `ItemKind::ExternCrate`, call `visitor.visit_name`)
- `walk_list` macro is called (what does it do)
- Still need some getting-used-to with pernosco
- My goal was to understand what's going on inside `walk_item` and `resolve_item`.
## next time
### bit of a structure emerging...
* we will first read deeply into a function + an example
* record logs you are interested in
* then use pernosco to explore it in action
* come up with some question(s) that you want to see answered
* then these can ideally turn into questions in the code