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

Nim 2.0.2 regression: macros.getType(…).kind of object type is nnkBracketExpr (was nnkObjectTy in Nim 2.0.0) #23112

Closed
flyx opened this issue Dec 21, 2023 · 3 comments · Fixed by #23115

Comments

@flyx
Copy link
Contributor

flyx commented Dec 21, 2023

Description

getType from macros returns an nnkBracketExpr for an object type with Nim 2.0.2 where Nim 2.0.0 returned an nnkObjectTy:

import macros

type
  Container = object
    foo: string

proc canBeImplicit(t: typedesc): string {.compileTime.} =
  ## returns empty string if type can be implicit, else the reason why it can't
  let tDesc = getType(t)
  if tDesc.kind != nnkObjectTy: return "type is not an object, but a " & $tDesc.kind
  return ""

const res = canBeImplicit(Container)
assert len(res) == 0, "unexpected error for canBeImplicit: " & res

Nim Version

Nim 2.0.2 on macOS, Linux and Window, see CI run of the test above

Current Output

/home/runner/work/NimYAML/NimYAML/test/tnimregress.nim(14) tnimregress
/home/runner/.choosenim/toolchains/nim-2.0.2/lib/std/assertions.nim(41) failedAssertImpl
/home/runner/.choosenim/toolchains/nim-2.0.2/lib/std/assertions.nim(36) raiseAssert
/home/runner/.choosenim/toolchains/nim-2.0.2/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: /home/runner/work/NimYAML/NimYAML/test/tnimregress.nim(14, 1) `len(res) == 0` unexpected error for canBeImplicit: type is not an object, but a nnkBracketExpr [AssertionDefect]
Error: execution of an external program failed: '/home/runner/work/NimYAML/NimYAML/test/tnimregress'
Error: Process completed with exit code 1.

Expected Output

(nothing)

Possible Solution

No response

Additional Information

This breaks NimYAML, specifically its implicit pragma. NimYAML callers that do not use this feature are not affected.

@flyx
Copy link
Contributor Author

flyx commented Dec 21, 2023

Let's try this bisecting feature.

!nim c

import macros

type
  Container = object
    foo: string

proc canBeImplicit(t: typedesc): string {.compileTime.} =
  ## returns empty string if type can be implicit, else the reason why it can't
  let tDesc = getType(t)
  if tDesc.kind != nnkObjectTy: return "type is not an object, but a " & $tDesc.kind
  return ""

const res = canBeImplicit(Container)
assert len(res) == 0, "unexpected error for canBeImplicit: " & res

@ringabout
Copy link
Member

ringabout commented Dec 22, 2023

!nim c

import macros

type
  Container = object
    foo: string

proc canBeImplicit(t: typedesc): string {.compileTime.} =
  ## returns empty string if type can be implicit, else the reason why it can't
  let tDesc = getType(t)
  if tDesc.kind != nnkObjectTy: return "type is not an object, but a " & $tDesc.kind
  return ""

const res = canBeImplicit(Container)
assert len(res) == 0, "unexpected error for canBeImplicit: " & res

@metagn
Copy link
Collaborator

metagn commented Dec 22, 2023

import macros

type
  Container = object
    foo: string

proc canBeImplicit(t: typedesc): string {.compileTime.} =
  let tDesc = getType(t)
  echo tDesc.treeRepr

const res = canBeImplicit(Container)
BracketExpr
  Sym "typeDesc"
  Sym "Container"

An easy workaround is:

var tDesc = getType(t)
while tDesc.kind == nnkBracketExpr and tDesc[0].eqIdent"typeDesc":
  tDesc = getType(tDesc[1])

Caused by #22581, hence this test change.

metagn added a commit to metagn/Nim that referenced this issue Dec 22, 2023
metagn added a commit to metagn/Nim that referenced this issue Dec 22, 2023
Araq pushed a commit that referenced this issue Jan 18, 2024
fixes #23112, fixes a mistake in
#22581

This makes `getType(t)` where `t` is a typedesc param with value `T`
equal to `getType(T)`.
narimiran pushed a commit that referenced this issue Jan 19, 2024
fixes #23112, fixes a mistake in
#22581

This makes `getType(t)` where `t` is a typedesc param with value `T`
equal to `getType(T)`.

(cherry picked from commit 3224337)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants