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

Type tyAnything can't be matched to proc argument of typedesc type #13193

Closed
mratsim opened this issue Jan 18, 2020 · 6 comments
Closed

Type tyAnything can't be matched to proc argument of typedesc type #13193

mratsim opened this issue Jan 18, 2020 · 6 comments

Comments

@mratsim
Copy link
Collaborator

mratsim commented Jan 18, 2020

Building on #13095 and its fix #13181, supportsCopyMem in a type section doesn't work with implicit generic parameters.
The error message in particular mentions type any which makes it hard to debug as there is no type any in the codebase.

Test case

import typetraits

type
  CpuStorage[T] {.shallow.} = ref object
    when supportsCopyMem(T):
      raw_buffer*: ptr UncheckedArray[T] # 8 bytes
      memalloc*: pointer                 # 8 bytes
      isMemOwner*: bool                  # 1 byte
    else: # Tensors of strings, other ref types or non-trivial destructors
      raw_buffer*: seq[T]                # 8 bytes (16 for seq v2 backed by destructors?)

  Tensor*[T] = object
    storage*: CpuStorage[T]

proc foo(t: Tensor): bool =
  # This breaks
  return false

proc bar[T](t: Tensor[T]): bool =
  return false

Error

Error: type mismatch: got <type any>
but expected one of: 
proc supportsCopyMem(t: typedesc): bool
  first type mismatch at position: 1
  required type for t: type T
  but expression 'T' is of type: type any

expression: supportsCopyMem(T)
@cooldome
Copy link
Member

Test case is not complete, it doesn't reproduce the issue

@mratsim
Copy link
Collaborator Author

mratsim commented Jan 19, 2020

It does for me on commit bc14453

image

@cooldome
Copy link
Member

cooldome commented Jan 19, 2020

Apologies, I managed to replicate this issue. I had a look, issue is not type trait specific. Generally type tyAnything can't be matches against type tyTypeDesc. Simplified example that fails with the same error

proc mycheck(t: typedesc): bool =
  true

type
  CpuStorage[T] {.shallow.} = ref object
    when mycheck(T):
      raw_buffer*: ptr UncheckedArray[T] # 8 bytes
      memalloc*: pointer                 # 8 bytes
      isMemOwner*: bool                  # 1 byte
    else: # Tensors of strings, other ref types or non-trivial destructors
      raw_buffer*: seq[T]                # 8 bytes (16 for seq v2 backed by destructors?)

proc foo(t: CpuStorage): bool =
  # This breaks
  return false

@cooldome cooldome changed the title supportsCopyMem in type section with implicit generic param Type tyAnything can't be matched to type tyTypeDesc Jan 19, 2020
@cooldome cooldome changed the title Type tyAnything can't be matched to type tyTypeDesc Type tyAnything can't be matched to type tyTypeDesc Jan 19, 2020
@cooldome cooldome changed the title Type tyAnything can't be matched to type tyTypeDesc Type tyAnything can't be matched to proc argument of typedesc type Jan 21, 2020
@Clyybber
Copy link
Contributor

Clyybber commented Apr 21, 2020

The underlying issue here is that in Generic[T], T defaults to any instead of type as it IMO should. (I think @timotheecour also discovered that in some PR)

Both examples compile after replacing CpuStorage[T] with CpuStorage[T: type]

@mratsim
Copy link
Collaborator Author

mratsim commented Sep 29, 2020

For posterity, from our IRC discussion back in April

From IRC: https://irclogs.nim-lang.org/21-04-2020.html#14:46:18

image

@metagn
Copy link
Collaborator

metagn commented Sep 16, 2023

Both examples above seem to work now? Probably because of #22029

@Araq Araq closed this as completed Sep 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants