Skip to content

Commit

Permalink
ast: Fix type check for objects with non-json keys
Browse files Browse the repository at this point in the history
Previously if there was a non-json key it would `panic("unreachable")`
but it can fall back to using the dynamic property for these types.

Fixes: #2183
Signed-off-by: Patrick East <east.patrick@gmail.com>
  • Loading branch information
patrick-east authored and tsandall committed Mar 16, 2020
1 parent 834aaee commit e869e88
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
12 changes: 12 additions & 0 deletions ast/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ func TestCheckInference(t *testing.T) {
Var("x"): types.NewObject([]*types.StaticProperty{{Key: json.Number("1"), Value: types.N}}, nil),
Var("y"): types.N,
}},
{"object-object-key", `x = {{{}: 1}: 1}`, map[Var]types.Type{
Var("x"): types.NewObject(
nil,
types.NewDynamicProperty(
types.NewObject(
[]*types.StaticProperty{types.NewStaticProperty(map[string]interface{}{}, types.N)},
nil,
),
types.N,
),
),
}},
{"sets", `x = {1, 2}; y = {{"foo", 1}, x}`, map[Var]types.Type{
Var("x"): types.NewSet(types.N),
Var("y"): types.NewSet(
Expand Down
16 changes: 8 additions & 8 deletions ast/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ func (env *TypeEnv) Get(x interface{}) types.Type {
x.Foreach(func(k, v *Term) {
if IsConstant(k.Value) {
kjson, err := JSON(k.Value)
if err != nil {
panic("unreachable")
if err == nil {
tpe := env.Get(v)
static = append(static, types.NewStaticProperty(kjson, tpe))
return
}
tpe := env.Get(v)
static = append(static, types.NewStaticProperty(kjson, tpe))
} else {
typeK := env.Get(k.Value)
typeV := env.Get(v.Value)
dynamic = types.NewDynamicProperty(typeK, typeV)
}
// Can't handle it as a static property, fallback to dynamic
typeK := env.Get(k.Value)
typeV := env.Get(v.Value)
dynamic = types.NewDynamicProperty(typeK, typeV)
})

if len(static) == 0 && dynamic == nil {
Expand Down

0 comments on commit e869e88

Please sign in to comment.