Open dxxb opened 1 year ago
The following will also produce the same result.
iterator iterSome(): int =
let something = 6
proc inner1() =
let othersomething = something
inner1()
for n in 0 .. 10:
inner1()
yield n
proc test() =
proc test1() =
for v in iterSome():
discard
test1()
test()
Is there anything I can do to contribute to a solution for this issue? I do not know enough about Nim's compiler internals so I'd need some pointers.
Still happens with:
$ nim -v
Nim Compiler Version 1.9.5 [MacOSX: amd64]
Compiled at 2023-07-08
Copyright (c) 2006-2023 by Andreas Rumpf
git hash: 6ec10a4c9182c513fecd17fde4a81a3a007bb2e9
active boot switches: -d:release
Look into compiler/lambdalifting.nim
!nim c
iterator iterSome(): int =
proc inner1() =
let something = 6
proc inner2() =
let othersomething = something
inner2()
for n in 0 .. 1:
inner1()
yield n
proc test() =
proc test1() =
for v in iterSome(): discard
test1()
test()
@juancarlospaco (contributor)devel :-1: FAIL
Output
Error: Command failed: nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings: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/work/Nim/Nim/temp.nim(4, 10) Error: internal error: inconsistent environment type
2023-07-09T14:12:16Z
2023-07-09T14:12:48
2023-07-09T14:12:48
now
nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
Error: Command failed: nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings: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/work/Nim/Nim/temp.nim(4, 10) Error: internal error: inconsistent environment type
2023-07-09T14:12:16Z
2023-07-09T14:12:49
2023-07-09T14:12:49
now
nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
Error: Command failed: nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings: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/work/Nim/Nim/temp.nim(4, 10) Error: internal error: inconsistent environment type
2023-07-09T14:12:16Z
2023-07-09T14:12:52
2023-07-09T14:12:53
now
nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
Error: Command failed: nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings: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/work/Nim/Nim/temp.nim(4, 10) Error: internal error: inconsistent environment type
2023-07-09T14:12:16Z
2023-07-09T14:12:55
2023-07-09T14:12:56
now
nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
Error: Command failed: nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings: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/work/Nim/Nim/temp.nim(4, 10) Error: internal error: inconsistent environment type
2023-07-09T14:12:16Z
2023-07-09T14:13:11
2023-07-09T14:13:11
now
nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
Error: Command failed: nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings: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/work/Nim/Nim/temp.nim(4, 10) Error: internal error: inconsistent environment type
2023-07-09T14:12:16Z
2023-07-09T14:13:24
2023-07-09T14:13:24
now
nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
39 minutes
bisecting 6
commits at 0
commits per second. Thank you @Araq. I guess I should have been more specific when I asked for pointers. I had found where the error came from (addClosureParam()
in compiler/lambdalifting.nim
) and figured the case of an inner function inside an iterator is not supported by the current implementation for some reason.
I see the environment for inner1()
is an :envP@...
and the environement for the iterator is iterSome@...
.
If I comment out the localError()
call from addClosureParam() compilation succeeds and the resulting executable runs OK, but surely the solution cannot be that easy: localError()
must have been there for a reason.
but surely the solution cannot be that easy
It can be that easy. However! We need to investigate why
var cp = getEnvParam(fn)
let owner = if fn.kind == skIterator: fn else: fn.skipGenericOwner
let t = c.getEnvTypeForOwner(owner, info)
cp.typ
and t
might differ and if they do, which one to pick. Ideally there is only one type that should be used, always, consistently. But I don't remember the code well enough to say for sure.
FYI at the point where the error is emitted inner1()
fn.kind == skProc
cp.typ
:
kind: tyRef, callConv: nimcall, flags: {tfHasAsgn, tfCheckedForDestructor},
sons: @[
(kind: tyObject, callConv: nimcall, flags: {tfCheckedForDestructor},
sons: @[(kind: tyObject, callConv: nimcall, flags: {tfInheritable},
sons: @[nil],
owner: system@33554432, sym: RootObj@33555817, loc: ..., typeInst: nil, uniqueId: (module: 2, item: 768))],
owner: iterSome@1056964609, sym: Env_innerprociter.nim_iterSome@1056964622, loc: ..., typeInst: nil, uniqueId: (module: 63, item: 19))],
owner: iterSome@1056964609, sym: nil, loc: ..., typeInst: nil, uniqueId: (module: 63, item: 18)
t
:
kind: tyRef, callConv: nimcall, flags: {},
sons: @[
(kind: tyObject, callConv: nimcall, flags: {},
sons: @[(kind: tyObject, callConv: nimcall, flags: {tfInheritable},
sons: @[nil],
owner: system@33554432, sym: RootObj@33555817, loc: ..., typeInst: nil, uniqueId: (module: 2, item: 768))],
owner: iterSome@1056964609, sym: Env_innerprociter.nim_iterSome@1056964717, loc: ..., typeInst: nil, uniqueId: (module: 63, item: 68))],
owner: iterSome@1056964609, sym: nil, loc: ..., typeInst: nil, uniqueId: (module: 63, item: 67)
Different TType
s same owner. Is t
is supposed to be different in this case? The presence of cp.typ != t and fn.kind != skIterator
suggests the asnwer is no. If so does the problem originate wherever the type of the environment for inner1()
is decided/assigned?
If so does the problem originate wherever the type of the environment for inner1() is decided/assigned?
Seems like it.
Digging a bit more I found the following:
-> liftLambdas() on test()
...
-> liftLambdas() on iterSome()
...
addClosureParam() on inner1() skProc, cp is `nil` so a new cp is created and stored in the lookup table
...
<- liftLambdas() on iterSome()
...
addClosureParam() on inner1() skProc, cp is not `nil` but when looking uit up by ID the value is different.
`Error: internal error: inconsistent environment type`
Note that liftLambdas()
is called recursively via transformBody()
and upon entering liftLambdas()
a new DetectionPass variable is initialized (so the env type lookup table used by the inner liftLambdas()
is not the same as the outer one).
Traceback (most recent call last)
Nim/compiler/nim.nim(164) nim
Nim/compiler/nim.nim(119) handleCmdLine
Nim/compiler/main.nim(307) mainCommand
Nim/compiler/main.nim(276) compileToBackend
Nim/compiler/main.nim(138) commandCompileToC
Nim/compiler/pipelines.nim(294) compilePipelineProject
Nim/compiler/pipelines.nim(224) compilePipelineModule
Nim/compiler/pipelines.nim(172) processPipelineModule
Nim/compiler/pipelines.nim(24) processPipeline
Nim/compiler/cgen.nim(2097) genTopLevelStmt
Nim/compiler/cgen.nim(1136) genProcBody
Nim/compiler/ccgstmts.nim(1650) genStmts
Nim/compiler/ccgexprs.nim(3094) expr
Nim/compiler/ccgexprs.nim(2788) genStmtList
Nim/compiler/ccgstmts.nim(1650) genStmts
Nim/compiler/ccgexprs.nim(3047) expr
Nim/compiler/ccgcalls.nim genCall
Nim/compiler/ccgcalls.nim(832) genAsgnCall
Nim/compiler/ccgcalls.nim(425) genPrefixCall
Nim/compiler/cgen.nim(740) initLocExpr
Nim/compiler/ccgexprs.nim(2966) expr
Nim/compiler/cgen.nim(1369) genProc
Nim/compiler/cgen.nim(1343) genProcNoForward
Nim/compiler/cgen.nim(1155) genProcAux
Nim/compiler/transf.nim(1197) transformBody
Nim/compiler/lambdalifting.nim(893) liftLambdas
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(458) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(527) detectCapturedVars
Nim/compiler/lambdalifting.nim(450) detectCapturedVars
Nim/compiler/lambdalifting.nim(439) addClosureParam
Nim/compiler/msgs.nim(581) liMessage
Nim/compiler/msgs.nim(440) handleError
Nim/compiler/msgs.nim(421) quit
I was hoping https://github.com/nim-lang/Nim/pull/24316 would solve this one too but it does not. I tried tracking this bug down a while ago but I could not. @yglukhov, any chance you could spot what is wrong here? Thank you.
Description
The following code will trigger the issue.
Nim Version
and
Current Output
Expected Output
Possible Solution
No response
Additional Information
I believe I encountered this issue a few times in the last month or so but only found time now to distill a minimal test case to reproduce it. In this instance the issue appeared when using
npeg
(https://github.com/zevv/npeg) in an iterator like so:I could not find an existing issue matching what I am seeing.