Skip to content

Commit 527c532

Browse files
committed
show declaration location also when type mismatch names clash
1 parent be4740d commit 527c532

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

compiler/types.nim

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,15 @@ proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; sym: PSym) =
130130
if optDeclaredLocs in conf.globalOptions and sym != nil:
131131
addDeclaredLoc(result, conf, sym)
132132

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+
133140
proc addDeclaredLocMaybe*(result: var string, conf: ConfigRef; typ: PType) =
134-
if optDeclaredLocs in conf.globalOptions:
135-
let typ = typ.skipTypes(abstractInst - {tyRange})
136-
result.add " [$1" % typ.kind.toHumanStr
137-
if typ.sym != nil:
138-
result.add " declared in " & toFileLineCol(conf, typ.sym.info)
139-
result.add "]"
141+
if optDeclaredLocs in conf.globalOptions: addDeclaredLoc(result, conf, typ)
140142

141143
proc addTypeHeader*(result: var string, conf: ConfigRef; typ: PType; prefer: TPreferedDesc = preferMixed; getDeclarationPath = true) =
142144
result.add typeToString(typ, prefer)
@@ -1480,13 +1482,19 @@ proc skipHiddenSubConv*(n: PNode): PNode =
14801482

14811483
proc typeMismatch*(conf: ConfigRef; info: TLineInfo, formal, actual: PType) =
14821484
if formal.kind != tyError and actual.kind != tyError:
1483-
let named = typeToString(formal)
1485+
let actualStr = typeToString(actual)
1486+
let formalStr = typeToString(formal)
14841487
let desc = typeToString(formal, preferDesc)
1485-
let x = if named == desc: named else: named & " = " & desc
1486-
var msg = "type mismatch: got <" & typeToString(actual) & ">"
1487-
msg.addDeclaredLocMaybe(conf, actual)
1488-
msg.add " but expected '" & x & "'"
1489-
msg.addDeclaredLocMaybe(conf, formal)
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)
14901498

14911499
if formal.kind == tyProc and actual.kind == tyProc:
14921500
case compatibleEffects(formal, actual)

0 commit comments

Comments
 (0)