From a0eb11c5220d8234f4a1e84ab96f1f15b3eb456a Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Thu, 26 Nov 2020 15:36:14 -0800 Subject: [PATCH 1/3] fix #16150 improve type mismatch errors --- compiler/sem.nim | 2 +- compiler/semcall.nim | 2 +- compiler/semexprs.nim | 2 +- compiler/semfields.nim | 5 +++-- compiler/types.nim | 4 ++-- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/compiler/sem.nim b/compiler/sem.nim index f90d9e1f9c3c..0fec8b7e3fc8 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -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 diff --git a/compiler/semcall.nim b/compiler/semcall.nim index 70a7e099ca62..1e8da0298892 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -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 diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 8728af27c59e..3bbf353b1370 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -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])) diff --git a/compiler/semfields.nim b/compiler/semfields.nim index d7563e33fef2..684f785ee2e4 100644 --- a/compiler/semfields.nim +++ b/compiler/semfields.nim @@ -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.." % actualStr + msg.add " got '$1' for '$2'" % [actualStr, n.renderTree] if verbose: msg.addDeclaredLoc(conf, actual) msg.add "\n" From 5631cc02047d8210d8c30f2fa66e572bde3f7f01 Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Sat, 28 Nov 2020 16:58:53 -0800 Subject: [PATCH 2/3] allow -d:nimLegacyTypeMismatch --- changelog.md | 2 ++ compiler/types.nim | 5 ++++- tests/config.nims | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index 05958118bcb1..c83762bbb644 100644 --- a/changelog.md +++ b/changelog.md @@ -74,6 +74,8 @@ - Added `nim --eval:cmd` to evaluate a command directly, see `nim --help`. - VM now supports `addr(mystring[ind])` (index + index assignment) +- Type mismatch errors now show more context, use `-d:nimLegacyTypeMismatch` for previous + behavior. ## Tool changes diff --git a/compiler/types.nim b/compiler/types.nim index 18b9de9bfe9f..535703a8cc3d 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -1491,7 +1491,10 @@ proc typeMismatch*(conf: ConfigRef; info: TLineInfo, formal, actual: PType, n: P let verbose = actualStr == formalStr or optDeclaredLocs in conf.globalOptions var msg = "type mismatch:" if verbose: msg.add "\n" - msg.add " got '$1' for '$2'" % [actualStr, n.renderTree] + if conf.isDefined("nimLegacyTypeMismatch"): + msg.add " got <$1>" % actualStr + else: + msg.add " got '$1' for '$2'" % [actualStr, n.renderTree] if verbose: msg.addDeclaredLoc(conf, actual) msg.add "\n" diff --git a/tests/config.nims b/tests/config.nims index 640df9cade42..ac5e019f5175 100644 --- a/tests/config.nims +++ b/tests/config.nims @@ -8,3 +8,4 @@ switch("path", "$lib/../testament/lib") switch("colors", "off") switch("listFullPaths", "off") switch("excessiveStackTrace", "off") +switch("define", "nimLegacyTypeMismatch") From 0eee6bc9aa3b0e7a3daa57abb67f496314a8dc8f Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Thu, 3 Dec 2020 10:21:30 -0800 Subject: [PATCH 3/3] address comment --- compiler/semfields.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/semfields.nim b/compiler/semfields.nim index 684f785ee2e4..602a7199d197 100644 --- a/compiler/semfields.nim +++ b/compiler/semfields.nim @@ -130,7 +130,7 @@ proc semForFields(c: PContext, n: PNode, m: TMagic): PNode = let calli = call[i] var tupleTypeB = skipTypes(calli.typ, skippedTypesForFields) if not sameType(tupleTypeA, tupleTypeB): - typeMismatch(c.config, call[i].info, tupleTypeA, tupleTypeB, calli) + typeMismatch(c.config, calli.info, tupleTypeA, tupleTypeB, calli) inc(c.p.nestedLoopCounter) if tupleTypeA.kind == tyTuple: