Skip to content

Instantly share code, notes, and snippets.

@gmpreussner
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmpreussner/a1ebb01db395be2529e6 to your computer and use it in GitHub Desktop.
Save gmpreussner/a1ebb01db395be2529e6 to your computer and use it in GitHub Desktop.
# this works ##########################
type
Foo[N: static[int], T] = object
elements: array[0..(N - 1), T]
var f2 = Foo[2, float](elements: [1.0, 2.0])
var f3 = Foo[3, float](elements: [1.0, 2.0, 3.0])
# this fails ##########################
type
FooTypes = enum
Fubar,
Snafu,
Tarfu
Foo[N: static[int], T; BT: static[FooTypes]] = object
elements: array[0..(N - 1), T]
var f2s = Foo[2, float, FooTypes.Snafu](elements: [1.0, 2.0])
var f2t = Foo[2, float, FooTypes.Tarfu](elements: [1.0, 2.0]) # Error: internal error: genObjConstr
@gmpreussner
Copy link
Author

also affects regular procs:

type
  FooTypes = enum
    Snafu,
    Tarfu

  Foo[N: static[int], T; BT: static[FooTypes]] = object
    elements: array[0..(N - 1), T]

proc foobar(f: Foo) =
  echo f.elements[0]

# note the missing '()' after the types, so we are actually assigning a type
# zah says: the compiler should inform us that this is illegal in a runtime context
#var f2s = Foo[2, float, FooTypes.Snafu]
#var f2t = Foo[2, float, FooTypes.Tarfu]
#foo f2s # Error: internal error: GetUniqueType

# here is the actual bug
var f2s = Foo[2, float, FooTypes.Snafu]()
var f2t = Foo[2, float, FooTypes.Tarfu]()
foo f2s # SIGSEGV: Illegal storage access.

@gmpreussner
Copy link
Author

I did some more tests. It looks like this problem only occurs if the proc signature is NOT fully qualified with all generic params. The example above can be fixed by rewriting 'foobar' as follows:

#proc foobar(f: Foo) =
# this works:
proc foobar[N, T, BT](f: Foo[N, T, BT]) =
  echo f.elements[0]

I have verified this with several other examples in my code base.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment