fix #16150 improve type mismatch errors (#16152)

* fix #16150 improve type mismatch errors

* allow -d:nimLegacyTypeMismatch

* address comment
This commit is contained in:
Timothee Cour 2020-12-09 07:19:39 -08:00 • committed by GitHub
commit f344a70412
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 14 additions and 7 deletions

View file

@ -77,6 +77,8 @@
- Added `nim --eval:cmd` to evaluate a command directly, see `nim --help`. - Added `nim --eval:cmd` to evaluate a command directly, see `nim --help`.
- VM now supports `addr(mystring[ind])` (index + index assignment) - VM now supports `addr(mystring[ind])` (index + index assignment)
- Type mismatch errors now show more context, use `-d:nimLegacyTypeMismatch` for previous
behavior.
## Tool changes ## Tool changes

View file

@ -96,7 +96,7 @@ proc fitNode(c: PContext, formal: PType, arg: PNode; info: TLineInfo): PNode =
else: else:
result = indexTypesMatch(c, formal, arg.typ, arg) result = indexTypesMatch(c, formal, arg.typ, arg)
if result == nil: if result == nil:
typeMismatch(c.config, info, formal, arg.typ) typeMismatch(c.config, info, formal, arg.typ, arg)
# error correction: # error correction:
result = copyTree(arg) result = copyTree(arg)
result.typ = formal result.typ = formal

View file

@ -476,7 +476,7 @@ proc inferWithMetatype(c: PContext, formal: PType,
result.typ = generateTypeInstance(c, m.bindings, arg.info, result.typ = generateTypeInstance(c, m.bindings, arg.info,
formal.skipTypes({tyCompositeTypeClass})) formal.skipTypes({tyCompositeTypeClass}))
else: else:
typeMismatch(c.config, arg.info, formal, arg.typ) typeMismatch(c.config, arg.info, formal, arg.typ, arg)
# error correction: # error correction:
result = copyTree(arg) result = copyTree(arg)
result.typ = formal result.typ = formal

View file

@ -1745,7 +1745,7 @@ proc semAsgn(c: PContext, n: PNode; mode=asgnNormal): PNode =
c.p.resultSym.typ = rhsTyp c.p.resultSym.typ = rhsTyp
c.p.owner.typ[0] = rhsTyp c.p.owner.typ[0] = rhsTyp
else: else:
typeMismatch(c.config, n.info, lhs.typ, rhsTyp) typeMismatch(c.config, n.info, lhs.typ, rhsTyp, rhs)
borrowCheck(c, n, lhs, rhs) borrowCheck(c, n, lhs, rhs)
n[1] = fitNode(c, le, rhs, goodLineInfo(n[1])) n[1] = fitNode(c, le, rhs, goodLineInfo(n[1]))

View file

@ -127,9 +127,10 @@ proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
localError(c.config, n.info, errGenerated, "no object or tuple type") localError(c.config, n.info, errGenerated, "no object or tuple type")
return result return result
for i in 1..<call.len: 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): if not sameType(tupleTypeA, tupleTypeB):
typeMismatch(c.config, call[i].info, tupleTypeA, tupleTypeB) typeMismatch(c.config, calli.info, tupleTypeA, tupleTypeB, calli)
inc(c.p.nestedLoopCounter) inc(c.p.nestedLoopCounter)
if tupleTypeA.kind == tyTuple: if tupleTypeA.kind == tyTuple:

View file

@ -1482,7 +1482,7 @@ proc skipHiddenSubConv*(n: PNode; idgen: IdGenerator): PNode =
else: else:
result = n 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: if formal.kind != tyError and actual.kind != tyError:
let actualStr = typeToString(actual) let actualStr = typeToString(actual)
let formalStr = typeToString(formal) let formalStr = typeToString(formal)
@ -1491,7 +1491,10 @@ proc typeMismatch*(conf: ConfigRef; info: TLineInfo, formal, actual: PType) =
let verbose = actualStr == formalStr or optDeclaredLocs in conf.globalOptions let verbose = actualStr == formalStr or optDeclaredLocs in conf.globalOptions
var msg = "type mismatch:" var msg = "type mismatch:"
if verbose: msg.add "\n" if verbose: msg.add "\n"
msg.add " got <$1>" % actualStr if conf.isDefined("nimLegacyTypeMismatch"):
msg.add " got <$1>" % actualStr
else:
msg.add " got '$1' for '$2'" % [actualStr, n.renderTree]
if verbose: if verbose:
msg.addDeclaredLoc(conf, actual) msg.addDeclaredLoc(conf, actual)
msg.add "\n" msg.add "\n"

View file

@ -8,3 +8,4 @@ switch("path", "$lib/../testament/lib")
switch("colors", "off") switch("colors", "off")
switch("listFullPaths", "off") switch("listFullPaths", "off")
switch("excessiveStackTrace", "off") switch("excessiveStackTrace", "off")
switch("define", "nimLegacyTypeMismatch")