nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.57k stars 1.47k forks source link

Overloaded template causes `untyped` param is resolved when called. #23662

Open litlighilit opened 5 months ago

litlighilit commented 5 months ago

Description

If a template with untyped params is overloaded, then when the variant with untyped param is called, such a argument (symbol) will be resolved there.

template t(n: float) = discard
template t(todecl: untyped, x: int) =
  discard  # or: let todecl = x

t(ii, 3)   # <-  here `ii` is resolved.

In addition, either of the followings compiles without mistake:

template t(todecl: untyped, x: int) = discard
template t(todecl: untyped, n: char) = discard
t(ii, 3)  # ✓ works

Or

template t(todecl: untyped, x: int) = discard
t(ii, 3)   # ✓ works

Nim Version

Nim Compiler Version 2.1.1 [Windows: amd64] Compiled at 2024-04-27 Copyright (c) 2006-2024 by Andreas Rumpf

active boot switches: -d:release

Current Output

Error: undeclared identifier: 'ii'

Expected Output

(None)

Possible Solution

No response

Additional Information

So does for macro, proc, etc.

juancarlospaco commented 5 months ago

!nim c

template t(n: float) = discard
template t(todecl: untyped, x: int) =
  discard

t(ii, 3)
github-actions[bot] commented 5 months ago
:penguin: Linux bisect by @juancarlospaco (collaborator)
devel :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim /home/runner/work/Nim/Nim/temp.nim(4, 3) Error: undeclared identifier: 'ii' assertions.nim(34) raiseAssert Error: unhandled exception: errGenerated [AssertionDefect] ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-05-30T13:13:12
  • Finished 2024-05-30T13:13:12
  • Duration

AST

```nim nnkStmtList.newTree( nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("n"), newIdentNode("float"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("todecl"), newIdentNode("untyped"), newEmptyNode() ), nnkIdentDefs.newTree( newIdentNode("x"), newIdentNode("int"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkCall.newTree( newIdentNode("t"), newIdentNode("ii"), newLit(3) ) ) ```
stable :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim /home/runner/work/Nim/Nim/temp.nim(4, 3) Error: undeclared identifier: 'ii' assertions.nim(34) raiseAssert Error: unhandled exception: options.nim(681, 5) `false` errGenerated [AssertionDefect] ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-05-30T13:13:13
  • Finished 2024-05-30T13:13:13
  • Duration

AST

```nim nnkStmtList.newTree( nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("n"), newIdentNode("float"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("todecl"), newIdentNode("untyped"), newEmptyNode() ), nnkIdentDefs.newTree( newIdentNode("x"), newIdentNode("int"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkCall.newTree( newIdentNode("t"), newIdentNode("ii"), newLit(3) ) ) ```
2.0.4 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim /home/runner/work/Nim/Nim/temp.nim(4, 3) Error: undeclared identifier: 'ii' assertions.nim(34) raiseAssert Error: unhandled exception: options.nim(681, 5) `false` errGenerated [AssertionDefect] ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-05-30T13:13:13
  • Finished 2024-05-30T13:13:13
  • Duration

AST

```nim nnkStmtList.newTree( nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("n"), newIdentNode("float"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("todecl"), newIdentNode("untyped"), newEmptyNode() ), nnkIdentDefs.newTree( newIdentNode("x"), newIdentNode("int"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkCall.newTree( newIdentNode("t"), newIdentNode("ii"), newLit(3) ) ) ```
2.0.0 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim /home/runner/work/Nim/Nim/temp.nim(4, 3) Error: undeclared identifier: 'ii' assertions.nim(34) raiseAssert Error: unhandled exception: options.nim(664, 5) `false` errGenerated [AssertionDefect] ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-05-30T13:13:17
  • Finished 2024-05-30T13:13:17
  • Duration

AST

```nim nnkStmtList.newTree( nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("n"), newIdentNode("float"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("todecl"), newIdentNode("untyped"), newEmptyNode() ), nnkIdentDefs.newTree( newIdentNode("x"), newIdentNode("int"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkCall.newTree( newIdentNode("t"), newIdentNode("ii"), newLit(3) ) ) ```
1.6.20 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim /home/runner/work/Nim/Nim/temp.nim(4, 3) Error: undeclared identifier: 'ii' candidates (edit distance, scope distance); see '--spellSuggest': (1, 3): 'in' [template declared in /home/runner/.choosenim/toolchains/nim-1.6.20/lib/system.nim(825, 10)] (1, 3): 'io' [module declared in /home/runner/.choosenim/toolchains/nim-1.6.20/lib/system.nim(3131, 14)] (1, 3): 'is' [proc declared in /home/runner/.choosenim/toolchains/nim-1.6.20/lib/system.nim(838, 6)] fatal.nim(54) sysFatal Error: unhandled exception: options.nim(662, 14) `false` errGenerated [AssertionDefect] ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-05-30T13:13:20
  • Finished 2024-05-30T13:13:20
  • Duration

AST

```nim nnkStmtList.newTree( nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("n"), newIdentNode("float"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("todecl"), newIdentNode("untyped"), newEmptyNode() ), nnkIdentDefs.newTree( newIdentNode("x"), newIdentNode("int"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkCall.newTree( newIdentNode("t"), newIdentNode("ii"), newLit(3) ) ) ```
1.4.8 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim /home/runner/work/Nim/Nim/temp.nim(4, 3) Error: undeclared identifier: 'ii' ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-05-30T13:13:22
  • Finished 2024-05-30T13:13:23
  • Duration

AST

```nim nnkStmtList.newTree( nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("n"), newIdentNode("float"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("todecl"), newIdentNode("untyped"), newEmptyNode() ), nnkIdentDefs.newTree( newIdentNode("x"), newIdentNode("int"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkCall.newTree( newIdentNode("t"), newIdentNode("ii"), newLit(3) ) ) ```
1.2.18 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim /home/runner/work/Nim/Nim/temp.nim(4, 3) Error: undeclared identifier: 'ii' ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-05-30T13:13:26
  • Finished 2024-05-30T13:13:26
  • Duration

AST

```nim nnkStmtList.newTree( nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("n"), newIdentNode("float"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("todecl"), newIdentNode("untyped"), newEmptyNode() ), nnkIdentDefs.newTree( newIdentNode("x"), newIdentNode("int"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkCall.newTree( newIdentNode("t"), newIdentNode("ii"), newLit(3) ) ) ```
1.0.10 :-1: FAIL

Output

``` Error: Command failed: nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim /home/runner/work/Nim/Nim/temp.nim(4, 3) Error: undeclared identifier: 'ii' ```

IR

Compiled filesize 0 bytes (0 bytes) ```cpp ```

Stats

  • Started 2024-05-30T13:13:28
  • Finished 2024-05-30T13:13:28
  • Duration

AST

```nim nnkStmtList.newTree( nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("n"), newIdentNode("float"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkTemplateDef.newTree( newIdentNode("t"), newEmptyNode(), newEmptyNode(), nnkFormalParams.newTree( newEmptyNode(), nnkIdentDefs.newTree( newIdentNode("todecl"), newIdentNode("untyped"), newEmptyNode() ), nnkIdentDefs.newTree( newIdentNode("x"), newIdentNode("int"), newEmptyNode() ) ), newEmptyNode(), newEmptyNode(), nnkStmtList.newTree( nnkDiscardStmt.newTree( newEmptyNode() ) ) ), nnkCall.newTree( newIdentNode("t"), newIdentNode("ii"), newLit(3) ) ) ```
Stats
  • GCC 11.4.0
  • Clang 14.0.0
  • NodeJS 20.3
  • Created 2024-05-30T13:12:41Z
  • Comments 1
  • Commands nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

:robot: Bug found in 20 minutes bisecting 8 commits at 0 commits per second

juancarlospaco commented 5 months ago

Same behaviour from 1.0 to devel. 🤷

beef331 commented 5 months ago

This is required for dispatch, overloaded macros and templates have to semantically check arguments for a call in cases of ambiguity.

metagn commented 5 months ago

https://github.com/nim-lang/RFCs/issues/402

litlighilit commented 5 months ago

Another workaround:

make the overloaded templates' param untyped, too.