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.nim148
-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.nim4
-rw-r--r--tests/discard/tneedsdiscard_in_for.nim22
-rw-r--r--tests/discard/tvoidcontext.nim2
8 files changed, 226 insertions, 4 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
index 99adcfd30..84b669ed8 100644
--- a/tests/discard/tdiscardable.nim
+++ b/tests/discard/tdiscardable.nim
@@ -1,3 +1,16 @@
+discard """
+output: '''
+tdiscardable
+1
+1
+something defered
+something defered
+hi
+'''
+"""
+
+echo "tdiscardable"
+
 # Test the discardable pragma
 
 proc p(x, y: int): int {.discardable.} =
@@ -13,7 +26,7 @@ q[float](0.8, 0.2)
 
 # bug #942
 
-template maybeMod(x: Tinteger, module:Natural):expr =
+template maybeMod(x: SomeInteger, module: Natural): untyped =
   if module > 0: x mod module
   else: x
 
@@ -27,3 +40,136 @@ proc bar(b: int):int =
 
 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
index 2df3531e7..75cd9d2df 100644
--- a/tests/discard/tneedsdiscard.nim
+++ b/tests/discard/tneedsdiscard.nim
@@ -1,10 +1,10 @@
 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
-  errormsg: "value of type 'bool' has to be discarded"
 """
 
 proc p =
-  var f: TFile
+  var f: File
   echo "hi"
 
   open(f, "arg.txt")
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
index c3ea68bae..9e2b913ad 100644
--- a/tests/discard/tvoidcontext.nim
+++ b/tests/discard/tvoidcontext.nim
@@ -1,5 +1,5 @@
 discard """
-  errormsg: "value of type 'string' has to be discarded"
+  errormsg: '''expression '"invalid"' is of type 'string' and has to be used (or discarded)'''
   line: 12
 """