diff --git a/compiler/lookups.nim b/compiler/lookups.nim index 0a6a0b950..9225706b5 100644 --- a/compiler/lookups.nim +++ b/compiler/lookups.nim @@ -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}: diff --git a/compiler/semcall.nim b/compiler/semcall.nim index dd2398f87..0446d28c2 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -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 diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 2de25dadb..b2f0207e7 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -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 diff --git a/compiler/types.nim b/compiler/types.nim index a3a4695b0..7e56f5002 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -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) diff --git a/tests/errmsgs/t8794.nim b/tests/errmsgs/t8794.nim index 22e4014f1..9db54a9c7 100644 --- a/tests/errmsgs/t8794.nim +++ b/tests/errmsgs/t8794.nim @@ -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)] ''' """ diff --git a/tests/errmsgs/undeclared_routime.nim b/tests/errmsgs/undeclared_routime.nim index 426507652..7ef12cc0e 100644 --- a/tests/errmsgs/undeclared_routime.nim +++ b/tests/errmsgs/undeclared_routime.nim @@ -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)]' ''' """ diff --git a/tests/misc/tnoop.nim b/tests/misc/tnoop.nim index a1365cfbe..7f5bb1045 100644 --- a/tests/misc/tnoop.nim +++ b/tests/misc/tnoop.nim @@ -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