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
This commit is contained in:
metagn
2023-04-13 12:56:16 +03:00
committed by GitHub
parent c33ab0ba38
commit 3f51b6f73d
4 changed files with 38 additions and 13 deletions

View File

@@ -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:

View File

@@ -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

View File

@@ -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

View File

@@ -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()