diff --git a/hclsyntax/expression.go b/hclsyntax/expression.go index 71eb46ee..8192d43a 100644 --- a/hclsyntax/expression.go +++ b/hclsyntax/expression.go @@ -721,18 +721,24 @@ func (e *ConditionalExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostic if trueResult.Type().IsCollectionType() && falseResult.Type().IsCollectionType() { if trueResult.Type().Equals(falseResult.Type()) { if !(trueResult.IsNull() || falseResult.IsNull()) { - trueLen := trueResult.Length() - falseLen := falseResult.Length() - if gt := trueLen.GreaterThan(falseLen); gt.IsKnown() { + // the bounds are not part of the final result value, so + // the marks are not needed + tr, _ := trueResult.Unmark() + fr, _ := falseResult.Unmark() + trueRange := tr.Range() + falseRange := fr.Range() + + if gt := trueResult.Length().GreaterThan(falseResult.Length()); gt.IsKnown() { + gt, _ := gt.Unmark() b := cty.UnknownVal(resultType).Refine() if gt.True() { b = b. - CollectionLengthLowerBound(falseResult.Range().LengthLowerBound()). - CollectionLengthUpperBound(trueResult.Range().LengthUpperBound()) + CollectionLengthLowerBound(falseRange.LengthLowerBound()). + CollectionLengthUpperBound(trueRange.LengthUpperBound()) } else { b = b. - CollectionLengthLowerBound(trueResult.Range().LengthLowerBound()). - CollectionLengthUpperBound(falseResult.Range().LengthUpperBound()) + CollectionLengthLowerBound(trueRange.LengthLowerBound()). + CollectionLengthUpperBound(falseRange.LengthUpperBound()) } b = b.NotNull() // If neither of the results is null then the result can't be either return b.NewValue().WithSameMarks(condResult).WithSameMarks(trueResult).WithSameMarks(falseResult), diags @@ -1740,7 +1746,8 @@ func (e *SplatExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) { if ty.IsListType() && sourceVal.Type().IsCollectionType() { // We can refine the length of an unknown list result based on // the source collection's own length. - sourceRng := sourceVal.Range() + sv, _ := sourceVal.Unmark() + sourceRng := sv.Range() ret = ret.Refine(). CollectionLengthLowerBound(sourceRng.LengthLowerBound()). CollectionLengthUpperBound(sourceRng.LengthUpperBound()). diff --git a/hclsyntax/expression_test.go b/hclsyntax/expression_test.go index e64047a0..3bfef63c 100644 --- a/hclsyntax/expression_test.go +++ b/hclsyntax/expression_test.go @@ -1971,6 +1971,20 @@ EOT cty.UnknownVal(cty.Set(cty.String)).Refine().NotNull().CollectionLengthLowerBound(1).CollectionLengthUpperBound(4).NewValue(), // deduced through refinements 0, }, + { + `unknown ? amr : bmr`, + &hcl.EvalContext{ + Variables: map[string]cty.Value{ + "unknown": cty.UnknownVal(cty.Bool), + "amr": cty.UnknownVal(cty.Set(cty.String)).Mark("test").Refine(). + CollectionLengthLowerBound(1).CollectionLengthUpperBound(2).NewValue(), + "bmr": cty.UnknownVal(cty.Set(cty.String)).Mark("test").Refine(). + CollectionLengthLowerBound(3).CollectionLengthUpperBound(4).NewValue(), + }, + }, + cty.UnknownVal(cty.Set(cty.String)).Refine().NotNull().CollectionLengthLowerBound(1).CollectionLengthUpperBound(4).NewValue().Mark("test"), // deduced through refinements + 0, + }, { `unknown ? a : b`, &hcl.EvalContext{