nim-works / nimskull

An in development statically typed systems programming language; with sustainability at its core. We, the community of users, maintain it.
https://nim-works.github.io/nimskull/index.html
Other
280 stars 39 forks source link

fix compiler crash when converting `nil` to `distinct pointer` #1404

Closed zerbina closed 3 months ago

zerbina commented 3 months ago

Summary

Expressions such as A(pointer(nil)) crashed the compiler when A is a distinct pointer type. This is now fixed.

Details

Said expression reached cgirgen as (LvalueConv (Type "A") (NilLit)), and since NilLit is not expected as the operand of an lvalue conversion, the compiler crashed. distinct ptr types weren't affected, due to semfold unconditionally folding ptr conversions away. A(nil) was not affected due to transf folding the expression into just a nil literal as part of its tyNil fixup.

In order to make pointer conversion behaviour consistent, semfold now folds all nil-to-pointer-like conversions into a properly typed nil literal. The tyNil fixup logic in transf thus becomes obsolete, and is removed.

Beyond fixing the crash, this also means that pointer(nil) is now detected as a constant expression without having to evaluate it with the VM.

saem commented 3 months ago

/merge

github-actions[bot] commented 3 months ago

Merge requested by: @saem

Contents after the first section break of the PR description has been removed and preserved below: