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 {