Skip to content

Commit

Permalink
fix a compiler crash with .varargs proc types (#1396)
Browse files Browse the repository at this point in the history
## Summary

Fix the compiler crashing when a non-closure `proc` type with C-style
varargs is used somewhere. Only the C backend was affected.

Fixes #1394.

## Details

* `mirtypes.callConv` didn't consider the `x` field storing a boolean
  value in the most-significant bit, resulting in range defect
* the integer value is now properly masked before converting it to a
  `TCallingConvention` enum value
  • Loading branch information
zerbina authored Aug 4, 2024
1 parent ec9b4ac commit ec713a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions compiler/mir/mirtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ const
MangleFlag = 0x4000'u16
NoAliasFlag = 0x8000'u16

VarargsFlag = 0x8000_0000'u32

func `==`*(a, b: FieldId): bool {.borrow, inline.}
func `==`(a, b: IntVal): bool {.borrow, inline.}

Expand Down Expand Up @@ -359,10 +361,11 @@ func numParams*(desc: TypeHeader): int =
int(desc.b - desc.a) - 1

func callConv*(desc: TypeHeader, env: TypeEnv): TCallingConvention =
TCallingConvention env.params[desc.a].x
# mask away the varargs flag
TCallingConvention(env.params[desc.a].x and not(VarargsFlag))

func hasVarargs*(desc: TypeHeader, env: TypeEnv): bool =
(env.params[desc.a].x and 0x8000_0000'u32) != 0
(env.params[desc.a].x and VarargsFlag) != 0

func retType*(desc: TypeHeader, env: TypeEnv): TypeId =
assert desc.kind in {tkProc, tkClosure}
Expand Down
10 changes: 10 additions & 0 deletions tests/ccgbugs/tvarargs_proc_type.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
discard """
description: '''
Regression test for a bug with querying the calling convention of a MIR
type. Derived from https://github.com/nim-works/nimskull/issues/1394.
'''
"""

# calling convention doesn't matter, as long as the proc type is not a closure
# type
var p: proc(x: int) {.nimcall, varargs.}

0 comments on commit ec713a1

Please sign in to comment.