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

expression in type body causes incorrect/useless error message #12415

Closed
krux02 opened this issue Oct 11, 2019 · 1 comment · Fixed by #22564
Closed

expression in type body causes incorrect/useless error message #12415

krux02 opened this issue Oct 11, 2019 · 1 comment · Fixed by #22564

Comments

@krux02
Copy link
Contributor

krux02 commented Oct 11, 2019

Example

import macros

macro isSomePointerImpl(t: typedesc): bool =
  var impl = t.getTypeInst[1].getTypeImpl
  if impl.kind == nnkDistinctTy:
    impl = impl[0].getTypeImpl
  if impl.kind in {nnkPtrTy,nnkRefTy}:this
    result = newLit(true)
  elif impl.kind == nnkSym and impl.eqIdent("pointer"):
    result = newLit(true)
  else:
    result = newLit(false)

proc isSomePointer[T](t: typedesc[T]): bool {.compileTime.} =
  isSomePointerImpl(t)

type
  Option*[T] = object
    ## An optional type that stores its value and state separately in a boolean.
    when isSomePointer(typedesc(T)):
      val: T
    else:
      val: T
      has: bool

Current Output

/tmp/scratch.nim(20, 23) Error: type mismatch: got <type T>
but expected one of: 
proc isSomePointer[T](t: typedesc[T]): bool
  first type mismatch at position: 1
  required type for t: type T
  but expression 'type(T)' is of type: type T
  • The expression is typedesc(T) not type(T), some remains of the failed attempt to allow the keyword type in more expressions.
  • Required is type T, but got type T? How is that really the cause of the problem?

Expected Output

This should just compile

Possible Solution

I think the body of the generic type is instantiated immediately. So isSomePointer(typedec(T)) is resolved immediately before T is even resolved to a concrete type value. I think the evaluation of isSomePointer(typedesc(T)) should be delayed until the parameter T has been give to the type, similar to how isSomePointer(typedesc(T)) is resolved within the body of a generic function.

Additional Information

This is code that I wrote in order to fix issue #9566. When this issue has been fixed, #9566 can be fixed as well.

@krux02 krux02 changed the title expression in type body causes a weird error message expression in type body causes incorrect/useless error message Oct 13, 2019
@metagn
Copy link
Collaborator

metagn commented Aug 26, 2023

Given example compiles in 2.0

I think the evaluation of isSomePointer(typedesc(T)) should be delayed until the parameter T has been give to the type

#22029

metagn added a commit to metagn/Nim that referenced this issue Aug 27, 2023
closes nim-lang#12582, closes nim-lang#19552, closes nim-lang#2465, closes nim-lang#4596, closes nim-lang#15246,
closes nim-lang#12683, closes nim-lang#7889, closes nim-lang#4547, closes nim-lang#12415, closes nim-lang#2002,
closes nim-lang#1771, closes nim-lang#5121

The test for nim-lang#5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.
@Araq Araq closed this as completed in c19fd69 Aug 27, 2023
narimiran pushed a commit that referenced this issue Sep 18, 2023
* test case haul for old generic/template/macro issues

closes #12582, closes #19552, closes #2465, closes #4596, closes #15246,
closes #12683, closes #7889, closes #4547, closes #12415, closes #2002,
closes #1771, closes #5121

The test for #5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.

* fix template gensym test

(cherry picked from commit c19fd69)
narimiran pushed a commit that referenced this issue Sep 18, 2023
* test case haul for old generic/template/macro issues

closes #12582, closes #19552, closes #2465, closes #4596, closes #15246,
closes #12683, closes #7889, closes #4547, closes #12415, closes #2002,
closes #1771, closes #5121

The test for #5648 is also moved into its own test
from `types/tissues_types` due to not being joinable.

* fix template gensym test

(cherry picked from commit c19fd69)
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.

2 participants