honor --declaredLocs in more places, including type mismatch errors; also show kind with --declaredLocs (#15673)

* honor --declaredLocs in more places, including type mismatch errors
* fix tests
* show declaration location also when type mismatch names clash
This commit is contained in:
Timothee Cour
2020-10-27 15:19:28 +02:00
committed by GitHub
parent 218acfe367
commit 0fb878324e
7 changed files with 40 additions and 24 deletions

View File

@@ -154,12 +154,14 @@ type
scope*: PScope
inSymChoice: IntSet
proc getSymRepr*(conf: ConfigRef; s: PSym): string =
proc getSymRepr*(conf: ConfigRef; s: PSym, getDeclarationPath = true): string =
case s.kind
of routineKinds, skType:
result = getProcHeader(conf, s)
result = getProcHeader(conf, s, getDeclarationPath = getDeclarationPath)
else:
result = s.name.s
result = "'$1'" % s.name.s
if getDeclarationPath:
result.addDeclaredLoc(conf, s)
proc ensureNoMissingOrUnusedSymbols(c: PContext; scope: PScope) =
# check if all symbols have been used and defined:
@@ -172,7 +174,7 @@ proc ensureNoMissingOrUnusedSymbols(c: PContext; scope: PScope) =
# and slow 'suggest' down:
if missingImpls == 0:
localError(c.config, s.info, "implementation of '$1' expected" %
getSymRepr(c.config, s))
getSymRepr(c.config, s, getDeclarationPath=false))
inc missingImpls
elif {sfUsed, sfExported} * s.flags == {}:
if s.kind notin {skForVar, skParam, skMethod, skUnknown, skGenericParam, skEnumField}:

View File

@@ -231,8 +231,7 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
doAssert err.firstMismatch.formal != nil
candidates.add("\n required type for " & nameParam & ": ")
candidates.add typeToString(wanted)
if wanted.sym != nil:
candidates.addDeclaredLocMaybe(c.config, wanted.sym)
candidates.addDeclaredLocMaybe(c.config, wanted)
candidates.add "\n but expression '"
if err.firstMismatch.kind == kVarNeeded:
candidates.add renderNotLValue(nArg)
@@ -242,9 +241,7 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
candidates.add "' is of type: "
var got = nArg.typ
candidates.add typeToString(got)
if got.sym != nil:
candidates.addDeclaredLocMaybe(c.config, got.sym)
candidates.addDeclaredLocMaybe(c.config, got)
doAssert wanted != nil
if got != nil: effectProblem(wanted, got, candidates, c)
of kUnknown: discard "do not break 'nim check'"
@@ -320,7 +317,7 @@ proc getMsgDiagnostic(c: PContext, flags: TExprFlags, n, f: PNode): string =
var o: TOverloadIter
var sym = initOverloadIter(o, c, f)
while sym != nil:
result &= "\n found '$1' of kind '$2'" % [getSymRepr(c.config, sym), sym.kind.toHumanStr]
result &= "\n found $1" % [getSymRepr(c.config, sym)]
sym = nextOverloadIter(o, c, f)
let ident = considerQuotedIdent(c, f, n).s

View File

@@ -966,8 +966,9 @@ proc semIndirectOp(c: PContext, n: PNode, flags: TExprFlags): PNode =
# t.kind != tySequence(It is tyProc)
if typ.sym != nil and sfAnon notin typ.sym.flags and
typ.kind == tyProc:
msg.add(" = " &
typeToString(typ, preferDesc))
# when can `typ.sym != nil` ever happen?
msg.add(" = " & typeToString(typ, preferDesc))
msg.addDeclaredLocMaybe(c.config, typ)
localError(c.config, n.info, msg)
return errorNode(c, n)
result = nil

View File

@@ -124,13 +124,22 @@ proc isFloatLit*(t: PType): bool {.inline.} =
result = t.kind == tyFloat and t.n != nil and t.n.kind == nkFloatLit
proc addDeclaredLoc*(result: var string, conf: ConfigRef; sym: PSym) =
# result.add " [declared in " & conf$sym.info & "]"
result.add " [declared in " & toFileLineCol(conf, sym.info) & "]"
result.add " [$1 declared in $2]" % [sym.kind.toHumanStr, toFileLineCol(conf, sym.info)]
proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; sym: PSym) =
if optDeclaredLocs in conf.globalOptions:
if optDeclaredLocs in conf.globalOptions and sym != nil:
addDeclaredLoc(result, conf, sym)
proc addDeclaredLoc(result: var string, conf: ConfigRef; typ: PType) =
let typ = typ.skipTypes(abstractInst - {tyRange})
result.add " [$1" % typ.kind.toHumanStr
if typ.sym != nil:
result.add " declared in " & toFileLineCol(conf, typ.sym.info)
result.add "]"
proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; typ: PType) =
if optDeclaredLocs in conf.globalOptions: addDeclaredLoc(result, conf, typ)
proc addTypeHeader*(result: var string, conf: ConfigRef; typ: PType; prefer: TPreferedDesc = preferMixed; getDeclarationPath = true) =
result.add typeToString(typ, prefer)
if getDeclarationPath: result.addDeclaredLoc(conf, typ.sym)
@@ -1473,12 +1482,19 @@ proc skipHiddenSubConv*(n: PNode; idgen: IdGenerator): PNode =
proc typeMismatch*(conf: ConfigRef; info: TLineInfo, formal, actual: PType) =
if formal.kind != tyError and actual.kind != tyError:
let named = typeToString(formal)
let actualStr = typeToString(actual)
let formalStr = typeToString(formal)
let desc = typeToString(formal, preferDesc)
let x = if named == desc: named else: named & " = " & desc
var msg = "type mismatch: got <" &
typeToString(actual) & "> " &
"but expected '" & x & "'"
let x = if formalStr == desc: formalStr else: formalStr & " = " & desc
let verbose = actualStr == formalStr or optDeclaredLocs in conf.globalOptions
var msg = "type mismatch:"
if verbose: msg.add "\n"
msg.add " got <$1>" % actualStr
if verbose:
msg.addDeclaredLoc(conf, actual)
msg.add "\n"
msg.add " but expected '$1'" % x
if verbose: msg.addDeclaredLoc(conf, formal)
if formal.kind == tyProc and actual.kind == tyProc:
case compatibleEffects(formal, actual)

View File

@@ -2,7 +2,7 @@ discard """
cmd: "nim check $options $file"
errormsg: ""
nimout: '''
t8794.nim(39, 27) Error: undeclared field: 'a3' for type m8794.Foo3 [declared in m8794.nim(1, 6)]
t8794.nim(39, 27) Error: undeclared field: 'a3' for type m8794.Foo3 [type declared in m8794.nim(1, 6)]
'''
"""

View File

@@ -2,8 +2,8 @@ discard """
cmd: '''nim c --hints:off $file'''
errormsg: "attempting to call routine: 'myiter'"
nimout: '''undeclared_routime.nim(13, 15) Error: attempting to call routine: 'myiter'
found 'undeclared_routime.myiter(a: string)[declared in undeclared_routime.nim(10, 9)]' of kind 'iterator'
found 'undeclared_routime.myiter()[declared in undeclared_routime.nim(11, 9)]' of kind 'iterator'
found 'undeclared_routime.myiter(a: string)[iterator declared in undeclared_routime.nim(10, 9)]'
found 'undeclared_routime.myiter()[iterator declared in undeclared_routime.nim(11, 9)]'
'''
"""

View File

@@ -1,6 +1,6 @@
discard """
nimout: '''
found 'a' of kind 'var'
found 'a' [var declared in tnoop.nim(11, 3)]
'''
file: "tnoop.nim"
line: 13