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

Concepts and cross-module generic binding madness #16755

Closed
alaviss opened this issue Jan 19, 2021 · 1 comment · Fixed by #24315
Closed

Concepts and cross-module generic binding madness #16755

alaviss opened this issue Jan 19, 2021 · 1 comment · Fixed by #24315

Comments

@alaviss
Copy link
Collaborator

alaviss commented Jan 19, 2021

Example

handles.nim:

type
  FD* = distinct cint

type
  AnyFD* = concept fd
    close(fd)

proc close*(fd: FD) =
  discard

type
  Handle*[T: AnyFD] = object
    fd: T

proc close*[T: AnyFD](h: var Handle[T]) =
  close h.fd

proc initHandle*[T: AnyFD](fd: T): Handle[T] =
  Handle[T](fd: fd)

files.nim:

import handles

type
  File* = ref object
    handle: Handle[FD]

proc close*[T: File](f: T) =
  f.handle.close()

proc newFile*(fd: FD): File =
  File(handle: initHandle(FD -1))

test.nim:

import files
from handles import FD
#import handles <- do this and it works

let wr = newFile(FD -1)
close wr

Current Output

test.nim(6, 7) template/generic instantiation of `close` from here
files.nim(8, 11) Error: type mismatch: got <Handle[handles.FD]>
but expected one of:
proc close(f: File)
  first type mismatch at position: 1
  required type for f: File
  but expression 'f.handle' is of type: Handle[handles.FD]
proc close(fd: FD)
  first type mismatch at position: 1
  required type for fd: FD
  but expression 'f.handle' is of type: Handle[handles.FD]
proc close[T: AnyFD](h: var Handle[T])
  first type mismatch at position: 1
  required type for h: var Handle[close.T]
  but expression 'f.handle' is of type: Handle[handles.FD]
test.nim(6, 7) template/generic instantiation of `close` from here
handles.nim(6, 10) AnyFD: type mismatch: got <Alias>
but expected one of:
proc close(f: File)
  first type mismatch at position: 1
  required type for f: File
  but expression 'fd' is of type: Alias
proc close[T: File](f: T)
  first type mismatch at position: 1
  required type for f: T: File
  but expression 'fd' is of type: Alias

expression: close(fd)
proc close[T: File](f: T)
  first type mismatch at position: 1
  required type for f: T: File
  but expression 'f.handle' is of type: Handle[handles.FD]

expression: close(f.handle)

Additional Information

$ nim -v
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-01-15
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 7b632f9ccbd4d15fa9fe4ca45b6fea22b259c81d
active boot switches: -d:release -d:nimUseLinenoise
@mratsim
Copy link
Collaborator

mratsim commented Jan 19, 2021

I would be surprised if it's not another instance of Generic sandwiches #11225. (tag #8677). I.e. not due to concept but by how generics are implemented and how generic (early) symbol resolution works.

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