Commit Graph

8171 Commits

Author SHA1 Message Date
ringabout
78c5846225 split PackedModule into reader and writer 2024-06-19 22:10:00 +08:00
ringabout
646bd99d46 [backport] fixes #23711; C code contains backtick`gensym (#23716)
fixes #23711
2024-06-19 08:33:38 +02:00
ringabout
9d08d26e33 adds a define nimHasJsNoLambdaLifting so we can use it in the config for compatibility (#23736) 2024-06-19 08:26:15 +08:00
Andreas Rumpf
3f1de49e26 IC: use tables instead of huge seqs because the compiler can create l… (#23737)
…ots of dummy syms and types
2024-06-18 22:41:22 +02:00
metagn
128090c593 ignore uninstantiated static on match to base type [backport:2.0] (#23731)
fixes #23730

Since #23188 the compiler errors when matching a type variable to an
uninstantiated static value. However sometimes an uninstantiated static
value is given even when only a type match is being performed to the
base type of the static type, in the given issue this case is:

```nim
proc foo[T: SomeInteger](x: T): int = int(x)
proc bar(x: static int): array[foo(x), int] = discard
discard bar(123)
```

To deal with this issue we only error when matching against a type
variable constrained to `static`.

Not sure if the `q.typ.kind == tyGenericParam and
q.typ.genericConstraint == tyStatic` check is necessary, the code above
for deciding whether the variable becomes `skConst` doesn't use it.
2024-06-18 06:54:12 +02:00
ringabout
4867931af3 implement legacy:jsNoLambdaLifting for compatibility (#23727) 2024-06-17 19:06:38 +02:00
ringabout
ae4b47c5bd fixes #20048; fixes #15746; don't sink object fields if it's of openarray type (#23608)
fixes #20048
fixes #15746
2024-06-15 16:07:49 +02:00
ringabout
948bb38335 ref #20653; fixes chronos empty case branches (#23706)
ref #20653

```nim
  Error* = object
    case kind*: ErrorType
    of ErrorA:
      discard
    of ErrorB:
      discard
```
For an object variants without fields, it shouldn't generate empty
brackets for default values since there are no fields at all in case
branches.
2024-06-14 15:55:08 +02:00
Andreas Rumpf
5996b12355 fixes a long standing bug with varargs type inference [backport] (#23720) 2024-06-14 09:16:39 +02:00
ringabout
0b5a938f57 nrvo for embedded importc'ed types (#23708) 2024-06-12 20:51:52 +02:00
ringabout
262ff648aa [backport] fixes #23690; SIGSEGV with object variants and RTTI (#23703)
fixes #23690

```nim
dest.`:state` = src.`:state`
var :tmp_553651276 = dest.e1.a
`=wasMoved`(dest.e1.a)
dest.e1.a.kind = src.e1.a.kind
case dest.e1.a.kind
of 0:
  dest.e1.a.a = src.e1.a.a
of 1:
  `=copy`(dest.e1.a.c, src.e1.a.c)
case :tmp_553651276.kind
of 0:
of 1:
  `=destroy`(:tmp_553651276.c)
```
`dest.e1.a.kind = src.e1.a.kind` changes the discrimant but it fails to
clear the memory of `dest.e1.a`. Before using hooks for copying, we need
to clear the dest, e.g. `=wasMoved(dest.e1.a.c)`.

```nim
dest.`:state` = src.`:state`
var :tmp_553651276 = dest.e1.a
`=wasMoved`(dest.e1.a)
dest.e1.a.kind = src.e1.a.kind
case dest.e1.a.kind
of 0:
  `=wasMoved`(dest.e1.a.a)
  dest.e1.a.a = src.e1.a.a
  `=wasMoved`(dest.e1.a.b)
of 1:
  `=wasMoved`(dest.e1.a.c)
  `=copy`(dest.e1.a.c, src.e1.a.c)
case :tmp_553651276.kind
of 0:
of 1:
  `=destroy`(:tmp_553651276.c)
```
2024-06-11 05:55:08 +02:00
Andreas Rumpf
8cbbe12ee4 fixes #22398; [backport] (#23704) 2024-06-10 18:43:23 +02:00
Andreas Rumpf
56c95758b2 fixes #23445; fixes #23418 [backport] (#23699) 2024-06-09 08:16:05 +02:00
Andreas Rumpf
7039b8b5bc fixes #23354; [backport] (#23685) 2024-06-07 09:01:30 +02:00
ringabout
8f5ae28fab fixes #22672; Destructor not called for result when exception is thrown (#23267)
fixes #22672
2024-06-06 11:51:41 +02:00
ringabout
2d1533f34f fixes #5901 #21211; don't fold cast function types because of gcc 14 (#23683)
follow up https://github.com/nim-lang/Nim/pull/6265

fixes #5901
fixes #21211

It causes many problems with gcc14 if we fold the cast function types.
Let's check what it will break
2024-06-05 20:54:00 +02:00
metagn
42e8472ca6 fix noreturn/implicit discard check logic (#23681)
fixes #10440, fixes #13871, fixes #14665, fixes #19672, fixes #23677

The false positive in #23677 was caused by behavior in
`implicitlyDiscardable` where only the last node of `if`/`case`/`try`
etc expressions were considered, as in the final node of the final
branch (in this case `else`). To fix this we use the same iteration in
`implicitlyDiscardable` that we use in `endsInNoReturn`, with the
difference that for an `if`/`case`/`try` statement to be implicitly
discardable, all of its branches must be implicitly discardable.
`noreturn` calls are also considered implicitly discardable for this
reason, otherwise stuff like `if true: discardableCall() else: error()`
doesn't compile.

However `endsInNoReturn` also had bugs, one where `finally` was
considered in noreturn checking when it shouldn't, another where only
`nkIfStmt` was checked and not `nkIfExpr`, and the node given for the
error message was bad. So `endsInNoReturn` now skips over
`skipForDiscardable` which no longer contains
`nkIfStmt`/`nkCaseStmt`/`nkTryStmt`, stores the first encountered
returning node in a var parameter for the error message, and handles
`finally` and `nkIfExpr`.

Fixing #23677 already broke a line in `syncio` so some package code
might be affected.
2024-06-05 20:53:05 +02:00
qiangxuhui
77c04092e0 Add linux/loongarch64 support in 'compiler/installer.ini' (#23672)
The files(like `build/build.sh`)generated by the command `koch csource`
do not contain complete `linux/loongarch64` support. This patch will fix
it.
2024-06-04 09:50:35 +02:00
ringabout
17475fc5d3 fixes openarray hoist with gcc 14 (#23647)
blocks https://github.com/nim-lang/Nim/pull/23673

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2024-06-04 09:43:12 +02:00
ringabout
4bd1cf2376 rework ctypes with gcc 14 (#23636) 2024-06-02 15:16:44 +02:00
ringabout
a9a32ca3b8 improve view types for jsgen; eliminate unnecessary copies of view types (#23654) 2024-06-02 15:15:31 +02:00
Juan M Gómez
cb0ebecb20 #Fixes #23657 C++ compilation fails with: 'T1_' was not declared in t… (#23666)
…his scope
2024-06-02 15:15:03 +02:00
ringabout
08f1eac8ac fixes#23665; rework spawn with gcc 14 and fixes other tests (#23660)
fixes #23665
2024-06-02 11:54:39 +02:00
ringabout
c615828ccb fixes #22852; fixes #23435; fixes #23645; SIGSEGV when slicing string or seq[T] with index out of range (#23279)
follow up https://github.com/nim-lang/Nim/pull/23013

fixes #22852
fixes #23435
fixes #23645

reports rangeDefect correctly

```nim
/workspaces/Nim/test9.nim(1) test9
/workspaces/Nim/lib/system/indices.nim(116) []
/workspaces/Nim/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: value out of range: -2 notin 0 .. 9223372036854775807 [RangeDefect]
```
2024-05-27 14:13:18 +02:00
Jason Beetham
d837d32fd5 Skip tyAlias inside semTypeTraits in case a concept accidently emits one (#23640) 2024-05-23 20:15:20 +02:00
Andreas Rumpf
6cd03bae29 Minor refactoring (#23637) 2024-05-23 08:53:45 +02:00
ringabout
5cd141cebb fixes reifiedOpenArray; nkHiddenStdConv is PathKinds1 not PathKinds0 (#23633) 2024-05-22 20:38:09 +08:00
ringabout
309f97af4c fixes #23627; Simple destructor code gives invalid C (#23631)
fixes #23627

```nim
type
  TestObj = object of RootObj

  TestTestObj = object of RootObj
    testo: TestObj

proc `=destroy`(x: TestTestObj) =
  echo "Destructor for TestTestObj"

proc testCaseT() =
  echo "\nTest Case T"
  let tt1 {.used.} = TestTestObj(testo: TestObj())
```

When generating const object fields, it's likely that
we need to generate type infos for the object, which may be an object
with
custom hooks. We need to generate potential consts in the hooks first.

https://github.com/nim-lang/Nim/pull/20433 changed the semantics of
initialization. It should evaluate`BracedInit` first.
2024-05-21 14:53:08 +02:00
ringabout
b87732b5f1 fixes #16671; openarray conversion for object construction (#23618)
fixes #16671

related to https://github.com/nim-lang/Nim/pull/18911
2024-05-16 23:27:08 +02:00
PHO
0ba932132e Support NetBSD/aarch64 (#23616)
I could trivially port Nim to NetBSD/aarch64 because it already
supported NetBSD and aarch64. I only needed to generate `c_code` for
this combination.
2024-05-16 23:22:49 +02:00
ringabout
2c8551556e fixes lifting subtype calling parent's hooks (#23612)
ref https://forum.nim-lang.org/t/11587

Tested with `gcc version 14.0.1 20240412` locally
2024-05-15 20:52:18 +02:00
ringabout
0fcd838fd9 fixes openarray views default values in JS (#23607) 2024-05-14 18:07:47 +02:00
metagn
81a937ce1f ignore modules when looking up symbol with expected type (#23597)
fixes #23596

When importing a module and declaring an overloadable symbol with the
same name as the module in the same scope, the module symbol can take
over and make the declared overload impossible to access. Previously
enum overloading had a quirk that bypassed this in a context where a
specific enum type was expected but this was removed in #23588. Now this
is bypassed in every place where a specific type is expected since
module symbols don't have a type and so wouldn't be compatible anyway.

But the issue still exists in places where no type is expected like `let
x = modulename`. I don't see a way of fixing this without nerfing module
symbols to the point where they're not accessible by default, which
might break some macro code.
2024-05-14 11:26:33 +02:00
Juan M Gómez
fcc43fa9c8 Allow to exportc params. (#23396)
By allowing `exportc` in params one can decide the name of a param or to
dont mangle them.
2024-05-10 10:35:23 +02:00
ringabout
42486e1b2f unordered enum for better interoperability with C (#23585)
ref https://forum.nim-lang.org/t/11564
```nim
block: # unordered enum
  block:
    type
      unordered_enum = enum
        a = 1
        b = 0

    doAssert (ord(a), ord(b)) == (1, 0)

  block:
    type
      unordered_enum = enum
        a = 1
        b = 0
        c

    doAssert (ord(a), ord(b), ord(c)) == (1, 0, 2)

  block:
    type
      unordered_enum = enum
        a = 100
        b
        c = 50
        d

    doAssert (ord(a), ord(b), ord(c), ord(d)) == (100, 101, 50, 51)

  block:
    type
      unordered_enum = enum
        a = 7
        b = 6
        c = 5
        d

    doAssert (ord(a), ord(b), ord(c), ord(d)) == (7, 6, 5, 8)
```
2024-05-10 10:32:07 +02:00
metagn
c101490a0c remove bad type inference behavior for enum identifiers (#23588)
refs
https://github.com/nim-lang/Nim/issues/23586#issuecomment-2102113750

In #20091 a bad kind of type inference was mistakenly left in where if
an identifier `abc` had an expected type of an enum type `Enum`, and
`Enum` had a member called `abc`, the identifier would change to be that
enum member. This causes bugs where a local symbol can have the same
name as an enum member but have a different value. I had assumed this
behavior was removed since but it wasn't, and CI seems to pass having it
removed.

A separate PR needs to be made for the 2.0 branch because these lines
were moved around during a refactoring in #23123 which is not in 2.0.
2024-05-10 10:30:57 +02:00
ringabout
2995a0318b fixes #23552; Invalid codegen when trying to mannualy delete distinct seq (#23558)
fixes #23552
2024-05-08 14:54:03 -06:00
metagn
09bd9d0b19 fix semFinishOperands for bracket expressions [backport:2.0] (#23571)
fixes #23568, fixes #23310

In #23091 `semFinishOperands` was changed to not be called for `mArrGet`
and `mArrPut`, presumably in preparation for #23188 (not sure why it was
needed in #23091, maybe they got mixed together), since the compiler
handles these later and needs the first argument to not be completely
"typed" since brackets can serve as explicit generic instantiations in
which case the first argument would have to be an unresolved generic
proc (not accepted by `finishOperand`).

In this PR we just make it so `mArrGet` and `mArrPut` specifically skip
calling `finishOperand` on the first argument. This way the generic
arguments in the explicit instantiation get typed, but not the
unresolved generic proc.
2024-05-08 09:35:26 -06:00
ringabout
e662043fd1 rework wasMoved, move on the JS backend (#23577)
`reset`, `wasMoved` and `move` doesn't support primitive types, which
generate `null` for these types. It is now produce `x = default(...)` in
the backend. Ideally it should be done by ast2ir in the future
2024-05-08 09:11:46 -06:00
ringabout
1ad4e80060 fixes #22409; don't check style for enumFieldSymChoice in the function (#23580)
fixes #22409
2024-05-08 09:10:48 -06:00
Juan M Gómez
3b4078a7f8 Skips generic owner when mangling instances (#23563) 2024-05-07 15:03:53 -06:00
ringabout
185e06c923 fixes #23419; internal error with void in generic array instantiation (#23550)
fixes #23419

`void` is only supported as fields of objects/tuples. It shouldn't allow
void in the array.

I didn't merge it with taField because that flag is also used for
tyLent, which is allowed in the fields of other types.
2024-05-01 09:02:43 +02:00
ringabout
d09c3c0f58 fixes #23321; Error: internal error: openArrayLoc: ref array[0..0, int] (#23548)
fixes #23321

In the function `mapType`, ptrs (tyPtr, tyVar, tyLent, tyRef)
are mapped into ctPtrToArray, the dereference of which is skipped
in the `genref`. We need to skip these ptrs in the function
`genOpenArraySlice`.
2024-04-29 16:58:33 +02:00
ringabout
f682dabf71 fixes #23531; fixes invalid meta type accepted in the object fields (#23532)
fixes #23531
fixes #19546
fixes #6982
2024-04-26 16:05:03 +02:00
ringabout
0b0f185bd1 fixes #23536; Stack trace with wrong line number when the proc called inside for loop (#23540)
fixes #23536
2024-04-26 16:02:02 +02:00
ringabout
407c0cb64a fixes #23522; fixes pre-existing wrong type for iter in liftIterSym (#23538)
fixes #23522
2024-04-26 19:00:25 +08:00
ringabout
4601bb0255 fixes #23525; an 'emit' pragma cannot be pushed (#23537)
fixes #23525
2024-04-24 18:43:29 +02:00
ringabout
a5c1a6f042 adds another fix for concept in JS (#23535)
ref https://github.com/nim-lang/Nim/issues/9550
2024-04-24 17:33:58 +02:00
ringabout
cd3cf3a20e fixes #23524; global variables cannot be analysed when injecting move (#23529)
fixes #23524

```nim
proc isAnalysableFieldAccess*(orig: PNode; owner: PSym): bool =
  ...
  result = n.kind == nkSym and n.sym.owner == owner and
    {sfGlobal, sfThread, sfCursor} * n.sym.flags == {} and
    (n.sym.kind != skParam or isSinkParam(n.sym))
```
In `isAnalysableFieldAccess`, globals, cursors are already rejected
2024-04-24 12:47:05 +02:00
Nikolay Nikolov
7e3bac9235 * fix for the debug line info code generation (#23488)
Previously, in certain cases, the compiler would generate debug info for
the correct line number, but for the wrong .nim source file.
2024-04-22 13:55:14 +02:00