Open veksha opened 11 months ago
comment on the forum by janAkali:
Converters enable implicit conversion. And combined with templates it causes unexpected results:
template inspect(a: typed) =
if a is seq and a.len > 0:
if a[0] is byte:
echo "It is a sequence of bytes!"
elif a[0] is char:
echo "It is a sequence of chars!"
else:
echo "It is something else."
converter `toString`(a: seq[byte]): string = cast[ptr string](a.addr)[]
echo char is byte # false
inspect(@[byte 1, 2, 3]) # compiles and outputs: it is a sequence of chars!
!nim c
converter toString(a: seq[byte]): string {.inline.} = cast[ptr string](a.addr)[]
var query = @[byte 1,2,3]
query[0..1] = @[byte 8,8]
echo query # outputs: @[8, 8, 3]
0 bytes (0 bytes)
```cpp
```
2023-12-18T11:20:48
2023-12-18T11:20:48
0 bytes (0 bytes)
```cpp
```
2023-12-18T11:20:49
2023-12-18T11:20:49
0 bytes (0 bytes)
```cpp
```
2023-12-18T11:20:49
2023-12-18T11:20:50
0 bytes (0 bytes)
```cpp
```
2023-12-18T11:20:52
2023-12-18T11:20:53
0 bytes (0 bytes)
```cpp
```
2023-12-18T11:20:55
2023-12-18T11:20:55
0 bytes (0 bytes)
```cpp
```
2023-12-18T11:20:57
2023-12-18T11:20:57
0 bytes (0 bytes)
```cpp
```
2023-12-18T11:20:59
2023-12-18T11:20:59
11.4.0
14.0.0
19.0
2023-12-18T11:20:17Z
2
nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
:robot: Bug found in 14 minutes
bisecting 7
commits at 0
commits per second
1.6.14 👎 FAIL Error: expression has no address; maybe use 'unsafeAddr'
maybe i used unsafeAddr
back then.
!nim c
converter toString(a: seq[byte]): string {.inline.} = cast[ptr string](a.unsafeAddr)[]
var query = @[byte 1,2,3]
query[0..1] = @[byte 8,8]
echo query # outputs: @[8, 8, 3]
0 bytes (0 bytes)
```cpp
```
2023-12-18T11:53:20
2023-12-18T11:53:20
0 bytes (0 bytes)
```cpp
```
2023-12-18T11:53:21
2023-12-18T11:53:21
0 bytes (0 bytes)
```cpp
```
2023-12-18T11:53:21
2023-12-18T11:53:22
109.23 Kb (111,848 bytes)
```cpp
#define NIM_INTBITS 64
#include "nimbase.h"
#include 2023-12-18T11:53:24
2023-12-18T11:53:25
106.20 Kb (108,744 bytes)
```cpp
#define NIM_INTBITS 64
#include "nimbase.h"
#include 2023-12-18T11:53:27
2023-12-18T11:53:27
106.23 Kb (108,776 bytes)
```cpp
#define NIM_INTBITS 64
#include "nimbase.h"
#include 2023-12-18T11:53:30
2023-12-18T11:53:30
101.98 Kb (104,424 bytes)
```cpp
#define NIM_INTBITS 64
#include "nimbase.h"
#include 2023-12-18T11:53:32
2023-12-18T11:53:33
2022-09-28 22:28:45 +0300
on commit #919a889ba with message:
```
moderate system cleanup & refactor (#20355)
* system refactor, move out 600 lines
* compilation, slice, backwardsindex, misc_num moved out of system
* some procs/types moved into arithmetics, basic_types
* system no longer depends on syncio
* some procs moved around to fit with their surroundings
* make exceptions an import, old ops to misc_num
* move instantiationInfo back
* move back nim version, fix windows echo
* include compilation
* better docs for imported modules, fix unsigned ops
also remove ze, ze64, toU8, toU16, toU32 with nimPreviewSlimSystem
* fix terminal
* workaround IC test & weird csize bug, changelog
* move NimMajor etc back to compilation, rebase for CI
* try ic fix
* form single `indices`, slim out TaintedString, try fix IC
* fix CI, update changelog, addQuitProc
* fix CI
* try fix CI
* actually fix CI finally hopefully
* Update lib/system/compilation.nim
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
* update kochdocs
* hopefully fix csize uses for slimsystem
* fix tquit
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
```
The bug is in the files:
```
changelog.md
compiler/lexer.nim
compiler/renderer.nim
doc/manual.md
lib/core/typeinfo.nim
lib/posix/posix_haiku.nim
lib/posix/posix_nintendoswitch.nim
lib/pure/memfiles.nim
lib/pure/nativesockets.nim
lib/pure/os.nim
lib/pure/ssl_certs.nim
lib/pure/terminal.nim
lib/std/syncio.nim
lib/system.nim
lib/system/ansi_c.nim
lib/system/arithmetics.nim
lib/system/basic_types.nim
lib/system/chcks.nim
lib/system/comparisons.nim
lib/system/compilation.nim
lib/system/ctypes.nim
lib/system/exceptions.nim
lib/system/indices.nim
lib/system/iterators.nim
lib/system_overview.rst
lib/windows/winlean.nim
nimsuggest/tests/tsug_typedecl.nim
nimsuggest/tests/ttype_decl.nim
tests/cpp/tretvar.nim
tests/misc/tradix.nim
tests/misc/tunsignedconv.nim
tests/misc/tvarnums.nim
tests/range/trange.nim
tests/stdlib/tquit.nim
tests/stdlib/tstrset.nim
tests/vm/tzero_extend.nim
tools/kochdocs.nim
```
The bug can be in the commits:
(Diagnostics sometimes off-by-one).11.4.0
14.0.0
19.0
2023-12-18T11:52:50Z
5
nim c --run -d:nimDebug -d:nimDebugDlOpen -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --verbosity:0 --hints:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
:robot: Bug found in 53 minutes
bisecting 1995
commits at 37
commits per second
The real culprit is #8817, for some reason it only triggered after #20355, possibly some symbol order ambiguity stuff. string
has a specific overload for []
but seq
has to use the unspecified magic overload, which it doesn't prefer over just converting to string
.
A choice would be to add specific overloads for all of the types []
can index, seq
, array
, etc. See here. There are some complications, like pointer versions of these also support subscripts, i.e. (ptr ptr ref ptr string)[0]
works, and tuples would need to be underspecified ((tuple, int): untyped
). Also int
is too specific for the index compared to what the compiler allows which is all int types, I don't know if this could cause issues with matching better/worse than user overloads.
Removing the overloads added in #8817 would break #8049 instead, the test for it has been changed to not be top level anymore which doesn't trigger the issue, so CI is misleading if this is attempted.
Description
In my full program i have the following converter
toString
. (though this small example i don't even use strings) later in the code i have slice assignmentquery[0..1] = @[byte 8,8]
.code compiled fine with Nim 1.x
Nim Version
Nim Compiler Version 2.1.1 [Windows: amd64] Compiled at 2023-11-19 Copyright (c) 2006-2023 by Andreas Rumpf
git hash: cecaf9c56b1240a44a4de837e03694a0c55ec379 active boot switches: -d:release
Current Output
Expected Output
Possible Solution
No response
Additional Information
please see forum thread https://forum.nim-lang.org/t/10655 for more info.