summary refs log tree commit diff stats
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2023-04-13 12:56:16 +0300
committerGitHub <noreply@github.com>2023-04-13 11:56:16 +0200
commit3f51b6f73ddc476c645f1e3e0205d2f203eaf8e2 (patch)
treec09632d8d7972deee15bfc1ff11197c926b8870e
parentc33ab0ba38408fc806b44246a3d393640d6fc9cb (diff)
downloadNim-3f51b6f73ddc476c645f1e3e0205d2f203eaf8e2.tar.gz
set module symbol type to None instead of nil for discard check (#21657)
* set module symbol type to None instead of nil

fixes #19225

* alright
-rw-r--r--compiler/semexprs.nim6
-rw-r--r--compiler/semstmts.nim25
-rw-r--r--testament/specs.nim5
-rw-r--r--tests/modules/tmodulesymtype.nim15
4 files changed, 38 insertions, 13 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index a765a92b9..7e7499e45 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -1353,6 +1353,12 @@ proc semSym(c: PContext, n: PNode, sym: PSym, flags: TExprFlags): PNode =
     markUsed(c, n.info, s)
     onUse(n.info, s)
     result = newSymNode(s, n.info)
+  of skModule:
+    # make sure type is None and not nil for discard checking
+    if efWantStmt in flags: s.typ = newTypeS(tyNone, c)
+    markUsed(c, n.info, s)
+    onUse(n.info, s)
+    result = newSymNode(s, n.info)
   else:
     let info = getCallLineInfo(n)
     #if efInCall notin flags:
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index ff144ea2e..3701e09f5 100644
--- a/compiler/semstmts.nim
+++ b/compiler/semstmts.nim
@@ -161,18 +161,19 @@ proc discardCheck(c: PContext, result: PNode, flags: TExprFlags) =
       if result.typ.kind == tyNone:
         localError(c.config, result.info, "expression has no type: " &
                renderTree(result, {renderNoComments}))
-      var n = result
-      while n.kind in skipForDiscardable:
-        if n.kind == nkTryStmt: n = n[0]
-        else: n = n.lastSon
-      var s = "expression '" & $n & "' is of type '" &
-          result.typ.typeToString & "' and has to be used (or discarded)"
-      if result.info.line != n.info.line or
-          result.info.fileIndex != n.info.fileIndex:
-        s.add "; start of expression here: " & c.config$result.info
-      if result.typ.kind == tyProc:
-        s.add "; for a function call use ()"
-      localError(c.config, n.info, s)
+      else:
+        var n = result
+        while n.kind in skipForDiscardable:
+          if n.kind == nkTryStmt: n = n[0]
+          else: n = n.lastSon
+        var s = "expression '" & $n & "' is of type '" &
+            result.typ.typeToString & "' and has to be used (or discarded)"
+        if result.info.line != n.info.line or
+            result.info.fileIndex != n.info.fileIndex:
+          s.add "; start of expression here: " & c.config$result.info
+        if result.typ.kind == tyProc:
+          s.add "; for a function call use ()"
+        localError(c.config, n.info, s)
 
 proc semIf(c: PContext, n: PNode; flags: TExprFlags; expectedType: PType = nil): PNode =
   result = n
diff --git a/testament/specs.nim b/testament/specs.nim
index 9edbde3b7..744d1f75a 100644
--- a/testament/specs.nim
+++ b/testament/specs.nim
@@ -222,7 +222,10 @@ proc extractErrorMsg(s: string; i: int; line: var int; col: var int; spec: var T
 
   while result < s.len-1:
     if s[result] == '\n':
-      msg.add '\n'
+      if result > 0 and s[result - 1] == '\r':
+        msg[^1] = '\n'
+      else:
+        msg.add '\n'
       inc result
       inc line
       col = 1
diff --git a/tests/modules/tmodulesymtype.nim b/tests/modules/tmodulesymtype.nim
new file mode 100644
index 000000000..b1378ab69
--- /dev/null
+++ b/tests/modules/tmodulesymtype.nim
@@ -0,0 +1,15 @@
+discard """
+cmd: "nim check $file"
+"""
+
+# bug #19225
+import std/sequtils
+sequtils #[tt.Error
+^ expression has no type: sequtils]#
+proc foo() =
+  block: #[tt.Error
+  ^ expression has no type: block:
+  sequtils]#
+    sequtils
+
+foo()