Skip to content

Commit 85fbd8e

Browse files
committed
fix: if_type_assert: add known broken case
Add reproducer for #91 and #92 Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent 7b51b9a commit 85fbd8e

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

compliance/tests/if_type_assert/expect-fail

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
Expected: string
2+
got string: string

compliance/tests/if_type_assert/if_type_assert.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,25 @@ func main() {
88
} else {
99
println("Not Expected: should be a string")
1010
}
11+
12+
// this is from go/ast/filter.go, line 117
13+
type KV struct {
14+
Key any
15+
}
16+
17+
var list []any
18+
kv := &KV{Key: "string"}
19+
list = []any{kv}
20+
for _, exp := range list {
21+
switch x := exp.(type) {
22+
case *KV:
23+
if x, ok := x.Key.(string); ok {
24+
println("got string:", x)
25+
} else {
26+
println("fail: should be string")
27+
}
28+
default:
29+
println("fail: should be KV")
30+
}
31+
}
1132
}

compliance/tests/if_type_assert/if_type_assert.gs.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,65 @@ export async function main(): Promise<void> {
1515
console.log("Not Expected: should be a string")
1616
}
1717
}
18+
19+
// this is from go/ast/filter.go, line 117
20+
export class KV {
21+
public get Key(): null | any {
22+
return this._fields.Key.value
23+
}
24+
public set Key(value: null | any) {
25+
this._fields.Key.value = value
26+
}
27+
28+
public _fields: {
29+
Key: $.VarRef<null | any>;
30+
}
31+
32+
constructor(init?: Partial<{Key?: null | any}>) {
33+
this._fields = {
34+
Key: $.varRef(init?.Key ?? null)
35+
}
36+
}
37+
38+
public clone(): KV {
39+
const cloned = new KV()
40+
cloned._fields = {
41+
Key: $.varRef(this._fields.Key.value)
42+
}
43+
return cloned
44+
}
45+
46+
// Register this type with the runtime type system
47+
static __typeInfo = $.registerStructType(
48+
'KV',
49+
new KV(),
50+
[],
51+
KV,
52+
{"Key": { kind: $.TypeKind.Interface, methods: [] }}
53+
);
54+
}
55+
56+
let list: $.Slice<null | any> = null
57+
let kv = new KV({Key: "string"})
58+
list = $.arrayToSlice<null | any>([kv])
59+
for (let _i = 0; _i < $.len(list); _i++) {
60+
const exp = list![_i]
61+
{
62+
$.typeSwitch(exp, [{ types: [{kind: $.TypeKind.Pointer, elemType: 'KV'}], body: (x) => {
63+
const _temp_x = x
64+
{
65+
let x, let ok = $.mustTypeAssert<string>(x.Key, {kind: $.TypeKind.Basic, name: 'string'})
66+
if (ok) {
67+
console.log("got string:", x)
68+
}
69+
else {
70+
console.log("fail: should be string")
71+
}
72+
}
73+
}}], () => {
74+
console.log("fail: should be KV")
75+
})
76+
}
77+
}
1878
}
1979

0 commit comments

Comments
 (0)