Skip to content

BUG: can't refer to generic type in return type #8551

Closed
@timotheecour

Description

@timotheecour

reduced a bit from PR #8531 where I'm forced to using auto return instead of explicit type

seems related but not same as #8545

# BUG: can't refer to generic type in return type
import macros

macro distinctBase2*(T: typedesc): untyped =
  let typeNode = getTypeImpl(T)
  expectKind(typeNode, nnkBracketExpr)
  if typeNode[0].typeKind != ntyTypeDesc:
    error "expected typeDesc, got " & $typeNode[0]
  var typeSym = typeNode[1]

  typeSym = getTypeImpl(typeSym)

  echo typeSym.treeRepr # Sym "T"

  if typeSym.typeKind != ntyDistinct:
    error "type is not distinct: " & $typeSym.typeKind

  typeSym = typeSym[0]
  typeSym

when defined(case1):
  # ok
  func distinctBase*[T](a: T): auto = distinctBase2(T)(a)
when defined(case2):
  # Error: type is not distinct: ntyGenericParam
  func distinctBase*[T](a: T): distinctBase2(T) = distinctBase2(T)(a)

when isMainModule:
  type T=distinct int
  discard distinctBase(T(0))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions