Comments (8)
I wish the API had it so that the second argument was the length of the openarray, not the end index, that would probably make more sense for the construction of the openarray too since it stores the length. For the start/end index version we could have overloaded it for HSlice
instead.
It's not really needed to fix this issue but we could still add an overload like toOpenArrayLen[I, T](a: array[I, T], start: I, len: int)
, so we could instead do:
v.toOpenArrayLen(0'i8, 0)
from nim.
I explained why this happens. I'm not saying the API is reasonable.
proc toOpenArray*[I, T](x: array[I, T]; first, last: I): openArray[T] {.
magic: "Slice".}
Isn't array[1, byte]
actually array[0..0, byte]
? So the type of first
and last
is range[0..0]
. It cannot accept -1
which is out of range. so toOpenArray(0'i8, -1'i8)
doesn't work since it can only match this one.
It is consistent with var x: range[0..0] = -1
.
toOpenArray(0, -1)
works because it matches proc toOpenArray*[T](x: openArray[T]; first, last: int): openArray[T]
instead of proc toOpenArray*[I, T](x: array[I, T]; first, last: I): openArray[T]
.
from nim.
I think there is no type resolution bug here but the code generation for some variant of magic: "Slice"
is overly picky.
from nim.
Just
echo v.toOpenArray(0, -1)
from nim.
The parameter type of first
, last
in toOpenArray[I, T](a: array[I, T], first, last: I)
is I
, not T
.
e.g. in your code var v: array[1, byte]
, the generic type I
is 0..0
(a slice of int
), so the type of first
, last
shall be of 0..0
instead of int8
.
Converting -1'i8
to 0..0
raises the error as shown above.
from nim.
the sanity ship of slice indexing in nim sailed, so we must do with what we have unfortunately.
an empty array is toOpenArray(0, -1)
because the second argument is inclusive and must therefore be -1
to point to the same location as the 0
- in particular, 0..0
is a slice of length 1.
int8
should promote to int
in all relevant aspects - this is a conversion bug.
it's a lot more dangerous at runtime:
var v: int
var v8: int8
var a: array[1, byte]
echo a.toOpenArray(v, v-1)
echo a.toOpenArray(v8, v8-1) # crashes here - obviously you should be able to use int8 like this.
from nim.
@arnetheduck toOpenArray
works for this case because proc toOpenArray*[T](x: openArray[T]; first, last: int): openArray[T]
takes precedence when both ends are integer.
var v: int
var a: array[1, byte]
echo a.toOpenArray(v, v-1)
So when implementing a toOpenarray
function
proc toOpenArray2*[I, T](x: array[I, T]; first, last: I) =
echo x
proc foo =
var v: array[1, byte]
toOpenArray2(v, 0, -1)
foo()
It gives Error: conversion from int literal(-1) to range 0..0(int) is invalid
as expected
from nim.
It gives Error: conversion from int literal(-1) to range 0..0(int) is invalid as expected
I'm not sure I understand your comment.
toOpenArray(0, -1)
is a valid slice over array[1, byte]
- in particular, it's the empty openarray.
toOpenArray(0'i8, -1'i8)
is also a valid slice range over array[1, byte]
- the same one. Both should work without compile-time error and/or crashes at runtime.
See https://play.nim-lang.org/#pasty=dAQERzuy
[]
/usercode/in.nim(6) in
/playground/nim/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: value out of range: -1 notin 0 .. 0 [RangeDefect]
we can see the empty openArray being printed, then the crash happening - it should have the same behavior regardless of int type.
from nim.
Related Issues (20)
- hello world Mummy web server crashes on devel HOT 17
- Sigsegv on std/tasks destructor HOT 2
- `=destroy` for non-var failed to compile when `nim doc` HOT 4
- regression from 2.0.4 to 2.0.6 cannot instantiate: 'SomeInteger' in return type HOT 1
- Deadloop in Windows, `os.sleep(-1)` HOT 5
- Deadloop in Windows, `os.sleep(-1)` HOT 1
- Inefficient codegen for field return HOT 2
- `setLen(0)` allocates memory causing performance regression HOT 3
- Generic sequence of proc type mismatch HOT 3
- `int64/int64` not compile for JS, but for C. HOT 4
- Local variables can be prematurely moved to closure, causing use-after-move HOT 2
- Regression: ambiguous identifier
- Regression from 1.6 to 2.0 using static parameter in return type with function taking typeclass HOT 3
- Warn on repeated non-trivial evaluations in templates
- Regression: compiler crash on static inference HOT 10
- in std/logging implement removeHandler
- `move(table)` does not move the table HOT 8
- Constructing case object with discriminator of field of other object fails with `the field(s) 'g' are in conflict with this value` HOT 4
- Method calling proc with return value overlapping declared generic type generates invalid C HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nim.