From db58b88c2d302773c44a2a7ebf8d3ea6e8b2edc7 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 15 Oct 2018 14:07:46 -0700 Subject: [PATCH] lang/funcs: short-circuit with unknown index and tuple collection Since we need to know the index to know the result type for a tuple, we need a special case here to deal with that situation and return cty.DynamicVal; we can't predict the result type exactly until we know the element type. --- lang/funcs/collection.go | 6 ++++++ lang/funcs/collection_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lang/funcs/collection.go b/lang/funcs/collection.go index e42ef0c4d..af8008c16 100644 --- a/lang/funcs/collection.go +++ b/lang/funcs/collection.go @@ -29,6 +29,12 @@ var ElementFunc = function.New(&function.Spec{ case listTy.IsListType(): return listTy.ElementType(), nil case listTy.IsTupleType(): + if !args[1].IsKnown() { + // If the index isn't known yet then we can't predict the + // result type since each tuple element can have its own type. + return cty.DynamicPseudoType, nil + } + etys := listTy.TupleElementTypes() var index int err := gocty.FromCtyValue(args[1], &index) diff --git a/lang/funcs/collection_test.go b/lang/funcs/collection_test.go index 7e2a49f87..42bcedf50 100644 --- a/lang/funcs/collection_test.go +++ b/lang/funcs/collection_test.go @@ -90,6 +90,24 @@ func TestElement(t *testing.T) { cty.NumberIntVal(2), cty.StringVal("hello"), }, + { + cty.TupleVal([]cty.Value{ + cty.StringVal("hello"), + cty.StringVal("bonjour"), + }), + cty.UnknownVal(cty.Number), + cty.DynamicVal, + }, + { + cty.UnknownVal(cty.Tuple([]cty.Type{cty.String, cty.Bool})), + cty.NumberIntVal(1), + cty.UnknownVal(cty.Bool), + }, + { + cty.UnknownVal(cty.Tuple([]cty.Type{cty.String, cty.String})), + cty.UnknownVal(cty.Number), + cty.DynamicVal, + }, } for _, test := range tests {