Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

splat expression upgrading an unknown value should be unknown #493

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion hclsyntax/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1432,9 +1432,22 @@ func (e *SplatExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
return cty.DynamicVal, diags
}

upgradedUnknown := false
if autoUpgrade {
// If we're upgrading an unknown value to a tuple/list, the result
// cannot be known. Otherwise a tuple containing an unknown value will
// 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.
upgradedUnknown = !sourceVal.IsKnown()

sourceVal = cty.TupleVal([]cty.Value{sourceVal})
sourceTy = sourceVal.Type()

}

// We'll compute our result type lazily if we need it. In the normal case
Expand Down Expand Up @@ -1499,7 +1512,7 @@ func (e *SplatExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
}
e.Item.clearValue(ctx) // clean up our temporary value

if !isKnown {
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()
Expand Down
14 changes: 11 additions & 3 deletions hclsyntax/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,16 @@ upper(
}),
0,
},
{
`unkstr[*]`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"unkstr": cty.UnknownVal(cty.String),
},
},
cty.UnknownVal(cty.Tuple([]cty.Type{cty.String})),
0,
},
{
`unkstr.*.name`,
&hcl.EvalContext{
Expand Down Expand Up @@ -1164,9 +1174,7 @@ upper(
})),
},
},
cty.TupleVal([]cty.Value{
cty.UnknownVal(cty.String),
}),
cty.UnknownVal(cty.Tuple([]cty.Type{cty.String})),
0,
},
{
Expand Down