From 448ca74bd3f66560c5df9888f57b7bb22037705a Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 3 Feb 2023 15:53:12 -0800 Subject: [PATCH] cty: Treat unrefined numeric ranges as infinities Previously we were treating these as unknown, which is also a reasonable way to model a lack of bounds but is less convenient when we want to do arithmetic against the bounds. --- cty/unknown_refinement.go | 12 ++++++++---- cty/value_range.go | 6 ++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cty/unknown_refinement.go b/cty/unknown_refinement.go index 2e1fd7af..bd2c4493 100644 --- a/cty/unknown_refinement.go +++ b/cty/unknown_refinement.go @@ -246,8 +246,10 @@ func (b *RefinementBuilder) NumberRangeLowerBound(min Value, inclusive bool) *Re } } - wip.min = min - wip.minInc = inclusive + if min != NegativeInfinity { + wip.min = min + wip.minInc = inclusive + } wip.assertConsistentBounds() return b @@ -293,8 +295,10 @@ func (b *RefinementBuilder) NumberRangeUpperBound(max Value, inclusive bool) *Re } } - wip.max = max - wip.maxInc = inclusive + if max != PositiveInfinity { + wip.max = max + wip.maxInc = inclusive + } wip.assertConsistentBounds() return b diff --git a/cty/value_range.go b/cty/value_range.go index 8c9e6954..e512e365 100644 --- a/cty/value_range.go +++ b/cty/value_range.go @@ -137,6 +137,9 @@ func (r ValueRange) NumberLowerBound() (min Value, inclusive bool) { panic(fmt.Sprintf("NumberLowerBound for %#v", r.ty)) } if rfn, ok := r.raw.(*refinementNumber); ok && rfn.min != NilVal { + if !rfn.min.IsKnown() { + return NegativeInfinity, true + } return rfn.min, rfn.minInc } return UnknownVal(Number), false @@ -159,6 +162,9 @@ func (r ValueRange) NumberUpperBound() (max Value, inclusive bool) { panic(fmt.Sprintf("NumberUpperBound for %#v", r.ty)) } if rfn, ok := r.raw.(*refinementNumber); ok && rfn.max != NilVal { + if !rfn.max.IsKnown() { + return PositiveInfinity, true + } return rfn.max, rfn.maxInc } return UnknownVal(Number), false