Skip to content

Commit

Permalink
cty: Treat unrefined numeric ranges as infinities
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apparentlymart committed Feb 3, 2023
1 parent b95d73b commit 448ca74
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cty/unknown_refinement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions cty/value_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 448ca74

Please sign in to comment.