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

Wrong bounds check using template [] to access array in a const object #3899

jxy opened this issue Feb 24, 2016 · 2 comments


None yet
3 participants
Copy link

commented Feb 24, 2016

type O = object
  a: array[1..2,float]
template `[]`(x: O, i: int): float =
const c = O(a: [1.0,2.0])
echo c[2]

makes compile time error

Hint: system [Processing]
Hint: t [Processing]
t.nim(4, 6) Error: index out of bounds

If echo c[1] used instead of echo c[2], the compiled program prints 2.0.
If c is declared as a var, the code works correctly.


This comment has been minimized.

Copy link

commented Feb 24, 2016

Workaround: Use addressing from 0 like the rest of us ;-)


This comment has been minimized.

Copy link

commented Jul 22, 2016

the error triggered here in semfold.nim:

proc foldArrayAccess(m: PSym, n: PNode): PNode =
  of nkBracket:
    idx = idx - x.typ.firstOrd
    if idx >= 0 and idx < x.len: result = x.sons[int(idx)]
    else: localError(, errIndexOutOfBounds)

and the problem come from here in semexprs.nim:

proc semArrayConstr(c: PContext, n: PNode, flags: TExprFlags): PNode =
  result.typ.sons[0] = makeRangeType(c, 0, sonsLen(result) - 1,

makeRangeType in the last line of semArrayConstr need correct firstOrd and lastOrd, and I still don't know where to look for the information from inside semArrayConstr

Vindaar pushed a commit to Vindaar/Nim that referenced this issue Jan 22, 2019

Vindaar added a commit to Vindaar/Nim that referenced this issue Jan 22, 2019

@Araq Araq closed this in 36e34d9 Jan 23, 2019

ThomasTJdev added a commit to ThomasTJdev/Nim that referenced this issue Jan 27, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.