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

Error: invalid type for const: T prevents default initializing at compile time when T has a reference field #8521

Closed
timotheecour opened this issue Aug 3, 2018 · 10 comments

Comments

@timotheecour
Copy link
Member

timotheecour commented Aug 3, 2018

proc defaultImpl[T]():T=
  var a: T
  return a

type App = object
  a: ref int

proc foo(): auto=
  let val = defaultImpl[App]()
  return val

proc test(): auto=
  when defined(case1):
    # ok
    const val = defaultImpl[ref int]()
    echo repr(val)

  when defined(case2):
    # Error: invalid type for const: App
    const val = defaultImpl[App]()

  when defined(case3):
    # ok
    let val = defaultImpl[App]()
    echo repr(val)

  when defined(case4):
    # Error: invalid type for const: App
    const val = foo()

  ## EDIT: case5 is unrelated => see https://github.com/nim-lang/Nim/issues/8733
  when defined(case5):
    # Error: cannot evaluate at compile time: val
    var val{.compileTime.} = defaultImpl[App]()
    echo repr(val)

test()

/cc @Araq
This is a blocker for doing compile time reflection (and doing many things that rely on it). Any chance this could be fixed?

const foo = defaultImpl[seq[seq[string]]]()

(seq and string are reference types)

@GULPF
Copy link
Member

GULPF commented Aug 3, 2018

I'm very surprised that const a: ref int = nil compiles. IMO const of a ref type doesn't make any sense. Note that the limitation is specifically for const, not for compile time:

# Works fine
static:
  var a: ref int
  new(a)
  a[] = 1
  echo a[]

@andreaferretti
Copy link
Collaborator

I think this makes sense, right? You cannot have a constant reference, the actual address will be unknown until runtime. You can do this in the case of nil, although I am not sure how useful this is

@timotheecour timotheecour changed the title [BLOCKER] Error: invalid type for const: T prevents default initializing at compile time when T has a reference field Error: invalid type for const: T prevents default initializing at compile time when T has a reference field Aug 3, 2018
@timotheecour
Copy link
Member Author

Note that the limitation is specifically for const, not for compile time:

when static: var a: int is inside a proc, it gives compile error.

@timotheecour
Copy link
Member Author

You cannot have a constant reference, the actual address will be unknown until runtime. You can do this in the case of nil, although I am not sure how useful this is

It's useful for generic programming. And yes, the value should obviously be nil, but it doesn't compile.

@Araq
Copy link
Member

Araq commented Aug 3, 2018

This is a blocker for doing compile time reflection (and doing many things that rely on it). Any chance this could be fixed?

No, it makes no sense to support every type T for a const section. And it doesn't block anybody from writing good software no matter how many grandiose nouns you use to describe the issue.

@timotheecour
Copy link
Member Author

the problem is when a type T contains a (sub-sub-...) field which has a ref. That field will (currently) prevent T from being in const section, even if we're not dereferencing that field.

@timotheecour
Copy link
Member Author

Note that the limitation is specifically for const, not for compile time

EDIT: just added a new test case (see case5) with var val{.compileTime.} = defaultImpl[App]() ; that also fails

@GULPF
Copy link
Member

GULPF commented Aug 18, 2018

@timotheecour I think case5 is unrelated. The following also fails to compile with the same error:

proc foo =
  var val {.compileTime.} = 1

It looks like {.compileTime.} is broken inside procs. The following example with {.compileTime.} and default works:

proc defaultImpl[T]():T=
  var a: T
  return a

type App = object
  a: ref int

var val {.compileTime.} = defaultImpl[App]()

static:
  echo val

@timotheecour
Copy link
Member Author

@GULPF you're right, case5 is unrelated, moved it to #8733

@Araq
Copy link
Member

Araq commented Jun 11, 2019

"Error: invalid type for const: App" is a good error message and works as designed. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants