From 5d3fbf40c15a231d4bf1dbf635a96063ff4f7ac7 Mon Sep 17 00:00:00 2001 From: Saem Ghani Date: Fri, 30 Aug 2024 14:33:45 -0700 Subject: [PATCH] fix(sem): errors in lambdas no longer crash (#1437) ## Summary Errors in lambdas requiring inference ( `proc(e: auto) = echo e` ), no longer crash the compiler. Instead errors within the lambda body are reported and lambda inference fails as it should. ## Details `semInferredLambda` failed to wrap its output when there were errors. This would reach `transf` and result in a compiler crash by hitting an `unreachable` assertion. Now errors in the body are reported immediately (to provide context for inference failure wrt matching the body) and AST output from the inference attempt is correctly wrapped such that inference appropriately fails. Fixes https://github.com/nim-works/nimskull/issues/1381 In addition, some minor refactoring was done to avoid shadowing the input parameter `n` and making it look like we were mutating the input. --------- Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com> --- compiler/sem/semstmts.nim | 45 ++++++++++--------- compiler/sem/sigmatch.nim | 7 ++- ...to_inference_failure_due_to_body_error.nim | 13 ++++++ 3 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 tests/lang_callable/generics/tauto_inference_failure_due_to_body_error.nim diff --git a/compiler/sem/semstmts.nim b/compiler/sem/semstmts.nim index 5acc11c8df9..9052ceeb6b7 100644 --- a/compiler/sem/semstmts.nim +++ b/compiler/sem/semstmts.nim @@ -2182,45 +2182,48 @@ proc semProcAnnotation(c: PContext, prc: PNode): PNode = proc semInferredLambda(c: PContext, pt: TIdTable, n: PNode): PNode {.nosinks.} = ## used for resolving 'auto' in lambdas based on their callsite addInNimDebugUtils(c.config, "semInferredLambda", n, result) - var n = n let original = n[namePos].sym let s = original #copySym(original, false) #incl(s.flags, sfFromGeneric) #s.owner = original - n = instantiateTypesInBody(c, pt, n, original) - result = n + result = instantiateTypesInBody(c, pt, n, original) s.ast = result - n[namePos].sym = s - n[genericParamsPos] = c.graph.emptyNode + result[namePos].sym = s + result[genericParamsPos] = c.graph.emptyNode # for LL we need to avoid wrong aliasing - n[paramsPos] = newNodeI(nkFormalParams, n[paramsPos].info, n.typ.n.len) - for i, p in n.typ.n.pairs: - n[paramsPos][i] = + result[paramsPos] = newNodeI(nkFormalParams, result[paramsPos].info, + result.typ.n.len) + for i, p in result.typ.n.pairs: + result[paramsPos][i] = case i of 0: # return type - newNodeIT(nkType, n.info, n.typ[0]) + newNodeIT(nkType, n.info, result.typ[0]) else: # copy instantiated parameters - n.typ.n[i] - s.typ = n.typ - let params = n.typ.n - for i in 1..= formalLen - 1 and # last or finished passing args f < formalLen and # still have more formal params - m.callee.n[f].typ.isVarargsUntyped: # current formal is varargs untped + m.callee.n[f].typ.isVarargsUntyped: # current formal is varargs untyped formal = m.callee.n[f].sym incl(marker, formal.position) diff --git a/tests/lang_callable/generics/tauto_inference_failure_due_to_body_error.nim b/tests/lang_callable/generics/tauto_inference_failure_due_to_body_error.nim new file mode 100644 index 00000000000..4c3a7f57a58 --- /dev/null +++ b/tests/lang_callable/generics/tauto_inference_failure_due_to_body_error.nim @@ -0,0 +1,13 @@ +discard """ + description: "Fail lambda inference due to error within the body." + errormsg: "undeclared identifier: 'i'" + line: 13 +""" + +# This is a regression test, ensure we report errors inside lambdas during +# inference + +proc test(p: proc(x: int)) = + discard + +test proc(e: auto) = i \ No newline at end of file