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.23k stars 1.47k forks source link

Compiler fails when reading a float64 from a stringstream in a const context #23468

Open tsoj opened 3 months ago

tsoj commented 3 months ago

Description

The compiler throws an error when trying to run the following code with nim r main.nim:

import std/streams

const hello = block:
  let strm = newStringStream("12345678")
  strm.readFloat64

echo hello

Nim Version

Nim Compiler Version 2.1.1 [Linux: amd64] Compiled at 2024-03-30 Copyright (c) 2006-2024 by Andreas Rumpf

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

Current Output

Hint: used config file '/home/tsoj/.choosenim/toolchains/nim-#devel/config/nim.cfg' [Conf]
Hint: used config file '/home/tsoj/.choosenim/toolchains/nim-#devel/config/config.nims' [Conf]
......................................................................fatal.nim(53)            sysFatal
Error: unhandled exception: field 'sym' is not accessible for type 'TNode' using 'kind = nkNilLit' [FieldDefect]

Expected Output

6.821320051701325e-38

Possible Solution

No response

Additional Information

Replacing the const with a let works, i.e. this compiles:

import std/streams

let hello = block:
  let strm = newStringStream("12345678")
  strm.readFloat64

echo hello

What also works is reading a char:

import std/streams

const hello = block:
  let strm = newStringStream("12345678")
  strm.readChar

echo hello
ringabout commented 3 months ago

!nim c

import std/streams

const hello = block:
  let strm = newStringStream("12345678")
  strm.readFloat64

echo hello
github-actions[bot] commented 3 months ago
:penguin: Linux bisect by @ringabout (member)
devel :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim fatal.nim(53) sysFatal Error: unhandled exception: field 'sym' is not accessible for type 'TNode' using 'kind = nkNilLit' [FieldDefect] ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-04-02T12:05:45
  • Finished 2024-04-02T12:05:45
  • Duration

AST

```nim nnkStmtList.newTree( nnkImportStmt.newTree( nnkInfix.newTree( newIdentNode("/"), newIdentNode("std"), newIdentNode("streams") ) ), nnkConstSection.newTree( nnkConstDef.newTree( newIdentNode("hello"), newEmptyNode(), nnkBlockStmt.newTree( newEmptyNode(), nnkStmtList.newTree( nnkLetSection.newTree( nnkIdentDefs.newTree( newIdentNode("strm"), newEmptyNode(), nnkCall.newTree( newIdentNode("newStringStream"), newLit("12345678") ) ) ), nnkDotExpr.newTree( newIdentNode("strm"), newIdentNode("readFloat64") ) ) ) ) ), nnkCommand.newTree( newIdentNode("echo"), newIdentNode("hello") ) ) ```
stable :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim fatal.nim(53) sysFatal Error: unhandled exception: field 'sym' is not accessible for type 'TNode' using 'kind = nkNilLit' [FieldDefect] ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-04-02T12:05:45
  • Finished 2024-04-02T12:05:46
  • Duration

AST

```nim nnkStmtList.newTree( nnkImportStmt.newTree( nnkInfix.newTree( newIdentNode("/"), newIdentNode("std"), newIdentNode("streams") ) ), nnkConstSection.newTree( nnkConstDef.newTree( newIdentNode("hello"), newEmptyNode(), nnkBlockStmt.newTree( newEmptyNode(), nnkStmtList.newTree( nnkLetSection.newTree( nnkIdentDefs.newTree( newIdentNode("strm"), newEmptyNode(), nnkCall.newTree( newIdentNode("newStringStream"), newLit("12345678") ) ) ), nnkDotExpr.newTree( newIdentNode("strm"), newIdentNode("readFloat64") ) ) ) ) ), nnkCommand.newTree( newIdentNode("echo"), newIdentNode("hello") ) ) ```
2.0.2 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim fatal.nim(53) sysFatal Error: unhandled exception: field 'sym' is not accessible for type 'TNode' using 'kind = nkNilLit' [FieldDefect] ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-04-02T12:05:46
  • Finished 2024-04-02T12:05:46
  • Duration

AST

```nim nnkStmtList.newTree( nnkImportStmt.newTree( nnkInfix.newTree( newIdentNode("/"), newIdentNode("std"), newIdentNode("streams") ) ), nnkConstSection.newTree( nnkConstDef.newTree( newIdentNode("hello"), newEmptyNode(), nnkBlockStmt.newTree( newEmptyNode(), nnkStmtList.newTree( nnkLetSection.newTree( nnkIdentDefs.newTree( newIdentNode("strm"), newEmptyNode(), nnkCall.newTree( newIdentNode("newStringStream"), newLit("12345678") ) ) ), nnkDotExpr.newTree( newIdentNode("strm"), newIdentNode("readFloat64") ) ) ) ) ), nnkCommand.newTree( newIdentNode("echo"), newIdentNode("hello") ) ) ```
1.6.14 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim fatal.nim(54) sysFatal Error: unhandled exception: field 'sym' is not accessible for type 'TNode' using 'kind = nkNilLit' [FieldDefect] ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-04-02T12:05:51
  • Finished 2024-04-02T12:05:51
  • Duration

AST

```nim nnkStmtList.newTree( nnkImportStmt.newTree( nnkInfix.newTree( newIdentNode("/"), newIdentNode("std"), newIdentNode("streams") ) ), nnkConstSection.newTree( nnkConstDef.newTree( newIdentNode("hello"), newEmptyNode(), nnkBlockStmt.newTree( newEmptyNode(), nnkStmtList.newTree( nnkLetSection.newTree( nnkIdentDefs.newTree( newIdentNode("strm"), newEmptyNode(), nnkCall.newTree( newIdentNode("newStringStream"), newLit("12345678") ) ) ), nnkDotExpr.newTree( newIdentNode("strm"), newIdentNode("readFloat64") ) ) ) ) ), nnkCommand.newTree( newIdentNode("echo"), newIdentNode("hello") ) ) ```
1.4.8 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim fatal.nim(49) sysFatal Error: unhandled exception: 'sym' is not accessible using discriminant 'kind' of type 'TNode' [FieldDefect] ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-04-02T12:05:53
  • Finished 2024-04-02T12:05:54
  • Duration

AST

```nim nnkStmtList.newTree( nnkImportStmt.newTree( nnkInfix.newTree( newIdentNode("/"), newIdentNode("std"), newIdentNode("streams") ) ), nnkConstSection.newTree( nnkConstDef.newTree( newIdentNode("hello"), newEmptyNode(), nnkBlockStmt.newTree( newEmptyNode(), nnkStmtList.newTree( nnkLetSection.newTree( nnkIdentDefs.newTree( newIdentNode("strm"), newEmptyNode(), nnkCall.newTree( newIdentNode("newStringStream"), newLit("12345678") ) ) ), nnkDotExpr.newTree( newIdentNode("strm"), newIdentNode("readFloat64") ) ) ) ) ), nnkCommand.newTree( newIdentNode("echo"), newIdentNode("hello") ) ) ```
1.2.18 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim /home/runner/.choosenim/toolchains/nim-1.2.18/lib/system/memory.nim(13, 5) Error: cannot 'importc' variable at compile time; c_memcpy ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-04-02T12:05:56
  • Finished 2024-04-02T12:05:56
  • Duration

AST

```nim nnkStmtList.newTree( nnkImportStmt.newTree( nnkInfix.newTree( newIdentNode("/"), newIdentNode("std"), newIdentNode("streams") ) ), nnkConstSection.newTree( nnkConstDef.newTree( newIdentNode("hello"), newEmptyNode(), nnkBlockStmt.newTree( newEmptyNode(), nnkStmtList.newTree( nnkLetSection.newTree( nnkIdentDefs.newTree( newIdentNode("strm"), newEmptyNode(), nnkCall.newTree( newIdentNode("newStringStream"), newLit("12345678") ) ) ), nnkDotExpr.newTree( newIdentNode("strm"), newIdentNode("readFloat64") ) ) ) ) ), nnkCommand.newTree( newIdentNode("echo"), newIdentNode("hello") ) ) ```
1.0.10 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim stack trace: (most recent call last) /home/runner/work/Nim/Nim/temp.nim(4, 7) /home/runner/.choosenim/toolchains/nim-1.0.10/lib/pure/streams.nim(805, 7) readFloat64 /home/runner/.choosenim/toolchains/nim-1.0.10/lib/pure/streams.nim(368, 14) read /home/runner/.choosenim/toolchains/nim-1.0.10/lib/pure/streams.nim(225, 26) readData /home/runner/.choosenim/toolchains/nim-1.0.10/lib/pure/streams.nim(1046, 27) ssReadData /home/runner/.choosenim/toolchains/nim-1.0.10/lib/pure/streams.nim(1046, 27) Error: limited VM support for 'addr' ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-04-02T12:05:59
  • Finished 2024-04-02T12:05:59
  • Duration

AST

```nim nnkStmtList.newTree( nnkImportStmt.newTree( nnkInfix.newTree( newIdentNode("/"), newIdentNode("std"), newIdentNode("streams") ) ), nnkConstSection.newTree( nnkConstDef.newTree( newIdentNode("hello"), newEmptyNode(), nnkBlockStmt.newTree( newEmptyNode(), nnkStmtList.newTree( nnkLetSection.newTree( nnkIdentDefs.newTree( newIdentNode("strm"), newEmptyNode(), nnkCall.newTree( newIdentNode("newStringStream"), newLit("12345678") ) ) ), nnkDotExpr.newTree( newIdentNode("strm"), newIdentNode("readFloat64") ) ) ) ) ), nnkCommand.newTree( newIdentNode("echo"), newIdentNode("hello") ) ) ```
Stats
  • GCC 11.4.0
  • Clang 14.0.0
  • NodeJS 19.1
  • Created 2024-04-02T12:05:10Z
  • Comments 1
  • Commands nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

:robot: Bug found in 18 minutes bisecting 7 commits at 0 commits per second

ringabout commented 3 months ago

It doesn't seem that readFloat64 supports VM or JS

tsoj commented 2 months ago

Likely related, the following example also doesn't work:

import std/[streams]

static:
  var strm = newStringStream()
  strm.write ""

Output:

Hint: used config file '/home/tsoj/.choosenim/toolchains/nim-#devel/config/nim.cfg' [Conf]
Hint: used config file '/home/tsoj/.choosenim/toolchains/nim-#devel/config/config.nims' [Conf]
......................................................................fatal.nim(53)            sysFatal
Error: unhandled exception: field 'sym' is not accessible for type 'TNode' using 'kind = nkNilLit' [FieldDefect]
litlighilit commented 2 months ago

strm.write ""

VM was tended to support write but in fact doesn't.

write invokes writeData whenvm, which invokes writeDataImpl field.

But that field was skipped to initialized ^impl, thus nil.