From 446501b53bfbb5aa0322e18967dbd0511ddbee37 Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 23 Aug 2024 19:01:43 +0300 Subject: [PATCH] fix error messages for wrongly typed generic param default values (#24006) fixes #21258 When a generic proc is instantiated, if one of the default values doesn't match the type of the parameter, `seminst` sets the default parameter node to an `nkEmpty` node with `tyError` type. `sigmatch` checks for this to give an error message if the default param is actually used, but only while actively matching the proc signature, before the proc is even instantiated. The error message also gives very little information. Now, we check for this in `updateDefaultParams` at the end of `semResolvedCall`, after the proc has been instantiated. The `nkEmpty` node also is given the original mismatching type instead rather than `tyError`, only setting `tyError` after erroring to prevent cascading errors. The error message is changed to the standard type mismatch error also giving the instantiation info of the routine. --- compiler/semcall.nim | 16 +++++++++++++--- compiler/seminst.nim | 6 ++++++ compiler/sigmatch.nim | 10 ++++++---- tests/proc/twrongdefaultvalue.nim | 25 +++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 tests/proc/twrongdefaultvalue.nim diff --git a/compiler/semcall.nim b/compiler/semcall.nim index 4240bc603b..7040c1ac66 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -595,7 +595,7 @@ proc inferWithMetatype(c: PContext, formal: PType, result = copyTree(arg) result.typ = formal -proc updateDefaultParams(call: PNode) = +proc updateDefaultParams(c: PContext, call: PNode) = # In generic procs, the default parameter may be unique for each # instantiation (see tlateboundgenericparams). # After a call is resolved, we need to re-assign any default value @@ -605,8 +605,18 @@ proc updateDefaultParams(call: PNode) = let calleeParams = call[0].sym.typ.n for i in 1..= 2 and (let t = n[1].typ; diff --git a/compiler/seminst.nim b/compiler/seminst.nim index 84ae9a26fe..b187f9ed8c 100644 --- a/compiler/seminst.nim +++ b/compiler/seminst.nim @@ -253,9 +253,13 @@ proc instantiateProcType(c: PContext, pt: TypeMapping, var typeToFit = resulti let needsStaticSkipping = resulti.kind == tyFromExpr + let needsTypeDescSkipping = resulti.kind == tyTypeDesc and tfUnresolved in resulti.flags result[i] = replaceTypeVarsT(cl, resulti) if needsStaticSkipping: result[i] = result[i].skipTypes({tyStatic}) + if needsTypeDescSkipping: + result[i] = result[i].skipTypes({tyTypeDesc}) + typeToFit = result[i] # ...otherwise, we use the instantiated type in `fitNode` if (typeToFit.kind != tyTypeDesc or typeToFit.base.kind != tyNone) and @@ -292,6 +296,8 @@ proc instantiateProcType(c: PContext, pt: TypeMapping, # the user calls an explicit instantiation of the proc (this is # the only way the default value might be inserted). param.ast = errorNode(c, def) + # we know the node is empty, we need the actual type for error message + param.ast.typ = def.typ else: param.ast = fitNodePostMatch(c, typeToFit, converted) param.typ = result[i] diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index af9b29718f..f2865347f7 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -2790,14 +2790,16 @@ proc matches*(c: PContext, n, nOrig: PNode, m: var TCandidate) = m.firstMismatch.formal = formal break else: + # mirrored with updateDefaultParams: if formal.ast.kind == nkEmpty: # The default param value is set to empty in `instantiateProcType` # when the type of the default expression doesn't match the type # of the instantiated proc param: - localError(c.config, m.call.info, - ("The default parameter '$1' has incompatible type " & - "with the explicitly requested proc instantiation") % - formal.name.s) + pushInfoContext(c.config, m.call.info, + if m.calleeSym != nil: m.calleeSym.detailedInfo else: "") + typeMismatch(c.config, formal.ast.info, formal.typ, formal.ast.typ, formal.ast) + popInfoContext(c.config) + formal.ast.typ = errorType(c) if nfDefaultRefsParam in formal.ast.flags: m.call.flags.incl nfDefaultRefsParam var defaultValue = copyTree(formal.ast) diff --git a/tests/proc/twrongdefaultvalue.nim b/tests/proc/twrongdefaultvalue.nim new file mode 100644 index 0000000000..2c36c22470 --- /dev/null +++ b/tests/proc/twrongdefaultvalue.nim @@ -0,0 +1,25 @@ +discard """ + cmd: "nim check $file" + action: reject + nimout: ''' +twrongdefaultvalue.nim(20, 12) template/generic instantiation of `doit` from here +twrongdefaultvalue.nim(17, 37) Error: type mismatch: got but expected 'Item[system.string]' +twrongdefaultvalue.nim(25, 3) template/generic instantiation of `foo` from here +twrongdefaultvalue.nim(23, 33) Error: type mismatch: got but expected 'int' +''' +""" + +block: # issue #21258 + type Item[T] = object + pos: int + proc initItem[T](p:int=10000) : Item[T] = + result = Item[T](p) + proc doit[T](x:Item[T], s:Item[T]=initItem) : string = + return $x.pos + let x = Item[string](pos:100) + echo doit(x) + +block: # issue #21258, reduced case + proc foo[T](x: seq[T], y: T = "foo") = + discard + foo @[1, 2, 3]