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

Internal Error with with static generic parameters #1082

Closed
BitPuffin opened this issue Apr 11, 2014 · 2 comments
Closed

Internal Error with with static generic parameters #1082

BitPuffin opened this issue Apr 11, 2014 · 2 comments
Assignees

Comments

@BitPuffin
Copy link

import linagl/vector

import math

type
  TMatrix*[T; R, C: static[int]] = array[0..R-1, array[0..C-1, T]] ## Row major matrix type. 
  TMat4* = TMatrix[float32, 4, 4]

template row*[T; R, C: static[int]](m: TMatrix[T, R, C], rowidx: range[0..R-1]): TVector[T, R] =
  m[rowidx]

proc col*[T; R, C: static[int]](m: TMatrix[T, R, C], colidx: range[0..C-1]): TVector[T, C] {.noSideEffect.} =
  for i in low(m)..high(m):
    result[i] = m[i][colidx]

proc `*`*[T; R, N, C: static[int]](a: TMatrix[T, R, N], b: TMatrix[T, N, C]): TMatrix[T, R, C] {.noSideEffect.} =
  for i in low(a)..high(a):
    for j in low(a[i])..high(a[i]):
      result[i][j] = a.row(i) *. b.col(j)

proc translate*(v: TVec4): TMat4 {.noSideEffect.} =
  result = [[1f32, 0f32, 0f32, 0f32],
            [0f32, 1f32, 0f32, 0f32],
            [0f32, 0f32, 1f32, 0f32],
            [v[0], v[1], v[2], 1f32]]

proc rotatex*(angle: float): TMat4 =
  result = [[1f32,          0f32,           0f32,           0f32],
            [0f32, cos(angle).float32, sin(angle).float32,  0f32],
            [0f32, -sin(angle).float32, cos(angle).float32, 0f32],
            [0f32,          0f32,           0f32,           1f32]]

proc orbitxAround(point: TVec4, angle: float): TMat4 =
  result = translate(-point)*rotatex(angle)*translate(point)

Error:

matrix.nim(34, 20) Info: instantiation from here
matrix.nim(34, 20) Info: instantiation from here
matrix.nim(16, 49) Error: internal error: cannot generate code for: R
No stack traceback available

It appears that it doesn't know ho to map the R and C of TMat4 to TMatrix. The instantiation is in orbitxAround

@BitPuffin
Copy link
Author

linagl/vector can be seen here: https://gist.github.com/BitPuffin/10470877#file-vector-nim-L1

@zah zah self-assigned this Apr 12, 2014
Araq added a commit that referenced this issue Apr 21, 2014
@dom96 dom96 added the Generics label Aug 14, 2014
@Araq Araq added the Static[T] label Mar 27, 2015
@gmpreussner
Copy link
Contributor

The error above has been fixed with Araq's latest static[T] fixes. However, there's now an internal compiler error. Here's compileable code:

import math

type
  TMatrix*[T; R, C: static[int]] = array[0..R-1, array[0..C-1, T]] ## Row major matrix type.
  TMat4* = TMatrix[float32, 4, 4]
  TVector*[T; C: static[int]] = array[0..C-1, T]
  TVec4* = TVector[float32, 4]

template row*[T; R, C: static[int]](m: TMatrix[T, R, C], rowidx: range[0..R-1]): TVector[T, R] =
  m[rowidx]

proc col*[T; R, C: static[int]](m: TMatrix[T, R, C], colidx: range[0..C-1]): TVector[T, C] {.noSideEffect.} =
  for i in low(m)..high(m):
    result[i] = m[i][colidx]

proc `*`*[T; R, N, C: static[int]](a: TMatrix[T, R, N], b: TMatrix[T, N, C]): TMatrix[T, R, C] {.noSideEffect.} =
  for i in low(a)..high(a):
    for j in low(a[i])..high(a[i]):
      result[i][j] = a.row(i) * b.col(j)

proc translate*(v: TVec4): TMat4 {.noSideEffect.} =
  result = [[1f32, 0f32, 0f32, 0f32],
            [0f32, 1f32, 0f32, 0f32],
            [0f32, 0f32, 1f32, 0f32],
            [v[0], v[1], v[2], 1f32]]

proc rotatex*(angle: float): TMat4 =
  result = [[1f32,          0f32,           0f32,           0f32],
            [0f32, cos(angle).float32, sin(angle).float32,  0f32],
            [0f32, -sin(angle).float32, cos(angle).float32, 0f32],
            [0f32,          0f32,           0f32,           1f32]]

proc orbitxAround(point: TVec4, angle: float): TMat4 =
  result = translate(point)*rotatex(angle)*translate(point)

Throws:

SIGSEGV: Illegal storage access. (Attempt to read from nil?)

zah added a commit that referenced this issue Jun 10, 2017
zah added a commit that referenced this issue Jun 10, 2017
zah added a commit that referenced this issue Jun 19, 2017
@Araq Araq closed this as completed in 24966e0 Jun 20, 2017
Clyybber pushed a commit to Clyybber/Nim that referenced this issue Feb 24, 2024
## Summary

Clean up the category and enable it for all targets by default. This
significantly increases test coverage of exceptions for the JS and VM
targets.

## Details

**General changes/cleanup:**
* references to issues like `nim-lang#1234` are replaced with proper GitHub
  links
* the `targets` key is removed for tests that should be run on all
  targets
* usage of `cmd` is replaced with usage of `matrix` (allowing for
  multi-target testing)
* `write(stdout, ...)` is replaced with `echo` in order to support the
  JS and VM targets
* execution of `static:` blocks is replaced with using the VM target,
  when the test is not specific to compile-time execution
* tests that don't work but should are marked as `knownIssue`

**Specific changes:**
* where not necessary for the test, `getCurrentExceptionMsg` is
  replaced with `getCurrentException().msg`, as the former is not yet
  supported by the VM
* `texceptions2.nim` is removed -- it's redundant with `texception.nim`
* `texception_message_null_byte.nim` is turned into a normal test that
  doesn't rely on self-execution
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