Commit Graph

22861 Commits

Author SHA1 Message Date
ringabout
5daa186845 fix #25508; ignores void types in the backends (#25550)
fix #25508

(cherry picked from commit e4b1d8eebc)
2026-03-09 10:47:45 +01:00
Andreas Rumpf
0ccee3b4c2 fixes #24746 (#25587)
(cherry picked from commit 0395af2b34)
2026-03-09 10:13:31 +01:00
Juan M Gómez
3298863c2f update nimble commit (#25537)
Co-authored-by: narimiran <narimiran@disroot.org>
(cherry picked from commit 60661f6569)
2026-03-09 10:13:22 +01:00
metagn
f201c4e225 fix compiler crash with uncheckedAssign and range/distinct discrims [backport] (#25585)
On simple code like:

```nim
type Foo = object
  case x: range[0..7]
  of 0..2:
    a: string
  else:
    b: string

var foo = Foo()
{.cast(uncheckedAssign).}:
  foo.x = 5
```

The compiler tries to generate a destructor for the variant fields by
checking if the discrim is equal to the old one, but the type is not
skipped when looking for an `==` operator in system, so any
discriminator with type `range`/`distinct`/etc crashes with:

```
(10, 9) Error: can't find magic equals operator for type kind tyRange
```

This is fixed by just skipping abstract types.

(cherry picked from commit 7a87e7d199)
2026-03-09 10:13:04 +01:00
Zoom
29b0e8342b nimdoc: fix char literal tokenization (#25576)
This fixes highlighter's tokenization of char literals inside
parentheses and brackets.

The Nim syntax highlighter in `docutils/highlite.nim` incorrectly
tokenizes character literals that appear after punctuation characters,
such as all kinds of brackets.

For `echo('v', "hello")`, the tokenizer treated the first `'` as
punctuation because the preceding token was punctuation `(`. As a
result, the second `'` (after `v`) was interpreted as the start of a
character literal and the literal incorrectly extended to the end of the
line.

See other examples in the screenshot:
<img width="508" height="266" alt="Screenshot 2026-03-04 at 16-09-06
_y_test"
src="https://github.com/user-attachments/assets/94d991ae-79d2-4208-a046-6ed4ddcb5c34"
/>

This regression originates from a condition added in PR #23015 that
prevented opening a `gtCharLit` token when the previous token kind was
punctuation. Nim syntax allows character literals after punctuation such
as `(`, `[`, `{`, `:`, `;`, or `,`, of course. The only case mentioned
in the manual explicitly that actually requires special handling is
stroped proc declaration for literals (see the [last paragraph
here](https://nim-lang.github.io/Nim/manual.html#lexical-analysis-character-literals)):

```nim
proc `'customLiteral`(s: string)
```

This PR narrows the conditional to not entering charlit only after
backticks.

(cherry picked from commit 269a1c1fec)
2026-03-09 10:04:25 +01:00
Andreas Rumpf
a3918a57bc fixes #25552 (#25582)
(cherry picked from commit c033ccd2e5)
2026-03-09 10:04:04 +01:00
Constantine Molchanov
2018b23dce Nimsuggest: Operators in symbol outline (#25565)
So the problem is that Nim Language Server won't show procs like \`+\`
and \`==\` in the Document Symbols or Workspace Symbols lists. Which is
really annoying given they are regular procs just named a bit
differently.

Initially, I thought the problem was with nim-lang/langserver and opened
an issue there: https://github.com/nim-lang/langserver/issues/380

But after an investigation, it turned out the issue is fixed on the
nimsuggest side.

Strangely enough, calling `outline foo.nim:0:0` in nimsuggest manually
does show \`+\` as well as regular procs (e.g. `foo`) but when
nimsuggest is invoked from lsp only `foo` would be there.

Anyway, with this fix all procs appear on the symbol lists.

(cherry picked from commit 2290c75f12)
2026-03-09 09:28:27 +01:00
ringabout
98c67dff7f fixes #25566; {.align.} pragma where each 16-byte-aligned (#25570)
fixes #25566

(cherry picked from commit 8e2547a5e2)
2026-03-04 09:17:19 +01:00
Ryan McConnell
e4f1ccba8b fixes #25572 ICE evaluating closure iter with object conversion (#25575)
(cherry picked from commit 46cddbccd6)
2026-03-04 09:17:07 +01:00
vercingetorx
b8e13bd421 Fix memory leak in asyncdispatch.withTimeout by clearing losing callbacks (#25567)
withTimeout currently leaves the “losing” callback installed:

  - when fut finishes first, timeout callback remains until timer fires,
- when timeout fires first, fut callback remains on the wrapped future.

Under high-throughput use with large future payloads, this retains
closures/future references longer than needed and causes large transient
RSS growth.
This patch clears the opposite callback immediately once outcome is
decided, reducing retention without changing API behavior.

(cherry picked from commit 9ed4077d9a)
2026-03-02 11:02:10 +01:00
Kevin Hovsäter
7f34de5e1b Fix warning admonition in std/streams (#25564)
The rest of the body must be indented in order to fall under the warning
admonition. Right now, only the first part of the warning is inside the
admonition, see [std/streams](https://nim-lang.org/docs/streams.html).

(cherry picked from commit e69d672354)
2026-03-02 11:02:01 +01:00
ringabout
dc8d538683 fixes #25262; proc v[T: typedesc]() = discard / v[0]() compiles even though 0 isn't a typedesc (#25558)
fixes #25262

```nim
if constraint != nil and constraint.kind == tyTypeDesc:
  n[i].typ = e.typ
else:
  n[i].typ = e.typ.skipTypes({tyTypeDesc})
```
at least when `constraint` is a typedesc, it should not skip
`tyTypeDesc`

```nim
if arg.kind != tyTypeDesc:
  arg = makeTypeDesc(m.c, arg)
```
Wrappers literals into typedesc, which can cause problems. Though, it
doesn't seem to be necessary

(cherry picked from commit bd709f9b4c)
2026-03-02 11:01:01 +01:00
ringabout
f7a18fceba fixes #25553; Invalid codegen for accessing tuple in array (#25555)
fixes #25553

(cherry picked from commit 4566ffaca9)
2026-03-02 10:58:24 +01:00
Kevin Hovsäter
da1c712b21 Fix a few typos (#25563)
While fixing a few things in the tutorial, I found a few other typos
lingering in the `doc/` directory.

---------

Co-authored-by: Andreas Rumpf <araq4k@proton.me>
(cherry picked from commit a2db2af5b6)
2026-03-02 10:56:11 +01:00
Kevin Hovsäter
a35e1c9373 Fix std/pegs sequence example (#25562)
This corrects the example used to describe `std/pegs` sequence notion.
It incorrectly used `Z` whereas `C` was expected.

(cherry picked from commit c36617c490)
2026-03-02 10:56:04 +01:00
Raka Hourianto
5eb96d40ee nre: fix replacement string parser OOB access, numeric refs, and unterminated named refs (#25560)
1. A trailing `$` at the end of a replacement string could read out of
bounds via `how[i + 1]`; this now raises `ValueError` instead.

2. Numeric capture parsing used `id += (id * 10) + digit` instead of `id
= (id * 10) + digit`, so multi-digit refs were parsed incorrectly (e.g.
`$12` resolved as capture 13 instead of 12).

4. Unterminated named replacement syntax (e.g. `${foo)` is now rejected
with ValueError instead of being accepted and parsed inconsistently.

Found and fixed by GPT 5.3 Codex.

(cherry picked from commit 9b2b286baf)
2026-03-02 10:55:53 +01:00
Christian Zietz
7b8ef1a901 Atomics can't cause exceptions with Microsoft Visual C++ (#25559)
The `enforcenoraises` pragma prevents generation of exception checking
code for atomic... functions when compiling with Microsoft Visual C++ as
backend.

Fixes #25445

Without this change, the following test program:
```nim
import std/sysatomics

var x: ptr uint64 = cast[ptr uint64](uint64(0))
var y: ptr uint64 = cast[ptr uint64](uint64(42))
let z = atomicExchangeN(addr x, y, ATOMIC_ACQ_REL)

let a = atomicCompareExchangeN(addr x, addr y, y, true, ATOMIC_ACQ_REL, ATOMIC_ACQ_REL)

var v = 42
atomicStoreN(addr v, 43, ATOMIC_ACQ_REL)
let w = atomicLoadN(addr v, ATOMIC_ACQ_REL)
```
... generates this C code when compiling with `--cc:vcc`:
```c
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
        {
        NU64* T1_;
NIM_BOOL T2_;
NI T3_;
NIM_BOOL* nimErr_;
        nimfr_("testexcept", "/tmp/testexcept.nim");
nimErr_ = nimErrorFlag();
        nimlf_(7, "/tmp/testexcept.nim");T1_ = ((NU64*) 0);
T1_ = atomicExchangeN__testexcept_u4((&x__testexcept_u2), y__testexcept_u3, ((int) 4));
if (NIM_UNLIKELY((*nimErr_))) {
        goto BeforeRet_;
}
z__testexcept_u32 = T1_;
        nimln_(9);T2_ = ((NIM_BOOL) 0);
T2_ = atomicCompareExchangeN__testexcept_u33((&x__testexcept_u2), (&y__testexcept_u3), y__testexcept_u3, NIM_TRUE, ((int) 4), ((int) 4));
if (NIM_UNLIKELY((*nimErr_))) {
        goto BeforeRet_;
}
a__testexcept_u45 = T2_;
        nimln_(12);atomicStoreN__testexcept_u47(((&v__testexcept_u46)), ((NI) 43));
if (NIM_UNLIKELY((*nimErr_))) {
        goto BeforeRet_;
}
        nimln_(13);T3_ = ((NI) 0);
T3_ = atomicLoadN__testexcept_u53(((&v__testexcept_u46)));
if (NIM_UNLIKELY((*nimErr_))) {
        goto BeforeRet_;
}
w__testexcept_u59 = T3_;
BeforeRet_: ;
        nimTestErrorFlag();
                popFrame();
}
}
```

Note the repeated checks for `*nimErr_`.

With this PR applied, the checks vanish:
```c
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
        {
                nimfr_("testexcept", "/tmp/testexcept.nim");
        nimlf_(7, "/tmp/testexcept.nim");z__testexcept_u32 = atomicExchangeN__testexcept_u4((&x__testexcept_u2), y__testexcept_u3, ((int) 4));
        nimln_(9);a__testexcept_u45 = atomicCompareExchangeN__testexcept_u33((&x__testexcept_u2), (&y__testexcept_u3), y__testexcept_u3, NIM_TRUE, ((int) 4), ((int) 4));
        nimln_(12);atomicStoreN__testexcept_u47(((&v__testexcept_u46)), ((NI) 43));
        nimln_(13);w__testexcept_u59 = atomicLoadN__testexcept_u53(((&v__testexcept_u46)));
nimTestErrorFlag();
                popFrame();
}
}
```

For reference, with gcc as backend the generated code looks as follows:
```c
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
        {
                nimfr_("testexcept", "/tmp/testexcept.nim");
        nimlf_(7, "/tmp/testexcept.nim");z__testexcept_u9 = __atomic_exchange_n((&x__testexcept_u2), y__testexcept_u3, __ATOMIC_ACQ_REL);
        nimln_(9);a__testexcept_u18 = __atomic_compare_exchange_n((&x__testexcept_u2), (&y__testexcept_u3), y__testexcept_u3, NIM_TRUE, __ATOMIC_ACQ_REL, __ATOMIC_ACQ_REL);
        nimln_(12);__atomic_store_n(((&v__testexcept_u19)), ((NI) 43), __ATOMIC_ACQ_REL);
        nimln_(13);w__testexcept_u29 = __atomic_load_n(((&v__testexcept_u19)), __ATOMIC_ACQ_REL);
nimTestErrorFlag();
                popFrame();
}
}
```

With this PR the program from #25445 yields the correct output `Error:
unhandled exception: index 4 not in 0 .. 3 [IndexDefect]` instead of
crashing with a SIGSEGV.

PS: Unfortunately, I did not find out how to run the tests with MSVC.
`./koch tests --cc:vcc` doesn't use MSVC.

(cherry picked from commit 49961a54dd)
2026-03-02 10:55:41 +01:00
Kevin Hovsäter
df41cb4b25 Fix casing of types in example (#25556)
From the Standard Library Style Guide:

> Type identifiers should be in PascalCase. All other identifiers should
> be in camelCase with the exception of constants which may use
> PascalCase but are not required to.

(cherry picked from commit 358d9b4497)
2026-03-02 10:55:29 +01:00
ringabout
ca9031f7f5 fixes #21281; proc f(x: static[auto]) doesn't treat x as static (#25543)
fixes #21281

(cherry picked from commit 74499e4561)
2026-02-26 18:18:23 +01:00
narimiran
4c15179df7 Revert "fixes #21281; proc f(x: static[auto]) doesn't treat x as static (#25543)"
This reverts commit dc1064da23.
2026-02-26 18:10:29 +01:00
ringabout
a9111b03e5 allows implicitRangeConvs for literals (#25542)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
(cherry picked from commit a3157537e1)
2026-02-26 17:35:22 +01:00
ringabout
dc1064da23 fixes #21281; proc f(x: static[auto]) doesn't treat x as static (#25543)
fixes #21281

(cherry picked from commit 74499e4561)
2026-02-26 17:35:13 +01:00
ringabout
8ccba2dc86 fixes #25509; removes void fields from a named tuple type (#25515)
fixes #25509

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
(cherry picked from commit b51be75613)
2026-02-26 17:34:42 +01:00
ringabout
bc1b7060b5 enable --warning:ImplicitRangeConversion (#25477)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
(cherry picked from commit 1451651fd9)
2026-02-26 17:32:51 +01:00
ringabout
b3ecf7dbef fixes #25338; Switch default mangling back to cpp (#25343)
fixes #25338

(cherry picked from commit ed8e5a7754)
2026-02-26 17:27:01 +01:00
Miroslav Shubernetskiy
45f1b92b72 fix: double check inputIndex in base64.decode (#25531)
fixes https://github.com/nim-lang/Nim/issues/25530

this double checks the index to make sure whitespace related index
increments cannot cause index defect error

(cherry picked from commit 86b9245dd6)
2026-02-26 17:25:46 +01:00
ringabout
5ec6391124 fixes #25005; new doesn't work with ref object (#25532)
fixes #25005

In `semTypeIdent`, when resolving a typedesc parameter inside a generic
instantiation, the code took a shortcut: it returned the symbol of the
element type (`bound = result.typ.elementType.sym`). However, for
generic types like `RpcResponse[T] = ref object`, the instantiated
object type (e.g., `RpcResponse:ObjectType[string]`) is a copy with a
new type ID but still points to the same symbol as the uninstantiated
generic body type. That symbol's .typ refers to the original
uninstantiated type, which still contains unresolved generic params `T`

(cherry picked from commit e58acc2e1e)
2026-02-26 17:25:33 +01:00
narimiran
40a3a6b8e7 bump NimVersion to 2.2.9 2026-02-26 17:24:58 +01:00
Miran
4f500679b1 bump Atlas' version (#25539)
(cherry picked from commit df42ebc5e6)
v2.2.8
2026-02-22 23:07:19 +01:00
narimiran
93cb5889f4 bump NimVersion to 2.2.8 2026-02-22 18:53:38 +01:00
Miran
2b075fc87d update the shipped tools (#25535)
(cherry picked from commit 44eafa7552)
2026-02-22 18:53:17 +01:00
ringabout
8f7fd28692 replace benign with gcsafe (#25527)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
(cherry picked from commit 15c6249f2c)
2026-02-21 12:58:25 +01:00
ringabout
74b30de1c6 improve alignment for refc (#25525)
(cherry picked from commit 1e3caf457b)
2026-02-20 09:02:47 +01:00
Zoom
6947d96b5f Docs: parseopt fixes, runnable examples (#25526)
Follow-up to #25506.
As I mentioned there, I was in the middle of an edit, so here it is.
Splitting to a separate doc skipped.

A couple of minor mistakes fixed, some things made a bit more concise
and short.

(cherry picked from commit 72e9bfe0a4)
2026-02-20 09:02:37 +01:00
Zoom
689111936c Feat: std: parseopt parser modes (#25506)
Adds configurable parser modes to std/parseopt module. **Take two.**

Initially solved the issue of not being able to pass arguments to short
options as you do with most everyday CLI programs, but reading the tests
made me add more features so that some of the behaviour could be changed
and here we are.

**`std/parseopt` now supports three parser modes** via an optional
`mode` parameter in `initOptParser` and `getopt`.

Three modes are provided:
- `NimMode` (default, fully backward compatible),
- `LaxMode` (POSIX-inspired with relaxed short option handling),
- `GnuMode` (stricter GNU-style conventions).

The new modes are marked as experimental in the documentation.

The parser behaviour is controlled by a new `ParserRules` enum, which
provides granular feature flags that modes are built from. This makes it
possible for users with specific requirements to define custom rule sets
by importing private symbols, this is mentioned but clearly marked as
unsupported.

**Backward compatibility:**

The default mode preserves existing behaviour completely, with a single
exception: `allowWhitespaceAfterColon` is deprecated.

Now, `allowWhitespaceAfterColon` doesn't make much sense as a single
tuning knob. The `ParserRule.prSepAllowDelimAfter` controls this now.
As `allowWhitespaceAfterColon` had a default, most calls never mention
it so they will silently migrate to the new `initOptParser` overload. To
cover cases when the proc param was used at call-site, I added an
overload, which modifies the default parser mode to reflect the required
`allowWhitespaceAfterColon` value. Should be all smooth for most users,
except the deprecation warning.

The only thing I think can be classified as the breaking change is a
surprising **bug** of the old parser:

```nim
let p = initOptParser("-n 10 -m20 -k= 30 -40",  shortNoVal =  {'v'})
#                                     ^-disappears
```

This is with the aforementioned `allowWhitespaceAfterColon` being true
by default, of course. In this case the `30` token is skipped
completely. I don't think that's right, so it's fixed.

Things I still don't like about how the old parser and the new default
mode behave:

1. **Parser behaviour is controlled by an emptiness of two containers**.
This is an interesting approach. It's also made more interesting because
the `shortNoVal`/`longNoVal` control both the namesakes, but *and also
how their opposites (value-taking opts) work*.
---

**Edit:**

2. `shortNoVal` is not mandatory:
    ```nim
	let p = initOptParser(@["-a=foo"], shortNoVal = {'a'})
	# Nim, Lax parses as: (cmdShortOption, "a", "foo")
	# GnuMode  parses as: (cmdShortOption, "a", "=foo")
	```
In this case, even though the user specified `a` as no no-val, parser
ignores it, relying only on the syntax to decide the kind of the
argument. This is especially problematic with the modes that don't use
the rule `prShortAllowSep` (GnuMode), in this case the provided input is
twice invalid, regardless of the `shortNoVal`.

With the current parser architecture, parsing it this way **is
inevitable**, though. We don't have any way to signal the error state
detected with the input, so the user is expected to validate the input
for mistakes.
Bundling positional arguments is nonsensical and short option can't use
the separator character, so `[cmd "a", arg "=foo"]` and `[cmd "a", cmd
"=", cmd "f"...]` are both out of the question **and** would complicate
validating, requiring keeping track of a previous argument. Hope I'm
clear enough on the issue.

**Future work:**

1. Looks like the new modes are already usable, but from the discussions
elsewhere it looks like we might want to support special-casing
multi-digit short options (`-XX..`) to allow numerical options greater
than 9. This complicates bundling, though, so requires a bit of thinking
through.

2. Signaling error state?

---------

Co-authored-by: Andreas Rumpf <araq4k@proton.me>
(cherry picked from commit 7c873ca615)
2026-02-20 09:02:26 +01:00
ringabout
34a594abec fixes #25475; incompatible types errors for array types with different index types (#25505)
fixes #25475

```nim
var x: array[0..1, int] = [0, 1]
var y: array[4'u..5'u, int] = [0, 3]

echo x == y
```

sigmatch treats array compatibility by element type + length, not by the
index (range) type. Perhaps backend should do the same check

(cherry picked from commit 97fed258ed)
2026-02-16 09:14:03 +01:00
Yuriy Glukhov
f10dda264c Importc codegen fix (#25511)
This fixes two issues with impotc'ed types.
1. Passing an importc'ed inherited object to where superclass is
expected emitted `v.Sup` previously. Now it emits `v`, similar to cpp
codegen.
2. Casting between different nim types that resolve to the same C type
previously was done like `*(T*)&v`, now it is just `v`.

(cherry picked from commit 937e647f4f)
2026-02-16 09:13:45 +01:00
Andreas Rumpf
c5455c1515 attempt to fix final issue with Nim's multi-threaded allocator (#25513)
(cherry picked from commit b41049988f)
2026-02-16 09:03:24 +01:00
ringabout
334f2d6a87 fixes #25457; make rawAlloc support alignment (#25476)
fixes https://github.com/nim-lang/Nim/issues/25457

Small chunks allocate memory in fixed-size cells. Each cell is
positioned at exact multiples of the cell size from the chunk's data
start, which makes it much harder to support alignment

```nim
sysAssert c.size == size, "rawAlloc 6"
if c.freeList == nil:
  sysAssert(c.acc.int + smallChunkOverhead() + size <= SmallChunkSize,
            "rawAlloc 7")
  result = cast[pointer](cast[int](addr(c.data)) +% c.acc.int)
  inc(c.acc, size)
```

See also https://github.com/nim-lang/Nim/pull/12926

While using big trunk, each allocation gets its own chunk

(cherry picked from commit 94008531c1)
2026-02-13 09:39:30 +01:00
ringabout
29125f0bc7 fixes #25494; [regression] Crash on enum ranges as default parameters in generic procs (#25496)
fixes #25494;

(cherry picked from commit ae5f864bff)
2026-02-10 17:22:52 +01:00
ringabout
becb06dd70 fixes #25488; Strings can be compared against nil (#25489)
fixes #25488
ref https://github.com/nim-lang/Nim/pull/20222

(cherry picked from commit 513c9aa69a)
2026-02-10 17:22:25 +01:00
Yuriy Glukhov
2ddeabceaf Fixes #25340 (#25389)
(cherry picked from commit 296b2789b5)
2026-02-10 17:20:32 +01:00
ringabout
9fa69d222f fixes #25482; ICE leaking temporary 3 slotTempInt (#25483)
fixes #25482

(cherry picked from commit a04f720217)
2026-02-10 17:20:21 +01:00
ringabout
d48fc130c1 fixes #24706; Warn on implicit range downsizing (#25451)
fixes #24706

(cherry picked from commit bfc2786718)
2026-02-02 07:59:57 +01:00
Tomohiro
c86e7a0ca9 fixes #25459; hashType returns different hash from instantiated generics with distinct types (#25471)
`hashType` proc returned the same hash value from different instanced
generics types like `D[int64]` and `D[F]`.
That caused the struct type with wrong field types.

object/tuple type size check code is generated when it is compiled with
`-d:checkAbi` option.

(cherry picked from commit 88e7adfcb7)
2026-02-02 07:57:46 +01:00
Tomohiro
8d553c5624 fixes #25231; print better error messages when generics instantiation… (#25460)
… has no params

(cherry picked from commit abf434a336)
2026-01-28 09:41:30 +01:00
Gianmarco
d9ed8f2717 Make it so that every feature can be used in panicoverride files (#25300)
Refer to #25298

(cherry picked from commit e7809364b3)
2026-01-26 09:13:58 +01:00
ringabout
35429f6252 fixes #25441; fixes #7355; deletes void args from the argument list (#25455)
fixes #25441; fixes #7355

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
(cherry picked from commit 81610095e6)
2026-01-26 09:13:27 +01:00
ringabout
7277d95b3c fixes #19831; add --styleCheck:warning (#25456)
fixes #19831

(cherry picked from commit f44700e638)
2026-01-26 09:13:17 +01:00
ringabout
ba8ff51f2d fixes #25074; Long integer literal truncated without warning (#25449)
fixes #25074

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
(cherry picked from commit ace09b3cab)
2026-01-26 09:13:06 +01:00