Skip to content

Commit 0fb8783

Browse files
authored
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
1 parent 218acfe commit 0fb8783

File tree

7 files changed

+40
-24
lines changed

7 files changed

+40
-24
lines changed

compiler/lookups.nim

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,14 @@ type
154154
scope*: PScope
155155
inSymChoice: IntSet
156156

157-
proc getSymRepr*(conf: ConfigRef; s: PSym): string =
157+
proc getSymRepr*(conf: ConfigRef; s: PSym, getDeclarationPath = true): string =
158158
case s.kind
159159
of routineKinds, skType:
160-
result = getProcHeader(conf, s)
160+
result = getProcHeader(conf, s, getDeclarationPath = getDeclarationPath)
161161
else:
162-
result = s.name.s
162+
result = "'$1'" % s.name.s
163+
if getDeclarationPath:
164+
result.addDeclaredLoc(conf, s)
163165

164166
proc ensureNoMissingOrUnusedSymbols(c: PContext; scope: PScope) =
165167
# check if all symbols have been used and defined:
@@ -172,7 +174,7 @@ proc ensureNoMissingOrUnusedSymbols(c: PContext; scope: PScope) =
172174
# and slow 'suggest' down:
173175
if missingImpls == 0:
174176
localError(c.config, s.info, "implementation of '$1' expected" %
175-
getSymRepr(c.config, s))
177+
getSymRepr(c.config, s, getDeclarationPath=false))
176178
inc missingImpls
177179
elif {sfUsed, sfExported} * s.flags == {}:
178180
if s.kind notin {skForVar, skParam, skMethod, skUnknown, skGenericParam, skEnumField}:

compiler/semcall.nim

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
231231
doAssert err.firstMismatch.formal != nil
232232
candidates.add("\n required type for " & nameParam & ": ")
233233
candidates.add typeToString(wanted)
234-
if wanted.sym != nil:
235-
candidates.addDeclaredLocMaybe(c.config, wanted.sym)
234+
candidates.addDeclaredLocMaybe(c.config, wanted)
236235
candidates.add "\n but expression '"
237236
if err.firstMismatch.kind == kVarNeeded:
238237
candidates.add renderNotLValue(nArg)
@@ -242,9 +241,7 @@ proc presentFailedCandidates(c: PContext, n: PNode, errors: CandidateErrors):
242241
candidates.add "' is of type: "
243242
var got = nArg.typ
244243
candidates.add typeToString(got)
245-
if got.sym != nil:
246-
candidates.addDeclaredLocMaybe(c.config, got.sym)
247-
244+
candidates.addDeclaredLocMaybe(c.config, got)
248245
doAssert wanted != nil
249246
if got != nil: effectProblem(wanted, got, candidates, c)
250247
of kUnknown: discard "do not break 'nim check'"
@@ -320,7 +317,7 @@ proc getMsgDiagnostic(c: PContext, flags: TExprFlags, n, f: PNode): string =
320317
var o: TOverloadIter
321318
var sym = initOverloadIter(o, c, f)
322319
while sym != nil:
323-
result &= "\n found '$1' of kind '$2'" % [getSymRepr(c.config, sym), sym.kind.toHumanStr]
320+
result &= "\n found $1" % [getSymRepr(c.config, sym)]
324321
sym = nextOverloadIter(o, c, f)
325322

326323
let ident = considerQuotedIdent(c, f, n).s

compiler/semexprs.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,9 @@ proc semIndirectOp(c: PContext, n: PNode, flags: TExprFlags): PNode =
966966
# t.kind != tySequence(It is tyProc)
967967
if typ.sym != nil and sfAnon notin typ.sym.flags and
968968
typ.kind == tyProc:
969-
msg.add(" = " &
970-
typeToString(typ, preferDesc))
969+
# when can `typ.sym != nil` ever happen?
970+
msg.add(" = " & typeToString(typ, preferDesc))
971+
msg.addDeclaredLocMaybe(c.config, typ)
971972
localError(c.config, n.info, msg)
972973
return errorNode(c, n)
973974
result = nil

compiler/types.nim

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,22 @@ proc isFloatLit*(t: PType): bool {.inline.} =
124124
result = t.kind == tyFloat and t.n != nil and t.n.kind == nkFloatLit
125125

126126
proc addDeclaredLoc*(result: var string, conf: ConfigRef; sym: PSym) =
127-
# result.add " [declared in " & conf$sym.info & "]"
128-
result.add " [declared in " & toFileLineCol(conf, sym.info) & "]"
127+
result.add " [$1 declared in $2]" % [sym.kind.toHumanStr, toFileLineCol(conf, sym.info)]
129128

130129
proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; sym: PSym) =
131-
if optDeclaredLocs in conf.globalOptions:
130+
if optDeclaredLocs in conf.globalOptions and sym != nil:
132131
addDeclaredLoc(result, conf, sym)
133132

133+
proc addDeclaredLoc(result: var string, conf: ConfigRef; typ: PType) =
134+
let typ = typ.skipTypes(abstractInst - {tyRange})
135+
result.add " [$1" % typ.kind.toHumanStr
136+
if typ.sym != nil:
137+
result.add " declared in " & toFileLineCol(conf, typ.sym.info)
138+
result.add "]"
139+
140+
proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; typ: PType) =
141+
if optDeclaredLocs in conf.globalOptions: addDeclaredLoc(result, conf, typ)
142+
134143
proc addTypeHeader*(result: var string, conf: ConfigRef; typ: PType; prefer: TPreferedDesc = preferMixed; getDeclarationPath = true) =
135144
result.add typeToString(typ, prefer)
136145
if getDeclarationPath: result.addDeclaredLoc(conf, typ.sym)
@@ -1473,12 +1482,19 @@ proc skipHiddenSubConv*(n: PNode; idgen: IdGenerator): PNode =
14731482

14741483
proc typeMismatch*(conf: ConfigRef; info: TLineInfo, formal, actual: PType) =
14751484
if formal.kind != tyError and actual.kind != tyError:
1476-
let named = typeToString(formal)
1485+
let actualStr = typeToString(actual)
1486+
let formalStr = typeToString(formal)
14771487
let desc = typeToString(formal, preferDesc)
1478-
let x = if named == desc: named else: named & " = " & desc
1479-
var msg = "type mismatch: got <" &
1480-
typeToString(actual) & "> " &
1481-
"but expected '" & x & "'"
1488+
let x = if formalStr == desc: formalStr else: formalStr & " = " & desc
1489+
let verbose = actualStr == formalStr or optDeclaredLocs in conf.globalOptions
1490+
var msg = "type mismatch:"
1491+
if verbose: msg.add "\n"
1492+
msg.add " got <$1>" % actualStr
1493+
if verbose:
1494+
msg.addDeclaredLoc(conf, actual)
1495+
msg.add "\n"
1496+
msg.add " but expected '$1'" % x
1497+
if verbose: msg.addDeclaredLoc(conf, formal)
14821498

14831499
if formal.kind == tyProc and actual.kind == tyProc:
14841500
case compatibleEffects(formal, actual)

tests/errmsgs/t8794.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ discard """
22
cmd: "nim check $options $file"
33
errormsg: ""
44
nimout: '''
5-
t8794.nim(39, 27) Error: undeclared field: 'a3' for type m8794.Foo3 [declared in m8794.nim(1, 6)]
5+
t8794.nim(39, 27) Error: undeclared field: 'a3' for type m8794.Foo3 [type declared in m8794.nim(1, 6)]
66
'''
77
"""
88

tests/errmsgs/undeclared_routime.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ discard """
22
cmd: '''nim c --hints:off $file'''
33
errormsg: "attempting to call routine: 'myiter'"
44
nimout: '''undeclared_routime.nim(13, 15) Error: attempting to call routine: 'myiter'
5-
found 'undeclared_routime.myiter(a: string)[declared in undeclared_routime.nim(10, 9)]' of kind 'iterator'
6-
found 'undeclared_routime.myiter()[declared in undeclared_routime.nim(11, 9)]' of kind 'iterator'
5+
found 'undeclared_routime.myiter(a: string)[iterator declared in undeclared_routime.nim(10, 9)]'
6+
found 'undeclared_routime.myiter()[iterator declared in undeclared_routime.nim(11, 9)]'
77
'''
88
"""
99

tests/misc/tnoop.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
discard """
22
nimout: '''
3-
found 'a' of kind 'var'
3+
found 'a' [var declared in tnoop.nim(11, 3)]
44
'''
55
file: "tnoop.nim"
66
line: 13

0 commit comments

Comments
 (0)