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
be dynamic.
  • Loading branch information
jbardin committed Dec 1, 2021
1 parent a497928 commit fadd95c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
14 changes: 9 additions & 5 deletions hclsyntax/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1439,10 +1439,10 @@ func (e *SplatExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
// upgrade to a different number of elements depending on whether
// sourceVal becomes null or not.
// We record this condition here so we can process any remaining
// expression after the * to derive the correct type. For example, it
// is valid to use a splat on a single object to retrieve a list of a
// single attribute, which means the final expression type still needs
// to be determined.
// expression after the * to verify the result of the traversal. For
// example, it is valid to use a splat on a single object to retrieve a
// list of a single attribute, but we still need to check if that
// attribute actually exists.
upgradedUnknown = !sourceVal.IsKnown()

sourceVal = cty.TupleVal([]cty.Value{sourceVal})
Expand Down Expand Up @@ -1512,7 +1512,11 @@ func (e *SplatExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
}
e.Item.clearValue(ctx) // clean up our temporary value

if !isKnown || upgradedUnknown {
if upgradedUnknown {
return cty.DynamicVal, diags
}

if !isKnown {
// We'll ingore the resultTy diagnostics in this case since they
// will just be the same errors we saw while iterating above.
ty, _ := resultTy()
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.DynamicVal,
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.DynamicVal,
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.DynamicVal,
0,
},
{
Expand Down

0 comments on commit fadd95c

Please sign in to comment.