Commit Graph

268 Commits

Author SHA1 Message Date
metagn
ea9811a4d2 reset inTypeofContext in generic instantiations (#24229)
fixes #24228, refs #22022

As described in
https://github.com/nim-lang/Nim/issues/24228#issuecomment-2392462221,
instantiating generic routines inside `typeof` causes all code inside to
be treated as being in a typeof context, and thus preventing compile
time proc folding, causing issues when code is generated for the
instantiated routine. Now, instantiated generic procs are treated as
never being inside a `typeof` context.

This is probably an arbitrary special case and more issues with the
`typeof` behavior from #22022 are likely. Ideally this behavior would be
removed but it's necessary to accomodate the current [proc `declval` in
the package `stew`](https://github.com/status-im/nim-stew/pull/190), at
least without changes to `compileTime` that would either break other
code (making it not eagerly fold by default) or still require a change
in stew (adding an option to disable the eager folding).

Alternatively we could also make the eager folding opt-in only for
generic compileTime procs so that #22022 breaks nothing whatsoever, but
a universal solution would be better. Edit: Done in #24230 via
experimental switch
2024-10-06 19:36:46 +02:00
metagn
d98ef312f0 don't construct array type for already typed nkBracket node (#24224)
fixes #23010, split from #24195

When resemming bracket nodes, the compiler currently unconditionally
makes a new node with an array type based on the node. However the VM
can generate bracket nodes with `seq` types, which this erases. To fix
this, if a bracket node already has a type, we still resem the bracket
node, but don't construct a new type for it, instead using the type of
the original node.

A version of this was rejected that didn't resem the node at all if it
was typed, but I can't find it. The difference with this one is that the
individual elements are still resemmed.

This should fix the break caused by #24184 so we could redo it after
this PR but it might still have issues, not to mention the related
pre-existing issues like #22793, #12559 etc.
2024-10-03 19:35:53 +02:00
metagn
a1777200c1 fix inTypeofContext leaking after compiles raises exception [backport:2.0] (#24152)
fixes #24150, refs #22022

An exception is raised in the `semExprWithType` call, which means `dec
c.inTypeofContext` is never called, but `compiles` allows compilation to
continue. This means `c.inTypeofContext` is left perpetually nonzero,
which prevents `compileTime` evaluation for the rest of the program.

To fix this, `defer:` is used for the `dec c.inTypeofContext` call, as
is done for
[`instCounter`](d51d88700b/compiler/seminst.nim (L374))
in other parts of the compiler.
2024-09-22 13:51:19 +02:00
metagn
1fbb67ffe9 make distinct conversions addressable in VM (#24124)
fixes #24097

For `nkConv` addresses where the conversion is between 2 types that are
equal between backends, treat assignments the same as assignments to the
argument of the conversion. In the VM this seems to be in `genAsgn` and
`genAsgnPatch`, as evidenced by the special logic for `nkDerefExpr` etc.

This doesn't handle ranges after #24037 because `sameBackendType` is
used and not `sameBackendTypeIgnoreRange`. This is so this is
backportable without #24037 and another PR can be opened that implements
it for ranges and adds tests as well. We can also merge
`sameBackendTypeIgnoreRange` with `sameBackendType` since it doesn't
seem like anything that uses it would be affected (only cycle checks and
the VM), but then we still have to add tests.
2024-09-17 06:29:49 +02:00
metagn
a6595e5b49 open new scope for const values (#24084)
fixes #5395

Previously values of `const` statements used the same scope as the
`const` statement itself, meaning variables could be declared inside
them and referred to in other statements in the same block. Now each
`const` value opens its own scope, so any variable declared in the value
of a constant can only be accessed for that constant.

We could change this to open a new scope for the `const` *section*
rather than each constant, so the variables can be used in other
constants, but I'm not sure if this is sound.
2024-09-09 11:29:30 +02:00
ringabout
9ff0333a4c fixes #21353; fixes default closure in the VM (#24070)
fixes #21353

```nim
  result = newNodeIT(nkTupleConstr, info, t)
  result.add(newNodeIT(nkNilLit, info, t))
  result.add(newNodeIT(nkNilLit, info, t))
```
The old implementation uses `t` which is the type of the closure
function as its type. It is not correct and generates ((nil, nil), (nil,
nil)) for `default(closures)`. This PR creates `(tyPointer, tyPointer)`
for fake closure types just like what cctypes do.
2024-09-09 11:22:37 +02:00
metagn
f69809bb17 proper error for calling nil closure in VM (#24059)
fixes #24057

Instead of crashing the compiler, the VM now gives a stacktrace if a nil
closure is attempted to be called.
2024-09-04 09:13:04 +02:00
autumngray
540b414c86 fixes #23925; VM generates wrong cast for negative enum values (#23951)
Follow up of #23927 which solves the build error.

This is still only a partial fix as it doesn't take into account
unordered enums. I'll make a separate issue for those.

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
2024-08-27 14:03:56 +02:00
ringabout
6336d2681b adds a ubuntu 24.04 matrix with gcc 14 for tests (#23673)
ref https://forum.nim-lang.org/t/11587
2024-08-20 16:07:11 +02:00
metagn
58813a3b2e make all generic aliases tyAlias (#23978)
fixes #23977

The problem is that for *any* body of a generic declaration,
[semstmts](2e4d344b43/compiler/semstmts.nim (L1610-L1611))
sets the sym of its value to the generic type name, and
[semtypes](2e4d344b43/compiler/semtypes.nim (L2143))
just directly gives the referenced type *specifically* when the
expression is a generic body. I'm blaming `semtypes` here because it's
responsible for the type given but the exact opposite behavior
specifically written in makes me think generating an alias type here
maybe breaks something.
2024-08-20 11:41:50 +02:00
metagn
f7c11a8978 allow generic compileTime proc folding (#22022)
fixes #10753, fixes #22021, refs #19365 (was fixed by #22029, but more
faithful test added)

For whatever reason `compileTime` proc calls did not fold if the proc
was generic ([since this folding was
introduced](c25ffbf262 (diff-539da3a63df08fa987f1b0c67d26cdc690753843d110b6bf0805a685eeaffd40))).
I'm guessing the intention was for *unresolved* generic procs to not
fold, which is now the logic.

Non-magic `compileTime` procs also now don't fold at compile time in
`typeof` contexts to avoid possible runtime errors (only the important)
and prevent double/needless evaluation.
2024-08-18 00:52:32 +02:00
ringabout
b9b24e192a fixes #23932; vmopsDanger for os.getCurrentDir errors (#23934)
fixes #23932
ref https://github.com/jmgomez/NimForUE/issues/36
2024-08-11 16:13:26 +02:00
ringabout
9092244f87 closes #22095; adds a test case (#23822)
closes #22095
2024-07-11 15:38:32 +02:00
ringabout
5c5e7a9b6e fixes #22389; fixes #19840; don't fold paths containing addr (#23807)
fixes #22389;
fixes #19840
2024-07-09 12:59:21 +02:00
ringabout
31d7554524 fixes #13481; fixes #22708; disable using union objects in VM (#23362)
fixes #13481;
fixes #22708

Otherwise it gives implicit results or bad codegen
2024-03-03 15:56:06 +01:00
ringabout
3fb46fac32 fixes #12334; keeps nkHiddenStdConv for cstring conversions (#23216)
fixes #12334

`nkHiddenStdConv` shouldn't be removed if the sources aren't literals,
viz. constant symbols.
2024-01-18 21:31:49 +01:00
metagn
3224337550 give typedesc param nodes type T not typedesc[T] [backport:2.0] (#23115)
fixes https://github.com/nim-lang/Nim/issues/23112, fixes a mistake in
https://github.com/nim-lang/Nim/pull/22581

This makes `getType(t)` where `t` is a typedesc param with value `T`
equal to `getType(T)`.
2024-01-18 14:50:36 +01:00
metagn
bd6adbcc9d fix isNil folding for compile time closures (#22574)
fixes #20543
2023-09-02 10:32:46 +02:00
metagn
2542dc09c8 use dummy dest for void branches to fix noreturn in VM (#22617)
fixes #22216
2023-09-01 15:38:25 +02:00
metagn
53d9fb259f don't update const symbol on const section re-sems (#22609)
fixes #19849
2023-09-01 08:59:48 +02:00
metagn
2e4e2f8f50 handle typedesc params in VM (#22581)
* handle typedesc params in VM

fixes #15760

* add test

* fix getType(typedesc) test
2023-08-30 07:23:14 +02:00
metagn
b6cea7b599 clearer error for different size int/float cast in VM (#22582)
refs #16547
2023-08-29 14:59:49 +02:00
metagn
942f846f04 fix getNullValue for cstring in VM, make other VM code aware of nil cstring (#22527)
* fix getNullValue for cstring in VM

fixes #22524

* very ugly fixes, but fix #15730

* nil cstring len works, more test lines

* fix high
2023-08-21 20:08:00 +02:00
Tomohiro
eb83d20d0d Add staticFileExists and staticDirExists (#22278) 2023-08-18 16:47:47 +02:00
Bung
3bb75f2dea close #18103 internal error: inconsistent environment type (#22451) 2023-08-11 18:50:31 +08:00
ringabout
31ba1046fc add a test case for #22190 in case of regression (#22217) 2023-07-04 10:58:14 +08:00
ringabout
41ec894cb0 alternative to #22183; nimscript shares the same compileTime sym with VM (#22184) 2023-06-29 11:21:22 +02:00
metagn
f718f295df fix VM uint conversion size bug, stricter int gen on JS (#22150)
* fix VM uint conversion bug, stricter int gen on JS

fixes #19929

* fix float -> uint64 conversion too

* no need to mask to source type

* simpler diff with explanation, add test for described issue
2023-06-25 00:01:08 +02:00
ringabout
a8d0dda833 allow addressing elements of openArray[char] in VM (#22045)
allow addressing elements of openArray[char]
2023-06-08 14:08:49 +02:00
metagn
b97d603cd0 some test cleanups & category reorganization (#22010)
* clean up some test categories

* mention exact slice issue

* magics into system

* move trangechecks into overflow

* move tmemory to system

* try fix CI

* try fix CI

* final CI fix
2023-06-06 06:54:07 +02:00
ringabout
eecf12c4b5 fixes #21708; skip colons for tuples in VM (#21850)
* fixes #21708; skip colon for tuples in VM

* skip nimnodes

* fixes types
2023-05-17 00:20:40 +02:00
ringabout
0ece98620f closes #7590; add a test case (#21846) 2023-05-14 13:59:41 +08:00
metagn
02be212dae clean up SOME pending/xxx/issue link comments (#21826)
* clean up SOME pending/xxx/issue link comments

* great
2023-05-11 10:23:52 +02:00
ringabout
53c15f24e9 fixes #21704; remove nfIsRef for genLit in VM (#21765)
* fixes #21704; remove `nfIsRef` for genLit

* remove nfIsRef from the output of macros

* make the logic better

* try again

* act together

* excl nfIsRef
2023-05-06 18:04:08 +02:00
ringabout
a154950570 closes #10108; add a test case (#21770) 2023-05-03 06:42:32 +02:00
ringabout
afc30ca879 fixes #19863; move sha1, md5 to nimble packages for 2.0 (#21702)
* move sha1, md5 to nimble packages

* boot the compiler

* fixes tests

* build the documentation

* fixes docs

* lol, I forgot koch.nim

* add `nimHasChecksums` define

* clone checksums but maybe copying is better

* bump nimble hash

* use ChecksumsStableCommit

* fixes tests

* deprecate them

* fixes paths

* fixes koch
2023-05-02 10:49:17 +02:00
ringabout
4fa86422c0 stdlib tests now check refc too (#21664)
* stdlib tests now check refc too

* typo

* fixes line numbers

* disable cpp

* do not touch
2023-04-21 15:37:58 +02:00
ringabout
b2c1dcbbc9 fixes explicit globals in macros (#21502) 2023-03-12 20:03:46 +01:00
ringabout
1b1412f3d1 fixes #10938; fixes #13312; fixes #13918; fixes #20985; always initializes global variables with null values in VM (#21351)
* fixes #10938; always initialize global variable in VM

* fixes importc vars

* there is a pre-existing issue regarding closure types in the VM

* add tests
2023-03-01 17:18:09 +01:00
ringabout
d4782c9e42 closes #17864; add a test case (#21434) 2023-02-25 00:53:04 +08:00
ringabout
b5f64f55d0 fixes #16790; fixes #19075; put big arrays on the constant seqs; don't inline them in the VM; big performance boost (#21318)
* don't inline arrays in VM

* add a test for #19075
2023-01-31 19:22:10 +01:00
ringabout
07be1791ba fix #21045; getTime with vmopsDanger is broken; alternative to #21054 (#21056)
* fix #21045 getTime with vmopsDanger is broken; alternative to #21054

* typo
2022-12-10 18:57:19 +01:00
ringabout
600b3a91ab fixes regression #20746; remove string copies for ORC booted compiler (#20776)
* fixes #20746; remove string copies for ORC booted compiler

* add a test case

* use `cursor` thanks to @beef331

* for old compilers

* change file extension

* change test cases
2022-11-07 14:36:43 +01:00
ringabout
a228e331f3 fixes regression #17121; adding doc comment in importc proc makes it silently noop at CT (#20766)
* fixes regression #17121; adding doc comment in importc proc makes it silently noop at CT

* Update compiler/vmgen.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2022-11-06 22:25:55 +08:00
ringabout
4b377b07fc fixes #19201; fixes sink causes crash in VM (#20658) 2022-10-25 20:08:36 +02:00
Jason Beetham
da0a2fdca2 Unpack mSlice tupleconstr for static openarrays (#20615) 2022-10-22 06:37:23 +02:00
ringabout
1db25ffcd3 closes #19969; add testcase for #19969 #15952 #16306 (#20610)
closes #19969; add testcase
2022-10-21 13:38:40 +08:00
Jason Beetham
4aa67ad7fd Implemented mSlice on the VM allowing toOpenArray to work at compile time. (#20586)
* Implemented opcSlice to make 'toOpenArray' work on the VM

* Added nkOpenArray for VM to reduce bodgeness

* Fixed range issues and erraneous comments

* Range check correctly for openArrays in opcLdArr

* Inverted logic for ldArr checking

* vm now supports slicing strings

* Added string tests

* Removed usage of 'nkOpenArray' and redundant operations

* Refactored vmSlice implementation, removing redundant and incorrect code

* Made tuples go throw opcWrObj for field assignment

* All strkinds should be considered for openarrays
2022-10-20 23:59:57 +02:00
ringabout
5602183234 'lock levels' are deprecated, now a noop (#20539)
* 'lock levels' are deprecated, now a noop

* fixes tests
2022-10-11 09:17:09 +02:00
ringabout
a132f5502a closes #12994; add testcase (#20511) 2022-10-08 00:27:17 +08:00