From e869e884c646279d13d7d0ffd582d576d14f8ec3 Mon Sep 17 00:00:00 2001 From: Patrick East Date: Wed, 11 Mar 2020 18:00:58 -0700 Subject: [PATCH] ast: Fix type check for objects with non-json keys 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 --- ast/check_test.go | 12 ++++++++++++ ast/env.go | 16 ++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/ast/check_test.go b/ast/check_test.go index 94d2edd5c3..0d73b4732a 100644 --- a/ast/check_test.go +++ b/ast/check_test.go @@ -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( diff --git a/ast/env.go b/ast/env.go index f0830e5bd8..0519c30be5 100644 --- a/ast/env.go +++ b/ast/env.go @@ -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 {