Commit Graph

6793 Commits

Author SHA1 Message Date
ringabout
8914baae78 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`.

(cherry picked from commit 611b8bbf67)
2025-11-21 13:28:13 +01:00
ringabout
fcf4f10c70 fixes #19728; setLen slow when shrinking seq due to zero-filling of released area (#24683)
fixes #19728

don't zero-filling memory for "trivial types" without destructor in
refc. I tested locally with internal apis.

(cherry picked from commit b421d0f8ee)
2025-11-21 08:57:40 +01:00
ringabout
2ddda806a9 fixes #25284; .global initialization inside method hoisted to preInitProc (#25285)
fixes #25284

```nim
proc m2()  =
  let v {.global, used.}: string = f2(f2("123"))
```

transform lifted `.global`statements in the top level scope

(cherry picked from commit 9becd1453d)
2025-11-15 12:27:12 +01:00
ringabout
a6988458c4 updates to macos-15 (#25278)
ref https://github.com/actions/runner-images/issues/13046

(cherry picked from commit d5549a3c65)
2025-11-15 12:26:15 +01:00
lit
9346b138e1 fixes #19846; std/unicode.strip trailing big chars (#25274)
fixes #19846

(cherry picked from commit 2679b3221c)
2025-11-15 12:26:05 +01:00
metagn
605180fcfa js: replace push.apply with for loop for string add [backport] (#25267)
While `a.push.apply(a, b)` is better for performance than the previous
`a = a.concat(b)` due to the fact that it doesn't create a new array,
there is a pretty big problem with it: depending on the JS engine, if
the second array is too long, it can [cause a
crash](https://tanaikech.github.io/2020/04/20/limitation-of-array.prototype.push.apply-under-v8-for-google-apps-script/)
due to the function `push` taking too many arguments. This has
unfortunately been what the codegen produces since 1.4.0 (commit
707367e1ca).

So string addition is now moved to a compilerproc that just uses a `for`
loop. From what I can tell this is the most compatible and the fastest.
Only potential problem compared to `concat` etc is with aliasing, i.e.
adding an array to itself, but I'm guessing it's enough that the length
from before the iteration is used, since it can only grow. The test
checks for aliased nim strings but I don't know if there's an extra
protection for them.

(cherry picked from commit 839cbeb371)
2025-11-08 16:40:36 +01:00
narimiran
be6e195585 remove wrong test 2025-11-08 16:40:00 +01:00
ringabout
4efece995e fixes #25251; SIGBUS with iterator over const Table lookup - premature temporary destruction (#25255)
fixes #25251

enforce a copy if the arg is a deref of a lent pointer since the arg
could be a temporary that will go out of scope

(cherry picked from commit 6f73094263)
2025-11-07 12:33:53 +01:00
ringabout
bd9cbc91ba fixes #25263; provides a new switch mangle:nim/cpp for debug name mangling (#25264)
fixes #25263

- [x] documentation and changelogs

(cherry picked from commit 1d08c4e241)
2025-11-07 12:33:17 +01:00
ringabout
61e98e9bf1 fixes #25252; Unexpected ambiguous call with fields over object with default fields (#25256)
fixes #25252

(cherry picked from commit d54b5f3ae1)
2025-11-05 11:36:17 +01:00
Yuriy Glukhov
365da2cb97 Fixes #25202 (#25244)
(cherry picked from commit 7af4e3eefd)
2025-10-28 13:50:59 +01:00
ringabout
3bbba9e0ff fixes #25008; Compiler internal error with static overload (#25234)
fixes #25008

It seems that `semOverloadedCall` evaluates the same node twice using
`tryConstExpr` in order for `efExplain` to print all the diagnostic
output. The problem is that `tryConstExpr` has side effects, i.e., it
changes the slot index of variables after VM execution.

(cherry picked from commit 130eac2f93)
2025-10-28 13:50:50 +01:00
ringabout
1374741fae fixes #25236; broken assignment hooks of union inside variant object in orc (#25238)
fixes #25236

(cherry picked from commit c449c72498)
2025-10-27 08:50:54 +01:00
ringabout
6cc267ab19 fixes #25226; VM repr raises RangeDefect for long string under refc (#25230)
fixes #25226

`int16` seems to be too small for a reasonable VM program

(cherry picked from commit 1eae14a3be)
2025-10-20 13:44:44 +02:00
ringabout
9af50a9c47 fixes #25123; fixes #11862; Case object from compileTime proc unable to be passed as static param (#25224)
fixes #25123; fixes #11862

follow up https://github.com/nim-lang/Nim/pull/24442
ref https://github.com/nim-lang/Nim/pull/24441

> To fix this, fields from inactive branches are now detected in
semmacrosanity.annotateType (called in fixupTypeAfterEval) and marked to
prevent the codegen of their assignments. In
https://github.com/nim-lang/Nim/pull/24441 these fields were excluded
from the resulting node, but this causes issues when the node is
directly supposed to go back into the VM, for example as const values. I
don't know if this is the only case where this happens, so I wasn't sure
about how to keep that implementation working.

Object variants fields coming from inactive branches from VM are now
flagged `nfPreventCg`. We can ignore them, as done by the C backends.

(cherry picked from commit 5abd21dfa5)
2025-10-17 09:24:11 +02:00
ringabout
33f12c8493 fixes #25208; generates a copy for opcLdConst in the assignments (#25211)
fixes #25208

```nim
type Conf = object
  val: int

const defaultConf = Conf(val: 123)
static:
  var conf: Conf
  conf = defaultConf
```

```nim
# opcLdConst is now always valid. We produce the necessary copy in the
# assignments now:
```

A `opcLdConst` is generated for `defaultConf` in `conf = defaultConf`.
According to the comment above, we need to handle the copy for
assignments of `opcLdConst`

(cherry picked from commit f009ea6c3e)
2025-10-17 09:24:04 +02:00
lit
60a204dfaa fixes #25222; cast[char](i) not trunc on JS (#25223)
fixes #25222

(cherry picked from commit 8f3bdb6951)
2025-10-17 09:23:59 +02:00
ringabout
cf5b89f573 fixes #25046; Infinite loop with anonymous iterator (#25221)
fixes #25046

```nim
proc makeiter(v: string): iterator(): string =
  return iterator(): string =
    yield v

# loops
for c in makeiter("test")():
  echo "loops ", c
```
becomes

```nim
var temp = makeiter("test")
for c in temp():
  echo "loops ", c
```
for closures that might have side effects

(cherry picked from commit 31d64b57d5)
2025-10-17 09:23:51 +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
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
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
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
ringabout
a2e2da2ab2 fixes #25167; fixes deref type (#25195)
fixes #25167

(cherry picked from commit fed0053481)
2025-09-29 08:44:50 +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
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
ringabout
cf5099cdba fixes #25173; SinglyLinkedList.remove broken / AssertionDefect (#25175)
fixes #25173

(cherry picked from commit 51a9ada043)
2025-09-17 09:04:31 +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
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
ringabout
377b6cc6bf disable thttpclient_ssl (#25164)
(cherry picked from commit bf2395a62e)
2025-09-12 14:42:48 +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
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
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
ringabout
e7f03b0604 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_);
```

(cherry picked from commit b527db9ddd)
2025-08-23 07:47:34 +02:00
ringabout
03dd55747c adds more functions to to dirs and files (#25083)
ref https://forum.nim-lang.org/t/13272

(cherry picked from commit e194c7cc87)
2025-08-18 17:27:15 +02:00
Yuriy Glukhov
ca74debfbf 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.

(cherry picked from commit 161b321796)
2025-08-18 17:27:06 +02:00
ringabout
8616161cc4 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

(cherry picked from commit 08d51e5c88)
2025-07-19 08:17:59 +02:00
ringabout
80b80f64f0 fixes #24719; improves order of destruction (#25060)
fixes #24719

(cherry picked from commit 8e57a9f623)
2025-07-19 08:17:38 +02:00
ringabout
27feeea129 fixes CI failures (#25058)
(cherry picked from commit f4ebabb9b3)
2025-07-17 13:42:54 +02:00
lit
f4f13fbcfc fixes #25043: js tyUserTypeClass internal error (#25044)
- **fixes #25043: `internal error: genTypeInfo(tyUserTypeClassInst)`**
- **chore(test): for 25043**

(cherry picked from commit 7e2df41850)
2025-07-17 13:39:41 +02:00
Emre Şafak
1a4a1ab747 Improve error message for keywords as parameters (#25052)
A function with an illegal parameter name like
```nim
proc myproc(type: int) =
  echo type
```
would uninformatively fail like so:
```nim
tkeywordparam.nim(1, 13) Error: expected closing ')'
```

This commit makes it return the following error:
```nim
tkeywordparam.nim(1, 13) Error: 'type' is a keyword and cannot be used as a parameter name
```

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: Emre Şafak <esafak@users.noreply.github.com>
Co-authored-by: Andreas Rumpf <araq4k@proton.me>
(cherry picked from commit 9c1e3bf8fb)
2025-07-17 13:39:26 +02:00
Miran
911a651984 Backport #25016 (#25053)
This is a `version-2-2` variant of the existing fix.

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
2025-07-15 11:06:18 +02:00
narimiran
8f49466c85 Revert "closes #24992; adds a test case (#24993)"
This reverts commit 58d4945c1c.
2025-07-15 09:17:44 +02:00