summary refs log tree commit diff stats
path: root/tests/discard
diff options
context:
space:
mode:
Diffstat (limited to 'tests/discard')
-rw-r--r--tests/discard/t23677.nim12
-rw-r--r--tests/discard/tdiscardable.nim175
-rw-r--r--tests/discard/tfinallyerrmsg.nim19
-rw-r--r--tests/discard/tillegaldiscard.nim9
-rw-r--r--tests/discard/tillegaldiscardtypes.nim14
-rw-r--r--tests/discard/tneedsdiscard.nim12
-rw-r--r--tests/discard/tneedsdiscard_in_for.nim22
-rw-r--r--tests/discard/tvoidcontext.nim12
8 files changed, 275 insertions, 0 deletions
diff --git a/tests/discard/t23677.nim b/tests/discard/t23677.nim
new file mode 100644
index 000000000..1ed7386bd
--- /dev/null
+++ b/tests/discard/t23677.nim
@@ -0,0 +1,12 @@
+discard """
+  errormsg: "expression '0' is of type 'int literal(0)' and has to be used (or discarded); start of expression here: t23677.nim(1, 1)"
+  line: 10
+  column: 3
+"""
+
+# issue #23677
+
+if true:
+  0
+else: 
+  raise newException(ValueError, "err") 
diff --git a/tests/discard/tdiscardable.nim b/tests/discard/tdiscardable.nim
new file mode 100644
index 000000000..84b669ed8
--- /dev/null
+++ b/tests/discard/tdiscardable.nim
@@ -0,0 +1,175 @@
+discard """
+output: '''
+tdiscardable
+1
+1
+something defered
+something defered
+hi
+'''
+"""
+
+echo "tdiscardable"
+
+# Test the discardable pragma
+
+proc p(x, y: int): int {.discardable.} =
+  return x + y
+
+# test that it is inherited from generic procs too:
+proc q[T](x, y: T): T {.discardable.} =
+  return x + y
+
+
+p(8, 2)
+q[float](0.8, 0.2)
+
+# bug #942
+
+template maybeMod(x: SomeInteger, module: Natural): untyped =
+  if module > 0: x mod module
+  else: x
+
+proc foo(b: int):int =
+  var x = 1
+  result = x.maybeMod(b) # Works fine
+
+proc bar(b: int):int =
+  result = 1
+  result = result.maybeMod(b) # Error: value returned by statement has to be discarded
+
+echo foo(0)
+echo bar(0)
+
+# bug #9726
+
+proc foo: (proc: int) =
+  proc bar: int = 1
+  return bar
+
+discard foo()
+
+# bug #10842
+
+proc myDiscardable(): int {.discardable.} =
+  discard
+
+proc main1() =
+  defer:
+    echo "something defered"
+  discard myDiscardable()
+
+proc main2() =
+  defer:
+    echo "something defered"
+  myDiscardable()
+
+main1()
+main2()
+
+block: # bug #13583
+  block:
+    proc hello(): int {.discardable.} = 12
+
+    iterator test(): int {.closure.} =
+      while true:
+        hello()
+
+    let t = test
+
+  block:
+    proc hello(): int {.discardable.} = 12
+
+    iterator test(): int {.closure.} =
+      while true:
+        block:
+          yield 12
+          hello()
+
+    let t = test
+    doAssert t() == 12
+
+  block:
+    proc hello(): int {.discardable.} = 12
+
+    iterator test(): int {.closure.} =
+      while true:
+        yield 12
+        hello()
+
+    let t = test
+    doAssert t() == 12
+
+block:
+  proc bar(): string {.discardable.} =
+    "15"
+
+  proc foo(): int =
+    while true:
+      raise newException(ValueError, "check")
+    12
+
+  doAssertRaises(ValueError):
+    doAssert foo() == 12
+
+block: # issue #10440
+  proc x(): int {.discardable.} = discard
+  try:
+    x()
+  finally:
+    echo "hi"
+
+import macros
+
+block: # issue #14665
+  macro test(): untyped =   
+    let b = @[1, 2, 3, 4]
+
+    result = nnkStmtList.newTree()
+    var i = 0
+    while i < b.len:
+      if false:
+        # this quote do is mandatory, removing it fixes the problem
+        result.add quote do:
+          let testtest = 5
+      else:
+        result.add quote do:
+          let test = 6
+        inc i
+        # removing this continue fixes the problem too
+        continue
+      inc i
+  test()
+
+block: # bug #23775
+  proc retInt(): int {.discardable.} =
+    42
+
+  proc retString(): string {.discardable.} =
+    "text"
+
+  type
+    Enum = enum
+      A, B, C, D
+
+  proc doStuff(msg: Enum) =
+    case msg:
+    of A:
+      retString()
+    of B:
+      retInt()
+    of C:
+      discard retString()
+    else:
+      let _ = retString()
+
+  doStuff(C)
+
+block:
+  proc test(): (int, int) {.discardable.} =
+    discard
+
+  if true:
+    test()
+  else:
+    quit()
diff --git a/tests/discard/tfinallyerrmsg.nim b/tests/discard/tfinallyerrmsg.nim
new file mode 100644
index 000000000..fbc8140aa
--- /dev/null
+++ b/tests/discard/tfinallyerrmsg.nim
@@ -0,0 +1,19 @@
+discard """
+  cmd: "nim check $file"
+"""
+
+block: # issue #19672
+  try:
+    10 #[tt.Error
+    ^ expression '10' is of type 'int literal(10)' and has to be used (or discarded); start of expression here: tfinallyerrmsg.nim(5, 1)]#
+  finally:
+    echo "Finally block"
+
+block: # issue #13871
+  template t(body: int) =
+    try:
+      body
+    finally:
+      echo "expression"
+  t: 2 #[tt.Error
+     ^ expression '2' is of type 'int literal(2)' and has to be used (or discarded)]#
diff --git a/tests/discard/tillegaldiscard.nim b/tests/discard/tillegaldiscard.nim
new file mode 100644
index 000000000..757f4e727
--- /dev/null
+++ b/tests/discard/tillegaldiscard.nim
@@ -0,0 +1,9 @@
+discard """
+  errormsg: "illegal discard"
+  line: 9
+"""
+
+proc pop[T](arg: T): T =
+  echo arg
+
+discard tillegaldiscard.pop
diff --git a/tests/discard/tillegaldiscardtypes.nim b/tests/discard/tillegaldiscardtypes.nim
new file mode 100644
index 000000000..b7877bcd2
--- /dev/null
+++ b/tests/discard/tillegaldiscardtypes.nim
@@ -0,0 +1,14 @@
+discard """
+  cmd: "nim check $file"
+  errormsg: "statement returns no value that can be discarded"
+  nimout: '''
+tillegaldiscardtypes.nim(11, 3) Error: statement returns no value that can be discarded
+tillegaldiscardtypes.nim(12, 3) Error: statement returns no value that can be discarded
+'''
+"""
+
+proc b(v: int) = # bug #21360
+  discard @[]
+  discard []
+
+b(0)
\ No newline at end of file
diff --git a/tests/discard/tneedsdiscard.nim b/tests/discard/tneedsdiscard.nim
new file mode 100644
index 000000000..75cd9d2df
--- /dev/null
+++ b/tests/discard/tneedsdiscard.nim
@@ -0,0 +1,12 @@
+discard """
+  errormsg: '''expression 'open(f, "arg.txt", fmRead, -1)' is of type 'bool' and has to be used (or discarded); start of expression here: tneedsdiscard.nim(7, 3)'''
+  line: 10
+"""
+
+proc p =
+  var f: File
+  echo "hi"
+
+  open(f, "arg.txt")
+
+p()
diff --git a/tests/discard/tneedsdiscard_in_for.nim b/tests/discard/tneedsdiscard_in_for.nim
new file mode 100644
index 000000000..7685bd577
--- /dev/null
+++ b/tests/discard/tneedsdiscard_in_for.nim
@@ -0,0 +1,22 @@
+discard """
+  errormsg: '''expression 'premultiply(app.gradient[i])' is of type 'Rgba8' and has to be used (or discarded)'''
+  line: 22
+"""
+
+# bug #9076
+type
+  Rgba8 = object
+
+proc premultiply*(c: var Rgba8): var Rgba8 =
+  return c
+
+type
+  App = ref object
+    gradient: seq[Rgba8]
+
+method onDraw(app: App) {.base.} =
+  var
+    width  = 100'f64
+
+  for i in 0..<width.int:
+    app.gradient[i].premultiply()
diff --git a/tests/discard/tvoidcontext.nim b/tests/discard/tvoidcontext.nim
new file mode 100644
index 000000000..9e2b913ad
--- /dev/null
+++ b/tests/discard/tvoidcontext.nim
@@ -0,0 +1,12 @@
+discard """
+  errormsg: '''expression '"invalid"' is of type 'string' and has to be used (or discarded)'''
+  line: 12
+"""
+
+proc valid*(): string =
+  let x = 317
+  "valid"
+
+proc invalid*(): string =
+  result = "foo"
+  "invalid"