Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fixes nim-lang#9297

* improve spacing
  • Loading branch information
cooldome authored and krux02 committed Oct 15, 2018
1 parent 1a05d00 commit ba4cc21
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ proc genVarTuple(p: BProc, n: PNode) =
field.r = "$1.Field$2" % [rdLoc(tup), rope(i)]
else:
if t.n.sons[i].kind != nkSym: internalError(p.config, n.info, "genVarTuple")
field.r = "$1.$2" % [rdLoc(tup), mangleRecFieldName(p.module, t.n.sons[i].sym, t)]
field.r = "$1.$2" % [rdLoc(tup), mangleRecFieldName(p.module, t.n.sons[i].sym)]
putLocIntoDest(p, v.loc, field)

proc genDeref(p: BProc, e: PNode, d: var TLoc; enforceDeref=false)
Expand Down
7 changes: 3 additions & 4 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,8 @@ proc genProcParams(m: BModule, t: PType, rettype, params: var Rope,
else: add(params, ")")
params = "(" & params

proc mangleRecFieldName(m: BModule; field: PSym, rectype: PType): Rope =
if (rectype.sym != nil) and
({sfImportc, sfExportc} * rectype.sym.flags != {}):
proc mangleRecFieldName(m: BModule; field: PSym): Rope =
if {sfImportc, sfExportc} * field.flags != {}:
result = field.loc.r
else:
result = rope(mangleField(m, field.name))
Expand Down Expand Up @@ -484,7 +483,7 @@ proc genRecordFieldsAux(m: BModule, n: PNode,
let field = n.sym
if field.typ.kind == tyVoid: return
#assert(field.ast == nil)
let sname = mangleRecFieldName(m, field, rectype)
let sname = mangleRecFieldName(m, field)
let ae = if accessExpr != nil: "$1.$2" % [accessExpr, sname]
else: sname
fillLoc(field.loc, locField, n, ae, OnUnknown)
Expand Down
10 changes: 5 additions & 5 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -587,12 +587,12 @@ proc semCaseBranch(c: PContext, t, branch: PNode, branchIndex: int,
checkForOverlap(c, t, i, branchIndex)

proc semRecordNodeAux(c: PContext, n: PNode, check: var IntSet, pos: var int,
father: PNode, rectype: PType)
father: PNode, rectype: PType, hasCaseFields = false)
proc semRecordCase(c: PContext, n: PNode, check: var IntSet, pos: var int,
father: PNode, rectype: PType) =
var a = copyNode(n)
checkMinSonsLen(n, 2, c.config)
semRecordNodeAux(c, n.sons[0], check, pos, a, rectype)
semRecordNodeAux(c, n.sons[0], check, pos, a, rectype, hasCaseFields = true)
if a.sons[0].kind != nkSym:
internalError(c.config, "semRecordCase: discriminant is no symbol")
return
Expand All @@ -619,13 +619,13 @@ proc semRecordCase(c: PContext, n: PNode, check: var IntSet, pos: var int,
checkSonsLen(b, 1, c.config)
else: illFormedAst(n, c.config)
delSon(b, sonsLen(b) - 1)
semRecordNodeAux(c, lastSon(n.sons[i]), check, pos, b, rectype)
semRecordNodeAux(c, lastSon(n.sons[i]), check, pos, b, rectype, hasCaseFields = true)
if chckCovered and covered != lengthOrd(c.config, a.sons[0].typ):
localError(c.config, a.info, "not all cases are covered")
addSon(father, a)

proc semRecordNodeAux(c: PContext, n: PNode, check: var IntSet, pos: var int,
father: PNode, rectype: PType) =
father: PNode, rectype: PType, hasCaseFields = false) =
if n == nil: return
case n.kind
of nkRecWhen:
Expand Down Expand Up @@ -694,7 +694,7 @@ proc semRecordNodeAux(c: PContext, n: PNode, check: var IntSet, pos: var int,
f.position = pos
if fieldOwner != nil and
{sfImportc, sfExportc} * fieldOwner.flags != {} and
f.loc.r == nil:
not hasCaseFields and f.loc.r == nil:
f.loc.r = rope(f.name.s)
f.flags = f.flags + ({sfImportc, sfExportc} * fieldOwner.flags)
inc(pos)
Expand Down
42 changes: 41 additions & 1 deletion tests/ccgbugs/tcgbug.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
discard """
file: "tcgbug.nim"
output: "success"
output: '''success
M1 M2
'''
"""

type
Expand Down Expand Up @@ -36,3 +38,41 @@ type

var k = PFuture[void]()


##bug #9297
import strutils

type
MyKind = enum
M1, M2, M3

MyObject {.exportc: "ExtObject"} = object
case kind: MyKind
of M1: a:int
of M2: b:float
of M3: c:cstring

MyObjectRef {.exportc: "ExtObject2"} = ref object
case kind: MyKind
of M1: a:int
of M2: b:float
of M3: c:cstring

proc newMyObject(kind: MyKind, val: string): MyObject =
result.kind = kind

case kind
of M1: result.a = parseInt(val)
of M2: result.b = parseFloat(val)
of M3: result.c = val

proc newMyObjectRef(kind: MyKind, val: string): MyObjectRef =
new(result)
result.kind = kind
case kind
of M1: result.a = parseInt(val)
of M2: result.b = parseFloat(val)
of M3: result.c = val


echo newMyObject(M1, "2").kind, " ", newMyObjectRef(M2, "3").kind

0 comments on commit ba4cc21

Please sign in to comment.