From 7f36ecf3dad809cef7b4b02d396dab1728262cd1 Mon Sep 17 00:00:00 2001 From: metagn Date: Sun, 8 Jan 2023 08:49:49 +0300 Subject: [PATCH] underscores for routine parameters (#21192) * underscores for routine parameters fixes #13443, fixes #13804, refs #21121 * add changelog + more tests * support generics and ensure inferred lambdas work --- changelogs/changelog_2_0_0.md | 15 +++++ compiler/lookups.nim | 1 + compiler/semtempl.nim | 5 +- compiler/semtypes.nim | 4 +- tests/proc/tunderscoreparam.nim | 109 ++++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 tests/proc/tunderscoreparam.nim diff --git a/changelogs/changelog_2_0_0.md b/changelogs/changelog_2_0_0.md index 9ff493b543cf6..c8427cc2a53fb 100644 --- a/changelogs/changelog_2_0_0.md +++ b/changelogs/changelog_2_0_0.md @@ -135,6 +135,21 @@ - The experimental strictFuncs feature now disallows a store to the heap via a `ref` or `ptr` indirection. +- Underscores (`_`) as routine parameters are now ignored and cannot be used in the routine body. + The following code now does not compile: + + ```nim + proc foo(_: int): int = _ + 1 + echo foo(1) + ``` + + Instead, the following code now compiles: + + ```nim + proc foo(_, _: int): int = 123 + echo foo(1, 2) + ``` + - - Added the `--legacy:verboseTypeMismatch` switch to get legacy type mismatch error messages. ## Standard library additions and changes diff --git a/compiler/lookups.nim b/compiler/lookups.nim index d50fc6e2757d7..3f4fcb4d0333e 100644 --- a/compiler/lookups.nim +++ b/compiler/lookups.nim @@ -323,6 +323,7 @@ proc wrongRedefinition*(c: PContext; info: TLineInfo, s: string; # xxx pending bootstrap >= 1.4, replace all those overloads with a single one: # proc addDecl*(c: PContext, sym: PSym, info = sym.info, scope = c.currentScope) {.inline.} = proc addDeclAt*(c: PContext; scope: PScope, sym: PSym, info: TLineInfo) = + if sym.name.s == "_": return let conflict = scope.addUniqueSym(sym) if conflict != nil: if sym.kind == skModule and conflict.kind == skModule and sym.owner == conflict.owner: diff --git a/compiler/semtempl.nim b/compiler/semtempl.nim index 0016e9edf0111..3b2f2dd9ebf46 100644 --- a/compiler/semtempl.nim +++ b/compiler/semtempl.nim @@ -642,8 +642,9 @@ proc semTemplateDef(c: PContext, n: PNode): PNode = # body by the absence of the sfGenSym flag: for i in 1..