My Nim Development Weekly Report (3/5)
Tags: nim
Published: 2023-03-06 02:54:38

## Progress

### chores

[remove nosinks hacks from compiler](https://github.com/nim-lang/Nim/pull/21469).

[test DLL generation with ORC](https://github.com/nim-lang/Nim/pull/21445).

[fixes sinkinference documentation, which has been disabled](https://github.com/nim-lang/Nim/pull/21470).

### implements wasMoved hook

[fixes #19291; implements wasMoved hook](https://github.com/nim-lang/Nim/pull/21303).

You can now register a `wasMoved` hook.

```nim
type
  Game = object

proc `=wasMoved`(x: var Game) {.error.}
```

### hash types based on custom paths

[fixes #20139; hash types based on custom paths](https://github.com/nim-lang/Nim/pull/21274).

Now the object type with the same name in the same file name but resides in different directories won't conflict with each other anymore. The PR takes the path of the type into consideration, which prevents conflicts.

### fixes top level variables inside loops are not reset in VM

[fixes #10938; fixes #13312; fixes #13918; fixes #20985; always initializes global variables with null values in VM](https://github.com/nim-lang/Nim/pull/21351).

```nim
static:
  for i in '1' .. '2':
    var s: set[char]
    doAssert s == {}
    incl(s, i)
```

The variable `s` wasn't initialized properly in the loop. It caused `s` to keep the old value after the first iteration. My solution is to initialize `s` each time the var section is executed, which is reset for every iteration.

### fixes type erasures for quoted variables

[fixes #21326; fixes #7375; fixes #11986; fixes #9607; rework quote do; getAst uses type info to annotate the type of quoted variables; no more type erasures for quoted variables](https://github.com/nim-lang/Nim/pull/21433).

```nim
import macros

macro foo(body: untyped): untyped =
  let a = body.lineInfoObj()
  result = quote do:
    echo `a`

foo:
  let c = 1
```

The VM is an untyped executor, which erases types of expressions after execution. In this case, `a` is evaluated in the VM, which loses its type, namely `LineInfo` after evaluation. So we should collect the type information and annotate the type of `a` when expanding `quote do`. I store the type information in the parameters of the dummy templates used by `quote do`, which is used to restore the type of quoted variables.

### replaces implicit passes array registered at runtime with explicit function calls; simplify compilation pipeline

[replaces implicit passes array registered at runtime with explicit function calls; simplify compilation pipeline](https://github.com/nim-lang/Nim/pull/21444).

```nim
proc processPipeline(graph: ModuleGraph; semNode: PNode; bModule: PPassContext): PNode =
  case graph.pipelinePass
  of CgenPass:
    result = semNode
    if bModule != nil:
      genTopLevelStmt(BModule(bModule), result)
  of JSgenPass:
    when not defined(leanCompiler):
      result = processJSCodeGen(bModule, semNode)
  of GenDependPass:
    result = addDotDependency(bModule, semNode)
  of SemPass:
    result = graph.emptyNode
  of Docgen2Pass, Docgen2TexPass:
    when not defined(leanCompiler):
      result = processNode(bModule, semNode)
  of Docgen2JsonPass:
    when not defined(leanCompiler):
      result = processNodeJson(bModule, semNode)
  of EvalPass, InterpreterPass:
    result = interpreterCode(bModule, semNode)
  of NonePass:
    doAssert false, "use setPipeLinePass to set a proper PipelinePass"
```

The `processPipeline` explicitly calls the code generator, which makes the code more clear. It is the second step towards to remove forward declaration.


## Weekly collection

https://forum.nim-lang.org/t/9908 (2/19)

https://forum.nim-lang.org/t/9940 (2/26)


## Participating in contributions

I have created [a matrix space](https://matrix.to/#/!TqycJdaMxeCOkccTqa:matrix.org?via=matrix.org) for contributions to the official Nim projects. If you are already a contributor of the official Nim projects, welcome to this space to discuss contributions. If you are willing to take up some quests for fun, I have prepared [a list](https://github.com/orgs/nim-lang/projects/8) for you, which contains a few contribution-friendly issues.

## Sponsorship

Many thanks to @Yepoleb, @lenis0012, @pietroppeter, @Clonkk, @mode80, @Phil, @CxPlanner, @shirleyquirk, @elcritch, @geotre, @thinkwelltwd, @xrfez, @enthus1ast for [sponsoring me on GitHub](https://github.com/sponsors/planety).