Commit Graph

22749 Commits

Author SHA1 Message Date
ringabout
ceaa7fb4e8 fixes #23949; cannot return lent expression from conditionals like case (#25190)
fixes #23949

It can also allow  `endsInNoReturn` in branches later
2025-09-24 06:29:57 +02:00
ringabout
d85c0324b7 fixes #25127; disable lent types as object fields in returns (#25189)
fixes #25127
2025-09-23 13:04:27 +02:00
Zoom
6938fce40c 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.
2025-09-22 11:02:50 +02:00
ringabout
e958f4a3cd 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
2025-09-22 10:50:57 +02:00
bptato
3f48576113 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>
2025-09-19 08:07:43 +02:00
ringabout
87ee9c84cb 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
```
2025-09-18 20:50:46 +02:00
Andreas Rumpf
16394c3772 fixes #24361 (#25179) 2025-09-18 19:44:39 +02:00
Jacek Sieka
41ce86b577 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).
2025-09-17 10:58:21 +02:00
ringabout
51a9ada043 fixes #25173; SinglyLinkedList.remove broken / AssertionDefect (#25175)
fixes #25173
2025-09-16 17:05:09 +02:00
Jacek Sieka
40fe59b6ef remove alloc cruft (#25170) 2025-09-15 15:09:42 +02:00
Jacek Sieka
8b9972c8b6 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.
2025-09-15 15:08:21 +02:00
ringabout
cdb750c962 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.
```
2025-09-15 15:04:35 +02:00
Miran
c49fb5ac5f replace outdated macos-13 runner (#25155) 2025-09-15 15:03:59 +02:00
ringabout
a77d1cc6c1 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`
2025-09-15 15:03:22 +02:00
lit
ff9cae896c fixes #25162; fixup 0f5732bc8c: withValue for immut tab wrong chk cond (#25163)
fixes #25162
ref https://github.com/nim-lang/Nim/pull/24825
2025-09-12 14:07:05 +02:00
ringabout
bf2395a62e disable thttpclient_ssl (#25164) 2025-09-12 14:06:29 +02:00
bptato
d60e0211bc 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>
2025-09-11 23:45:47 +02:00
Ryan McConnell
88da5e8cee 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>
2025-09-11 20:50:11 +08:00
bptato
d73f478bdc 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
2025-09-11 09:22:47 +02:00
ringabout
f90951cc61 move std/parsesql to nimble packages (#25156)
pending https://github.com/nim-lang/packages/pull/3117

ref https://github.com/nim-lang/parsesql
2025-09-11 08:47:02 +02:00
Andreas Rumpf
af6be4f839 GDB script: minor improvements (#24965) 2025-09-10 15:38:25 +02:00
Yuriy Glukhov
49e66e80f0 Optimize @, fixes #25063 (#25064)
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
2025-09-10 15:37:55 +02:00
Judd
4f09675d8a 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`?
2025-09-10 15:37:09 +02:00
ringabout
76d07e8caa fixes #25078; filterIt wrongly results in rvalue (#25139)
fixes #25078
2025-09-10 15:36:39 +02:00
Jacek Sieka
5ba279276e sequtils: findIt (#25134)
Complements `anyIt`, `find`, etc, plugging an odd gap in the `xxxIt`
family of functions
2025-09-09 20:05:12 +02:00
ringabout
c8456eacd5 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`
2025-09-09 16:22:05 +02:00
ringabout
34bb37ddda fixes #25120; don't generate hooks for NimNode (#25144)
fixes #25120
2025-09-09 16:17:22 +02:00
ringabout
08d74a1c27 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
2025-09-09 16:16:12 +02:00
Tomohiro
8ea8755cc0 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`.
2025-09-04 06:46:50 +02:00
Tomohiro
87dc1820c0 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.
2025-09-01 18:29:58 +02:00
Tomohiro
065c4b443b fixes #25125 (#25126)
`strutils.formatSize` returns correct strings from large values close to
`int64.high`.
Round down `bytes` when it is converted to float.
2025-08-28 21:56:46 +02:00
ringabout
0a8f618e2b fixes #25121; [FieldDefect] with iterator-loop (#25130)
fixes #25121
2025-08-28 21:51:45 +02:00
Andreas Rumpf
d472022a77 fixes #25114 (#25124) 2025-08-27 12:23:04 +02:00
ringabout
e2a294504e fixes #25066; forbids comparing pointers at compile time (#25103)
fixes #25066

Probably it is not worth implementing comparing pointers at compile
time. For a starter, we can improve the error message instead of letting
it crash
2025-08-27 10:14:46 +02:00
ringabout
b527db9ddd fixes #25109; fixes #25111 transform addr(conv(x)) -> conv(addr(x)) (#25112)
follows up https://github.com/nim-lang/Nim/pull/24818
relates to https://github.com/nim-lang/Nim/issues/23923

fixes #25109
fixes #25111

transform `addr ( conv ( x ) )` -> `conv ( addr ( x ) )` so that it is
the original value that is being modified


```c
T1_ = ((unsigned long long*) ((&a_1)));
r(T1_);
```
2025-08-21 13:31:55 +02:00
RAMLAH MUNIR
c6352ce0ab closes #25084 : docs: fix example for *+ operator (#25102)
## Description

Fixed an inconsistency in the Nim manual's example for the `*+`
operator.

Previously, the example on line 4065 of `doc/manual.md` used variables
`a`, `b`, and `c`:

```nim
assert `*+`(3, 4, 6) == `+`(`*`(a, b), c)
```

This did not match the preceding call which directly used literals `3`,
`4`, `6`.

Updated the example to:

```nim
assert `*+`(3, 4, 6) == `+`(`*`(3, 4), 6)
```

This change makes the example consistent with the function call and
immediately understandable to readers without requiring prior variable
definitions.

## Rationale

* Improves clarity by avoiding undefined variables in a code snippet.
* Matches the example usage in the preceding line.
* Helps beginners understand the operator's behavior without additional
context.

## Changes

* **Edited**: `doc/manual.md` line 4065 — replaced variables `a`, `b`,
`c` with literals `3`, `4`, `6`.

## Issue

Closes #25084
2025-08-14 16:33:52 +02:00
Laylie
53bb0b591a Link to nims docs from nimc docs (#25095) 2025-08-10 11:38:39 +02:00
ringabout
a0b3048f3f Revert "std/locks use header files instead of dlls on windows" (#25091)
Reverts nim-lang/Nim#25090


It seems to cause problems for C++ and i686


```
2025-08-08T02:37:55.5976232Z c:/a/nightlies/nightlies/external/mingw32/bin/../lib/gcc/i686-w64-mingw32/11.1.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\runneradmin\nimcache\manual_experimental_snippet_106_d\@pstd@sprivate@ssyslocks.nim.c.o:@pstd@sprivate@ssyslocks.nim.c:(.text+0x29): undefined reference to `SleepConditionVariableCS'
2025-08-08T02:37:55.5978066Z c:/a/nightlies/nightlies/external/mingw32/bin/../lib/gcc/i686-w64-mingw32/11.1.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\runneradmin\nimcache\manual_experimental_snippet_106_d\@pthreadpool.nim.c.o:@pthreadpool.nim.c:(.text+0x26): undefined reference to `InitializeConditionVariable'
2025-08-08T02:37:55.5980101Z c:/a/nightlies/nightlies/external/mingw32/bin/../lib/gcc/i686-w64-mingw32/11.1.0/../../../../i686-w64-mingw32/bin/ld.exe: C:\Users\runneradmin\nimcache\manual_experimental_snippet_106_d\@pthreadpool.nim.c.o:@pthreadpool.nim.c:(.text+0x116): undefined reference to `WakeConditionVariable'
2025-08-08T02:37:55.5981093Z collect2.exe: error: ld returned 1 exit status
2025-08-08T02:37:55.5988564Z Error: execution of an external program failed: 'gcc.exe   -o 
```
2025-08-08 22:22:23 +08:00
ringabout
02e3487c9c std/locks use header files instead of dlls on windows (#25090)
ref https://github.com/nim-lang/nimony/pull/1370
2025-08-07 17:45:00 +02:00
ringabout
e194c7cc87 adds more functions to to dirs and files (#25083)
ref https://forum.nim-lang.org/t/13272
2025-08-01 12:34:01 +02:00
Yuriy Glukhov
161b321796 SOCKS5H support for httpclient (#25070)
- Added support for SOCKS5h (h for proxy-side DNS resolving) to
httpclient
- Deprecated `auth` arguments for `newProxy` constructors, for auth to
be embedded in the url.

Unfortunately `http://example.com` is not currently reachable from
github CI, so the tests fail there for a few days already, I'm not sure
what can be done here.
2025-07-30 00:14:41 +02:00
Yuriy Glukhov
9b527a51b8 Fixed typos in comments (#25071) 2025-07-23 23:50:03 +02:00
Emre Şafak
bb93b39b58 docs: Add example to tutorial for interfaces using closures (#25068)
* Add a new section to doc/tut2.md explaining interfaces.
* Provide a code example demonstrating how to simulate interfaces using
objects of closures.
* The example shows a basic IntFieldInterface with getter and setter
procedures.

This PR was inspired by the discussion in
https://forum.nim-lang.org/t/13217

---------

Co-authored-by: Emre Şafak <esafak@users.noreply.github.com>
Co-authored-by: Andreas Rumpf <araq4k@proton.me>
2025-07-23 23:49:31 +02:00
Juan M Gómez
cd806f9dbe Bumps nimble 0.20.1 (#25062) 2025-07-19 08:16:51 +02:00
ringabout
08d51e5c88 fixes #7179; Floats are not range checked (#25050)
fixes #7179


```nim
var f = 751.0
echo f.int8
```

In this case, `int8(float)` yields different numbers for different
optimization levels, since float to int conversions are undefined
behaviors. In this PR, it mitigates this problem by conversions to same
size integers before converting to the final type: i.e.
`int8(int64(float))`, which has UB problems but is better than before
2025-07-18 21:30:50 +02:00
ringabout
5b5cd7fa67 fixes inefficient codegen for field return (#24874)
fixes https://github.com/nim-lang/Nim/issues/23395
fixes https://github.com/nim-lang/Nim/issues/23395
2025-07-18 21:30:32 +02:00
ringabout
8e57a9f623 fixes #24719; improves order of destruction (#25060)
fixes #24719
2025-07-18 23:02:16 +08:00
Nikolay Nikolov
478773ffb1 NimSuggest: Fix for the inlay exception hints with generic procs (#23610)
Based on the fix, started by SirOlaf in #23414

---------

Co-authored-by: SirOlaf <>
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
2025-07-18 08:44:36 +02:00
ringabout
f4ebabb9b3 fixes CI failures (#25058) 2025-07-17 19:32:41 +08:00
ringabout
611b8bbf67 fixes #25007; implements setLenUninit for refc (#25022)
fixes #25007

```nim
proc setLengthSeqUninit(s: PGenericSeq, typ: PNimType, newLen: int, isTrivial: bool): PGenericSeq {.
    compilerRtl.} =
```

In this added function, only the line `zeroMem(dataPointer(result,
elemAlign, elemSize, newLen), (result.len-%newLen) *% elemSize)` is
removed from `proc setLengthSeqV2` when enlarging a sequence.

JS and VM versions simply use `setLen`.
2025-07-14 23:19:58 +02:00