Skip to content

Commit

Permalink
make int literals with range type match their base type better than o…
Browse files Browse the repository at this point in the history
…ther int types (#24017)

This is a very niche case encountered in #24012, where an int literal
got a `range` type as a result of a generic instantiation (in
`tgenericcomputedrange`), I can't think of another test case. The base
type of the range being `int` made it match `int` with `isSubrange` as
in the first `if` branch, but other int types like `int32` matched with
`isFromIntLit` which is a better match.

Instead, int literals with range type now:

1. match their base type with `isFromIntLit`,
2. don't match other int types with `isFromIntLit`, instead giving
`isConvertible` as in the last `if` branch in `handleRange`.
  • Loading branch information
metagn authored Aug 27, 2024
1 parent 7e88091 commit f09c549
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,16 @@ proc handleRange(c: PContext, f, a: PType, min, max: TTypeKind): TTypeRelation =
let k = ab.kind
let nf = c.config.normalizeKind(f.kind)
let na = c.config.normalizeKind(k)
if k == f.kind: result = isSubrange
elif k == tyInt and f.kind in {tyRange, tyInt..tyInt64,
tyUInt..tyUInt64} and
if k == f.kind:
# `a` is a range type matching its base type
# see very bottom for range types matching different types
if isIntLit(ab):
# range type can only give isFromIntLit for base type
result = isFromIntLit
else:
result = isSubrange
elif a.kind == tyInt and f.kind in {tyRange, tyInt..tyInt64,
tyUInt..tyUInt64} and
isIntLit(ab) and getInt(ab.n) >= firstOrd(nil, f) and
getInt(ab.n) <= lastOrd(nil, f):
# passing 'nil' to firstOrd/lastOrd here as type checking rules should
Expand Down

0 comments on commit f09c549

Please sign in to comment.