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

A bizzare static int related error #9114

Closed
dawkot opened this issue Sep 29, 2018 · 8 comments
Closed

A bizzare static int related error #9114

dawkot opened this issue Sep 29, 2018 · 8 comments

Comments

@dawkot
Copy link

dawkot commented Sep 29, 2018

When either of the commented lines is uncommented the code compiles, but if both are uncommented an error is returned.

import sugar

type Obj[N: static int] = object

func arr(N: static int, fn: 0..N -> void): Obj[N] =
  for i in (0 ..< N): fn i

func map[N](a: Obj[N], fn: () -> void): Obj[N] =
  arr(N, (i: 0..N) => fn())

static:
  # discard Obj[2]().map(() => nil)
  # discard Obj[3]().map(() => nil)
  discard

error message:

 error2.nim(13, 19) template/generic instantiation from here
 error2.nim(9, 6) Error: type mismatch: got <static[int literal(3)](3), proc (i: range 0..3(int)){.closure, noSideEffect, gcsafe, locks: <unknown>.}>
 but expected one of:
 func arr(N: static int; fn: 0 .. N -> void): Obj[N]
   first type mismatch at position: 2
   required type: proc (i0: range 0..2(int)){.closure.}
   but expression 'proc (i: 0 .. N): auto = fn()' is of type: proc (i: range 0..3(int)){.closure, noSideEffect, gcsafe, locks: <unknown>.}
 
 expression: arr(N, proc (i: 0 .. N): auto = fn())
@mratsim
Copy link
Collaborator

mratsim commented Sep 29, 2018

Not too sure how to solve that but the relevant part of the error message is this:

required type: proc (i0: range 0..2(int)){.closure.}
expression ... is of type: proc (i: range 0..3(int))

@zah zah added the Static[T] label Sep 29, 2018
@jangko
Copy link
Contributor

jangko commented Sep 29, 2018

the symptoms looks very much like #8569, #7976

@metagn
Copy link
Collaborator

metagn commented Sep 29, 2018

Is the -> macro causing it?

@dawkot
Copy link
Author

dawkot commented Sep 29, 2018

Is the -> macro causing it?

No. But, sure, I shouldn't use it in the example in the first place. This returns the same error:

type Obj[N: static int] = object

func arr(N: static int, fn: proc(_: 0..N): void): Obj[N] =
  for i in (0 .. N): fn i

func map[N](a: Obj[N], fn: proc()): Obj[N] =
  arr(N, proc(i: 0..N) = fn())

static:
  # discard Obj[2]().map(proc() = discard)
  # discard Obj[3]().map(proc() = discard)
  discard

@LemonBoy
Copy link
Contributor

type Obj[N: static int] = object

func arr[N: static int](fn: proc(_: 0..N): void): Obj[N] =
  for i in (0 .. N): fn i

func map[N](a: Obj[N], fn: proc()): Obj[N] =
  arr[N](proc(i: 0..N) = fn())

static:
  discard Obj[2]().map(proc() = discard)
  discard Obj[3]().map(proc() = discard)

Using a static int parameter makes the compiler instantiate the proc(_: 0..N): void) expression as proc(_: 0..2): void) and doesn't make the arr procedure generic (even though the N parameter is lifted as an implicit generic) so it is only instantiated once.

@krux02
Copy link
Contributor

krux02 commented Nov 12, 2018

@dawkot is the question solved?

@dawkot
Copy link
Author

dawkot commented Nov 12, 2018

@krux02 Sure, but at least the error message should be more clear.

@dawkot
Copy link
Author

dawkot commented Nov 3, 2019

The code compiles now on

Nim Compiler Version 1.0.2 [Windows: amd64]
Compiled at 2019-10-22

@dawkot dawkot closed this as completed Nov 3, 2019
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

7 participants