Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disallow string range as of label #1147

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/ast/report_enums.nim
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ type
rsemTIsNotAConcreteType
rsemProcIsNotAConcreteType
rsemRangeIsEmpty
rsemStringRangeNotAllowed

rsemCannotInstantiate
rsemCannotInstantiateWithParameter
Expand Down
3 changes: 3 additions & 0 deletions compiler/front/cli_reporter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,9 @@ proc reportBody*(conf: ConfigRef, r: SemReport): string =
of rsemRangeIsEmpty:
result = "range is empty"

of rsemStringRangeNotAllowed:
result = "cannot create a range of strings"

of rsemExpectedOrdinalOrFloat:
result = "ordinal or float type expected"

Expand Down
3 changes: 3 additions & 0 deletions compiler/sem/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,9 @@ proc semBranchRange(c: PContext, t, a, b: PNode, covered: var Int128): PNode =
result.add(bt)
if emptyRange(ac, bc):
localReport(c.config, b, reportSem rsemRangeIsEmpty)
elif t[0].typ.skipTypes(abstractInst).kind == tyString:
# XXX: ``nkError`` needs to be used here
localReport(c.config, b, reportSem rsemStringRangeNotAllowed)
else: covered = covered + getOrdValue(bc) + 1 - getOrdValue(ac)

proc semCaseBranchRange(c: PContext, t, b: PNode,
Expand Down
9 changes: 9 additions & 0 deletions tests/errmsgs/tstring_range.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
discard """
errormsg: "cannot create a range of strings"
line: 8
"""

var x = "a"
case x
of "a".."def":
discard
56 changes: 39 additions & 17 deletions tests/lang_stmts/casestmt/tcasestmt.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
discard """
targets: "c js vm"
knownIssue.js: '''
The code generator rejects 'of' branch ranges > 2^16
'''
output:
'''
Not found!
Expand All @@ -14,7 +18,9 @@ an identifier
OK
OK
OK
ayyydd
a
yyy
dd
'''
"""

Expand Down Expand Up @@ -133,12 +139,12 @@ block tcasestm:
i: int

case y
of eA: write(stdout, "a")
of eB, eC: write(stdout, "b or c")
of eA: echo "a"
of eB, eC: echo "b or c"

case x
of "Andreas", "Rumpf": write(stdout, "Hallo Meister!")
of "aa", "bb": write(stdout, "Du bist nicht mein Meister")
of "Andreas", "Rumpf": echo "Hallo Meister!"
of "aa", "bb": echo "Du bist nicht mein Meister"
of "cc", "hash", "when": discard
of "will", "it", "finally", "be", "generated": discard

Expand All @@ -148,7 +154,7 @@ block tcasestm:
elif x == "Ha":
"cc"
elif x == "yyy":
write(stdout, x)
echo x
"dd"
else:
"zz"
Expand Down Expand Up @@ -178,8 +184,8 @@ block tcasestm:
doAssert(a == true)
doAssert(b == false)

static:
#bug #7407
block:
# bug https://github.com/nim-lang/nim/issues/7407
let bstatic = "N".toBool()
doAssert(bstatic == false)

Expand Down Expand Up @@ -272,18 +278,34 @@ func foo2(b, input: string): int =
else: continue
else: return


static:
doAssert(foo("3") == 3)
doAssert(foo("a") == 0)
doAssert(foo2("Y", "a2") == 0)
doAssert(foo2("Y", "2a") == 2)
doAssert(foo2("N", "a3") == 3)
doAssert(foo2("z", "2") == 0)

doAssert(foo("3") == 3)
doAssert(foo("a") == 0)
doAssert(foo2("Y", "a2") == 0)
doAssert(foo2("Y", "2a") == 2)
doAssert(foo2("N", "a3") == 3)
doAssert(foo2("z", "2") == 0)

block float_slice_list:
## Tests for slice lists of float values
proc p(f: float): int =
case f
of 5.0, -2.0 .. 1.2, 6.0: 0 # slice-list with gaps
of 1.5: 1
of 20.0 .. 32.0: 2
else: 3

# direct matches
doAssert p(5.0) == 0
doAssert p(-2.0) == 0
doAssert p(1.2) == 0
doAssert p(6.0) == 0
doAssert p(1.5) == 1
doAssert p(20.0) == 2
doAssert p(32.0) == 2

# in ranges
doAssert p(0.1) == 0
doAssert p(-1.0) == 0
doAssert p(1.1) == 0
doAssert p(1.6) == 3
doAssert p(25.0) == 2
51 changes: 0 additions & 51 deletions tests/vm/tcasestmt.nim

This file was deleted.

Loading