Skip to content

Commit bbda9f1

Browse files
authored
fix cast of null-valued union (#6673)
A cast of a null-valued union results in an error. Fix that so it results in a null-valued nullable union.
1 parent a5bdf1e commit bbda9f1

19 files changed

+48
-15
lines changed

runtime/sam/expr/function/cast.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (c *cast) Call(args []super.Value) super.Value {
4545
}
4646

4747
func (c *cast) Cast(from super.Value, to super.Type) super.Value {
48+
from = from.Deunion()
4849
switch fromType := from.Type(); {
4950
case fromType == to:
5051
return from
@@ -70,7 +71,6 @@ func (c *cast) Cast(from super.Value, to super.Type) super.Value {
7071
case *super.TypeNamed:
7172
return c.toNamed(from, to)
7273
default:
73-
from = from.Under()
7474
caster := expr.LookupPrimitiveCaster(c.sctx, to)
7575
if caster == nil {
7676
return c.error(from, to)
@@ -84,7 +84,6 @@ func (c *cast) error(from super.Value, to super.Type) super.Value {
8484
}
8585

8686
func (c *cast) toRecord(from super.Value, to *super.TypeRecord) super.Value {
87-
from = from.Under()
8887
fromRecType := super.TypeRecordOf(from.Type())
8988
if fromRecType == nil {
9089
return c.error(from, to)
@@ -142,7 +141,6 @@ func derefWithNone(typ *super.TypeRecord, bytes scode.Bytes, name string) (super
142141
}
143142

144143
func (c *cast) toArrayOrSet(from super.Value, to super.Type) super.Value {
145-
from = from.Under()
146144
fromInner := super.InnerType(from.Type())
147145
toInner := super.InnerType(to)
148146
if fromInner == nil {
@@ -196,7 +194,6 @@ func (c *cast) maybeConvertToUnion(vals []super.Value, types map[super.Type]stru
196194
}
197195

198196
func (c *cast) toMap(from super.Value, to *super.TypeMap) super.Value {
199-
from = from.Under()
200197
fromType, ok := from.Type().(*super.TypeMap)
201198
if !ok {
202199
return c.error(from, to)

runtime/ztests/expr/cast/bool.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ input: |
1717
"false"
1818
"blah"
1919
null
20+
null::({}|null)
2021
"1"
2122
error("bad")
2223
{x:1}
@@ -39,6 +40,7 @@ output: |
3940
false
4041
error({message:"cannot cast to bool",on:"blah"})
4142
null::(bool|null)
43+
null::(bool|null)
4244
true
4345
error("bad")
4446
error({message:"cannot cast to bool",on:{x:1}})

runtime/ztests/expr/cast/bytes.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ input: |
88
"bar"::=named
99
""
1010
null
11+
null::({}|null)
1112
{x:"foo"}
1213
error("bad")
1314
@@ -17,5 +18,6 @@ output: |
1718
0x626172
1819
0x
1920
null::(bytes|null)
21+
null::(bytes|null)
2022
error({message:"cannot cast to bytes",on:{x:"foo"}})
2123
error("bad")

runtime/ztests/expr/cast/duration.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ input: |
1111
1::=named
1212
1e+19
1313
null
14+
null::({}|null)
1415
error("bad")
1516
{x:1}
1617
[1,2]
@@ -26,6 +27,7 @@ output: |
2627
1ns
2728
error({message:"cannot cast to duration",on:1e+19})
2829
null::(duration|null)
30+
null::(duration|null)
2931
error("bad")
3032
error({message:"cannot cast to duration",on:{x:1}})
3133
error({message:"cannot cast to duration",on:[1,2]})

runtime/ztests/expr/cast/enum.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ input: |
77
"e3"
88
0
99
null
10+
null::({}|null)
1011
error("bad")
1112
1213
output: |
1314
"e2"::enum(e1,e2)
1415
error({message:"no such symbol in enum(e1,e2)",on:"e3"})
1516
error({message:"cannot cast to enum",on:0})
1617
null::(null|enum(e1,e2))
18+
null::(null|enum(e1,e2))
1719
error("bad")

runtime/ztests/expr/cast/float.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ input: |
1212
false
1313
true
1414
null
15+
null::({}|null)
1516
1617
output: |
1718
1.5::float16
@@ -41,6 +42,9 @@ output: |
4142
null::(float16|null)
4243
null::(float32|null)
4344
null::(float64|null)
45+
null::(float16|null)
46+
null::(float32|null)
47+
null::(float64|null)
4448
4549
---
4650

runtime/ztests/expr/cast/int.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ input: |
1414
false
1515
true
1616
null
17+
null::({}|null)
1718
1819
output: |
1920
-1::int8
@@ -56,6 +57,10 @@ output: |
5657
null::(int16|null)
5758
null::(int32|null)
5859
null::(int64|null)
60+
null::(int8|null)
61+
null::(int16|null)
62+
null::(int32|null)
63+
null::(int64|null)
5964
6065
---
6166

runtime/ztests/expr/cast/ip.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ input: |
77
"127.0.0.1"
88
"2001:0000:130F:0000:0000:09C0:876A:130B"
99
null
10+
null::({}|null)
1011
1.1.1.1
1112
1.1.1.2::=named
1213
34
@@ -18,6 +19,7 @@ output: |
1819
127.0.0.1
1920
2001:0:130f::9c0:876a:130b
2021
null::(ip|null)
22+
null::(ip|null)
2123
1.1.1.1
2224
1.1.1.2
2325
error({message:"cannot cast to ip",on:34})

runtime/ztests/expr/cast/map.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ input: |
77
|{}|
88
|{1:|{"2":3,4:"5"}|,"7":8,"9":|{10:[],[]:11,null:null}|}|
99
null
10+
null::({}|null)
1011
1
1112
error("bad")
1213
1314
output: |
1415
|{}|
15-
|{1:|{"2":3,"4":5}|,7:error({message:"cannot cast to |{string:int64}|",on:8}),9:|{"10":error({message:"cannot cast to int64",on:[]::[null]}),"[]":11,error({message:"cannot cast to string",on:null}):error({message:"cannot cast to int64",on:null})}|}|
16+
|{1:|{"2":3,"4":5}|,7:error({message:"cannot cast to |{string:int64}|",on:8}),9:|{"10":error({message:"cannot cast to int64",on:[]::[null]}),"[]":11,null::(string|null):null::(int64|null)}|}|
17+
null::(null||{int64:|{string:int64}|}|)
1618
null::(null||{int64:|{string:int64}|}|)
1719
error({message:"cannot cast to |{int64:|{string:int64}|}|",on:1})
1820
error("bad")

runtime/ztests/expr/cast/net.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ input: |
66
"192.168.1.0/24"
77
"2001:db8::/32"
88
null
9+
null::({}|null)
910
10.0.0.0/8
1011
10.0.0.0/8::=named
1112
-35
@@ -16,6 +17,7 @@ output: |
1617
192.168.1.0/24
1718
2001:db8::/32
1819
null::(net|null)
20+
null::(net|null)
1921
10.0.0.0/8
2022
10.0.0.0/8
2123
error({message:"cannot cast to net",on:-35})

0 commit comments

Comments
 (0)