Commit Graph

7986 Commits

Author SHA1 Message Date
ringabout
7ea5aaaebb fixes #23001; give a better warning for PtrToCstringConv (#23005)
fixes #23001
2023-11-30 14:12:12 +01:00
ringabout
b5f5b74fc8 enable vtable implementation for C++ and make it an experimental feature (#23004)
follow up https://github.com/nim-lang/Nim/pull/22991

- [x] turning it into an experimental feature

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2023-11-30 14:05:45 +01:00
SirOlaf
9140f8e221 Fix endsInNoReturn for case statements (#23009)
While looking at the CI I noticed that there's a couple false positives
for `case` statements that cannot be checked for exhaustiveness since my
changes, this should resolve them.

---------

Co-authored-by: SirOlaf <>
2023-11-30 11:01:42 +01:00
ringabout
96513b2506 fixes #22926; Different type inferred when setting a default value for an array field (#22999)
fixes #22926
2023-11-29 10:36:20 +01:00
ringabout
795aad4f2a fixes #22996; typeAllowedCheck for default fields (#22998)
fixes #22996
2023-11-29 10:35:50 +01:00
ringabout
30cf33f04d rework the vtable implementation embedding the vtable array directly with new strictions on methods (#22991)
**TODO**
- [x] fixes changelog
With the new option `nimPreviewVtables`, `methods` are confined in the
same module where the type of the first parameter is defined

- [x] make it opt in after CI checks its feasibility

## In the following-up PRs

- [ ] in the following PRs, refactor code into a more efficient one

- [ ] cpp needs special treatments since it cannot embed array in light
of the preceding limits: ref
https://github.com/nim-lang/Nim/pull/20977#discussion_r1035528927; we
can support cpp backends with vtable implementations later on the
comprise that uses indirect vtable access

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2023-11-28 15:11:43 +01:00
Jake Leahy
8cad6ac048 Don't try and get enum value if its invalid (#22997)
Currently running `nimsuggest`/`check` on this code causes the compiler
to raise an exception

```nim
type
  Test = enum
    A = 9.0 
```

```
assertions.nim(34)       raiseAssert
Error: unhandled exception: int128.nim(69, 11) `arg.sdata(3) == 0` out of range [AssertionDefect]
```

Issue was the compiler still trying to get the ordinal value even if it
wasn't an ordinal
2023-11-28 09:38:10 +01:00
Jake Leahy
c31bbb07fb Register declaration of enum field has a use (#22990)
Currently when using `use` with nimsuggest on an enum field, it doesn't
return the definition of the field.

Breaks renaming in IDEs since it will replace all the usages, but not
the declaration
2023-11-27 22:08:05 +01:00
ringabout
379299a5ac fixes #22286; enforce Non-var T destructors by nimPreviewNonVarDestructor (#22975)
fixes #22286
ref https://forum.nim-lang.org/t/10642

For backwards compatibilities, we might need to keep the changes under a
preview compiler flag. Let's see how many packags it break.

**TODO** in the following PRs

- [ ] Turn the `var T` destructors warning into an error with
`nimPreviewNonVarDestructor`

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2023-11-25 18:27:27 +01:00
Nikolay Nikolov
502a4486ae nimsuggest: Added optional command line option '--clientProcessId:XXX' (#22969)
When it is specified, the nimsuggest instance monitors whether this
process is still alive. In case it's found to be dead, nimsuggest shuts
itself down. Currently only implemented on POSIX and Windows platforms.
The switch is silently ignored on other platforms. Note that the Nim
language server should still try to shut down its child nimsuggest
processes. This switch just adds extra protection against crashing Nim
language server and gets rid of the remaining nimsuggest processes,
which consume memory and system resources.
2023-11-24 19:55:53 +01:00
Andreas Rumpf
ce1a5cb165 progress: 'm' command line switch (#22976) 2023-11-22 09:58:17 +01:00
Pylgos
eba87c7e97 fixes #22971; inferGenericTypes does not work with method call syntax (#22972)
fixes #22971
2023-11-22 07:50:38 +01:00
Jake Leahy
81c0513644 Don't provide suggestions for enum fields (#22959)
Currently the suggestions create a lot of noise when creating enum
fields. I don't see any way of a macro creating fields (when called
inside an enum) so it should be safe to not show suggestions

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2023-11-20 21:12:54 +01:00
Andreas Rumpf
02be027e9b IC: progress and refactorings (#22961) 2023-11-20 21:12:13 +01:00
ringabout
cecaf9c56b fixes #22939; fixes #16890; push should but doesn't apply to importc … (#22944)
…var/let symbols


fixes #22939
fixes #16890

Besides, it was applied to let/const/var with pragmas, now it is
universally applied.

```nim
{.push exportc.}
proc foo =
  let bar = 12
  echo bar
{.pop.}
```

For example, the `bar` variable will be affected by `exportc`.
2023-11-19 17:53:25 +01:00
ringabout
5dafcf4957 fixes #22913; fixes #12985 differently push-ing pragma exportc genera… (#22941)
…tes invalid C identifiers

fixes #22913
fixes #12985 differently


`{.push.} now does not apply to generic instantiations`
2023-11-19 17:52:42 +01:00
ringabout
09ea1b168f fixes #22947; static integers in quote do [backport] (#22948)
fixes #22947
2023-11-18 09:40:28 +01:00
Nikolay Nikolov
3680200df4 nimsuggest: Instead of checking for protocol version 3 exactly, check for version 3 or later. (#22945)
Refactored the way nimsuggest checks for protocol version 3. Instead of
checking for version 3 exactly, it now checks for version 3 or later.
This way, once a version 4 is introduced, it will use version 3 as a
base line, and then extra changes to the protocol can be added on top.
No functional changes are introduced in this commit.
2023-11-15 18:41:58 +01:00
Jake Leahy
60597adb10 Fix using --stdout with jsondoc (#22925)
Fixes the assertion defect that happens when using `jsondoc --stdout`
(There is no outfile since its just stdout)

```
Error: unhandled exception: options.nim(732, 3) `not conf.outFile.isEmpty`  [AssertionDefect]
```

Also makes the output easier to parse by ending each module output with
a new line.
2023-11-09 07:33:57 +01:00
Andreas Rumpf
e081f565cb IC: use better packed line information format (#22917) 2023-11-07 11:25:57 +01:00
Nikolay Nikolov
f5bbdaf906 Inlay hints for types of consts (#22916)
This adds inlay hint support for the types of consts.
2023-11-07 11:25:13 +01:00
ringabout
2e070dfc76 fixes #22673; Cannot prove that result is initialized for a placehold… (#22915)
…er base method returning a lent


fixes #22673
2023-11-06 19:36:26 +01:00
Andreas Rumpf
b79b39128e NIR: C codegen additions (#22914) 2023-11-06 18:33:28 +01:00
Andreas Rumpf
eb8824d71c NIR: C codegen, WIP (#22903) 2023-11-05 20:25:25 +01:00
Solitude
ec37b59a65 Add missing std prefix (#22910)
without it, devels fails to build with `-d:useLinenoise`
2023-11-04 17:46:59 +08:00
Nikolay Nikolov
3f2b9c8bcf Inlay hints support (#22896)
This adds inlay hints support to nimsuggest. It adds a new command to
nimsuggest, called 'inlayHints'.

Currently, it provides type information to 'var' and 'let' variables. In
the future, inlay hints can also be added for 'const' and for function
parameters. The protocol also reserves space for a tooltip field, which
is not used, yet, but support for it can be added in the future, without
further changing the protocol.

The change includes refactoring to allow the 'inlayHints' command to
return a completely different structure, compared to the other
nimsuggest commands. This will allow other future commands to have
custom return types as well. All the previous commands return the same
structure as before, so perfect backwards compatibility is maintained.

To use this feature, an update to the nim language server, as well as
the VS code extension is needed.

Related PRs:
nimlangserver: https://github.com/nim-lang/langserver/pull/53
VS code extension: https://github.com/saem/vscode-nim/pull/134

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2023-11-04 08:51:09 +01:00
ringabout
95e5ad6927 fixes #22902; borrow from proc return type mismatch (#22908)
fixes #22902
2023-11-04 08:50:30 +01:00
ringabout
b68e0aab4c fixes #22866; fixes #19998; ensure destruction for Object construction with custom destructors (#22901)
fixes #22866;
fixes #19998
2023-11-02 11:14:50 +01:00
Andreas Rumpf
801c02bf48 so close... (#22885) 2023-10-31 21:32:09 +01:00
ringabout
afa2f2ebf6 fixes nightlies; fixes incompatible types with csource_v2 (#22889)
ref
https://github.com/nim-lang/nightlies/actions/runs/6686795512/job/18166625042#logs

> /home/runner/work/nightlies/nightlies/nim/compiler/nir/nirvm.nim(163,
35) Error: type mismatch: got 'BiggestInt' for
'b.m.lit.numbers[litId(b.m.types.nodes[int(y)])]' but expected 'int'

Or unifies the type in one way or another
2023-10-30 17:03:19 +01:00
Andreas Rumpf
403e0118ae NIR: progress (#22884) 2023-10-29 21:53:28 +01:00
ringabout
e17237ce9d prepare for the enforcement of std prefix (#22873)
follow up https://github.com/nim-lang/Nim/pull/22851
2023-10-29 14:48:11 +01:00
Andreas Rumpf
0c26d19e22 NIR: VM + refactorings (#22835) 2023-10-29 14:47:22 +01:00
Juan M Gómez
ca577dbab1 C++: ptr fields now pulls the whole type if it's a member in nkDotExpr (#22855) 2023-10-23 08:59:14 +02:00
SirOlaf
c13c48500b Fix #22826: Don't skip generic instances in type comparison (#22828)
Close #22826

I am not sure why this code skips generic insts, so letting CI tell me.
Update: It has told me nothing. Maybe someone knows during review.

Issue itself seems to be that the generic instance is skipped thus it
ends up being just `float` which makes it use the wrong generic instance
of the proc because it matches the one in cache

---------

Co-authored-by: SirOlaf <>
2023-10-21 22:00:16 +02:00
Andreas Rumpf
3c48af7ebe NIR: temporary ID generation bugfix (#22830) 2023-10-16 15:47:13 +02:00
Andreas Rumpf
10c3ab6269 NIR: store sizes, alignments and offsets in the type graph; beginning… (#22822)
…s of a patent-pending new VM
2023-10-16 00:01:33 +02:00
ringabout
f5d70e7fa7 fixes #19250; fixes #22259; ORC AssertionDefect not containsManagedMemory(n.typ) (#22823)
fixes #19250
fixes #22259

The strings, seqs, refs types all have this flag, why should closures be
treated differently?

follow up https://github.com/nim-lang/Nim/pull/14336
2023-10-13 21:34:13 +02:00
ringabout
61145b1d4b fixes #22354; Wrong C++ codegen for default parameter values in ORC (#22819)
fixes #22354

It skips `nkHiddenAddr`. No need to hoist `var parameters` without side
effects. Besides, it saves lots of temporary variables in ORC.
2023-10-13 10:58:43 +02:00
Andreas Rumpf
8990626ca9 NIR: progress (#22817)
Done:

- [x] Implement conversions to openArray/varargs.
- [x] Implement index/range checking.
2023-10-12 23:33:38 +02:00
SirOlaf
68ba45cc04 Import std/stackframes in ast2ir.nim (#22815)
Ref https://github.com/nim-lang/Nim/pull/22777#issuecomment-1758090410

Co-authored-by: SirOlaf <>
2023-10-11 21:05:51 +02:00
Andreas Rumpf
816589b667 NIR: Nim intermediate representation (#22777)
Theoretical Benefits / Plans: 

- Typed assembler-like language.
- Allows for a CPS transformation.
- Can replace the existing C backend by a new C backend.
- Can replace the VM.
- Can do more effective "not nil" checking and static array bounds
checking.
- Can be used instead of the DFA.
- Easily translatable to LLVM.
- Reasonably easy to produce native code from.
- Tiny memory consumption. No pointers, no cry.

**In very early stages of development.**

Todo:
- [x] Map Nim types to IR types.
- [ ] Map Nim AST to IR instructions:
  - [x] Map bitsets to bitops.
  - [ ] Implement string cases.
  - [ ] Implement range and index checks.
  - [x] Implement `default(T)` builtin.
  - [x] Implement multi string concat.
- [ ] Write some analysis passes.
- [ ] Write a backend.
- [x] Integrate into the compilation pipeline.
2023-10-11 17:44:14 +02:00
ringabout
ecaccafa6c fixes #22790; use cast suppress AnyEnumConv warnings for enums withou… (#22813)
…t holes

fixes #22790
2023-10-11 17:18:54 +02:00
ringabout
14d25eedfd suppress incorrect var T destructor warnings for newFinalizer in stdlib (#22810)
in `std/nre`
```nim
proc initRegex(pattern: string, flags: int, study = true): Regex =
  new(result, destroyRegex)
```
gives incorrect warnings like

```
C:\Users\blue\Documents\Nim\lib\impure\nre.nim(252, 6) Error: A custom '=destroy' hook which takes a 'var T' parameter is deprecated; it should take a 'T' parameter [Deprecated
```
2023-10-11 13:27:22 +02:00
ringabout
2cf214d6d4 allows cast int to bool/enum in VM (#22809)
Since they are integer types, by mean of allowing cast integer to enums
in VM, we can suppress some enum warnings in the stdlib in the unified
form, namely using a cast expression.
2023-10-11 12:06:42 +02:00
Juan M Gómez
bf72d87f24 adds support for noDecl in constructor (#22811)
Notice the test wouldnt link before
2023-10-11 08:28:00 +02:00
ringabout
81b2ae747e fixes #8893; guard against array access in renderer (#22807)
fixes #8893
2023-10-09 15:36:56 +02:00
Juan M Gómez
8ac466980f marking a field with noInit allows to skip constructor initialiser (#22802)
Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2023-10-08 23:51:44 +02:00
Juan M Gómez
c3774c8821 fixes nimsuggest false error on lifetime tracking hook fixes #22794 (#22805)
fixes #22794

Not sure if it's too much.
2023-10-08 20:22:35 +02:00
ringabout
efa64aa49b fixes #22787; marks var section in the loop as reassign preventing cursor (#22800)
fixes #22787

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2023-10-07 07:43:39 +02:00