Commit Graph

22729 Commits

Author SHA1 Message Date
ringabout
d260855f35 fixes #25048; Closure environment wrongly marked as cyclic (#25220)
fixes  #25048

```nim
proc canFormAcycleAux =
  of tyObject:
    # Inheritance can introduce cyclic types, however this is not relevant
    # as the type that is passed to 'new' is statically known!
    # er but we use it also for the write barrier ...
    if tfFinal notin t.flags:
      # damn inheritance may introduce cycles:
      result = true
```

It seems that all objects without `tfFinal` in their flags are
registering cycles. It doesn't seem that `Env` can be a cyclic type
because of inheritance since it is not going to be inherited after all
by another `Env` object type

(cherry picked from commit f191ba8ddd)
2025-10-15 10:10:20 +02:00
ringabout
8b8272d729 fixes #25210; VM error when passing object field ref to proc(var T): var T (#25213)
fixes #25210
no longer transform `addr(obj.field[])` into `obj.field` to keep the
addressing needed for VM

(cherry picked from commit fb4a82f5cc)
2025-10-15 10:10:14 +02:00
ringabout
48d71e7ead fixes nightlies due to UB errors; increase maxCPU hard limits (#25219)
ref https://github.com/nim-lang/Nim/pull/25217

The issues is actually that there is a hard limit for max cpus in
niminst: https://github.com/nim-lang/Nim/pull/25219, which set to 20
while there is a 21 cpus now

(cherry picked from commit c0fa86872b)
2025-10-14 11:37:24 +02:00
Juan Carlos
0c2d7d9307 Fix Bisect (#25218)
- Nim requires `SSL_library_init`, OpenSSL 3.x removed
`SSL_library_init`, Windows defaults to OpenSSL 3.x, then install
OpenSSL 1.x on Windows.
- Keep `jiro4989/setup-nim-action` at `v1`, because `v2` uses a YAML
"hardcoded" matrix of Nim versions, but this Bisect "dynamically" finds
the Nim version with a bug, therefore we cant hardcode Nim versions in
the YAML, the Bisect programmatically installs required Nim versions as
it goes bisecting commit-by-commit.
- Update `actions/checkout` from `v4` to `v5`.
- Add support for Nim `2.2.4`.

@ringabout

(cherry picked from commit 1ef81f4190)
2025-10-13 09:18:34 +02:00
ringabout
83b9c834e8 nightlies regressions: CPU order matters for C sources? (#25217)
ref https://github.com/nim-lang/Nim/pull/25056

https://github.com/nim-lang/nightlies/actions/runs/18053288396/job/51378922406#step:12:1572

```
bin/nim compile -f --incremental:off --compileonly --gen_mapping --cc:gcc --skipUserCfg --os:windows --cpu:loongarch64 -d:danger -d:gitHash:f4497c61584dca8acd489ceb7ba862b150f5cf55 compiler/nim.nim
```

`loongarch64` is applied to all the platforms wrongly. Presumably it was
caused by the order?

(cherry picked from commit 3962264c35)
2025-10-13 09:18:26 +02:00
ringabout
f66f9f261a fixes #25204; Uninitialized variable usage in resize__system_u... in @psystem.nim.c in ORC (#25209)
fixes #25204

```nim
  of mUnaryMinusI..mAbsI: unaryArithOverflow(p, e, d, op)
  of mAddI..mPred: binaryArithOverflow(p, e, d, op)
```
Arithmetic operations may raise exceptions. So we cannot entrust the
optimizer to skip `result` initialization in this situation, as
complained righteously by `gcc` and `clang`: `warning: ‘result’ may be
used uninitialized [-Wmaybe-uninitialize]`.

With this PR, `clang -c -Wuninitialized -O1 @psystem.nim.c` no longer
gives warnings

(cherry picked from commit 7c65d9e747)
2025-10-13 09:18:09 +02:00
Andreas Rumpf
47c41955bb unittest: show proper stack trace for 'check' (#25212)
(cherry picked from commit c4c51d7e78)
2025-10-13 09:17:20 +02:00
ringabout
9595e17ba5 fixes #25205 #14873; resets importc obj with nimZeroMem in specializeResetT for refc (#25207)
fixes #25205
fixes #14873

```nim
  type
    SysLockObj {.importc: "pthread_mutex_t", pure, final,
               header: """#include <sys/types.h>
                          #include <pthread.h>""", byref.} = object
      when defined(linux) and defined(amd64):
        abi: array[40 div sizeof(clong), clong]
```

Before this PR, in refc, `resetLoc` generates field assignments for each
fields of `importc` object. But the field `abi` is not a genuine field,
which doesn't exits in the struct. We could use `zeroMem` to reset the
memory if not leave it alone

(cherry picked from commit 02609f1872)
2025-10-08 08:33:16 +02:00
narimiran
2136f349f4 Revert "fixes #25205 #14873; resets importc obj with nimZeroMem in specializeResetT for refc (#25207)"
This reverts commit 9628c7a4f8.
2025-10-08 07:03:51 +02:00
Gleb
4ed76ff007 fix spawn not used on linux (#25206)
Subj, among other things slows down the compilation of large projects on
linux significantly.

(cherry picked from commit 440b55a44a)
2025-10-07 13:19:49 +02:00
ringabout
9628c7a4f8 fixes #25205 #14873; resets importc obj with nimZeroMem in specializeResetT for refc (#25207)
fixes #25205
fixes #14873

```nim
  type
    SysLockObj {.importc: "pthread_mutex_t", pure, final,
               header: """#include <sys/types.h>
                          #include <pthread.h>""", byref.} = object
      when defined(linux) and defined(amd64):
        abi: array[40 div sizeof(clong), clong]
```

Before this PR, in refc, `resetLoc` generates field assignments for each
fields of `importc` object. But the field `abi` is not a genuine field,
which doesn't exits in the struct. We could use `zeroMem` to reset the
memory if not leave it alone

(cherry picked from commit 02609f1872)
2025-10-07 13:19:43 +02:00
bptato
cfe163795f Fix POSIX signal(3) binding's type signature; remove bsd_signal (#24400)
POSIX signal has an identical definition to ISO C signal:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/signal.html

```c
void (*signal(int sig, void (*func)(int)))(int);

/* more readably restated by glibc as */
typedef void (*sighandler_t)(int);

sighandler_t signal(int signum, sighandler_t handler);
```

However, std/posix had omitted the function's return value; this fixes
that.

To prevent breaking every single line of code ever that touched this
binding (including mine...), I've also made it discardable.

Additionally, I have noticed that bsd_signal's type signature is wrong -
it should have been identical to signal. But bsd_signal was already
removed in POSIX 2008, and sigaction is the recommended, portable POSIX
signal interface. So I just deleted the bsd_signal binding.

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
(cherry picked from commit 483389d399)
2025-09-29 08:45:59 +02:00
ringabout
8a5067912c fixes #21138; closure func used in the loop (#25196)
fixes #21138

(cherry picked from commit cc49bf07fe)
2025-09-29 08:45:49 +02:00
J. Neuschäfer
3774f84e8c Improve s390x CPU support (#25056)
TODO list, copied from the documentation:

- [x] compiler/platform.nim Add os/cpu properties.
- [x] lib/system.nim Add os/cpu to the documentation for system.hostOS
and system.hostCPU.
- [x] ~~compiler/options.nim Add special os/cpu property checks in
isDefined.~~ seems unnecessary; isn't dont for most CPUs
- [x] compiler/installer.ini Add os/cpu to Project.Platforms field.
- [x] lib/system/platforms.nim Add os/cpu.
- [x] ~~std/private/osseps.nim Add os specializations.~~
- [x] ~~lib/pure/distros.nim Add os, package handler.~~
- [x] ~~tools/niminst/makefile.nimf Add os/cpu compiler/linker flags.~~
already done in https://github.com/nim-lang/Nim/pull/20943
- [x] tools/niminst/buildsh.nimf Add os/cpu compiler/linker flags.

For csource:

- [x] have compiler/platform.nim updated
- [x] have compiler/installer.ini updated
- [x] have tools/niminst/buildsh.nimf updated
- [x] have tools/niminst/makefile.nimf updated
- [ ] be backported to the Nim version used by the csources
- [ ] the new csources must be pushed
- [ ] the new csources revision must be updated in
config/build_config.txt

Additionally:

- [x] check relation to https://github.com/nim-lang/Nim/pull/20943

Possible future work:

- Porting Nim to s390x-specific operating systems, notably z/OS

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
(cherry picked from commit f4497c6158)
2025-09-29 08:45:30 +02:00
ringabout
a2e2da2ab2 fixes #25167; fixes deref type (#25195)
fixes #25167

(cherry picked from commit fed0053481)
2025-09-29 08:44:50 +02:00
Andreas Rumpf
3d40226993 fixes #24261 (#25193)
(cherry picked from commit 9f74712ec6)
2025-09-26 08:56:47 +02:00
ringabout
73d9194449 fixes #21476; internal error: proc has no result symbol (#25192)
fixes #21476

(cherry picked from commit 3e2852cb1b)
2025-09-26 08:56:09 +02:00
ringabout
0a18975472 fixes #23949; cannot return lent expression from conditionals like case (#25190)
fixes #23949

It can also allow  `endsInNoReturn` in branches later

(cherry picked from commit ceaa7fb4e8)
2025-09-24 08:54:39 +02:00
ringabout
716642567c fixes #25127; disable lent types as object fields in returns (#25189)
fixes #25127

(cherry picked from commit d85c0324b7)
2025-09-24 08:54:33 +02:00
Zoom
b28b321eba stdlib: system: fix incorrect VM detection in substr impls (#25182)
...introduced by me in #24792. Sorry.

This fix doesn't avoid copying the `restrictedBody` twice in the
generated code but has the benefit of working.

Proper fix needs a detection that can set a const bool for a module
once. `when nimvm` is restricted in use and is difficult to dance
around. Some details in: #12517, #12518, #13038

I might have copied the buggy solution from some discussion and it might
have worked at some point, but it's small excuse.

(cherry picked from commit 6938fce40c)
2025-09-24 08:54:20 +02:00
ringabout
5724c685e9 fixes #24760; Noncopyable base type ignored (#24777)
fixes #24760

I tried `incl` `tfHasAsgn` to nontrivial assignment, but that solution
seems to break too many things. Instead, in this PR, `passCopyToSink`
now checks nontrivial assignment

(cherry picked from commit e958f4a3cd)
2025-09-24 08:54:10 +02:00
bptato
5d3d8b52cd Disable strict aliasing on clang (#25067)
Workaround for #24596.

I also took the liberty to disable it on all targets with GCC, since
their documentation claims that it is also enabled on -Os.

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
(cherry picked from commit 3f48576113)
2025-09-22 08:49:04 +02:00
ringabout
ee2b480da6 makes DuplicateModuleImport back to an error (#25178)
fixes #24998

Basically it retraces back to the situation before
https://github.com/nim-lang/Nim/pull/18366 and
https://github.com/nim-lang/Nim/pull/18362, i.e.

```nim
import fuzz/a
import fuzz/a
```

```nim
import fuzz/a
from buzz/a
```

```nim
import fuzz/a except nil
from fuzz/a import addInt
```

All of these cases are now flagged as invalid and triggers a
redefinition error, i.e., each module name importing is treated as
consistent as the symbol definition

kinda annoying for importing/exporting with `when conditions` though

ref https://github.com/nim-lang/Nim/issues/18762
https://github.com/nim-lang/Nim/issues/20907

```nim
from std/strutils import toLower
when not defined(js):
  from std/strutils import toUpper
```

(cherry picked from commit 87ee9c84cb)
2025-09-22 08:48:48 +02:00
Andreas Rumpf
79e9634369 fixes #24361 (#25179)
(cherry picked from commit 16394c3772)
2025-09-22 08:47:23 +02:00
Jacek Sieka
2c4b889d0a Remove Nim signal handler for SIGINT (#25169)
Inside a signal handler, you cannot allocate memory because the signal
handler, being implemented with a C
[`signal`](https://en.cppreference.com/w/c/program/signal) call, can be
called _during_ a memory allocation - when that happens, the CTRL-C
handler causes a segfault and/or other inconsistent state.

Similarly, the call can happen from a non-nim thread or inside a C
library function call etc, most of which do not support reentrancy and
therefore cannot be called _from_ a signal handler.

The stack trace facility used in the default handler is unfortunately
beyond fixing without more significant refactoring since it uses
garbage-collected types in its API and implementation.

As an alternative to https://github.com/nim-lang/Nim/pull/25110, this PR
removes the most problematic signal handler, namely the one for SIGINT
(ctrl-c) - SIGINT is special because it's meant to cause a regular
shutdown of the application and crashes during SIGINT handling are both
confusing and, if turned into SIGSEGV, have downstream effects like core
dumps and OS crash reports.

The signal handlers for the various crash scenarios remain as-is - they
may too cause their own crashes but we're already going down in a bad
way, so the harm is more limited - in particular, crashing during a
crash handler corrupts `core`/crash dumps. Users wanting to keep their
core files pristine should continue to use `-d:noSignalHandler` - this
is usually the better option for production applications since they
carry more detail than the Nim stack trace that gets printed.

Finally, the example of a ctrl-c handler performs the same mistake of
calling `echo` which is not well-defined - replace it with an example
that is mostly correct (except maybe for the lack of `volatile` for the
`stop` variable).

(cherry picked from commit 41ce86b577)
2025-09-22 08:47:08 +02:00
ringabout
cf5099cdba fixes #25173; SinglyLinkedList.remove broken / AssertionDefect (#25175)
fixes #25173

(cherry picked from commit 51a9ada043)
2025-09-17 09:04:31 +02:00
Jacek Sieka
8ea9c6454c remove alloc cruft (#25170)
(cherry picked from commit 40fe59b6ef)
2025-09-17 09:04:24 +02:00
Jacek Sieka
2123969cc4 orc: fix overflow checking regression (#25089)
Raising exceptions halfway through a memory allocation is undefined
behavior since exceptions themselves require multiple allocations and
the allocator functions are not reentrant.

It is of course also expensive performance-wise to introduce lots of
exception-raising code everywhere since it breaks many optimisations and
bloats the code.

Finally, performing pointer arithmetic with signed integers is incorrect
for example on on a 32-bit systems that allows up to 3gb of address
space for applications (large address extensions) and unnecessary
elsewhere - broadly, stuff inside the memory allocator is generated by
the compiler or controlled by the standard library meaning that
applications should not be forced to pay this price.

If we wanted to check for overflow, the right way would be in the
initial allocation location where both the size and count of objects is
known.

The code is updated to use the same arithmetic operator style as for
refc with unchecked operations rather than disabling overflow checking
wholesale in the allocator module - there are reasons for both, but
going with the existing flow seems like an easier place to start.

(cherry picked from commit 8b9972c8b6)
2025-09-17 09:04:16 +02:00
ringabout
f0b22a7620 minor improvements of error messages of objvariants (#25040)
Because `prevFields` and `currentFields` have been already quoted by
`'`, no need to add another.

The error message was

```
The fields ''x'' and ''y'' cannot be initialized together, because they are from conflicting branches in the case object.
```

(cherry picked from commit cdb750c962)
2025-09-17 09:04:05 +02:00
Miran
b5dd9735f4 replace outdated macos-13 runner (#25155)
(cherry picked from commit c49fb5ac5f)
2025-09-17 09:03:54 +02:00
ringabout
2fc23370ec fixes #24844; Invalid C codegen refc with generic types containing gc memory (#25160)
fixes #24844

it may not be used in other places except in `genTraverseProc`,
we have to generate a `typedesc` for this case, not a weak `typedec`

(cherry picked from commit a77d1cc6c1)
2025-09-17 09:03:43 +02:00
lit
4c7ddcd79a fixes #25162; fixup 0f5732bc8c: withValue for immut tab wrong chk cond (#25163)
fixes #25162
ref https://github.com/nim-lang/Nim/pull/24825

(cherry picked from commit ff9cae896c)
2025-09-12 14:42:59 +02:00
ringabout
377b6cc6bf disable thttpclient_ssl (#25164)
(cherry picked from commit bf2395a62e)
2025-09-12 14:42:48 +02:00
bptato
569968a916 Fix nimIoselector define in std/selectors (#25104)
Also added some documentation to the header.

See: https://forum.nim-lang.org/t/13311

> I did try using the flag, but couldn't get it to work. If I do
-d:nimIoSelector, the defined check passes, but the other code fails to
compile because there is no const named nimIoSelector. It seemed like a
bug to me, do you have a working number compiler invocation?

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
(cherry picked from commit d60e0211bc)
2025-09-12 14:42:42 +02:00
Ryan McConnell
a6585c1df9 two small concept patches (#25076)
- slightly better typeclass logic (eg for bare `range`)
- reverse matching now substitutes potential implementation for `Self`

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
(cherry picked from commit 88da5e8cee)
2025-09-12 14:42:35 +02:00
bptato
d84ca9c013 Allow assignment of nested non-closure procs to globals (#25154)
For memory-safety, this only seems problematic in case of closures, so I
just special cased that.

Fixes #25131

(cherry picked from commit d73f478bdc)
2025-09-12 14:42:24 +02:00
ringabout
8ea5ba7000 move std/parsesql to nimble packages (#25156)
pending https://github.com/nim-lang/packages/pull/3117

ref https://github.com/nim-lang/parsesql

(cherry picked from commit f90951cc61)
2025-09-12 14:42:17 +02:00
Andreas Rumpf
bd22f6e9fd GDB script: minor improvements (#24965)
(cherry picked from commit af6be4f839)
2025-09-12 14:41:53 +02:00
Yuriy Glukhov
87cc6d0a91 Optimize @, fixes #25063 (#25064)
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
(cherry picked from commit 49e66e80f0)
2025-09-12 14:41:42 +02:00
Judd
031bbcdece Update asyncfile.nim: support write to > 2GB file on Windows (#25105)
`DWORD` is defined as `int32`, so `DWORD(...)` would not work as
expected. When writing to files larger than 2GB, exception occurs:

```
unhandled exception: value out of range: 4294967295 notin -2147483648 .. 2147483647 [RangeDefect]
```

This PR is a quick fix for this.

P.S. Why `DWORD` is defined as `int32`?

(cherry picked from commit 4f09675d8a)
2025-09-12 14:41:35 +02:00
ringabout
2031f9e202 fixes #25078; filterIt wrongly results in rvalue (#25139)
fixes #25078

(cherry picked from commit 76d07e8caa)
2025-09-12 14:41:15 +02:00
Jacek Sieka
8f7b312f24 sequtils: findIt (#25134)
Complements `anyIt`, `find`, etc, plugging an odd gap in the `xxxIt`
family of functions

(cherry picked from commit 5ba279276e)
2025-09-10 07:58:50 +02:00
ringabout
576c401816 fixes #25117; requiresInit not checked for result if it has been used (#25151)
fixes #25117

errors on `requiresInit` of `result` if it is used before
initialization. Otherwise

```nim
    # prevent superfluous warnings about the same variable:
    a.init.add s.id
```

It produces a warning, and this line prevents it from being recognized
by the `requiresInit` check in `trackProc`

(cherry picked from commit c8456eacd5)
2025-09-10 07:58:44 +02:00
ringabout
1ab6879799 fixes #25120; don't generate hooks for NimNode (#25144)
fixes #25120

(cherry picked from commit 34bb37ddda)
2025-09-10 07:58:27 +02:00
ringabout
99b09e6609 fixes #24093; Dereferencing result of cast in single expression triggers unnecessary copy (#25143)
fixes #24093

transforms
```nim
let a = new array[1000, byte]
block:
  for _ in cast[typeof(a)](a)[]:
    discard
```
into
```nim
let a = new array[1000, byte]
block:
  let temp = cast[typeof(a)](a)
  for _ in temp[]:
    discard
```
So it keeps the same behavior with the manual version

(cherry picked from commit 08d74a1c27)
2025-09-10 07:58:21 +02:00
Tomohiro
516f5141ba fixes tnewruntime_strutils.nim not to raise AssertionDefect (#25142)
Follow up to https://github.com/nim-lang/Nim/pull/25126
It changed `formatSize` outputs from some inputs, so some of existing
test code related to it need to be updated.
Sorry, I didn't know `tests/destructor/tnewruntime_strutils.nim` has
tests calls `formatSize`.

(cherry picked from commit 8ea8755cc0)
2025-09-05 09:33:14 +02:00
Tomohiro
fe12553cfb fixes overflow defect when compiled with js backend (#25132)
Follow up to https://github.com/nim-lang/Nim/pull/25126.
This fixes overflow defect when `tests/stdlib/tstrutils.nim` was
compiled with js backend.

(cherry picked from commit 87dc1820c0)
2025-09-02 14:28:56 +02:00
Tomohiro
55806c8b36 fixes #25125 (#25126)
`strutils.formatSize` returns correct strings from large values close to
`int64.high`.
Round down `bytes` when it is converted to float.

(cherry picked from commit 065c4b443b)
2025-08-29 08:12:46 +02:00
ringabout
4cbdebcd50 fixes #25121; [FieldDefect] with iterator-loop (#25130)
fixes #25121

(cherry picked from commit 0a8f618e2b)
2025-08-29 08:12:39 +02:00
Andreas Rumpf
fef0b5a351 fixes #25114 (#25124)
(cherry picked from commit d472022a77)
2025-08-29 08:12:09 +02:00