nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.54k stars 1.47k forks source link

Calling type aliased type class through untyped template param fails. #18202

Open tersec opened 3 years ago

tersec commented 3 years ago

Calling type aliased type class through untyped template param fails.

The example shows the complete cartesian product of

Three of these four work, but using joined functions and the type alias U doesn't.

Example

type
  R = object
  S = object
  U = R | S

  L = object
  B = object
  C = B | L

proc f(n: L, q: R | S) = echo "L"
proc f(n: B, q: R | S) = echo "B"

proc g(n: C, q: R | S) = echo (when n is L: "L" else: "B")

proc h(n: L, q: U) = echo "L"
proc h(n: B, q: U) = echo "B"

proc j(n: C, q: U) = echo (when n is L: "L" else: "B")

proc e(n: B | L, a: R) =
  template t(operations: untyped, fn: untyped) = fn(n, operations)

  # Work as expected
  t(a, f)
  t(a, g)
  t(a, j)

  # Error: type mismatch: got <R, proc [*missing parameters*](n: B, q: U) | proc [*missing parameters*](n: L, q: U)>
  t(a, h)

e(L(), R())
e(B(), R())

Current Output

please check whether the problem still exists in git head before posting, see rebuilding the compiler.

type_aliases_with_template.nim(29, 4) Error: type mismatch: got <R, proc [*missing parameters*](n: B, q: U) | proc [*missing parameters*](n: L, q: U)>
but expected one of: 
template t(operations: untyped; fn: untyped)
  first type mismatch at position: 2
  required type for fn: untyped
  but expression 'h' is of type: None

expression: t(a, h)

Tested to be the same on:

$ ./env.sh nim --version
Nim Compiler Version 1.2.12 [Linux: amd64]
Compiled at 2021-06-02
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 121628357ec7fae91335bd392f03b0e6dd249319
active boot switches: -d:release
Nim Compiler Version 1.4.2 [Linux: amd64]
Compiled at 2021-05-13
Copyright (c) 2006-2020 by Andreas Rumpf

active boot switches: -d:release

and the current git head,

Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-06-06
Copyright (c) 2006-2021 by Andreas Rumpf

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

Expected Output

L
L
L
L
B
B
B
B
metagn commented 2 weeks ago

Works in devel, even when changing the signature of t to include typed or have return type untyped. No idea what the root cause was, can't say for certain there isn't another related issue. Doesn't work in 2.0.8. Doesn't seem to need --experimental:openSym.