Skip to content

Commit 0606b6a

Browse files
Jasper JenkinsAraq
Jasper Jenkins
authored andcommitted
fix range[enum] type conversion (#13204) [backport]
1 parent 470faa9 commit 0606b6a

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

compiler/sigmatch.nim

+3-2
Original file line numberDiff line numberDiff line change
@@ -1149,9 +1149,10 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
11491149
f.n[i] = tryResolvingStaticExpr(c, f.n[i])
11501150
result = typeRangeRel(f, a)
11511151
else:
1152-
if skipTypes(f, {tyRange}).kind == a.kind:
1152+
let f = skipTypes(f, {tyRange})
1153+
if f.kind == a.kind and (f.kind != tyEnum or sameEnumTypes(f, a)):
11531154
result = isIntConv
1154-
elif isConvertibleToRange(skipTypes(f, {tyRange}), a):
1155+
elif isConvertibleToRange(f, a):
11551156
result = isConvertible # a convertible to f
11561157
of tyInt: result = handleRange(f, a, tyInt8, tyInt32)
11571158
of tyInt8: result = handleRange(f, a, tyInt8, tyInt8)

tests/range/tenums.nim

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
discard """
2+
cmd: "nim check --hints:off $file"
3+
errormsg: "type mismatch: got <BC>"
4+
nimout: '''
5+
tenums.nim(32, 20) Error: type mismatch: got <Letters>
6+
but expected one of:
7+
proc takesChristmasColor(color: ChristmasColors)
8+
first type mismatch at position: 1
9+
required type for color: ChristmasColors
10+
but expression 'A' is of type: Letters
11+
12+
expression: takesChristmasColor(A)
13+
tenums.nim(33, 20) Error: type mismatch: got <BC>
14+
but expected one of:
15+
proc takesChristmasColor(color: ChristmasColors)
16+
first type mismatch at position: 1
17+
required type for color: ChristmasColors
18+
but expression 'BC(C)' is of type: BC
19+
20+
expression: takesChristmasColor(BC(C))
21+
'''
22+
"""
23+
24+
type
25+
Colors = enum Red, Green, Blue
26+
ChristmasColors = range[Red .. Green]
27+
Letters = enum A, B, C
28+
BC = range[B .. C]
29+
30+
proc takesChristmasColor(color: ChristmasColors) = discard
31+
takesChristmasColor(Green)
32+
takesChristmasColor(A)
33+
takesChristmasColor(BC(C))

0 commit comments

Comments
 (0)