Skip to content

Commit d173116

Browse files
committed
preserve unknown hash key-type errors in fast-path checks
1 parent 6cb766f commit d173116

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

vibes/execution_types_validation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ func typeAllowsStringHashKey(ty *TypeExpr) (bool, bool) {
200200
}
201201

202202
switch ty.Kind {
203+
case TypeUnknown:
204+
// Unknown key types must flow through full matching so callers preserve
205+
// unknown-type errors instead of silently treating them as mismatches.
206+
return false, false
203207
case TypeAny, TypeString:
204208
return true, true
205209
case TypeUnion:
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package vibes
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestTypeAllowsStringHashKeyDefersUnknownUnions(t *testing.T) {
9+
keyType := &TypeExpr{
10+
Kind: TypeUnion,
11+
Union: []*TypeExpr{
12+
{Name: "typo", Kind: TypeUnknown},
13+
{Name: "string", Kind: TypeString},
14+
},
15+
}
16+
17+
decided, matches := typeAllowsStringHashKey(keyType)
18+
if decided {
19+
t.Fatalf("expected unknown key union to defer to full matcher")
20+
}
21+
if matches {
22+
t.Fatalf("unexpected string-key fast-path match for unknown key union")
23+
}
24+
}
25+
26+
func TestValueMatchesTypeHashUnknownKeyUnionReturnsError(t *testing.T) {
27+
hashType := &TypeExpr{
28+
Kind: TypeHash,
29+
TypeArgs: []*TypeExpr{
30+
{
31+
Kind: TypeUnion,
32+
Union: []*TypeExpr{
33+
{Name: "typo", Kind: TypeUnknown},
34+
{Name: "string", Kind: TypeString},
35+
},
36+
},
37+
{Name: "int", Kind: TypeInt},
38+
},
39+
}
40+
41+
matches, err := valueMatchesType(NewHash(map[string]Value{
42+
"score": NewInt(1),
43+
}), hashType)
44+
if err == nil {
45+
t.Fatalf("expected unknown type error")
46+
}
47+
if matches {
48+
t.Fatalf("unknown key union should not report a match")
49+
}
50+
if !strings.Contains(err.Error(), "unknown type typo") {
51+
t.Fatalf("expected unknown type error, got %v", err)
52+
}
53+
}

0 commit comments

Comments
 (0)