Skip to content

Commit

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

* make tests green
  • Loading branch information
Araq authored Mar 5, 2020
1 parent 62c113e commit 83e715c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
2 changes: 2 additions & 0 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,8 @@ proc typeSectionFinalPass(c: PContext, n: PNode) =
checkConstructedType(c.config, s.info, s.typ)
if s.typ.kind in {tyObject, tyTuple} and not s.typ.n.isNil:
checkForMetaFields(c, s.typ.n)
# fix bug #5170: ensure locally scoped object types get a unique name:
if s.typ.kind == tyObject and not isTopLevel(c): incl(s.flags, sfGenSym)
#instAllTypeBoundOp(c, n.info)


Expand Down
19 changes: 9 additions & 10 deletions compiler/sighashes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type
CoIgnoreRange
CoConsiderOwned
CoDistinct
CoHashTypeInsideNode

proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag])

Expand All @@ -61,7 +62,7 @@ proc hashTypeSym(c: var MD5Context, s: PSym) =
c &= "."
it = it.owner

proc hashTree(c: var MD5Context, n: PNode) =
proc hashTree(c: var MD5Context, n: PNode; flags: set[ConsiderFlag]) =
if n == nil:
c &= "\255"
return
Expand All @@ -75,6 +76,8 @@ proc hashTree(c: var MD5Context, n: PNode) =
c &= n.ident.s
of nkSym:
hashSym(c, n.sym)
if CoHashTypeInsideNode in flags and n.sym.typ != nil:
hashType(c, n.sym.typ, flags)
of nkCharLit..nkUInt64Lit:
let v = n.intVal
lowlevel v
Expand All @@ -84,7 +87,7 @@ proc hashTree(c: var MD5Context, n: PNode) =
of nkStrLit..nkTripleStrLit:
c &= n.strVal
else:
for i in 0..<n.len: hashTree(c, n[i])
for i in 0..<n.len: hashTree(c, n[i], flags)

proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]) =
if t == nil:
Expand Down Expand Up @@ -155,11 +158,7 @@ proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]) =
let oldFlags = t.sym.flags
# Mild hack to prevent endless recursion.
t.sym.flags = t.sym.flags - {sfAnon, sfGenSym}
for n in t.n:
assert(n.kind == nkSym)
let s = n.sym
c.hashSym s
c.hashType s.typ, flags
hashTree(c, t.n, flags + {CoHashTypeInsideNode})
t.sym.flags = oldFlags
else:
# The object has no fields: we _must_ add something here in order to
Expand All @@ -176,7 +175,7 @@ proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]) =
if tfVarIsPtr in t.flags: c &= ".varisptr"
of tyFromExpr:
c &= char(t.kind)
c.hashTree(t.n)
c.hashTree(t.n, {})
of tyTuple:
c &= char(t.kind)
if t.n != nil and CoType notin flags:
Expand All @@ -192,11 +191,11 @@ proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]) =
of tyRange:
if CoIgnoreRange notin flags:
c &= char(t.kind)
c.hashTree(t.n)
c.hashTree(t.n, {})
c.hashType(t[0], flags)
of tyStatic:
c &= char(t.kind)
c.hashTree(t.n)
c.hashTree(t.n, {})
c.hashType(t[0], flags)
of tyProc:
c &= char(t.kind)
Expand Down
6 changes: 3 additions & 3 deletions tests/errmsgs/tsigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ expression: fun1(default(Mystring), "asdf")


## line 100
block:
when true:
# bug #11061 Type mismatch error "first type mismatch at" points to wrong argument/position
# Note: the error msg now gives correct position for mismatched argument
type
A = object of RootObj
B = object of A

block:
proc f(b: B) = discard
proc f(a: A) = discard

Expand Down Expand Up @@ -163,7 +163,7 @@ block:
var foo = ""
f(foo, a0 = 12)

block:
when true:
type Mystring = string
type MyInt = int
proc fun1(a1: MyInt, a2: Mystring) = discard
Expand Down
4 changes: 2 additions & 2 deletions tests/errmsgs/tsigmatch2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ expression: foo 1


# line 30

type Foo = object
block: # issue #13182
proc myproc(a: int): string = $("myproc", a)
proc foo(args: varargs[string, myproc]): string = $args
type Foo = object

proc foo(i: Foo): string = "in foo(i)"
static: doAssert foo(Foo()) == "in foo(i)"
static: doAssert foo(1) == """["(\"myproc\", 1)"]"""
Expand Down
30 changes: 30 additions & 0 deletions tests/types/tissues_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ ptr Foo
(member: 123.456)
(member: "hello world", x: ...)
(member: 123.456, x: ...)
0
false
'''
joinable: false
"""
Expand Down Expand Up @@ -78,3 +80,31 @@ block t7905:

foobarRec("hello world")
foobarRec(123.456'f64)

# bug #5170

when true:
type Foo = object
bar: bool

type Bar = object
sameBody: string

var b0: Bar
b0.sameBody = "abc"

block:
type Foo = object
baz: int

type Bar = object
sameBody: string

var b1: Bar
b1.sameBody = "def"

var f2: Foo
echo f2.baz

var f1: Foo
echo f1.bar

0 comments on commit 83e715c

Please sign in to comment.