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

BackwardsIndex on a ref array fails to compile in procs #17357

Open
ShadowElf37 opened this issue Mar 12, 2021 · 2 comments
Open

BackwardsIndex on a ref array fails to compile in procs #17357

ShadowElf37 opened this issue Mar 12, 2021 · 2 comments

Comments

@ShadowElf37
Copy link

BackwardsIndex [^i] on a ref array will fail to compile inside procedures, iterators, funcs, and methods. It will compile if you dereference the array explicitly, but this is contrary to the behavior of normal indices, as well as backwards indices outside a proc.

Example

var a = new array[1, int]
a[] = [2,]

# WORKS
echo a[0]

# WORKS
echo a[^1]

# WORKS
proc proc1[S: static[int], T](arr: ref array[S, T]): int =
    return arr[][0]

# WORKS
proc proc2[S: static[int], T](arr: ref array[S, T]): int =
    return arr[0]

# WORKS
proc proc3[S: static[int], T](arr: ref array[S, T]): int =
    return arr[][^1]

# DOESNT COMPILE
proc proc4[S: static[int], T](arr: ref array[S, T]): int =
    return arr[^1]

Error

Error: type mismatch: got <ref array[0..0, int], BackwardsIndex>
but expected one of: 
proc `[]`(s: string; i: BackwardsIndex): char
  first type mismatch at position: 0
proc `[]`[I: Ordinal; T](a: T; i: I): T
  first type mismatch at position: 0
proc `[]`[Idx, T, U, V](a: array[Idx, T]; x: HSlice[U, V]): seq[T]
  first type mismatch at position: 0
proc `[]`[Idx, T](a: array[Idx, T]; i: BackwardsIndex): T
  first type mismatch at position: 0
proc `[]`[Idx, T](a: var array[Idx, T]; i: BackwardsIndex): var T
  first type mismatch at position: 0
proc `[]`[T, U, V](s: openArray[T]; x: HSlice[U, V]): seq[T]
  first type mismatch at position: 0
proc `[]`[T, U](s: string; x: HSlice[T, U]): string
  first type mismatch at position: 0
proc `[]`[T](s: openArray[T]; i: BackwardsIndex): T
  first type mismatch at position: 0
proc `[]`[T](s: var openArray[T]; i: BackwardsIndex): var T
  first type mismatch at position: 0
template `[]`(s: string; i: int): char
  first type mismatch at position: 0

expression: `[]`(arr, BackwardsIndex(1))

Nim Compiler Version 1.4.4 [Windows: amd64]
Compiled at 2021-02-23
Copyright (c) 2006-2020 by Andreas Rumpf
@ghost
Copy link

ghost commented Mar 12, 2021

relevant piece of code in the compiler: https://github.com/nim-lang/Nim/blob/devel/compiler/semexprs.nim#L1475 (if I understand correctly)

@metagn
Copy link
Collaborator

metagn commented Nov 3, 2024

relevant piece of code in the compiler

Also mentioned here: #22970 (comment)

The solution here wouldn't be in the compiler though, BackwardsIndex which is implemented in user code just isn't implemented for ref array, for which normal indexing happens to work

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

2 participants