From ca28c256f3b5214b59700d0be260f700459c19d0 Mon Sep 17 00:00:00 2001 From: metagn Date: Sun, 8 Sep 2024 23:49:27 +0300 Subject: [PATCH] fix subscript in generics, typeof, `lent` with bracket (#24067) fixes #15959 Another followup of #22029 and #24005, subscript expressions now recognize when their parameters are generic types, then generating tyFromExpr. `typeof` also now properly sets `tfNonConstExpr` to make it usable in proc signatures. `lent` with brackets like `lent[T]` is also now allowed. --- compiler/semmagic.nim | 11 ++++++++++ compiler/semtypes.nim | 10 ++++++++++ .../generics/tuninstantiatedgenericcalls.nim | 20 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim index 524c9a0abd4fa..360d234779f7d 100644 --- a/compiler/semmagic.nim +++ b/compiler/semmagic.nim @@ -53,6 +53,8 @@ proc semTypeOf(c: PContext; n: PNode): PNode = let typExpr = semExprWithType(c, n[1], if m == 1: {efInTypeof} else: {}) dec c.inTypeofContext result.add typExpr + if typExpr.typ.kind == tyFromExpr: + typExpr.typ.flags.incl tfNonConstExpr result.typ = makeTypeDesc(c, typExpr.typ) type @@ -68,6 +70,15 @@ proc semArrGet(c: PContext; n: PNode; flags: TExprFlags): PNode = if result.isNil: let x = copyTree(n) x[0] = newIdentNode(getIdent(c.cache, "[]"), n.info) + if c.inGenericContext > 0: + for i in 0.. + proc byLent3[T](a: T): lent typeof(a[0]) = a[0] # ditto + proc byLent4[T](a: T): lent[type(a[0])] = a[0] # Error: no generic parameters allowed for lent + var x = @[1, 2, 3] + doAssert my(x) is int + doAssert my2(x) is array[sizeof(int), seq[int]] + doAssert byLent2(x) == 1 + doAssert byLent2(x) is lent int + doAssert byLent3(x) == 1 + doAssert byLent3(x) is lent int + doAssert byLent4(x) == 1 + doAssert byLent4(x) is lent int + proc fn[U](a: U): auto = a + proc my3[T](a: T, b: typeof(fn(a))) = discard + my3(x, x) + doAssert not compiles(my3(x, x[0])) + block: # issue #22342, type section version of #22607 type GenAlias[isInt: static bool] = ( when isInt: