summary refs log tree commit diff stats
path: root/compiler/semstmts.nim
Commit message (Collapse)AuthorAgeFilesLines
* fix calls to untyped arbitrary expressions in generics (#24100)metagn2024-09-131-3/+1
| | | | | | | | | | | | | | | | | | | | fixes #24099 If an arbitrary expression without a proc type (in this case `tyFromExpr`) is called, the compiler [passes it to overload resolution](https://github.com/nim-lang/Nim/blob/793cee4de1934fd1f6271cf5fed46f01c5abb19b/compiler/semexprs.nim#L1223). But overload resolution also can't handle arbitrary expressions and treats them as not participating at all, matching with the state `csEmpty`. The compiler checks for this and gives an "identifier expected" error. Instead, now if we are in a generic context and an arbitrary expression call matched with `csEmpty`, we now return `csNoMatch` so that `semResolvedCall` can leave it untyped as appropriate. The expression flag `efNoDiagnostics` is replaced with this check. It's not checked anywhere else and the only place that uses it is `handleCaseStmtMacro`. Replacing it with `efNoUndeclared`, we just get `csEmpty` instead of `csNoMatch`, which is handled the same way here. So `efNoDiagnostics` is now removed and `efNoUndeclared` is used instead.
* open new scope for const values (#24084)metagn2024-09-091-0/+2
| | | | | | | | | | | | | 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.
* fix `include` in templates, with prefix operators (#24029)metagn2024-08-291-12/+15
| | | | | | | | | | | fixes #12539, refs #21636 `evalInclude` now uses `getPIdent` to check if a symbol isn't `/` anymore instead of assuming it's an ident, which it's not when used in templates. Also it just checks if the symbol is `as` now, because there are other infix operators that could be used like `/../`. It also works with prefix operators now by copying what was done in #21636.
* remove fauxMatch for tyFromExpr, remove tyProxy and tyUnknown aliases (#24018)metagn2024-08-281-1/+1
| | | | | | | | | | | | | | | | | | | | | updated version of #22193 After #22029 and the followups #23983 and #24005 which fixed issues with it, `tyFromExpr` no longer match any proc params in generic type bodies but delay all non-matching calls until the type is instantiated. Previously the mechanism `fauxMatch` was used to pretend that any failing match against `tyFromExpr` actually matched, but prevented the instantiation of the type until later. Since this mechanism is not needed anymore for `tyFromExpr`, it is now only used for `tyError` to prevent cascading errors and changed to a bool field for simplicity. A change in `semtypes` was also needed to prevent calling `fitNode` on default param values resolving to type `tyFromExpr` in generic procs for params with non-generic types, as this would try to coerce the expression into a concrete type when it can't be instantiated yet. The aliases `tyProxy` and `tyUnknown` for `tyError` and `tyFromExpr` are also removed for uniformity.
* fixes #23943; simple default value for range (#23996)ringabout2024-08-221-0/+5
| | | | | | | fixes #23943 --------- Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* allow qualifying macro pragmas (#23985)metagn2024-08-201-5/+5
| | | fixes #12696
* remove nontoplevel type hack + consider symbol disamb in type hash (#23969)metagn2024-08-161-4/+0
| | | | | | | | | | | | | | fixes #22571 Removes the hack added in #13589 which made non-top-level object type symbols `gensym` because they couldn't be mangled into different names for codegen vs. top-level types. Now we consider the new `disamb` field (added in #21667) of the type symbols in the type hash (which is used for the mangled name) to differentiate between the types. In other parts of the compiler, specifically the [proc mangling](https://github.com/nim-lang/Nim/blob/298ada3412c9cf5971abc2b3b891b9bb8612e170/compiler/mangleutils.nim#L59), `itemId.item` is used instead of the `disamb` field, but I didn't use it in case it's the outdated method.
* fixes semi-regression; discard check now skips `nkHiddenSubConv` (#23840)ringabout2024-07-161-1/+1
| | | | | follow up https://github.com/nim-lang/Nim/pull/23681 ref https://forum.nim-lang.org/t/11987
* fixes regression; block can have arbitrary exit points; too hard for a ↵ringabout2024-07-161-5/+6
| | | | | | | | simple analysis (#23839) follow up https://github.com/nim-lang/Nim/pull/23681 ref https://forum.nim-lang.org/t/11987 ref https://github.com/nim-lang/Nim/issues/23836#issuecomment-2227267251
* [minor] fixes wrong error messages (#23841)ringabout2024-07-161-1/+1
|
* fixes #3011; handles meta fields defined in the ref object (#23818)ringabout2024-07-111-2/+7
| | | | | | | | | | | | | | | | | | | | | | | fixes #3011 In https://github.com/nim-lang/Nim/pull/23532, meta fields that defined in the object are handled. In this PR, RefObjectTy is handled as well: ```nim type Type = ref object context: ref object ``` Ref alias won't trigger mata fields checking so there won't have cascaded errors on `TypeBase`. ```nim type TypeBase = object context: ref object Type = ref TypeBase context: ref object ```
* fixes #23775; injectdestructors now handles discardable statements (#23780)ringabout2024-07-021-0/+1
| | | fixes #23775
* [backport] fixes #23711; C code contains backtick`gensym (#23716)ringabout2024-06-191-1/+1
| | | fixes #23711
* fix noreturn/implicit discard check logic (#23681)metagn2024-06-051-13/+132
| | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* fixes #23531; fixes invalid meta type accepted in the object fields (#23532)ringabout2024-04-261-9/+17
| | | | | fixes #23531 fixes #19546 fixes #6982
* Fix duplicated member declarations in structs for C++ backend (#23512)HexSegfaultCat2024-04-181-1/+1
| | | | | | | | | | | | | | | When forward declaration is used with pragmas `virtual` or `member`, the declaration in struct is added twice. It happens because of missing check for `sfWasForwarded` pragma. Current compiler generates the following C++ code: ```cpp struct tyObject_Foo__fFO9b6HU7kRnKB9aJA1RApKw { N_LIB_PRIVATE N_NOCONV(void, abc)(NI x_p1); N_LIB_PRIVATE N_NOCONV(virtual void, def)(NI y_p1); N_LIB_PRIVATE N_NOCONV(void, abc)(NI x_p1); N_LIB_PRIVATE N_NOCONV(virtual void, def)(NI y_p1); }; ```
* fixes addr/hiddenAddr in strictdefs (#23477)ringabout2024-04-101-2/+2
|
* refactoring: no inheritance for PType/PSym (#23403)Andreas Rumpf2024-03-141-1/+1
|
* fixes #22284; fixes #22282; don't override original parameters of inferred ↵ringabout2024-03-091-1/+0
| | | | | | | | | | | | | | | | | lambdas (#23368) fixes #22284 fixes #22282 ``` Error: j(uRef, proc (config: F; sources: auto) {.raises: [].} = discard ) can raise an unlisted exception: Exception ``` The problem is that `n.typ.n` contains the effectList which shouldn't appear in the parameter of a function defintion. We could not simply use `n.typ.n` as `n[paramsPos]`. The effect lists should be stripped away anyway.
* fixes refc with non-var destructor; cancel warnings (#23156)ringabout2024-02-131-3/+13
| | | fixes https://forum.nim-lang.org/t/10807
* fixes #18104; tranform one liner var decl before templates expansion (#23294)ringabout2024-02-131-0/+18
| | | fixes #18104
* fixes #12334; keeps `nkHiddenStdConv` for cstring conversions (#23216)ringabout2024-01-181-0/+2
| | | | | | fixes #12334 `nkHiddenStdConv` shouldn't be removed if the sources aren't literals, viz. constant symbols.
* fixes #23180; fixes #19805; prohibits invalid tuple unpacking code in for ↵ringabout2024-01-131-1/+4
| | | | | | loop (#23185) fixes #23180 fixes #19805
* retain postfix node in type section typed AST, with docgen fix (#23101)metagn2023-12-231-3/+19
| | | | | | Continued from #23096 which was reverted due to breaking a package and failing docgen tests. Docgen should now work, but ~~a PR is still pending for the package: https://github.com/SciNim/Unchained/pull/45~~ has been merged
* Revert "retain postfix node in type section typed AST" (#23098)metagn2023-12-191-19/+3
| | | Reverts nim-lang/Nim#23096
* retain postfix node in type section typed AST (#23096)metagn2023-12-181-3/+19
| | | fixes #22933
* Mark `macros.error` as `.noreturn.` (#23081)Jake Leahy2023-12-171-0/+5
| | | | | | | | | | | | | | | Closes #14329 Marks `macros.error` as `.noreturn` so that it can be used in expressions. This also fixes the issue that occurred in #19659 where a stmt that could be an expression (Due to having `discardable` procs at the end of other branches) would believe a `noreturn` proc is returning the same type e.g. ```nim proc bar(): int {.discardable.} = discard if true: bar() else: quit(0) # Says that quit is of type `int` and needs to be used/discarded except it actually has no return type ```
* type graph refactor; part 3 (#23064)Andreas Rumpf2023-12-141-6/+6
|
* type refactoring: part 2 (#23059)Andreas Rumpf2023-12-131-25/+29
|
* Types: Refactorings; step 1 (#23055)Andreas Rumpf2023-12-121-64/+64
|
* Only suggest symbols that could be pragmas when typing a pragma (#23040)Jake Leahy2023-12-071-0/+4
| | | | | | Currently pragmas just fall through to `suggestSentinel` and show everything which isn't very useful. Now it filters for symbols that could be pragmas (templates with `{.pragma.}`, macros, user pragmas) and only shows them
* Fix nimsuggest `def` being different on proc definition/use (#23025)Jake Leahy2023-12-041-2/+12
| | | | | | | | | | | | | | | | | | Currently the documentation isn't shown when running `def` on the definition of a proc (Which works for things like variables). `gcsafe`/`noSideEffects` status also isn't showing up when running `def` on the definition Images of current behavior. After PR both look like "Usage" **Definition** ![image](https://github.com/nim-lang/Nim/assets/19339842/bf75ff0b-9a96-49e5-bf8a-d2c503efa784) **Usage** ![image](https://github.com/nim-lang/Nim/assets/19339842/15ea3ebf-64e1-48f5-9233-22605183825f) Issue was the symbol getting passed too early to nimsuggest so it didn't have all that info, now gets passed once proc is fully semmed
* Fix endsInNoReturn for case statements (#23009)SirOlaf2023-11-301-8/+2
| | | | | | | | | 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 <>
* IC: progress and refactorings (#22961)Andreas Rumpf2023-11-201-5/+5
|
* Inlay hints for types of consts (#22916)Nikolay Nikolov2023-11-071-0/+4
| | | This adds inlay hint support for the types of consts.
* Inlay hints support (#22896)Nikolay Nikolov2023-11-041-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* suppress incorrect var T destructor warnings for newFinalizer in stdlib (#22810)ringabout2023-10-111-2/+2
| | | | | | | | | | | | 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 ```
* fixes compiler crash by preventing exportc on generics (#22731)Juan M Gómez2023-09-201-3/+6
| | | Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* `constructor` now uses `result` instead of `this` (#22724)Juan M Gómez2023-09-191-28/+8
|
* prevents declaring a constructor without importcpp fixes #22712 (#22715)Juan M Gómez2023-09-181-2/+7
| | | Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* implements RFC: [C++] Constructors as default initializers (#22694)Juan M Gómez2023-09-141-29/+50
| | | Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* fixes #22669 constructor pragma doesnt init Nim default fields (#22670)Juan M Gómez2023-09-101-0/+2
| | | | | | | fixes #22669 constructor pragma doesnt init Nim default fields --------- Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* don't update const symbol on const section re-sems (#22609)metagn2023-09-011-5/+10
| | | fixes #19849
* Fix #22604: Make endsInNoReturn traverse the tree (#22612)SirOlaf2023-09-011-3/+1
| | | | | | | | | | | | | | | | | * Rewrite endsInNoReturn * Handle `try` stmt again and add tests * Fix unreachable code warning * Remove unreachable code in semexprs again * Check `it.len` before skip * Move import of assertions --------- Co-authored-by: SirOlaf <>
* type annotations for variable tuple unpacking, better error messages (#22611)metagn2023-09-011-2/+8
| | | | | | | | | * type annotations for variable tuple unpacking, better error messages closes #17989, closes https://github.com/nim-lang/RFCs/issues/339 * update grammar * fix test
* round out tuple unpacking assignment, support underscores (#22537)metagn2023-08-241-7/+10
| | | | | | | | | | | | | | | * round out tuple unpacking assignment, support underscores fixes #18710 * fix test messages * use discard instead of continue Co-authored-by: Andreas Rumpf <rumpf_a@web.de> --------- Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
* unpublic the sons field of PType; the precursor to PType refactorings (#22446)ringabout2023-08-111-7/+7
| | | | | | | | | | | * unpublic the sons field of PType * tiny fixes * fixes an omittance * fixes IC * fixes
* fix #19304 Borrowing std/times.format causes Error: illformed AST (#20659)Bung2023-08-101-12/+21
| | | | | | | | | * fix #19304 Borrowing std/times.format causes Error: illformed AST * follow suggestions * mitigate for #4121 * improve error message
* [C++] Member pragma RFC (https://github.com/nim-lang/RFCs/issues/530) (#22272)Juan M Gómez2023-08-071-7/+8
| | | | | | | | | | * [C++] Member pragma RFC #530 rebase devel * changes the test so `echo` is not used before Nim is init * rebase devel * fixes Error: use explicit initialization of X for clarity [Uninit]
* use strictdefs for compiler (#22365)ringabout2023-08-061-6/+25
| | | | | | | | | | | | | | | * wip; use strictdefs for compiler * checkpoint * complete the chores * more fixes * first phase cleanup * Update compiler/bitsets.nim * cleanup