Skip to content

Commit

Permalink
fix #16150 improve type mismatch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Nov 26, 2020
1 parent 9f1c5f6 commit ef0b7ec
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ proc fitNode(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =
else:
result = indexTypesMatch(c, formal, arg.typ, arg)
if result == nil:
typeMismatch(c.config, info, formal, arg.typ)
typeMismatch(c.config, info, formal, arg.typ, arg)
# error correction:
result = copyTree(arg)
result.typ = formal
Expand Down
2 changes: 1 addition & 1 deletion compiler/semcall.nim
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ proc inferWithMetatype(c: PContext, formal: PType,
result.typ = generateTypeInstance(c, m.bindings, arg.info,
formal.skipTypes({tyCompositeTypeClass}))
else:
typeMismatch(c.config, arg.info, formal, arg.typ)
typeMismatch(c.config, arg.info, formal, arg.typ, arg)
# error correction:
result = copyTree(arg)
result.typ = formal
Expand Down
2 changes: 1 addition & 1 deletion compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ proc semAsgn(c: PContext, n: PNode; mode=asgnNormal): PNode =
c.p.resultSym.typ = rhsTyp
c.p.owner.typ[0] = rhsTyp
else:
typeMismatch(c.config, n.info, lhs.typ, rhsTyp)
typeMismatch(c.config, n.info, lhs.typ, rhsTyp, rhs)
borrowCheck(c, n, lhs, rhs)

n[1] = fitNode(c, le, rhs, goodLineInfo(n[1]))
Expand Down
5 changes: 3 additions & 2 deletions compiler/semfields.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
localError(c.config, n.info, errGenerated, "no object or tuple type")
return result
for i in 1..<call.len:
var tupleTypeB = skipTypes(call[i].typ, skippedTypesForFields)
let calli = call[i]
var tupleTypeB = skipTypes(calli.typ, skippedTypesForFields)
if not sameType(tupleTypeA, tupleTypeB):
typeMismatch(c.config, call[i].info, tupleTypeA, tupleTypeB)
typeMismatch(c.config, call[i].info, tupleTypeA, tupleTypeB, calli)

inc(c.p.nestedLoopCounter)
if tupleTypeA.kind == tyTuple:
Expand Down
4 changes: 2 additions & 2 deletions compiler/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ proc skipHiddenSubConv*(n: PNode; idgen: IdGenerator): PNode =
else:
result = n

proc typeMismatch*(conf: ConfigRef; info: TLineInfo, formal, actual: PType) =
proc typeMismatch*(conf: ConfigRef; info: TLineInfo, formal, actual: PType, n: PNode) =
if formal.kind != tyError and actual.kind != tyError:
let actualStr = typeToString(actual)
let formalStr = typeToString(formal)
Expand All @@ -1491,7 +1491,7 @@ proc typeMismatch*(conf: ConfigRef; info: TLineInfo, formal, actual: PType) =
let verbose = actualStr == formalStr or optDeclaredLocs in conf.globalOptions
var msg = "type mismatch:"
if verbose: msg.add "\n"
msg.add " got <$1>" % actualStr
msg.add " got '$1' for '$2'" % [actualStr, n.renderTree]
if verbose:
msg.addDeclaredLoc(conf, actual)
msg.add "\n"
Expand Down

0 comments on commit ef0b7ec

Please sign in to comment.