Skip to content

Commit

Permalink
unknown upgraded splat values may have no elems
Browse files Browse the repository at this point in the history
When upgrading an unknown splat value, the resulting collection may have
0 elements if the value ends up being `null`. This means the type must
either be dynamic or a list.
  • Loading branch information
jbardin committed Dec 1, 2021
1 parent a497928 commit 1e7e388
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
21 changes: 18 additions & 3 deletions hclsyntax/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1513,9 +1513,24 @@ func (e *SplatExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
e.Item.clearValue(ctx) // clean up our temporary value

if !isKnown || upgradedUnknown {
// We'll ingore the resultTy diagnostics in this case since they
// will just be the same errors we saw while iterating above.
ty, _ := resultTy()
// Only check the type diags for errors to return a DynamicVal when
// upgrading instead of a tuple, but return the original diagnostics
// which already contain the relevant errors.
ty, tyDiags := resultTy()
if tyDiags.HasErrors() {
ty = cty.DynamicPseudoType
}

if upgradedUnknown {
// Because of the way null values are upgraded we may have 0 or 1
// elements, so we must convert the type to an unknown list as long
// as we have a single element type.
if ty.IsTupleType() && ty.Length() == 1 {
// All upgraded should
ty = cty.List(ty.TupleElementType(0))
}
}

return cty.UnknownVal(ty), diags
}

Expand Down
18 changes: 15 additions & 3 deletions hclsyntax/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ upper(
"unkstr": cty.UnknownVal(cty.String),
},
},
cty.UnknownVal(cty.Tuple([]cty.Type{cty.String})),
cty.UnknownVal(cty.List(cty.String)),
0,
},
{
Expand All @@ -1152,7 +1152,7 @@ upper(
"unkstr": cty.UnknownVal(cty.String),
},
},
cty.UnknownVal(cty.Tuple([]cty.Type{cty.DynamicPseudoType})),
cty.DynamicVal,
1, // a string has no attribute "name"
},
{
Expand All @@ -1174,7 +1174,19 @@ upper(
})),
},
},
cty.UnknownVal(cty.Tuple([]cty.Type{cty.String})),
cty.UnknownVal(cty.List(cty.String)),
0,
},
{
`unkobj.*.names`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"unkobj": cty.UnknownVal(cty.Object(map[string]cty.Type{
"names": cty.List(cty.String),
})),
},
},
cty.UnknownVal(cty.List(cty.List(cty.String))),
0,
},
{
Expand Down

0 comments on commit 1e7e388

Please sign in to comment.