Skip to content

Commit

Permalink
experimental switch for generic compileTime, remove typeof behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Oct 3, 2024
1 parent d6a71a1 commit 688d104
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
1 change: 1 addition & 0 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ type
openSym, # remove nfDisabledOpenSym when this is default
# alternative to above:
genericsOpenSym
genericProcCompileTime
vtables

LegacyFeature* = enum
Expand Down
1 change: 0 additions & 1 deletion compiler/semdata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ type
inUncheckedAssignSection*: int
importModuleLookup*: Table[int, seq[int]] # (module.ident.id, [module.id])
skipTypes*: seq[PNode] # used to skip types between passes in type section. So far only used for inheritance, sets and generic bodies.
inTypeofContext*: int
TBorrowState* = enum
bsNone, bsReturnNotMatch, bsNoDistinct, bsGeneric, bsNotSupported, bsMatch

Expand Down
9 changes: 6 additions & 3 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -995,8 +995,11 @@ proc evalAtCompileTime(c: PContext, n: PNode): PNode =

if callee.magic notin ctfeWhitelist: return

if callee.kind notin {skProc, skFunc, skConverter, skConst} or
callee.isGenericRoutineStrict:
if callee.kind notin {skProc, skFunc, skConverter, skConst} or (
if genericProcCompileTime in c.features:
callee.isGenericRoutineStrict
else:
callee.isGenericRoutine):
return

if n.typ != nil and typeAllowed(n.typ, skConst, c) != nil: return
Expand Down Expand Up @@ -1136,7 +1139,7 @@ proc afterCallActions(c: PContext; n, orig: PNode, flags: TExprFlags; expectedTy
not (result.typ.kind == tySequence and result.elementType.kind == tyEmpty):
liftTypeBoundOps(c, result.typ, n.info)
#result = patchResolvedTypeBoundOp(c, result)
if c.matchedConcept == nil and (c.inTypeofContext == 0 or callee.magic != mNone):
if c.matchedConcept == nil:
# don't fold calls in concepts and typeof
result = evalAtCompileTime(c, result)

Expand Down
2 changes: 0 additions & 2 deletions compiler/semmagic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ proc semTypeOf(c: PContext; n: PNode): PNode =
else:
m = mode.intVal
result = newNodeI(nkTypeOfExpr, n.info)
inc c.inTypeofContext
defer: dec c.inTypeofContext # compiles can raise an exception
let typExpr = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {})
result.add typExpr
if typExpr.typ.kind == tyFromExpr:
Expand Down
4 changes: 0 additions & 4 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1870,8 +1870,6 @@ proc semStaticType(c: PContext, childNode: PNode, prev: PType): PType =

proc semTypeOf(c: PContext; n: PNode; prev: PType): PType =
openScope(c)
inc c.inTypeofContext
defer: dec c.inTypeofContext # compiles can raise an exception
let t = semExprWithType(c, n, {efInTypeof})
closeScope(c)
fixupTypeOf(c, prev, t)
Expand All @@ -1888,8 +1886,6 @@ proc semTypeOf2(c: PContext; n: PNode; prev: PType): PType =
localError(c.config, n.info, "typeof: cannot evaluate 'mode' parameter at compile-time")
else:
m = mode.intVal
inc c.inTypeofContext
defer: dec c.inTypeofContext # compiles can raise an exception
let t = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {})
closeScope(c)
fixupTypeOf(c, prev, t)
Expand Down
22 changes: 12 additions & 10 deletions tests/vm/tgenericcompiletimeproc.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
block:
# don't fold compile time procs in typeof with experimental switch disabled
proc fail[T](x: T): T {.compileTime.} =
doAssert false
x
doAssert typeof(fail(123)) is typeof(123)
proc p(x: int): int = x

type Foo = typeof(p(fail(123)))

{.experimental: "genericProcCompileTime".}

block: # issue #10753
proc foo(x: int): int {.compileTime.} = x
const a = foo(123)
Expand All @@ -18,16 +30,6 @@ block: # issue #19365
doAssert f(123) == 246
doAssert f(1.0) == 2.0

block:
# don't fold compile time procs in typeof
proc fail[T](x: T): T {.compileTime.} =
doAssert false
x
doAssert typeof(fail(123)) is typeof(123)
proc p(x: int): int = x

type Foo = typeof(p(fail(123)))

block: # issue #24150, related regression
proc w(T: type): T {.compileTime.} = default(ptr T)[]
template y(v: auto): auto = typeof(v) is int
Expand Down

0 comments on commit 688d104

Please sign in to comment.