Skip to content

Commit e5ecb58

Browse files
authored
Merge pull request #493 from hashicorp/jbardin/unknown-splat
splat expression upgrading an unknown value should be unknown
2 parents ca99bd2 + c3f2300 commit e5ecb58

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

hclsyntax/expression.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,9 +1432,22 @@ func (e *SplatExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
14321432
return cty.DynamicVal, diags
14331433
}
14341434

1435+
upgradedUnknown := false
14351436
if autoUpgrade {
1437+
// If we're upgrading an unknown value to a tuple/list, the result
1438+
// cannot be known. Otherwise a tuple containing an unknown value will
1439+
// upgrade to a different number of elements depending on whether
1440+
// sourceVal becomes null or not.
1441+
// We record this condition here so we can process any remaining
1442+
// expression after the * to derive the correct type. For example, it
1443+
// is valid to use a splat on a single object to retrieve a list of a
1444+
// single attribute, which means the final expression type still needs
1445+
// to be determined.
1446+
upgradedUnknown = !sourceVal.IsKnown()
1447+
14361448
sourceVal = cty.TupleVal([]cty.Value{sourceVal})
14371449
sourceTy = sourceVal.Type()
1450+
14381451
}
14391452

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

1502-
if !isKnown {
1515+
if !isKnown || upgradedUnknown {
15031516
// We'll ingore the resultTy diagnostics in this case since they
15041517
// will just be the same errors we saw while iterating above.
15051518
ty, _ := resultTy()

hclsyntax/expression_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,16 @@ upper(
11351135
}),
11361136
0,
11371137
},
1138+
{
1139+
`unkstr[*]`,
1140+
&hcl.EvalContext{
1141+
Variables: map[string]cty.Value{
1142+
"unkstr": cty.UnknownVal(cty.String),
1143+
},
1144+
},
1145+
cty.UnknownVal(cty.Tuple([]cty.Type{cty.String})),
1146+
0,
1147+
},
11381148
{
11391149
`unkstr.*.name`,
11401150
&hcl.EvalContext{
@@ -1164,9 +1174,7 @@ upper(
11641174
})),
11651175
},
11661176
},
1167-
cty.TupleVal([]cty.Value{
1168-
cty.UnknownVal(cty.String),
1169-
}),
1177+
cty.UnknownVal(cty.Tuple([]cty.Type{cty.String})),
11701178
0,
11711179
},
11721180
{

0 commit comments

Comments
 (0)