summary refs log tree commit diff stats
path: root/tests/generics
Commit message (Collapse)AuthorAgeFilesLines
* fixes #23790; roll back instCounter properly in case of exceptions (#23802)Alexander Kernozhitsky2024-07-061-0/+14
| | | fixes #23790
* ignore uninstantiated static on match to base type [backport:2.0] (#23731)metagn2024-06-181-0/+5
| | | | | | | | | | | | | | | | | | | | | 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.
* fix semFinishOperands for bracket expressions [backport:2.0] (#23571)metagn2024-05-081-0/+24
| | | | | | | | | | | | | | | | 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.
* fixes #23233; Regression when using generic type with Table/OrderedTable ↵ringabout2024-01-191-1/+21
| | | | | (#23235) fixes #23233
* delay resolved procvar check for proc params + acknowledge unresolved ↵metagn2024-01-113-3/+159
| | | | | | | | | | | | | | | | | | | | | | | | | statics (#23188) fixes #23186 As explained in #23186, generics can transform `genericProc[int]` into a call `` `[]`(genericProc, int) `` which causes a problem when `genericProc` is resemmed, since it is not a resolved generic proc. `[]` needs unresolved generic procs since `mArrGet` also handles explicit generic instantiations, so delay the resolved generic proc check to `semFinishOperands` which is intentionally not called for `mArrGet`. The root issue for [t6137](https://github.com/nim-lang/Nim/blob/devel/tests/generics/t6137.nim) is also fixed (because this change breaks it otherwise), the compiler doesn't consider the possibility that an assigned generic param can be an unresolved static value (note the line `if t.kind == tyStatic: s.ast = t.n` below the change in sigmatch), now it properly errors that it couldn't instantiate it as it would for a type param. ~~The change in semtypinst is just for symmetry with the code above it which also gives a `cannot instantiate` error, it may or may not be necessary/correct.~~ Now removed, I don't think it was correct. Still possible that this has unintended consequences.
* add switch, warning, and `bind` support for new generic injection behavior ↵metagn2023-12-222-0/+79
| | | | | | | | | | | | | | | | | | | | | (#23102) refs #23091, especially post merge comments Unsure if `experimental` and `bind` are the perfect constructs to use but they seem to get the job done here. Symbol nodes do not get marked `nfOpenSym` if the `bind` statement is used for their symbol, and `nfOpenSym` nodes do not get replaced by new local symbols if the experimental switch is not enabled in the local context (meaning it also works with `push experimental`). However this incurs a warning as the fact that the node is marked `nfOpenSym` means we did not `bind` it, so we might want to do that or turn on the experimental switch if we didn't intend to bind it. The experimental switch name is arbitrary and could be changed. --------- Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* allow replacing captured syms in macro calls in generics (#23091)metagn2023-12-181-0/+86
| | | | | | | | | | | | | | | | | | fixes #22605, separated from #22744 This marks symbol captures in macro calls in generic contexts as `nfOpenSym`, which means if there is a new symbol in the local instantiatied body during instantiation time, this symbol replaces the captured symbol. We have to be careful not to consider symbols outside of the instantiation body during instantiation, because this will leak symbols from the instantiation context scope rather than the original declaration scope. This is done by checking if the local context owner (maybe should be the symbol of the proc currently getting instantiated instead? not sure how to get this) is the same as or a parent owner of the owner of the replacement candidate symbol. This solution is distinct from the symchoice mechanisms which we originally assumed had to be related, if this assumption was wrong it would explain why this solution took so long to arrive at.
* fixes #9381; Fix double evaluation of types in generic objects (#23072)Pylgos2023-12-141-0/+14
| | | fixes https://github.com/nim-lang/Nim/issues/9381
* enable vtable implementation for C++ and make it an experimental feature ↵ringabout2023-11-301-1/+1
| | | | | | | | | | | (#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>
* rework the vtable implementation embedding the vtable array directly with ↵ringabout2023-11-281-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | 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>
* fixes #22971; `inferGenericTypes` does not work with method call syntax (#22972)Pylgos2023-11-221-1/+23
| | | fixes #22971
* Fix #22826: Don't skip generic instances in type comparison (#22828)SirOlaf2023-10-211-0/+8
| | | | | | | | | | | | | | 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 <>
* fixes #22753; Nimsuggest segfault with invalid assignment to table (#22781)ringabout2023-10-021-2/+2
| | | | | | | | fixes #22753 ## Future work We should turn all the error nodes into nodes of a nkError kind, which could be a industrious task. But perhaps we can add a special treatment for error nodes to make the transition smooth.
* second test case haul for templates and generics (#22728)metagn2023-09-191-0/+7
| | | | closes #8390, closes #11726, closes #8446, closes #21221, closes #7461, closes #7995
* implement semgnrc for tuple and object type nodes (#22709)metagn2023-09-162-1/+11
| | | fixes #22699
* Fix #21742: Check generic alias depth before skip (#22443)SirOlaf2023-09-081-0/+10
| | | | | | | | | | | | | | | Close #21742 Checking if there's any side-effects and if just changing typeRel is adequate for this issue before trying to look into related ones. `skipBoth` is also not that great, it can lead to code that works sometimes but fails when the proc is instantiated with branching aliases. This is mostly an issue with error clarity though. --------- Co-authored-by: SirOlaf <unknown> Co-authored-by: SirOlaf <>
* Fix #17509: Continue instead of return with unfinished generics (#22563)SirOlaf2023-09-071-0/+25
| | | | | | | | | | | | | | | | | | | Close #17509 Current knowledge: - delaying cache fixes the issue - changing return of `if inst.len < key.len:` in `searchInstTypes` to `continue` fixes the issue. With return the broken types are also cached over and over Related issues are completely unaffected as of now, so there must be something deeper. I am also still trying to find the true cause, so feel free to ignore for now --------- Co-authored-by: SirOlaf <>
* make getType nodes of generic insts have full inst type (#22655)metagn2023-09-071-0/+35
| | | | | | | | | | | | | fixes #22639 for the third time Nodes generated by `getType` for `tyGenericInst` types, instead of having the original `tyGenericInst` type, will have the type of the last child (due to the `mapTypeToAst` calls which set the type to the given argument). This will cause subsequent `getType` calls to lose information and think it's OK to use the sym of the instantiated type rather than fully expand the generic instantiation. To prevent this, update the type of the node from the `mapTypeToAst` calls to the full generic instantiation type.
* fully revert generic inst sym change, test #22646 (#22653)metagn2023-09-061-35/+0
| | | | | | | reverts #22642, reopens #22639, closes #22646, refs #22650, refs https://github.com/alaviss/union/issues/51, refs #22652 The fallout is too much from #22642, we can come back to it if we can account for all the affected code.
* fix sym of created generic instantiation type (#22642)metagn2023-09-051-4/+35
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | fixes #22639 A `tyGenericInst` has its last son as the instantiated body of the original generic type. However this type keeps its original `sym` field from the original generic types, which means the sym's type is uninstantiated. This causes problems in the implementation of `getType`, where it uses the `sym` fields of types to represent them in AST, the relevant example for the issue being [here](https://github.com/nim-lang/Nim/blob/d13aab50cf465a7f2edf9c37a4fa30e128892e72/compiler/vmdeps.nim#L191) called from [here](https://github.com/nim-lang/Nim/blob/d13aab50cf465a7f2edf9c37a4fa30e128892e72/compiler/vmdeps.nim#L143). To fix this, create a new symbol from the original symbol for the instantiated body during the creation of `tyGenericInst`s with the appropriate type. Normally `replaceTypeVarsS` would be used for this, but here it seems to cause some recursion issue (immediately gives an error like "cannot instantiate HSlice[T, U]"), so we directly set the symbol's type to the instantiated type. Avoiding recursion means we also cannot use `replaceTypeVarsN` for the symbol AST, and the symbol not having any AST crashes the implementation of `getType` again [here](https://github.com/nim-lang/Nim/blob/d13aab50cf465a7f2edf9c37a4fa30e128892e72/compiler/vmdeps.nim#L167), so the symbol AST is set to the original generic type AST for now which is what it was before anyway. Not sure about this because not sure why the recursion issue is happening, putting it at the end of the proc doesn't help either. Also not sure if the `cl.owner != nil and s.owner != cl.owner` condition from `replaceTypeVarsS` is relevant here. This might also break some code if it depended on the original generic type symbol being given.
* unify explicit generic param semchecking in calls (#22618)metagn2023-09-011-1/+20
| | | fixes #9040
* fixes internal error: no generic body fixes #1500 (#22580)Juan M Gómez2023-09-011-0/+8
| | | | | | | | | | | * fixes internal error: no generic body fixes #1500 * adds guard * adds guard * removes unnecessary test * refactor: extracts containsGenericInvocationWithForward
* resolve local symbols in generic type call RHS (#22610)metagn2023-09-012-0/+20
| | | | | resolve local symbols in generic type call fixes #14509
* test case haul for old generic/template/macro issues (#22564)metagn2023-08-272-0/+73
| | | | | | | | | | | | * test case haul for old generic/template/macro issues closes #12582, closes #19552, closes #2465, closes #4596, closes #15246, closes #12683, closes #7889, closes #4547, closes #12415, closes #2002, closes #1771, closes #5121 The test for #5648 is also moved into its own test from `types/tissues_types` due to not being joinable. * fix template gensym test
* Fix #21760 (#22422)SirOlaf2023-08-101-0/+8
| | | | | | | | | | | * Remove call-specific replaceTypeVarsN * Run for all call kinds and ignore typedesc * Testcase --------- Co-authored-by: SirOlaf <>
* fix #12938 index type of array in type section without static (#20529)Bung2023-08-092-0/+15
| | | | | | | | | | | | | * fix #12938 nim compiler assertion fail when literal integer is passed as template argument for array size * use new flag tfImplicitStatic * fix * fix #14193 * correct tfUnresolved add condition * clean test
* block ambiguous type conversion dotcalls in generics (#22375)metagn2023-08-094-0/+46
| | | fixes #22373
* fix #18823 Passing Natural to bitops.BitsRange[T] parameter in generi… ↵Bung2023-08-081-0/+6
| | | | | (#20683) * fix #18823 Passing Natural to bitops.BitsRange[T] parameter in generic proc is compile error
* Let inferGenericTypes continue if a param is already bound (#22384)SirOlaf2023-08-061-1/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | | * Play with typeRel * Temp solution: Fixup call's param types * Test result type with two generic params * Asserts * Tiny cleanup * Skip sink * Ignore proc * Use changeType * Remove conversion * Remove last bits of conversion * Flag --------- Co-authored-by: SirOlaf <>
* Fix crash when using uninstantiated generic (#22379)Jake Leahy2023-08-041-0/+16
| | | | | | | * Add test case * Add in a bounds check when accessing generic types Removes idnex out of bounds exception when comparing a generic that isn't fully instantiated
* Add experimental inferGenericTypes switch (#22317)SirOlaf2023-08-031-0/+139
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Infer generic bindings * Simple test * Add t * Allow it to work for templates too * Fix some builds by putting bindings in a template * Fix builtins * Slightly more exotic seq test * Test value-based generics using array * Pass expectedType into buildBindings * Put buildBindings into a proc * Manual entry * Remove leftover ` * Improve language used in the manual * Experimental flag and fix basic constructors * Tiny commend cleanup * Move to experimental manual * Use 'kind' so tuples continue to fail like before * Explicitly disallow tuples * Table test and document tuples * Test type reduction * Disable inferGenericTypes check for CI tests * Remove tuple info in manual * Always reduce types. Testing CI * Fixes * Ignore tyGenericInst * Prevent binding already bound generic params * tyUncheckedArray * Few more types * Update manual and check for flag again * Update tests/generics/treturn_inference.nim * var candidate, remove flag check again for CI * Enable check once more --------- Co-authored-by: SirOlaf <> Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* fix scoping regression with calls in generic bodies (#22115)metagn2023-06-172-0/+34
| | | refs #22029, refs https://github.com/status-im/nim-libp2p/actions/runs/5263850340/jobs/9514434659
* fix calls in generic bodies, delay typecheck when no overloads match (#22029)metagn2023-06-132-36/+72
| | | | | | | | | | | | | | | | | | | | | | | * sacrifice "tgenericshardcases" for working statics * legacy switch for CI, maybe experimental later * convert to experimental * apparently untyped needs the experimental switch * try special case call semcheck * try fix * fix compilation * final cleanup, not experimental, make `when` work * remove last needed use of untyped * fix unused warning in test * remove untyped feature
* more test cases for generic object impl AST (#22077)metagn2023-06-112-21/+49
| | | closes #9899, closes #14708, refs #21017
* some test cleanups & category reorganization (#22010)metagn2023-06-064-5/+38
| | | | | | | | | | | | | | | | | * 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
* add test case for #7839 (#22006)Bung2023-06-051-0/+9
|
* Add anti-regression for #21958 (#21960)Mamy Ratsimbazafy2023-05-301-0/+11
| | | Add anti-regression test to close #21958
* when T is both a type symbol and a routine symbol in scope of a generic proc ↵metagn2023-05-242-0/+9
| | | | | do not account for the type symbol when doing `a.T()` (#21899) fix #21883
* fix #14254 (#21837)metagn2023-05-202-0/+6
| | | | | | | * fix #14254 * use temporary PR branch for neo * fix url
* consistent use of scForceOpen for generic dot field symbols (#21738)metagn2023-05-053-1/+62
| | | | | | | | | | | | | | | | | | | | | * always force open generic dot field symbols? fixes #21724 but might break code * alternative, should fix CI * other alternative, add test for previous CI failure * not needed * make sure call doesn't compile too * ok actual second test * ok final actual correct test * apply performance idea * don't make fromDotExpr static
* fixes #20900; Calling template through from generic function across module ↵ringabout2023-04-132-2/+11
| | | | | | | fails to build (#21649) * fixes #20900; Calling template through from generic function across module fails to build * sanother way
* fixes #3770; templates with untyped parameters resolve private fields ↵ringabout2023-03-212-0/+15
| | | | | | | | | wrongly in generics (#21554) * fixes #3770; templates with untyped parameters resolve private fields wrongly * add a test case for #3770 * rename to `nfSkipFieldChecking`
* closes #8295; add a test case (#21486)ringabout2023-03-071-0/+13
|
* closes #6231; add a test case (#21485)ringabout2023-03-071-0/+5
|
* Fix #20416. Enable the recursion limit for ref/ptr types. (#21092)Aditya Siram2022-12-131-0/+20
|
* fix #16639 (#21017)metagn2022-12-051-0/+21
|
* fix #20996 (#21016)metagn2022-12-041-0/+15
| | | | | * fix #20996 * hopefully fix
* fix #6637 array index type depends generic (#20673)Bung2022-10-271-0/+9
|
* add test case for #500 (#20661)Bung2022-10-261-0/+8
|
* closes #4466; add testcase (#20625)ringabout2022-10-231-0/+9
|