Skip to content

Segfault with lambda expression as default parameter #26021

Description

@Victorious3

Nim Version

Nim Compiler Version 2.3.1 [MacOSX: arm64]
Compiled at 2026-07-16
Copyright (c) 2006-2026 by Andreas Rumpf

git hash: a6fa322
active boot switches: -d:release

Description

import std/sugar

proc foo(a: int) =
  echo "Test"

proc outer() =
  let a = 5
  proc test(p: proc() = () => foo(a)) =
    p()

This example fails specifically when there is a capture from the enclosing proc.

Current Output

vic@Mac ircdv2 % nim c test.nim
Hint: used config file '/Users/vic/.choosenim/toolchains/nim-#devel/config/nim.cfg' [Conf]
Hint: used config file '/Users/vic/.choosenim/toolchains/nim-#devel/config/config.nims' [Conf]
Hint: used config file '/Users/vic/Documents/Programming/ircdv2/config.nims' [Conf]
..........................................................................SIGSEGV: Illegal storage access. (Attempt to read from nil?)
zsh: segmentation fault  nim c test.nim

Expected Output

Compilation succeeds

Known Workarounds

It is possible to turn the lambda expression into a nested proc, that seems to work fine:

proc foo(a: int) =
  echo "Test"

proc outer() =
  let a = 5
  proc default() = foo(a)

  proc test(p: proc() = default) =
    p()

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions