From 749e37ba2edc5cdc9b49066e3a11b232636fd5c9 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 22 Jun 2022 18:06:56 -0400 Subject: [PATCH] fix typo causing a panic in tuple unification The `unifyObjectTypesToMap` function was inadvertently called from the tuple-list unification function, which would panic since the value has no attributes. The given test example is possible speculatively unify, however we don't have a codepath to recursively unify these yet, so we will just fail to unify for now. This will fix the crashes in downstream consumers, allowing the possibility of working around the shortcoming by using more specific or different combinations of types. --- cty/convert/unify.go | 5 ++--- cty/convert/unify_test.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cty/convert/unify.go b/cty/convert/unify.go index 144acd85..ac6b64db 100644 --- a/cty/convert/unify.go +++ b/cty/convert/unify.go @@ -447,7 +447,6 @@ func unifyTupleTypes(types []cty.Type, unsafe bool, hasDynamic bool) (cty.Type, conversions[i] = GetConversion(ty, retTy) } if conversions[i] == nil { - // Shouldn't be reachable, since we were able to unify return unifyTupleTypesToList(types, unsafe) } } @@ -483,8 +482,8 @@ func unifyTupleTypesToList(types []cty.Type, unsafe bool) (cty.Type, []Conversio conversions[i] = GetConversion(ty, retTy) } if conversions[i] == nil { - // Shouldn't be reachable, since we were able to unify - return unifyObjectTypesToMap(types, unsafe) + // no conversion was found + return cty.NilType, nil } } return retTy, conversions diff --git a/cty/convert/unify_test.go b/cty/convert/unify_test.go index 8dd16c39..6ec5c671 100644 --- a/cty/convert/unify_test.go +++ b/cty/convert/unify_test.go @@ -163,6 +163,23 @@ func TestUnify(t *testing.T) { cty.List(cty.Map(cty.String)), []bool{true, true}, }, + { + // The second tuple value could be anything, so we can't unify + // these as a list. + // FIXME: While a unification is possible, we get a NilType for + // now until we can handle more complex recursive unification. + []cty.Type{ + cty.Tuple([]cty.Type{ + cty.Object(map[string]cty.Type{ + "a": cty.String, + }), + cty.DynamicPseudoType, + }), + cty.List(cty.DynamicPseudoType), + }, + cty.NilType, + nil, + }, { // unifies to the same result as above, since the only difference // is the addition of a list