adds modifierMode parameter to typeof (#25815)

This PR adds 3 modes to `typeof` to specify how to handle type modifiers
`var`, `sink` and `lent`.

- typeOfModCompatible
Remove or keep type modifiers in the same way as old typeof. That means
keep `sink` but remove `var` and `lent`.
- typeOfModRemoveModifier
  Remove type modifiers.
- typeOfModKeepModifier
  Keep type modifiers.

Related to https://github.com/nim-lang/Nim/pull/25779
https://github.com/nim-lang/Nim/issues/25786

(cherry picked from commit 48621c217f)
This commit is contained in:
Tomohiro
2026-06-10 03:55:30 +09:00
committed by narimiran
parent 848188512c
commit db595b397c
6 changed files with 161 additions and 22 deletions

View File

@@ -54,7 +54,12 @@ type
typeOfProc, ## Prefer the interpretation that means `x` is a proc call.
typeOfIter ## Prefer the interpretation that means `x` is an iterator call.
proc typeof*(x: untyped; mode = typeOfIter): typedesc {.
TypeOfModifiers* = enum ## Modes to handle type modifiers `var`, `sink` and `lent`.
CompatibleTypeModifiers, ## Remove or keep type modifiers in the same way as old typeof. That means keep `sink` but remove `var` and `lent`.
RemoveTypeModifiers, ## Remove type modifiers.
KeepTypeModifiers, ## Keep type modifiers.
proc typeof*(x: untyped; mode = typeOfIter; modifierMode = CompatibleTypeModifiers): typedesc {.
magic: "TypeOf", noSideEffect, compileTime.} =
## Builtin `typeof` operation for accessing the type of an expression.
## Since version 0.20.0.
@@ -76,6 +81,11 @@ proc typeof*(x: untyped; mode = typeOfIter): typedesc {.
# since `typeOfProc` expects a typed expression and `myFoo2()` can
# only be used in a `for` context.
proc varParam(x: var int;
y: typeof(x, modifierMode = RemoveTypeModifiers);
z: typeof(x, modifierMode = KeepTypeModifiers)) = discard
doAssert varParam is proc (x: var int; y: int; z: var int) {.nimcall.}
proc `or`*(a, b: typedesc): typedesc {.magic: "TypeTrait", noSideEffect.}
## Constructs an `or` meta class.