nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.57k stars 1.47k forks source link

Compiler segfaults on lambda sugar (nim 1.6.16) #22820

Open mynameisdaniil opened 1 year ago

mynameisdaniil commented 1 year ago

Description

I've been trying to use some functional style programming to access type lifted into Option.

import sugar

p.localNode.address.map((a) => $a.port).get("0")
# where
address: Option[Address]

type Address* = object
  ip*: ValidIpAddress
  port*: Port

If you re-arrange this code differently i.e. move map((a) => $a.port).get("0") part into separate proc this issue goes away. Which makes me think it is somehow connected to type inference.

To replicate this:

git clone https://github.com/status-im/nimbus-eth1.git
cd nimbus-eth1
git checkout compiler-segfault
# Optional, select version of nim compiler to compile with
# This one for 1.6.16
export NIM_COMMIT=3c9b68dc157804885b14a1984efc25e8b7cc861d

source env.sh
make fluffy -j4

Nim Version

Nim Compiler Version 1.6.16 [MacOSX: arm64] Compiled at 2023-10-12 Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 3c9b68dc157804885b14a1984efc25e8b7cc861d active boot switches: -d:release

Current Output

SIGSEGV: Illegal storage access. (Attempt to read from nil?)
<...>/nimbus-eth1/vendor/nimbus-build-system/scripts/env.sh: line 85: 81689 Segmentation fault: 11  "$@"

Expected Output

No response

Possible Solution

No response

Additional Information

No response

juancarlospaco commented 1 year ago

Can you provide a reduced assertive source code repro sample ?.

mynameisdaniil commented 1 year ago

@juancarlospaco Sorry, I was trying to make minimal working example, but was unable to. But I can assist you with the codebase where this bug is reproducible.

tersec commented 5 months ago

Repro:

protocol.nim:

{.push raises: [].}

import
  "."/[node]

import
  std/[options, sugar]

var closestNodes: seq[Node]
for cn in closestNodes:
  discard cn.address.map(a => a.port)

node.nim

import std/options

type
  Address* = object
    port*: int

  Node* = ref object
    address*: Option[Address]

On 1.6 (any version I tried, up to current version-1-6 from branch):

SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Segmentation fault (core dumped)

2.0 and devel don't seem to have this problem.

Example Nim versions:

SIGSEGV upon compilation:

Nim Compiler Version 1.6.21 [Linux: amd64]
Compiled at 2024-05-27
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: 38640664088251bbc88917b4bacfd86ec53014b8
active boot switches: -d:release

No SIGSEGV:

Nim Compiler Version 2.0.5 [Linux: amd64]
Compiled at 2024-05-27
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: ab053fd56a6517531441cdbf0be5536189831c9c
active boot switches: -d:release
Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2024-05-27
Copyright (c) 2006-2024 by Andreas Rumpf

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