Commit Graph

8470 Commits

Author SHA1 Message Date
metagn
0861dabfa7 add ambiguous identifier message to generic instantiations (#24646)
fixes #24644

Another option is to include the symbol names and owners in the type
listing as in #24645 but this is a bit verbose.
2025-01-31 08:44:44 +01:00
metagn
647c6687f1 don't mark captured field sym in template as fully used (#24660)
fixes #24657
2025-01-31 08:44:02 +01:00
Peter Munch-Ellingsen
1f9cac1f5c Enable macros to use certain things from the OS module when the target OS is not supported (#24639)
Essentially this PR removes the `{.error.}` pragmas littered around in
the OS module and submodules which prevents them from being imported if
the target OS is not supported. This made it impossible to use certain
supported features of the OS module in macros from a supported host OS.
Instead of the `{.error.}` pragmas the `oscommon` module now has a
constant `supportedSystem` which is false in the cases where the
`{.error.}` pragmas where generated. All procedures which can't be run
by macros is also not declared when `supportedSystem` is false.

It would be possible to create dummy versions of the omitted functions
with an `{.error.}` pragma that would trigger upon their use, but this
is currently not done.

This properly fixes #19414
2025-01-24 13:02:59 +01:00
ringabout
67f9bc2f4b fixes #21923; nimsuggest "outline" output does not list templates (#24643)
fixes #21923

---------

Co-authored-by: Louis Berube <louis.p.berube@gmail.com>
2025-01-24 13:00:08 +01:00
ringabout
d6d28a9c79 fixes #24623; fixes #23692; size pragma only allowed for imported types and enum types (#24640)
fixes #24623
fixes #23692

ref
https://nim-lang.org/docs/manual.html#implementation-specific-pragmas-size-pragma

confines `size` pragma to `enums` and imported `objects` for now

The `typeDefLeftSidePass` carries out the check for pragmas, but the
type is not complete yet. So the `size` pragma checking is postponed at
the final pass.
2025-01-23 20:10:14 +01:00
metagn
6d59680217 don't try to transform objconstr/cast type nodes (#24636)
fixes #24631

[Object
constructors](793baf34ff/compiler/semobjconstr.nim (L462)),
[casts](793baf34ff/compiler/semexprs.nim (L494))
and [type
conversions](793baf34ff/compiler/semexprs.nim (L419))
copy their type nodes verbatim instead of producing semchecked type
nodes. This causes a crash in transf when an untyped expression in the
type node has `nil` type. To deal with this, don't try to transform the
type node in these expressions at all. I couldn't reproduce the problem
with type conversion nodes though so those are unchanged in transf.
2025-01-22 14:08:21 +01:00
ringabout
2f402fcb82 fixes #24630; static openArray backed by seq cannot be passed to another function (#24638)
fixes #24630
2025-01-22 14:05:57 +01:00
metagn
793baf34ff generate destructor in nodestroy proc for explicit destructor call (#24627)
fixes #24626

`createTypeboundOps` in sempass2 is called when generating destructors
for types including for explicit destructor calls, however it blocks
destructors from getting generated in a `nodestroy` proc. This causes
issues when a destructor is explicitly called in a `nodestroy` proc. To
fix this, allow destructors to get generated only for explicit
destructor calls in nodestroy procs.
2025-01-20 10:12:38 +01:00
ringabout
2af9ddc286 revert strictDefs as the default (#24620)
revert https://github.com/nim-lang/Nim/pull/24225

see also https://forum.nim-lang.org/t/12646
2025-01-17 16:08:47 +01:00
ringabout
70d057fcc6 fixes compile crashes with one parameter (#24618)
`{.compile("foo.c").}` makes Nim compiler crash
2025-01-16 15:43:18 +01:00
metagn
8d0e853e0a ignore match errors to expected types of tuple constructor elements (#24611)
fixes #24609

A tuple may have an incompatible expected type if there is a converter
match to it. So the compiler should not error when trying to match the
individual elements in the constructor to the elements of the expected
tuple type, this will be checked when the tuple is entirely constructed
anyway.
2025-01-15 20:01:56 +01:00
ringabout
aeeccee50a fixes #24599; misleading error message with large array bounds (#24601)
fixes #24599
2025-01-05 18:10:36 +01:00
ringabout
3dda60a8ce Update copyright year 2025 (#24593) 2024-12-31 18:06:55 +08:00
chloefeal
cd220fe3e1 docs: fix typos (#24573)
Signed-off-by: chloefeal <188809157+chloefeal@users.noreply.github.com>
2024-12-27 19:42:18 +01:00
ringabout
3c4246dd24 fixes #23114; Nim v2 regression emit / asm var param dereference inconsistency (#24547)
fixes #23114

As in https://github.com/nim-lang/Nim/pull/22074, expressions in
bracketed emit are strictly typechecked, this PR applies the same check
for symbols in asm statements in order to keep them consistent.
2024-12-25 09:23:25 +01:00
Jake Leahy
6bc52737b3 Make 'field is not accessible' and 'field initialized twice' errors point to the field inside the obj construction (#24557)
Fixes two line infos to make the error's clearer inside editors

- 'field is not accessible' would point to the whole object construction
instead of just the field inside the construction
- 'field initialized twice' would point to the colon instead of the
field
2024-12-22 14:25:36 +01:00
metagn
986ca7dcd4 check if unused import warning is enabled before adding import to stack (#24554)
fixes #24552

Could also implement `{.used.}` for imports but this wouldn't be
backwards compatible. The same problem as #24552 also exists for
`{.hint[XDeclaredButNotUsed].}` but this isn't as much of a problem
since `{.used.}`/`{.push used.}` exist.
2024-12-20 22:09:03 +01:00
Jake Leahy
f80ce139d5 Make expandMacro show private fields (#24522)
Was debugging with `--expandMacro` and noticed that private fields
weren't exported.
Passes extra flags to the renderer to make them be shown
2024-12-17 18:17:04 +01:00
ringabout
d5c7abe3d2 fixes #17681; enforce codegen for exportc consts (#24546)
fixes #17681
2024-12-17 18:16:34 +01:00
ringabout
81d8c0fc17 fixes #20908; Unknown warnings and hints now give a warning (#24543)
fixes #20908

This PR unifies the treatment of unknown warnings and hints, which now
gives an `warnUnknownNotes` instead of an error.
2024-12-17 06:41:38 +01:00
metagn
b9c593404c proper error for const defines with unsupported types (#24540)
fixes #24539
2024-12-16 12:22:10 +01:00
Ryan McConnell
e0197a8380 couple cases of valid concept bindings (#24513)
see tests

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2024-12-13 15:13:19 +01:00
ringabout
d2d810585c fixes strictdefs warnings continue (#24520) 2024-12-13 15:04:49 +01:00
ringabout
f7a461a30c fixes #22101; std/pegs with nim cpp --mm:orc --exceptions:goto creates invalid C++ (#24531)
fixes #22101

The old implementation generates

`auto T = value;` for the cpp backend which causes problems for goto
exceptions. This PR puts the declaration of `T` to the cpsLocals parts
and makes it compatible with goto exceptions.
2024-12-12 22:56:41 +01:00
Jake Leahy
69e0cdb6c0 Fix line info for import (#24523)
Refs #24158


Fixes the line info of the module symbol (cases like `import as` and
grouped imports had wrong line info). Since that symbol's line info is
now used for the warnings, there isn't a separate line info stored for
`unusedImports`

Examples of fixed cases
```nim
import strutils as test #[
                ^ before
                   ^ after ]#

# This case was fixed by #24158, but only for unused imports
import std/[strutils, strutils] #[
        ^ before
                      ^ after ]#

from strutils import split #[
^ before 
     ^ after ]#
```
2024-12-11 20:55:55 +01:00
metagn
b529f69518 fix nil node in sym ast of exported ref objects [backport:2.2] (#24527)
fixes #24526, follows up #23101

The `shallowCopy` calls do not keep the original node's children, they
just make a new seq with the same length, so the `Ident "*"` node from
the original postfix nodes was not carried over, making it `nil` and
causing the segfault.
2024-12-10 13:20:40 +01:00
metagn
6f4106bf5d track call depth separately from loop count in VM (#24512)
refs #24503

Infinite recursions currently are not tracked separately from infinite
loops, because they also increase the loop counter. However the max
infinite loop count is very high by default (10 million) and does not
reliably catch infinite recursions before consuming a lot of memory. So
to protect against infinite recursions, we separately track call depth,
and add a separate option for the maximum call depth, much lower than
the maximum iteration count by default (2000, the same as
`nimCallDepthLimit`).

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
2024-12-06 19:00:59 +08:00
ringabout
d0288d3b57 fixes #24504; fixes ensureMove for refs (#24505)
fixes #24504
2024-12-05 21:37:21 +01:00
ringabout
2e9e7f13ee don't track result initialization if it is marked noinit (#24499)
We don't track `noinit` for variables introduced in
https://github.com/nim-lang/Nim/pull/10566. It should be applied to
`result` if the function is marked `noinit`
2024-12-04 15:12:30 +01:00
metagn
2529f33760 remove inserted derefs for ref object fields when transforming to dot call (#24498)
fixes #24492

Kind of a goofy way of doing this, but we count how many derefs were
used for the first parameter before calling `builtinFieldAccess`, then
count after, and if there are more now than before, we remove the added
derefs. I thought maybe getting rid of #18298 would simplify it but
maybe this would still be the best way.

For better encapsulation we could make `dotTransformation` take an
`nOrig` param instead but this would be less efficient since it would
need a copy, though `semAsgn` already makes one.
2024-12-04 12:16:37 +08:00
ringabout
3bee04d9f3 prefix NimDestroyGlobals with nimMainPrefix (#24493)
ref https://github.com/nim-lang/Nim/issues/24471

---------

Co-authored-by: metagn <metagngn@gmail.com>
2024-12-03 13:05:14 +08:00
metagn
05bba15623 fix crash with undeclared proc type pragma macro in generics (#24490)
fixes #24489
2024-12-02 05:21:12 +01:00
ringabout
5340005869 fixes #24476; remove proc type cast if they are same types for backends (#24480)
fixes #24476

closes https://github.com/nim-lang/Nim/pull/24479
2024-11-27 17:36:57 +01:00
Ryan McConnell
e479151473 Fixes 3 small issues with concepts (#24481)
issue 1 - statics in the type:
This probably only handles simple cases. It's probably too accepting
only comparing the base, but that should only affect candidate selection
I think.
issue 2 - `tyArray` of length 3:
This is just a work around since I couldn't get the fix right in
previous PR
issue 3 - shadowing:
The part in `concepts.nim` that iterates candidates does not consider
imported idents if at least once module level ident matches. It does not
have to match in any other way then name.

EDIT: 2 more
issue 4 - composite typeclasses:
when declared in both the concept and the `proc` can cause problems
issue 5 - recursion:
simple recursion and scenarios where more than one concepts recurse
together (only tested two)
2024-11-27 10:15:22 +01:00
ringabout
e7f48cdd5c fixes #24472; let symbol created by template is reused in nimvm branch (#24473)
fixes #24472

Excluding variables which are initialized in the nimvm branch so that
they won't interfere the other branch
2024-11-26 12:35:48 +01:00
ringabout
2df633180a enable experimental:strictDefs (#24225) 2024-11-23 22:01:39 +01:00
ringabout
555191a3f0 fix #19600; No error checking on fclose (#24468)
fix #19600
2024-11-23 14:21:08 +01:00
metagn
96043bdbb7 fix crash with tyBuiltInTypeClass matching itself (#24462)
fixes #24449

The standalone `seq` type is a `tyBuiltInTypeClass` with a single child
of kind `tySequence`, which itself has no children. This is also the
case for most other `tyBuiltInTypeClass` kinds. However this can cause a
crash in sigmatch when calling `isEmptyContainer` on this child type,
which expects the sequence type to have children. This check was added
in #5557 to prevent empty collections like `@[]` from matching their
respective typeclass, but it's not useful when matching against another
typeclass (which is done here to resolve an ambiguity). So to avoid the
crash, this empty container check is disabled when matching against
another typeclass.
2024-11-23 14:20:15 +01:00
Ryan McConnell
08c2a1741d fixes #24451; concept matching generic body (#24458)
I think this might not be a comprehensive solution to dealing with
`tyGenericBody` but we take a step forward
#24451
2024-11-22 06:52:48 +01:00
metagn
b7b1003e6a fix #pragma pack generation regression in packed case objects (#24461)
Regression in #24145
2024-11-21 14:29:18 +03:00
metagn
e28d2f42e9 include new concepts in typeclasses, makes containsGenericType work (#24453)
fixes #24450

The new concepts were previously not included in
[containsGenericType][1] which prevents them from being instantiated.
Here they are included by being added to `tyTypeClasses` though this
doesn't have to be done, they can also be added manually to
`containsGenericTypeIter`, but this might be too specific.

[1]:
a2031ec6cf/compiler/types.nim (L1507-L1517)
2024-11-19 10:06:41 +01:00
metagn
a610f23060 disable weird type inference for object constructors (#24455)
closes #24372, refs #20091

This was added in #20091 for some reason but doesn't actually work and
only makes error messages more obscure. So for now, it's disabled.

Can also be backported to 2.0 if necessary.
2024-11-19 09:49:41 +01:00
ringabout
d8391a5c49 fixes nightlies failures (#24456)
https://github.com/nim-lang/nightlies/actions/runs/11904588983/job/33173656837#step:12:91

```
/home/runner/work/nightlies/nightlies/nim/compiler/ccgexprs.nim(706, 23) Error: type mismatch
Expression: cIntType(getSize(p.module.g.config, t) * 8)
  [1] getSize(p.module.g.config, t) * 8: int64

Expected one of (first mismatch at [position]):
[1] proc cIntType(bits: int): Snippet
```
2024-11-19 08:51:54 +01:00
metagn
a2031ec6cf remove structural equality check for objects and distinct types (#24448)
follows up #24425, fixes #18861, fixes #22445

Since #24425 generic object and distinct types now accurately link back
to their generic instantiations. To work around this issue previously,
type matching checked if generic objects/distinct types were
*structurally* equal, which caused false positives with types that
didn't use generic parameters in their structures. This structural check
is now removed, in cases where generic objects/distinct types require a
nontrivial equality check, the generic parameters of the `typeInst`
fields are checked for equality instead.

The check is copied from `tyGenericInst`, but the check in
`tyGenericInst` is not always sufficient as this type can be skipped or
unreachable in the case of `ref object`s.
2024-11-18 17:37:47 +01:00
metagn
712f5be7eb cbuilder: use constants for type names, some cleanups (#24438)
As described in #24432
2024-11-18 17:34:37 +01:00
metagn
f053767132 make some trivial sizeof calls in codegen use types/literals (#24445)
Partial alternative to #24433 that should be harmless and is the minimal
amount of changes that #24438 depends on.
2024-11-17 20:37:34 +01:00
metagn
05c74d6844 always reinstantiate nominal values of generic instantiations (#24425)
fixes #22479, fixes #24374, depends on #24429 and #24430

When instantiating generic types which directly have nominal types
(object, distinct, ref/ptr object but not enums[^1]) as their values,
the nominal type is now copied (in the case of ref objects, its child as
well) so that it receives a fresh ID and `typeInst` field. Previously
this only happened if it contained any generic types in its structure,
as is the case for all other types.

This solves #22479 and #24374 by virtue of the IDs being unique, which
is what destructors check for. Technically types containing generic
param fields work for the same reason. There is also the benefit that
the `typeInst` field is correct. However issues like #22445 aren't
solved because the compiler still uses structural object equality checks
for inheritance etc. which could be removed in a later PR.

Also fixes a pre-existing issue where destructors bound to object types
with generic fields would not error when attempting to define a user
destructor after the fact, but the error message doesn't show where the
implicit destructor was created now since it was only created for
another instance. To do this, a type flag is used that marks the generic
type symbol when a generic instance has a destructor created. Reusing
`tfCheckedForDestructor` for this doesn't work.

Maybe there is a nicer design that isn't an overreliance on the ID
mechanism, but the shortcomings of `tyGenericInst` are too ingrained in
the compiler to use for this. I thought about maybe adding something
like `tyNominalGenericInst`, but it's really much easier if the nominal
type itself directly contains the information of its generic parameters,
or at least its "symbol", which the design is heading towards.

[^1]: See [this
test](21420d8b09/lib/std/enumutils.nim (L102))
in enumutils. The field symbols `b0`/`b1` always have the uninstantiated
type `B` because enum fields don't expect to be generic, so no generic
instance of `B` matches its own symbols. Wouldn't expect anyone to use
generic enums but maybe someone does.
2024-11-16 10:48:01 +01:00
metagn
75b512bc6a prevent codegen of inactive case fields in VM object constructor nodes (#24442)
fixes #17571

Objects in the VM are represented as object constructor nodes that
contain every single field, including ones in different case branches.
This is so that every field has a unique invariant index in the object
constructor that can be written to and read from. However when
converting this node back into semantic code, fields from inactive case
branches can remain in the constructor which causes bad codegen,
generating assignments to fields from other case branches.

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 #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.
2024-11-16 10:43:58 +01:00
metagn
e239968b80 fix wrong error for iterators with no body and pragma macro (#24440)
fixes #16413

`semIterator` checks if the original iterator passed to it has no body,
but it should check the processed node created by `semProcAux`.
2024-11-15 22:52:38 +01:00
ringabout
cd9ce377f8 fixes #24434; C gen issue using nim-lang/opengl (#24435)
fixes #24434

In https://github.com/nim-lang/Nim/pull/24432

```nim
let ex = "NIM_EXTERNC N_NIMCALL(void, nimLoadProcs$1)(void) {$2}$N$N" %
        [(i.ord - '0'.ord).rope, extract(el)]
```

```nim
procs.addDeclWithVisibility(ExternC):
   procs.addProcHeader(ccNimCall, "nimLoadProcs" & $(i.ord - '0'.ord), "void", cProcParams())
```

extern "C" makes a function-name in C++ have C linkage; it should be
effaced with C compiler
2024-11-15 22:51:58 +01:00