Skip to content

Commit

Permalink
fix range[enum] type conversion (#13204) [backport]
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper Jenkins authored and Araq committed Jan 20, 2020
1 parent 470faa9 commit 0606b6a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
5 changes: 3 additions & 2 deletions compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1149,9 +1149,10 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
f.n[i] = tryResolvingStaticExpr(c, f.n[i])
result = typeRangeRel(f, a)
else:
if skipTypes(f, {tyRange}).kind == a.kind:
let f = skipTypes(f, {tyRange})
if f.kind == a.kind and (f.kind != tyEnum or sameEnumTypes(f, a)):
result = isIntConv
elif isConvertibleToRange(skipTypes(f, {tyRange}), a):
elif isConvertibleToRange(f, a):
result = isConvertible # a convertible to f
of tyInt: result = handleRange(f, a, tyInt8, tyInt32)
of tyInt8: result = handleRange(f, a, tyInt8, tyInt8)
Expand Down
33 changes: 33 additions & 0 deletions tests/range/tenums.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
discard """
cmd: "nim check --hints:off $file"
errormsg: "type mismatch: got <BC>"
nimout: '''
tenums.nim(32, 20) Error: type mismatch: got <Letters>
but expected one of:
proc takesChristmasColor(color: ChristmasColors)
first type mismatch at position: 1
required type for color: ChristmasColors
but expression 'A' is of type: Letters
expression: takesChristmasColor(A)
tenums.nim(33, 20) Error: type mismatch: got <BC>
but expected one of:
proc takesChristmasColor(color: ChristmasColors)
first type mismatch at position: 1
required type for color: ChristmasColors
but expression 'BC(C)' is of type: BC
expression: takesChristmasColor(BC(C))
'''
"""

type
Colors = enum Red, Green, Blue
ChristmasColors = range[Red .. Green]
Letters = enum A, B, C
BC = range[B .. C]

proc takesChristmasColor(color: ChristmasColors) = discard
takesChristmasColor(Green)
takesChristmasColor(A)
takesChristmasColor(BC(C))

0 comments on commit 0606b6a

Please sign in to comment.