Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Map function with a variable under walkDirRec caused a compile error

Open hokamoto opened this issue 6 years ago • 3 comments

Using a map function with a variable under walkDirRec iterator caused a compile error.

Example

The below snippet itself does not make sense because I made up it for reproducing the problem with minimum code. I came across the problem during coding a real application.

import os, sequtils, sequtils, sugar

for file in walkDirRec("./"):
  let multiplier = 5
  echo(toSeq(1..4).map(x => x * multiplier))

Current Output

Error: execution of an external compiler program 'clang -c  -w  -I/Users/hokamoto/.choosenim/toolchains/nim-1.0.0/lib -I/Users/hokamoto/NimProjects/video_thumbnail -o /Users/hokamoto/.cache/nim/a_d/a.nim.c.o /Users/hokamoto/.cache/nim/a_d/a.nim.c' failed with exit code: 1

/Users/hokamoto/.cache/nim/a_d/a.nim.c:727:45: error: use of undeclared identifier 'multiplier__oOhn9b9auJ3LN3lvBg5aNedw_2'; did you mean 'multiplier__oOhn9b9auJ3LN3lvBg5aNedw_3'?
        TM__R8RUzYq41iOx0I9bZH5Nyrw_17 = mulInt(x, multiplier__oOhn9b9auJ3LN3lvBg5aNedw_2);
                                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                   multiplier__oOhn9b9auJ3LN3lvBg5aNedw_3
/Users/hokamoto/.cache/nim/a_d/a.nim.c:326:4: note: 'multiplier__oOhn9b9auJ3LN3lvBg5aNedw_3' declared here
NI multiplier__oOhn9b9auJ3LN3lvBg5aNedw_3;
   ^
1 error generated.

Expected Output

@[5, 10, 15, 20]

Possible Solution

I found the error did not occur when not using variable or not under walkDirRec.

Work (Not using a variable)

import os, sequtils, sequtils, sugar

for file in walkDirRec("./"):
  echo(toSeq(1..4).map(x => x * 5))

Work (Not under walkDirRec)

import os, sequtils, sequtils, sugar

let multiplier = 5
echo(toSeq(1..4).map(x => x * multiplier))

Additional Information

$ nim -v
Nim Compiler Version 1.0.0 [MacOSX: amd64]
Compiled at 2019-09-23
Copyright (c) 2006-2019 by Andreas Rumpf

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

hokamoto avatar Oct 07 '19 14:10 hokamoto

A similar example, but using mapIt, works:

import os, sequtils, sequtils, sugar

for file in walkDirRec("./"):
  let multiplier = 5
  echo(toSeq(1..4).mapIt(it * multiplier))

narimiran avatar Oct 07 '19 14:10 narimiran

Thanks. mapIt works for me! But do you know why map caused the compile error?

hokamoto avatar Oct 07 '19 14:10 hokamoto

This issue has not been fixed on v1.6.6.

% nim -v
Nim Compiler Version 1.6.6 [MacOSX: amd64]
Compiled at 2022-07-01
Copyright (c) 2006-2021 by Andreas Rumpf

active boot switches: -d:release

% nim c issue12375.nim
Hint: used config file '/Users/hokamoto/.choosenim/toolchains/nim-1.6.6/config/nim.cfg' [Conf]
Hint: used config file '/Users/hokamoto/.choosenim/toolchains/nim-1.6.6/config/config.nims' [Conf]
.......................................................................................
/Users/hokamoto/issue12375.nim(1, 22) Hint: duplicate import of 'sequtils'; previous import here: /Users/hokamoto/issue12375.nim(1, 12) [DuplicateModuleImport]
..
CC: stdlib_digitsutils.nim
CC: stdlib_assertions.nim
CC: stdlib_dollars.nim
CC: stdlib_io.nim
CC: stdlib_system.nim
CC: stdlib_pathnorm.nim
CC: stdlib_posix.nim
CC: stdlib_times.nim
CC: stdlib_os.nim
CC: issue12375.nim
/Users/hokamoto/.cache/nim/issue12375_d/@missue12375.nim.c:633:19: error: use of undeclared identifier 'multiplier__issue4950515553_6'; did you mean 'multiplier__issue4950515553_369'?
        if (nimMulInt(x, multiplier__issue4950515553_6, &TM__kIv9ayHDPjlWRZ8dPrg9c9cdQ_15)) { raiseOverflow(); };
                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                         multiplier__issue4950515553_369
/Users/hokamoto/.choosenim/toolchains/nim-1.6.6/lib/nimbase.h:595:63: note: expanded from macro 'nimMulInt'
    #define nimMulInt(a, b, res) __builtin_smulll_overflow(a, b, (long long int*)res)
                                                              ^
/Users/hokamoto/.cache/nim/issue12375_d/@missue12375.nim.c:315:18: note: 'multiplier__issue4950515553_369' declared here
N_LIB_PRIVATE NI multiplier__issue4950515553_369;
                 ^
1 error generated.
Error: execution of an external compiler program 'clang -c  -w -ferror-limit=3   -I/Users/hokamoto/.choosenim/toolchains/nim-1.6.6/lib -I/Users/hokamoto -o /Users/hokamoto/.cache/nim/issue12375_d/@missue12375.nim.c.o /Users/hokamoto/.cache/nim/issue12375_d/@missue12375.nim.c' failed with exit code: 1

hokamoto avatar Aug 08 '22 05:08 hokamoto