Roof operator on string in template crashes compiler (Error: unhandled exception: sons is not accessible [FieldError]) #3545

Closed
singularperturbation opened this Issue Nov 14, 2015 · 3 comments

Projects

None yet

3 participants

@singularperturbation
Contributor

Causes issue 70 (nim-lang/nimforum#70) in nim-lang/nimforum.
When you have a string as the result of an if statement in a template, the "roof" operator doesn't work correctly; based on following the stack trace it looks like it's trying to get openarray length and then crashes b/c the string is not an openarray / does not have sons.

Sample code

#test_roof_constexpr.nim
# This crashes the compiler, but not if doTest is a proc
template doTest(a: string,c: int) =
  var b =
    if c mod 2 == 0: a[^5 .. ^1]
    else: "SOME CONSTANT STRING"
  echo b

when isMainModule:
  doTest("THIS IS A TEST",5)

Compilation message

singularperturbation@localhost ~/nim> nim c test_roof_constexpr.nim
Hint: system [Processing]
Hint: test_roof_constexpr [Processing]
Traceback (most recent call last)
nim.nim(107)             nim
nim.nim(71)              handleCmdLine
main.nim(251)            mainCommand
main.nim(63)             commandCompileToC
modules.nim(221)         compileProject
modules.nim(169)         compileModule
passes.nim(203)          processModule
passes.nim(137)          processTopLevelStmt
sem.nim(460)             myProcess
sem.nim(446)             semStmtAndGenerateGenerics
transf.nim(886)          transformStmt
transf.nim(815)          processTransf
transf.nim(801)          transform
transf.nim(106)          transformSons
transf.nim(788)          transform
transf.nim(106)          transformSons
transf.nim(795)          transform
transf.nim(106)          transformSons
transf.nim(801)          transform
transf.nim(106)          transformSons
transf.nim(801)          transform
transf.nim(106)          transformSons
transf.nim(760)          transform
transf.nim(641)          transformCall
transf.nim(106)          transformSons
transf.nim(760)          transform
transf.nim(641)          transformCall
transf.nim(106)          transformSons
transf.nim(760)          transform
transf.nim(641)          transformCall
transf.nim(106)          transformSons
transf.nim(804)          transform
semfold.nim(693)         getConstExpr
semfold.nim(507)         magicCall
semfold.nim(295)         evalOp
ast.nim(1251)            sonsLen
system.nim(2533)         sysFatal
Error: unhandled exception: sons is not accessible [FieldError]
singularperturbation@localhost ~/nim>
@dom96
Member
dom96 commented Nov 15, 2015

Compiles fine for me, are you using devel?

@singularperturbation
Contributor

Very strange - I'm using the devel version of the compiler on amd64 linux.

singularperturbation@localhost ~/Nim> nim --version
Nim Compiler Version 0.12.1 (2015-11-13) [Linux: amd64]
Copyright (c) 2006-2015 by Andreas Rumpf

git hash: 0f7fdd8bf4d4f747f01c4d95ce64d96763286afc
active boot switches:
singularperturbation@localhost ~/Nim> uname -a
Linux localhost 3.10.18 #1 SMP Thu Oct 15 02:16:25 PDT 2015 x86_64 x86_64 x86_64 GNU/Linux
singularperturbation@localhost ~/Nim> 

As of writing, I think this has the latest commit from nim-lang/Nim

@singularperturbation
Contributor

Debugging info from adding a renderTree to transform proc in transf.nim for the test file:
http://pastebin.com/wezLAjUP

Compiler does not give above error if built in release mode (for me).

@dom96 dom96 added the Semcheck label Jan 16, 2016
@Araq Araq added the Crash label May 31, 2016
@Araq Araq added a commit that closed this issue Jul 15, 2016
@Araq Araq fixes #3545 ab3c684
@Araq Araq closed this in ab3c684 Jul 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment