8658 Commits

Author SHA1 Message Date
Tomohiro
c48347136f Refactoring #25302; don't store procedure's parameter types to PType.sonsImpl (#25351) 2025-12-26 21:59:38 +01:00
ringabout
a41bbf6901 fixes #25387; embedsrc breaks with Line Continuation (#25388)
fixes #25387


https://stackoverflow.com/questions/30286253/how-to-escape-backslash-in-comment

- adding a whitespace or `\t` after `\` breaks the `goto` block
- `\* *\` doesn't support nesting, causing problems for using it in the
Nim comments
2025-12-25 21:02:54 +01:00
ringabout
5e53a70e62 fixes #25254; fixes #10395; Invalid pred in when swallowed (#25385)
fixes #25254
fixes #10395
2025-12-25 00:04:18 +01:00
elijahr
b819472e74 Fix sizeof(T) in typedesc templates called from generic type when clauses (#25374)
The `hasValuelessStatics` function in `semtypinst.nim` only checked for
`tyStatic`, missing `tyTypeDesc(tyGenericParam)`. This caused
`sizeof(T)` inside a typedesc template called from a generic type's
`when` clause to error with "'sizeof' requires '.importc' types to be
'.completeStruct'".

The fix adds a check for `tyTypeDesc` wrapping `tyGenericParam`,
recognizing it as an unresolved generic parameter that needs resolution
before evaluation.

Also documents the `completeStruct` pragma in the manual.
2025-12-21 07:37:26 +01:00
Andreas Rumpf
b901a80710 IC: progress (#25368)
Co-authored-by: Jacek Sieka <arnetheduck@gmail.com>
Co-authored-by: Ryan McConnell <rammcconnell@gmail.com>
2025-12-20 11:27:46 +01:00
elijahr
1324183c38 fix #17630: Implement cycle detection for recursive concepts (#25353)
fixes #17630

## Recursive Concept Cycle Detection

- Track (conceptId, typeId) pairs during matching to detect cycles
- Changed marker from IntSet to HashSet[ConceptTypePair]
- Removed unused depthCount field
- Added recursive concepts documentation to manual
- Added tests for recursive concepts, distinct chains, and co-dependent
concepts

## Fix Flaky `tasyncclosestall` Test

The macOS ARM64 CI jobs were failing due to a flaky async socket test
(unrelated to concepts).

The test only accepted `EBADF` as a valid error code when closing a
socket with pending writes. However, depending on timing, the kernel may
report `ECONNRESET` or `EPIPE` instead:

- **EBADF**: Socket was closed locally before kernel detected remote
state
- **ECONNRESET**: Remote peer sent RST packet (detected first)  
- **EPIPE**: Socket is no longer connected (broken pipe)

All three are valid disconnection errors. The fix accepts any of them,
making the test reliable across platforms.

---------

Co-authored-by: Andreas Rumpf <araq4k@proton.me>
2025-12-20 08:56:10 +01:00
ringabout
548b1c6ef8 fixes #25369 (#25370)
fixes #25369
2025-12-18 18:54:03 +01:00
Andreas Rumpf
9bb57a64ba IC: keep package information (#25350) 2025-12-18 09:34:39 +01:00
Andreas Rumpf
80cf9a8ce8 system.nim: memory must be part of system so that its compilerprocs c… (#25365)
…an work for IC
2025-12-18 04:53:09 +01:00
Jacek Sieka
1527c13273 Align treetab hash with equivalence (#25354)
In particular, hash `typ` for `nkType`, `nkNilLit` or they end up
generating collisions

<img width="989" height="612" alt="image"
src="https://github.com/user-attachments/assets/a5c6366f-1214-443e-98d5-52ce95fc3555"
/>
2025-12-15 13:19:56 +01:00
ringabout
6f3245f06a fixes documentation building failures for nightlies (#25345)
```
Error: '`' expected
```
2025-12-11 18:23:04 +01:00
Andreas Rumpf
cbb2fe0a63 IC: progress (#25344)
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
2025-12-11 18:22:38 +01:00
metagn
44d2472b08 consider generic param type as typedesc in tuple type expressions (#25316)
fixes #25312

Tuple expressions `(a, b, c)` can be either types or values depending on
if their elements are typedescs or values, this is checked by checking
if the type of the element is `tyTypeDesc`. However when an
`skGenericParam` symbol is semchecked by `semSym` it is given its own
`tyGenericParam` type rather than a `tyTypeDesc` type, this seems to be
necessary for signatures to allow wildcard generic params passed to
static constrained generic params (tested in #25315). The reason
`semSym` is called is that `semGeneric` for generic invocations calls
`matches` which sems its arguments like normal expressions.

To deal with this, an expression of type `tyGenericParam` and with a
`skGenericParam` sym is allowed as a type in the tuple expression. A
problem is that this might consider a value with a wildcard generic
param type as a type. But this is a very niche problem, and I'm not sure
how to check for this. `skGenericParam` symbols stay as idents when
semchecked so it can't be checked that the node is an `skGenericParam`
symbol. It could be checked that it's an ident but I don't know how
robust this is. And maybe there is another way to refer to a wildcard
generic param type instead of just its symbol, i.e. another kind of
node.

This also makes #5647 finally work but a test case for that can be added
after.
2025-12-09 09:45:37 +01:00
ringabout
ed8e5a7754 fixes #25338; Switch default mangling back to cpp (#25343)
fixes #25338
2025-12-09 07:16:08 +01:00
Andreas Rumpf
fa4d79f519 IC: progress (#25339) 2025-12-07 13:07:44 +01:00
elijahr
099ee1ce4a Fixes #25341; Invalid C code for lifecycle hooks for distinct types based on generics (#25342) 2025-12-07 12:59:42 +01:00
Andreas Rumpf
c3a20fa890 IC: progress (#25332) 2025-12-06 11:45:01 +01:00
Yuriy Glukhov
8f8814b495 Fixes #25330 (#25336)
Fixed state optimizer. It did not replace deleted states in
`excLandingState`.
2025-12-05 15:27:38 +01:00
Ryan McConnell
86bbc73b3a concept patch: inheritance (#25317)
adds some inheritance support

---------

Co-authored-by: Andreas Rumpf <araq4k@proton.me>
2025-12-03 17:51:18 +01:00
ringabout
1da0dc74d9 fixes #22305; Combination of generic destructor and closure fails in certain cases (#25327)
fixes #22305

It seems that the generic type is cached somehow so that no hooks are
instantiated for the generic type. There are only hooks for the
instantiated type. When `lambdalifting` tries to create type bounds for
the generic type, it cannot either find the instantiated hooks or
instantiate the generic hooks since it lacks `SemContext`. It can use
hooks for the instantiated type in this case
2025-12-03 17:29:45 +01:00
Andreas Rumpf
a773178e2b IC: progress (#25314) 2025-12-01 22:59:12 +01:00
Yuriy Glukhov
6656084004 Fixes #25261 (#25310)
Returning or yielding from a closureiter must restore "external"
exception, but `popCurrentException` from `blockLeaveActions` was
getting in the way. So now `blockLeaveActions` doesn't emit
`popCurrentException` for returns in closureiters. I'm not a fan of this
"abstraction leakage", but don't see a better solution yet. Any input is
much appreciated.

---------

Co-authored-by: Andreas Rumpf <araq4k@proton.me>
2025-11-27 10:09:52 +01:00
Andreas Rumpf
0486a2df51 IC progress (#25283)
bugfix: produce the required nimcache subdir
2025-11-25 12:49:23 +01:00
Peter Munch-Ellingsen
6543040d40 Fixes #25304 proper test for hlo recursion limit (#25305)
The `warnUser` message kind is probably not the right one, but I left it
as a placeholder. It should probably at least warn if not just straight
up throw an error, was very hard to figure out what went wrong without
any indication. The hard coded 300 should possibly also be changed to
`evalTemplateLimit` or the VM call recursion limit or something.
2025-11-21 21:26:43 +01:00
Ryan McConnell
79ddb7d89e concept patch for tyGenericInvocation (#25288)
matching between some generic invocations and equivalent instantiations
did not have a code path
2025-11-15 12:52:16 +01:00
lit
39be9b981d fixes #25227; crash when codegen user-defined tuple iterate (#25228)
fixes #25227
2025-11-14 18:43:13 +01:00
ringabout
9becd1453d 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
2025-11-14 16:20:42 +01:00
Andreas Rumpf
f608e109c9 massive refactoring for IC (#25282)
TODO:

- [ ] test writing of .nif files
- [x] implement loading of fields in PType/PSym that might not have been
loaded
- [ ] implement interface logic
- [ ] implement pragma "replays"
- [ ] implement special logic for `converter`
- [ ] implement special logic for `method`
- [ ] test the logic holds up for `export`
- [ ] implement logic to free the memory of PSym/PType if memory
pressure is high
- [ ] implement logic to close memory mapped files if too many are open.

---------

Co-authored-by: demotomohiro <gpuppur@gmail.com>
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
Co-authored-by: Jacek Sieka <arnetheduck@gmail.com>
2025-11-13 21:31:24 +01:00
Andreas Rumpf
5da72efbde VM: refactoring [backport] (#25280)
Note to @narimiran backport because IC requires it.
2025-11-12 19:04:21 +01:00
Ryan McConnell
cc4c7377b2 silence mass dump of BareExcept when using unittest (#25260)
Seems better to change it to `CatchableError` instead?
2025-11-10 07:27:50 +01:00
ringabout
92468e99f7 fixes #25265; fixes #23453; Unable to build Nim 2.2.6 tools from source (#25269)
fixes #25265;
fixes #23453

`(addr deref (ptr object))` generated weak typedesc before, which causes
problems for old GCC versions. As a bonus, by generating a typedesc for
`deref (ptr object)`, it also fixes #23453
2025-11-08 06:04:05 +01:00
metagn
839cbeb371 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.
2025-11-07 20:19:50 +08:00
Andreas Rumpf
809662a228 VM: optimize 'return' slots; saves millions of node allocations for N… (#25266)
…imbus
2025-11-07 11:05:13 +01:00
ringabout
6f73094263 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
2025-11-07 10:06:05 +01:00
ringabout
1d08c4e241 fixes #25263; provides a new switch mangle:nim/cpp for debug name mangling (#25264)
fixes #25263

- [x] documentation and changelogs
2025-11-06 16:41:13 +01:00
Andreas Rumpf
cfefd1d95b produces vastly better error messages for implicit --import and --inc… (#25258)
…lude configuration options
2025-11-04 17:54:23 +01:00
ringabout
d54b5f3ae1 fixes #25252; Unexpected ambiguous call with fields over object with default fields (#25256)
fixes #25252
2025-11-04 20:08:07 +08:00
Yuriy Glukhov
99a222d63d Respect noinit for generic types (#25250) 2025-10-31 16:59:24 +01:00
Yuriy Glukhov
7af4e3eefd Fixes #25202 (#25244) 2025-10-28 12:48:22 +01:00
ringabout
130eac2f93 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.
2025-10-28 11:47:20 +01:00
ringabout
b8ce11dd9d fixes #25027; nim doc uses doc comment from private field for public field (#25239)
fixes #25027
2025-10-23 19:19:27 +02:00
Ryan McConnell
544c26c0b8 add srcDir variable to nim.cfg (#24919)
There might be a way to do this but I couldn't find anything about it.
This is a very simple thing that goes a long way in certain situations.
Trying to avoid needing to switch to nimscript just to get:
```nim
# config.nims
import os
let srcDir = currentSourcePath.parentDir()
switch("define", &"ProjPath:\"{srcDir}\"")
```
with this change just needs:
```
# nim.cfg
d %= "ProjPath=$srcDir"
```
2025-10-23 09:05:05 +02:00
ringabout
c449c72498 fixes #25236; broken assignment hooks of union inside variant object in orc (#25238)
fixes #25236
2025-10-21 16:59:22 +02:00
ringabout
1eae14a3be 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
2025-10-17 17:32:28 +02:00
ringabout
5abd21dfa5 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.
2025-10-16 18:22:46 +02:00
ringabout
f009ea6c3e 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`
2025-10-16 18:22:06 +02:00
lit
8f3bdb6951 fixes #25222; cast[char](i) not trunc on JS (#25223)
fixes #25222
2025-10-16 18:21:37 +02:00
ringabout
31d64b57d5 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
2025-10-15 12:11:15 +02:00
ringabout
f191ba8ddd 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
2025-10-15 07:24:48 +02:00
ringabout
fb4a82f5cc 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
2025-10-15 07:21:08 +02:00