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

unknown upgraded splat values may have no elements #495

Merged
merged 1 commit into from
Dec 1, 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
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