Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static generic parameter does not work in param type scopes in procs #22607

Closed
ASVIEST opened this issue Aug 31, 2023 · 2 comments · Fixed by #24066
Closed

Static generic parameter does not work in param type scopes in procs #22607

ASVIEST opened this issue Aug 31, 2023 · 2 comments · Fixed by #24066

Comments

@ASVIEST
Copy link
Contributor

ASVIEST commented Aug 31, 2023

Description

proc test[x: static bool](
  t: (
    when x:
      int
    else:
      float
     )
) = discard

Nim Version

Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2023-08-09
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: d136af0
active boot switches: -d:release

Nim Compiler Version 2.1.1 [Linux: amd64]
Compiled at 2023-08-31
Copyright (c) 2006-2023 by Andreas Rumpf

git hash: b3912c2
active boot switches: -d:release

Current Output

Error: cannot generate code for: x

Expected Output

No response

Possible Solution

No response

Additional Information

No response

@ASVIEST ASVIEST changed the title static generic param not working in CT static generic param not working in proc param types CT Aug 31, 2023
@ASVIEST ASVIEST changed the title static generic param not working in proc param types CT Static generic parameter does not work in param type scopes in procs Aug 31, 2023
@github-actions
Copy link
Contributor

@ASVIEST (none)

devel 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings: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(3, 10) Error: cannot generate code for: x

Stats

  • Started 2023-08-31T18:20:13
  • Finished 2023-08-31T18:20:13
  • Duration

IR

Compiled filesize 0 bytes (0 bytes)

AST

nnkStmtList.newTree(
  nnkProcDef.newTree(
    newIdentNode("test"),
    newEmptyNode(),
    nnkGenericParams.newTree(
      nnkIdentDefs.newTree(
        newIdentNode("x"),
        nnkCommand.newTree(
          newIdentNode("static"),
          newIdentNode("bool")
        ),
        newEmptyNode()
      )
    ),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("t"),
        nnkPar.newTree(
          nnkWhenStmt.newTree(
            nnkElifExpr.newTree(
              newIdentNode("x"),
              nnkStmtList.newTree(
                newIdentNode("int")
              )
            ),
            nnkElseExpr.newTree(
              nnkStmtList.newTree(
                newIdentNode("float")
              )
            )
          )
        ),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkDiscardStmt.newTree(
        newEmptyNode()
      )
    )
  )
)
stable 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings: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(3, 10) Error: cannot generate code for: x

Stats

  • Started 2023-08-31T18:20:13
  • Finished 2023-08-31T18:20:14
  • Duration

IR

Compiled filesize 0 bytes (0 bytes)

AST

nnkStmtList.newTree(
  nnkProcDef.newTree(
    newIdentNode("test"),
    newEmptyNode(),
    nnkGenericParams.newTree(
      nnkIdentDefs.newTree(
        newIdentNode("x"),
        nnkCommand.newTree(
          newIdentNode("static"),
          newIdentNode("bool")
        ),
        newEmptyNode()
      )
    ),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("t"),
        nnkPar.newTree(
          nnkWhenStmt.newTree(
            nnkElifExpr.newTree(
              newIdentNode("x"),
              nnkStmtList.newTree(
                newIdentNode("int")
              )
            ),
            nnkElseExpr.newTree(
              nnkStmtList.newTree(
                newIdentNode("float")
              )
            )
          )
        ),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkDiscardStmt.newTree(
        newEmptyNode()
      )
    )
  )
)
2.0.0 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings: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(3, 10) Error: cannot generate code for: x

Stats

  • Started 2023-08-31T18:20:14
  • Finished 2023-08-31T18:20:14
  • Duration

IR

Compiled filesize 0 bytes (0 bytes)

AST

nnkStmtList.newTree(
  nnkProcDef.newTree(
    newIdentNode("test"),
    newEmptyNode(),
    nnkGenericParams.newTree(
      nnkIdentDefs.newTree(
        newIdentNode("x"),
        nnkCommand.newTree(
          newIdentNode("static"),
          newIdentNode("bool")
        ),
        newEmptyNode()
      )
    ),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("t"),
        nnkPar.newTree(
          nnkWhenStmt.newTree(
            nnkElifExpr.newTree(
              newIdentNode("x"),
              nnkStmtList.newTree(
                newIdentNode("int")
              )
            ),
            nnkElseExpr.newTree(
              nnkStmtList.newTree(
                newIdentNode("float")
              )
            )
          )
        ),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkDiscardStmt.newTree(
        newEmptyNode()
      )
    )
  )
)
1.6.0 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings: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(3, 10) Error: cannot generate code for: x

Stats

  • Started 2023-08-31T18:20:17
  • Finished 2023-08-31T18:20:18
  • Duration

IR

Compiled filesize 0 bytes (0 bytes)

AST

nnkStmtList.newTree(
  nnkProcDef.newTree(
    newIdentNode("test"),
    newEmptyNode(),
    nnkGenericParams.newTree(
      nnkIdentDefs.newTree(
        newIdentNode("x"),
        nnkCommand.newTree(
          newIdentNode("static"),
          newIdentNode("bool")
        ),
        newEmptyNode()
      )
    ),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("t"),
        nnkPar.newTree(
          nnkWhenStmt.newTree(
            nnkElifExpr.newTree(
              newIdentNode("x"),
              nnkStmtList.newTree(
                newIdentNode("int")
              )
            ),
            nnkElseExpr.newTree(
              nnkStmtList.newTree(
                newIdentNode("float")
              )
            )
          )
        ),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkDiscardStmt.newTree(
        newEmptyNode()
      )
    )
  )
)
1.4.0 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings: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(3, 10) Error: cannot generate code for: x

Stats

  • Started 2023-08-31T18:20:20
  • Finished 2023-08-31T18:20:21
  • Duration

IR

Compiled filesize 0 bytes (0 bytes)

AST

nnkStmtList.newTree(
  nnkProcDef.newTree(
    newIdentNode("test"),
    newEmptyNode(),
    nnkGenericParams.newTree(
      nnkIdentDefs.newTree(
        newIdentNode("x"),
        nnkCommand.newTree(
          newIdentNode("static"),
          newIdentNode("bool")
        ),
        newEmptyNode()
      )
    ),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("t"),
        nnkPar.newTree(
          nnkWhenStmt.newTree(
            nnkElifExpr.newTree(
              newIdentNode("x"),
              nnkStmtList.newTree(
                newIdentNode("int")
              )
            ),
            nnkElseExpr.newTree(
              nnkStmtList.newTree(
                newIdentNode("float")
              )
            )
          )
        ),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkDiscardStmt.newTree(
        newEmptyNode()
      )
    )
  )
)
1.2.0 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings: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(3, 10) Error: cannot generate code for: x

Stats

  • Started 2023-08-31T18:20:36
  • Finished 2023-08-31T18:20:36
  • Duration

IR

Compiled filesize 0 bytes (0 bytes)

AST

nnkStmtList.newTree(
  nnkProcDef.newTree(
    newIdentNode("test"),
    newEmptyNode(),
    nnkGenericParams.newTree(
      nnkIdentDefs.newTree(
        newIdentNode("x"),
        nnkCommand.newTree(
          newIdentNode("static"),
          newIdentNode("bool")
        ),
        newEmptyNode()
      )
    ),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("t"),
        nnkPar.newTree(
          nnkWhenStmt.newTree(
            nnkElifExpr.newTree(
              newIdentNode("x"),
              nnkStmtList.newTree(
                newIdentNode("int")
              )
            ),
            nnkElseExpr.newTree(
              nnkStmtList.newTree(
                newIdentNode("float")
              )
            )
          )
        ),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkDiscardStmt.newTree(
        newEmptyNode()
      )
    )
  )
)
1.0.0 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings: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(3, 10) Error: cannot generate code for: x

Stats

  • Started 2023-08-31T18:20:49
  • Finished 2023-08-31T18:20:50
  • Duration

IR

Compiled filesize 0 bytes (0 bytes)

AST

nnkStmtList.newTree(
  nnkProcDef.newTree(
    newIdentNode("test"),
    newEmptyNode(),
    nnkGenericParams.newTree(
      nnkIdentDefs.newTree(
        newIdentNode("x"),
        nnkCommand.newTree(
          newIdentNode("static"),
          newIdentNode("bool")
        ),
        newEmptyNode()
      )
    ),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("t"),
        nnkPar.newTree(
          nnkWhenStmt.newTree(
            nnkElifExpr.newTree(
              newIdentNode("x"),
              nnkStmtList.newTree(
                newIdentNode("int")
              )
            ),
            nnkElseExpr.newTree(
              nnkStmtList.newTree(
                newIdentNode("float")
              )
            )
          )
        ),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkDiscardStmt.newTree(
        newEmptyNode()
      )
    )
  )
)
0.20.2 👎 FAIL

Output

Error: Command failed: nim c --run  -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings: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(3, 10) Error: cannot generate code for: x

Stats

  • Started 2023-08-31T18:20:58
  • Finished 2023-08-31T18:20:59
  • Duration

IR

Compiled filesize 0 bytes (0 bytes)

AST

nnkStmtList.newTree(
  nnkProcDef.newTree(
    newIdentNode("test"),
    newEmptyNode(),
    nnkGenericParams.newTree(
      nnkIdentDefs.newTree(
        newIdentNode("x"),
        nnkCommand.newTree(
          newIdentNode("static"),
          newIdentNode("bool")
        ),
        newEmptyNode()
      )
    ),
    nnkFormalParams.newTree(
      newEmptyNode(),
      nnkIdentDefs.newTree(
        newIdentNode("t"),
        nnkPar.newTree(
          nnkWhenStmt.newTree(
            nnkElifExpr.newTree(
              newIdentNode("x"),
              nnkStmtList.newTree(
                newIdentNode("int")
              )
            ),
            nnkElseExpr.newTree(
              nnkStmtList.newTree(
                newIdentNode("float")
              )
            )
          )
        ),
        newEmptyNode()
      )
    ),
    newEmptyNode(),
    newEmptyNode(),
    nnkStmtList.newTree(
      nnkDiscardStmt.newTree(
        newEmptyNode()
      )
    )
  )
)
Stats
  • GCC 11.4.0
  • LibC 2.35
  • Valgrind 3.18.1
  • NodeJS 17.1
  • Linux 5.15.0
  • Created 2023-08-31T18:19:37Z
  • Comments 1
  • Commands nim c --run -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

🤖 Bug found in 49 minutes bisecting 8 commits at 0 commits per second.

@metagn
Copy link
Collaborator

metagn commented Aug 31, 2023

Unfortunate workaround:

template foo(x): untyped = x
type T[x: static bool] = foo(when x: int else: float)
proc test[x: static bool](
  t: T[x]
) = discard

Really tempted to add the untyped escape hatch removed from #22029 that would make this as simple as untyped(when x: int else: float)

Related #22342

Araq pushed a commit that referenced this issue Aug 26, 2024
…n fixes (#24005)

fixes #4228, fixes #4990, fixes #7006, fixes #7008, fixes #8406, fixes
#8551, fixes #11112, fixes #20027, fixes #22647, refs #23854 and #23855
(remaining issue fixed), refs #8545 (works properly now with
`cast[static[bool]]` changed to `cast[bool]`), refs #22342 and #22607
(disabled tests added), succeeds #23194

Parameter and return type nodes in generic procs now undergo the same
`inGenericContext` treatment that nodes in generic type bodies do. This
allows many of the fixes in #22029 and followups to also apply to
generic proc signatures. Like #23983 however this needs some more
compiler fixes, but this time mostly in `sigmatch` and type
instantiations.

1. `tryReadingGenericParam` no longer treats `tyCompositeTypeClass` like
a concrete type anymore, so expressions like `Foo.T` where `Foo` is a
generic type don't look for a parameter of `Foo` in non-generic code
anymore. It also doesn't generate `tyFromExpr` in non-generic code for
any generic LHS. This is to handle a very specific case in `asyncmacro`
which used `FutureVar.astToStr` where `FutureVar` is generic.
2. The `tryResolvingStaticExpr` call when matching `tyFromExpr` in
sigmatch now doesn't consider call nodes in general unresolved, only
nodes with `tyFromExpr` type, which is emitted on unresolved expressions
by increasing `c.inGenericContext`. `c.inGenericContext == 0` is also
now required to attempt instantiating `tyFromExpr`. So matching against
`tyFromExpr` in proc signatures works in general now, but I'm
speculating it depends on constant folding in `semExpr` for statics to
match against it properly.
3. `paramTypesMatch` now doesn't try to change nodes with `tyFromExpr`
type into `tyStatic` type when fitting to a static type, because it
doesn't need to, they'll be handled the same way (this was a workaround
in place of the static type instantiation changes, only one of the
fields in the #22647 test doesn't work with it).
4. `tyStatic` matching now uses `inferStaticParam` instead of just range
type matching, so `Foo[N div 2]` can infer `N` in the same way `array[N
div 2, int]` can. `inferStaticParam` also disabled itself if the
inferred static param type already had a node, but `makeStaticExpr`
generates static types with unresolved nodes, so we only disable it if
it also doesn't have a binding. This might not work very well but the
static type instantiation changes should really lower the amount of
cases where it's encountered.
5. Static types now undergo type instantiation. Previously the branch
for `tyStatic` in `semtypinst` was a no-op, now it acts similarly to
instantiating any other type with the following differences:
- Other types only need instantiation if `containsGenericType` is true,
static types also get instantiated if their value node isn't a literal
node. Ideally any value node that is "already evaluated" should be
ignored, but I'm not sure of a better way to check this, maybe if
`evalConstExpr` emitted a flag. This is purely for optimization though.
- After instantiation, `semConstExpr` is called on the value node if
`not cl.allowMetaTypes` and the type isn't literally a `static` type.
Then the type of the node is set to the base type of the static type to
deal with `semConstExpr` stripping abstract types.
We need to do this because calls like `foo(N)` where `N` is `static int`
and `foo`'s first parameter is just `int` do not generate `tyFromExpr`,
they are fully typed and so `makeStaticExpr` is called on them, giving a
static type with an unresolved node.
metagn added a commit to metagn/Nim that referenced this issue Sep 5, 2024
@Araq Araq closed this as completed in 7cd1777 Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants