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

Exit with code 1 while compile simple file #5359

Closed SBBerkov closed 6 years ago

SBBerkov commented 7 years ago

Can't compile file netconfutils.nim with


import tables, parsexml, streams

const NETCONF_PATH_SEPARATOR* = ";"

# TODO: replace with some fem object
type Element* = ref object of RootObj
  name* : string
  attributes : TableRef[string, string]
  childs* : seq[Element]
  value* : string

proc newElement*(name : string, value : string = nil) : Element =
  new(result)
  result.childs = newSeq[Element]()
  result.name = name
  result.value = value

proc getAttribute*(this : Element, name : string) : string =
  if this.isNil :
    return nil
  if this.attributes.isNil :
    return nil
  return this.attributes.getOrDefault(name)

proc setAttribute*(this : Element, name, value : string) : string =
  if this.isNil :
    assert false, "Can't set attribute to nil element"
  if this.attributes.isNil :
    this.attributes = newTable[string, string]() 
  this.attributes[name] = value             

proc toStrPath(path : seq[string]) : string =
  result = ""
  if path.len > 0 :
    result = path[0]
  for i in 1 .. path.len - 1 :
    result.add(NETCONF_PATH_SEPARATOR)
    result.add(path[i])      

proc parseMessage*(message : string) : seq[(string, string)] =
  result = newSeq[(string, string)]()
  var x : XmlParser
  open(x, newStringStream(message), "msg")

  var path = newSeq[string]()
  while true:
    x.next()
    case x.kind:
    of XmlEventKind.xmlElementStart, XmlEventKind.xmlElementOpen:
      path.add(x.elementName)
      x.next() # skip elementName
      var data = ""
      while x.kind == XmlEventKind.xmlCharData:
        data.add(x.charData)
        x.next()
      #echo path, ":", data  
      result.add((path.toStrPath, data))
      if x.kind == XmlEventKind.xmlElementEnd:
        path.del(path.len-1) 
    of XmlEventKind.xmlEof: break
    else: discard

  x.close()

proc plainSeq(elements : seq[Element]) : seq[(string, string)] =
  return parseMessage($elements[0])  

proc parseMessageTree*(message : string) : seq[Element] =
  var curPath : seq[Element] = newSeq[Element]()
  curPath.add(newElement(""))
  var x : XmlParser
  open(x, newStringStream(message), "msg")

  while true:
    x.next()
    case x.kind:
    of XmlEventKind.xmlElementStart, XmlEventKind.xmlElementOpen:
      let elName = x.elementName
      x.next() # skip elementName
      var data = ""
      while x.kind == XmlEventKind.xmlCharData:
        data.add(x.charData)
        x.next()
      #echo path, ":", data  
      let lastEl = curPath[curPath.len-1]
      var newEl = newElement(elName, data)
      lastEl.childs.add(newEl)
      curPath.add(newEl)
      #result.add((path.toStrPath, data))
      if x.kind == XmlEventKind.xmlElementEnd:
        #path.del(path.len-1) 
        curPath.delete(curPath.len-1)
    of XmlEventKind.xmlEof: break
    else: discard

  x.close()
  return curPath[0].childs
if isMainModule:
  echo "COMPILED!"

My system is:

$uname -a Linux x220 4.4.0-59-generic #80-Ubuntu SMP Fri Jan 6 17:47:47 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

and in console i get

$nim -version
Nim Compiler Version 0.16.1 (2017-02-09) [Linux: amd64]
Copyright (c) 2006-2017 by Andreas Rumpf

git hash: 5ff6ff28bf9abf12fa9d82f0323954516a855b6a
active boot switches: -d:release

$nim c -r netconfutils.nim
Hint: used config file '/home/sbelyak/work/xored/Nim/config/nim.cfg' [Conf]
Hint: used config file '/home/sbelyak/work/xored/datafabric/ferma/src/ssh_grain/nim.cfg' [Conf]
Hint: system [Processing]
Hint: netconfutils [Processing]
Hint: tables [Processing]
Hint: hashes [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: algorithm [Processing]
Hint: etcpriv [Processing]
Hint: parsexml [Processing]
Hint: lexbase [Processing]
Hint: streams [Processing]
Hint: unicode [Processing]
$echo $?
1

if I commit plainSeq proc all work fine.

Araq commented 7 years ago

Fails for me with a proper error message (I'm on OSX though):

temp3.nim(70, 23) Error: type mismatch: got (Element)
but expected one of:
proc `$`(x: int): string
proc `$`[T: tuple |
    object](x: T): string
proc `$`[T](x: seq[T]): string
proc `$`(x: uint64): string
proc `$`[T](x: set[T]): string
proc `$`(x: int64): string
proc `$`(s: WideCString): string
proc `$`(x: string): string
proc `$`(x: char): string
proc `$`(x: cstring): string
proc `$`(x: bool): string
proc `$`[Enum: enum](x: Enum): string
proc `$`(x: float): string
proc `$`(w: WideCString; estimate: int; replacement: int = 0x0000FFFD): string
proc `$`[A, B](t: Table[A, B]): string
proc `$`[A, B](t: OrderedTable[A, B]): string
proc `$`[A, B](t: TableRef[A, B]): string
proc `$`[A](t: CountTableRef[A]): string
proc `$`[A](t: CountTable[A]): string
proc `$`[A, B](t: OrderedTableRef[A, B]): string
SBBerkov commented 7 years ago

after recompile nim with -d:debug I get:

nim c -r test.nim
Hint: used config file '/home/sbelyak/work/xored/Nim/config/nim.cfg' [Conf]
Hint: used config file '/home/sbelyak/work/xored/datafabric/ferma/src/ssh_grain/nim.cfg' [Conf]
Hint: system [Processing]
Hint: test [Processing]
Hint: tables [Processing]
Hint: hashes [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: algorithm [Processing]
Hint: etcpriv [Processing]
Hint: parsexml [Processing]
Hint: lexbase [Processing]
Hint: streams [Processing]
Hint: unicode [Processing]
Traceback (most recent call last)
nim.nim(121)             nim
nim.nim(77)              handleCmdLine
main.nim(163)            mainCommand
main.nim(74)             commandCompileToC
modules.nim(240)         compileProject
modules.nim(180)         compileModule
passes.nim(213)          processModule
passes.nim(135)          processTopLevelStmt
sem.nim(507)             myProcess
sem.nim(479)             semStmtAndGenerateGenerics
semstmts.nim(1609)       semStmt
semexprs.nim(819)        semExprNoType
semexprs.nim(2360)       semExpr
semstmts.nim(1380)       semProc
semstmts.nim(1314)       semProcAux
semexprs.nim(1429)       semProcBody
semexprs.nim(2342)       semExpr
semstmts.nim(1563)       semStmtList
semexprs.nim(2354)       semExpr
semexprs.nim(1417)       semReturn
semexprs.nim(1385)       semAsgn
semexprs.nim(42)         semExprWithType
semexprs.nim(2263)       semExpr
semexprs.nim(802)        semDirectOp
semexprs.nim(640)        semOverloadedCallAnalyseEffects
semcall.nim(384)         semOverloadedCall
semcall.nim(199)         resolveOverloads
semcall.nim(87)          pickBestCandidate
sigmatch.nim(1789)       matches
sigmatch.nim(1746)       matchesAux
sigmatch.nim(1583)       prepareOperand
(1874 calls omitted) ...
sigmatch.nim(1746)       matchesAux
sigmatch.nim(1583)       prepareOperand
semexprs.nim(26)         semOperand
semexprs.nim(2275)       semExpr
semexprs.nim(802)        semDirectOp
semexprs.nim(640)        semOverloadedCallAnalyseEffects
semcall.nim(390)         semOverloadedCall
semcall.nim(353)         semResolvedCall
seminst.nim(296)         generateInstance
seminst.nim(136)         instantiateBody
semexprs.nim(1429)       semProcBody
semexprs.nim(2342)       semExpr
semstmts.nim(1563)       semStmtList
semexprs.nim(2352)       semExpr
semstmts.nim(670)        semFor
semfields.nim(153)       semForFields
semfields.nim(100)       semForObjectFields
semfields.nim(71)        semForObjectFields
semstmts.nim(1609)       semStmt
semexprs.nim(819)        semExprNoType
semexprs.nim(2342)       semExpr
semstmts.nim(1563)       semStmtList
semexprs.nim(2282)       semExpr
semexprs.nim(1874)       semWhen
sem.nim(303)             semConstExpr
semexprs.nim(42)         semExprWithType
semexprs.nim(2264)       semExpr
semexprs.nim(1780)       semMagic
semexprs.nim(1723)       semCompiles
semexprs.nim(1698)       tryExpr
semexprs.nim(2275)       semExpr
semexprs.nim(802)        semDirectOp
semexprs.nim(640)        semOverloadedCallAnalyseEffects
semcall.nim(385)         semOverloadedCall
semcall.nim(353)         semResolvedCall
seminst.nim(296)         generateInstance
seminst.nim(136)         instantiateBody
semexprs.nim(1429)       semProcBody
semexprs.nim(2342)       semExpr
semstmts.nim(1563)       semStmtList
semexprs.nim(2342)       semExpr
semstmts.nim(1563)       semStmtList
semexprs.nim(2323)       semExpr
semstmts.nim(182)        semIf
semstmts.nim(120)        semExprBranchScope
semstmts.nim(111)        semExprBranch
semexprs.nim(2342)       semExpr
semstmts.nim(1563)       semStmtList
semexprs.nim(2352)       semExpr
semstmts.nim(661)        semFor
semexprs.nim(58)         semExprNoDeref
semexprs.nim(2275)       semExpr
semexprs.nim(802)        semDirectOp
semexprs.nim(637)        semOverloadedCallAnalyseEffects
semcall.nim(385)         semOverloadedCall
semcall.nim(353)         semResolvedCall
seminst.nim(296)         generateInstance
seminst.nim(136)         instantiateBody
semexprs.nim(1429)       semProcBody
semexprs.nim(2342)       semExpr
semstmts.nim(1563)       semStmtList
semexprs.nim(2352)       semExpr
semstmts.nim(661)        semFor
semexprs.nim(58)         semExprNoDeref
semexprs.nim(2275)       semExpr
semexprs.nim(802)        semDirectOp
semexprs.nim(637)        semOverloadedCallAnalyseEffects
semcall.nim(385)         semOverloadedCall
semcall.nim(353)         semResolvedCall
seminst.nim(296)         generateInstance
seminst.nim(136)         instantiateBody
semexprs.nim(1429)       semProcBody
semexprs.nim(2342)       semExpr
semstmts.nim(1563)       semStmtList
semexprs.nim(2342)       semExpr
semstmts.nim(1563)       semStmtList
semexprs.nim(2287)       semExpr
semexprs.nim(2342)       semExpr
semstmts.nim(1563)       semStmtList
semexprs.nim(2349)       semExpr
semstmts.nim(58)         semWhile
semexprs.nim(42)         semExprWithType
semexprs.nim(2275)       semExpr
semexprs.nim(802)        semDirectOp
semexprs.nim(640)        semOverloadedCallAnalyseEffects
semcall.nim(384)         semOverloadedCall
semcall.nim(199)         resolveOverloads
semcall.nim(87)          pickBestCandidate
sigmatch.nim(1789)       matches
sigmatch.nim(1643)       matchesAux
ast.nim(1468)            copyTree
ast.nim(1451)            copyTree
ast.nim(1002)            newNode
gc.nim(503)              newObj
alloc.nim(517)           rawAlloc
osalloc.nim              roundup
Stack overflow
refi64 commented 7 years ago

Please use triple backticks around multi-line code blocks. It makes it much easier to read and doesn't butcher the formatting.

dom96 commented 7 years ago

@SBBerkov I fixed your comment's formatting for you, hope you don't mind :)

Parashurama commented 7 years ago

Can't reproduce issue: ie proper error message same as @Araq Changing

return parseMessage($elements[0]) 

to

return parseMessage($elements[0][]) # ref value need to be dereferenced.

produce the expected output: COMPILED!

platform: Linux XXXX 4.6.4-040604-generic

Nim Compiler Version 0.17.0 (2017-05-31) [Linux: amd64]
Copyright (c) 2006-2017 by Andreas Rumpf

git hash: fd0ab1df3efa51870d6746ed0ce6479f8934369d
active boot switches: -d:release
ghost commented 7 years ago

@Araq maybe close? seems to be fixed as well (even if compiled with -d:debug)

Araq commented 7 years ago

I know the underlying problem is still an issue.

Araq commented 6 years ago

Duplicate of the "infinite instantiation" bug. Closing.