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

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

Closed
timotheecour opened this issue Nov 13, 2020 · 3 comments · Fixed by #24067

Comments

@timotheecour
Copy link
Member

timotheecour commented Nov 13, 2020

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

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
@cooldome
Copy link
Member

cooldome commented Nov 14, 2020

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

@timotheecour timotheecour changed the title lent type(expr) gives Error: type mismatch params and return can't call procs in for their type declaration; gives Error: type mismatch Nov 14, 2020
@timotheecour timotheecour changed the title params and return can't call procs in for their type declaration; gives Error: type mismatch in generics, params and return can't call procs in for their type declaration; gives Error: type mismatch Nov 14, 2020
@timotheecour
Copy link
Member Author

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

@metagn
Copy link
Collaborator

metagn commented Aug 26, 2024

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

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.

3 participants