Nim icon indicating copy to clipboard operation
Nim copied to clipboard

in generics, params and return can't call procs in for their type declaration; gives Error: type mismatch

Open timotheecour opened this issue 5 years ago • 3 comments

Example 1

lent type(expr) gives Error: type mismatch

proc byLent2[T](a: T): lent type(a[0]) = a[0] # Error: type mismatch: got <T, int literal(0)>
# proc byLent2[T](a: T): lent typeof(a[0]) = a[0] # ditto
# proc byLent2[T](a: T): lent[type(a[0])] = a[0] # Error: no generic parameters allowed for lent

Current Output

Error: type mismatch: got <T, int literal(0)>

Expected Output

works

[EDIT] Example 2

when true: # templates work; and `typeof` special case also works:
  template fn(a): untyped = a
  proc my[T](a: T): typeof(fn(a)) = discard

when true: # procs don't
  proc fn[U](a: U): auto = a
  proc my[T](a: T): typeof(fn(a)) = discard

when true: # ditto with params
  proc fn[U](a: U): auto = a
  proc my[T](a: T, b: typeof(fn(a))) = discard

when true: # non-generic procs don't have this problem
  proc fn[U](a: U): auto = a
  proc my(a: int): typeof(fn(a)) = discard

Additional Information

  • devel 1.5.1 c39fa0d49535da7f803d9972abdb15d09eed92a6
  • might be related to static early symbol resolution (eg https://github.com/nim-lang/Nim/issues/8677)
  • added disabled test case in https://github.com/nim-lang/Nim/pull/15960

notes

default values for optionals can call procs, unrestricted:

when true:
  proc fn[U](a: U): auto = a
  proc my[T](a: T, b = typeof(fn(a)).default) = discard

timotheecour avatar Nov 13 '20 21:11 timotheecour

It is not lent specific. It is known fact that you can't use function calls in return type definition for generic procs. These don't compile:

proc my[T](a: T): typeof(a[0]) =
  discard

proc my2[T](a: T): array[sizeof(a[0]), T] =
  discard

cooldome avatar Nov 14 '20 22:11 cooldome

thanks for pointing this out, I've updated title + added Example 2

timotheecour avatar Nov 14 '20 23:11 timotheecour

After #24005 we just need to update subscript sem to generate tyFromExpr for generic types when c.inGenericContext > 0, the overload method doesn't work because the magic subscript overloads always match

metagn avatar Aug 26 '24 05:08 metagn