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

getImpl on types return incorrect tree in when branches #16639

Closed
mratsim opened this issue Jan 8, 2021 · 0 comments · Fixed by #21017
Closed

getImpl on types return incorrect tree in when branches #16639

mratsim opened this issue Jan 8, 2021 · 0 comments · Fixed by #21017

Comments

@mratsim
Copy link
Collaborator

mratsim commented Jan 8, 2021

getImpl on types returns an incorrect AST with when branches in a typesection.

Example case:

import macros

type
  TrustedSig = object
  ValidatorSig = object

  Untrusted = object
  SigVerified = object
  Trusted = object

  CommitteeValidatorsBits = object
  AttestationData = object

  Attestation*[TrustLevel] = object
    aggregation_bits*: CommitteeValidatorsBits
    data*: AttestationData
    when TrustLevel is SigVerified|Trusted:
      signature*: TrustedSig
    else:
      signature*: ValidatorSig


macro collectImpl(x: type): untyped =
  let typeAst = getType(x)[1]
  let typeImpl = getImpl(typeAst) # <----- no IdentDefs in when section
  echo typeImpl.treerepr
  echo "---------------------------------"
  let typeImpl2 = getTypeImpl(typeAst)
  echo typeImpl2.treerepr

  result = newStmtList()

collectImpl(Attestation[Untrusted])

# TypeDef
#   Sym "Attestation"
#   GenericParams
#     Sym "TrustLevel"
#   ObjectTy
#     Empty
#     Empty
#     RecList
#       IdentDefs
#         Postfix
#           Ident "*"
#           Ident "aggregation_bits"
#         Sym "CommitteeValidatorsBits"
#         Empty
#       IdentDefs
#         Postfix
#           Ident "*"
#           Ident "data"
#         Sym "AttestationData"
#         Empty
#       RecWhen
#         ElifBranch
#           Infix
#             Sym "is"
#             Sym "TrustLevel"
#             Type
#           Sym "signature" <--------------------- Type disappeared
#         Else
#           Sym "signature" <--------------------- Type disappeared
# ---------------------------------
# ObjectTy
#   Empty
#   Empty
#   RecList
#     IdentDefs
#       Sym "aggregation_bits"
#       Sym "CommitteeValidatorsBits"
#       Empty
#     IdentDefs
#       Sym "data"
#       Sym "AttestationData"
#       Empty
#     RecWhen
#       ElifBranch
#         Infix
#           IdentDefs
#             Sym "is"
#             ProcTy
#               FormalParams
#                 Sym "bool"
#                 IdentDefs
#                   Sym "x"
#                   Sym "T"
#                   Empty
#                 IdentDefs
#                   Sym "y"
#                   Sym "S"
#                   Empty
#               Empty
#             Empty
#           IdentDefs
#             Sym "TrustLevel"
#             Sym "TrustLevel"
#             Empty
#           Type
#         IdentDefs
#           Sym "signature"
#           Sym "TrustedSig"
#           Empty
#       Else
#         IdentDefs
#           Sym "signature"
#           Sym "ValidatorSig"
#           Empty

Another instance of nim-lang/RFCs#44

@mratsim mratsim changed the title getImpl on types return incorrect tree with in when branches getImpl on types return incorrect tree in when branches Jan 8, 2021
metagn added a commit to metagn/Nim that referenced this issue Dec 4, 2022
@metagn metagn mentioned this issue Dec 4, 2022
Araq pushed a commit that referenced this issue Dec 5, 2022
survivorm pushed a commit to survivorm/Nim that referenced this issue Feb 28, 2023
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
narimiran pushed a commit that referenced this issue Apr 25, 2023
(cherry picked from commit e98efdc)
narimiran pushed a commit that referenced this issue May 12, 2023
(cherry picked from commit e98efdc)
narimiran pushed a commit that referenced this issue May 15, 2023
(cherry picked from commit e98efdc)
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
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.

1 participant