Skip to content

passing tuple with items defined as concept (UserDefinedClass) causes compiler crash #25227

@litlighilit

Description

@litlighilit

Nim Version

2.3.1
2.*
1.*

Description

import std/macros
macro items*(t: tuple): untyped =
  result = newNimNode nnkBracket
  if t.len != 0:
    for i in t:
      result.add i
  else:
    let nElem = t.getTypeImpl.len
    if nElem == 0:
      return parseExpr(
        "items(array[0, int]([]))"
      ) # any type shall be ok
    for i in 0..<nElem:
      result.add nnkBracketExpr.newTree(t, newLit i)
  result = newCall(ident"items", result)

type
  Iterable*[T] = concept self  ## Mimic Pythons Iterable. But not checks `iter`
    for i in self: i is T

proc foo[T](it: Iterable[T]) =
  for i in it: echo i

foo (1, 2)

Current Output

.......................................................................assertions.nim(34)       raiseAssert                                                 
Error: unhandled exception: ccgexprs.nim(848, 3) `tupType.kind == tyTuple`  [AssertionDefect]

Expected Output

1
2

Known Workarounds

No response

Additional Information

For JS backend, nim crashes with internalError:

Error: internal error: genFieldAddr

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