fixes #6016 union alias trigger bad codegen (#6117)

* fixes #6016 union alias trigger bad codegen
* cpp test ok
* merging some test into one file
This commit is contained in:
andri lim 2017-08-03 16:03:47 +07:00 • committed by Andreas Rumpf
commit 57edf619fe
6 changed files with 176 additions and 141 deletions

158
tests/types/taliasbugs.nim Normal file
View file

@ -0,0 +1,158 @@
discard """
msg: '''true
true
true
true
true
true'''
output: '''true
true
true
true
true
true
R
R
R
R
19
(c: 0)
(c: 13)
@[(c: 11)]
@[(c: 17)]
100'''
"""
# bug #5360
import macros
type
Order = enum
R
OrderAlias = Order
template getOrderTypeA(): typedesc = Order
template getOrderTypeB(): typedesc = OrderAlias
type
OrderR = getOrderTypeA()
OrderG = getOrderTypeB()
macro typeRep(a, b: typed): untyped =
if sameType(a, b):
echo "true"
else:
echo "false"
template test(a, b: typedesc) =
when a is b:
echo "true"
else:
echo "false"
test(OrderAlias, Order)
test(OrderR, Order)
test(OrderG, Order)
test(OrderR, OrderG)
test(OrderR, OrderAlias)
test(OrderG, OrderAlias)
typeRep(OrderAlias.R, Order.R) # true
typeRep(OrderR.R, Order.R) # true
typeRep(OrderG.R, Order.R) # true
typeRep(OrderR.R, OrderAlias.R) # true
typeRep(OrderG.R, OrderAlias.R) # true
typeRep(OrderR.R, OrderG.R) # true
echo OrderR.R # R
echo OrderG.R # R
echo OrderAlias.R # R
echo Order.R # R
# bug #5238
type
Rgba8 = object
c: int
BlenderRgb*[ColorT] = object
template getColorType*[C](x: typedesc[BlenderRgb[C]]): typedesc = C
type
ColorT = getColorType(BlenderRgb[int])
proc setColor(c: var ColorT) =
c = 19
var n: ColorT
n.setColor()
echo n
type
ColorType = getColorType(BlenderRgb[Rgba8])
var x: ColorType
echo x
proc setColor(c: var ColorType) =
c = Rgba8(c: 13)
proc setColor(c: var seq[ColorType]) =
c[0] = Rgba8(c: 11)
proc setColorArray(c: var openArray[ColorType]) =
c[0] = Rgba8(c: 17)
x.setColor()
echo x
var y = @[Rgba8(c:15)]
y.setColor()
echo y
y.setColorArray()
echo y
#bug #6016
type
Onion {.union.} = object
field1: int
field2: uint64
Stroom = Onion
PStroom = ptr Stroom
proc pstruct(u: PStroom) =
echo u.field2
var oni = Onion(field1: 100)
pstruct(oni.addr)
# bug #4124
import sequtils
type
Foo = distinct string
var
foo: Foo
type
Alias = (type(foo))
var
a: Alias
a = foo
when true:
var xs = @[1,2,3]
proc asFoo(i: string): Foo =
Foo(i)
var xx = xs.mapIt(asFoo($(it + 5)))