Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions compiler/lookups.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}:
Expand Down
9 changes: 3 additions & 6 deletions compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -965,8 +965,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
Expand Down
32 changes: 24 additions & 8 deletions compiler/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1473,12 +1482,19 @@ proc skipHiddenSubConv*(n: PNode): 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)
Expand Down
2 changes: 1 addition & 1 deletion tests/errmsgs/t8794.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
'''
"""

Expand Down
4 changes: 2 additions & 2 deletions tests/errmsgs/undeclared_routime.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)]'
'''
"""

Expand Down
2 changes: 1 addition & 1 deletion tests/misc/tnoop.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down