Skip to content

Commit

Permalink
fix subscript in generics, errors, lent with bracket
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Sep 6, 2024
1 parent 7cd1777 commit eeeb068
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions compiler/semmagic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ proc semTypeOf(c: PContext; n: PNode): PNode =
dec c.inTypeofContext
result.add typExpr
result.typ = makeTypeDesc(c, typExpr.typ)
if result.typ.kind == tyFromExpr:
result.typ.flags.incl tfNonConstExpr

type
SemAsgnMode = enum asgnNormal, noOverloadedSubscript, noOverloadedAsgn
Expand All @@ -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..<n.len:
let a = n[i]
if a.typ != nil and a.typ.kind in {tyGenericParam, tyFromExpr}:
# expression is compiled early in a generic body
result = semGenericStmt(c, x)
result.typ = makeTypeFromExpr(c, copyTree(result))
result.typ.flags.incl tfNonConstExpr
return
bracketNotFoundError(c, x, flags)
#localError(c.config, n.info, "could not resolve: " & $n)
result = errorNode(c, n)
Expand Down
10 changes: 10 additions & 0 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,8 @@ proc semTypeOf(c: PContext; n: PNode; prev: PType): PType =
closeScope(c)
fixupTypeOf(c, prev, t)
result = t.typ
if result.kind == tyFromExpr:
result.flags.incl tfNonConstExpr

proc semTypeOf2(c: PContext; n: PNode; prev: PType): PType =
openScope(c)
Expand All @@ -1880,6 +1882,8 @@ proc semTypeOf2(c: PContext; n: PNode; prev: PType): PType =
closeScope(c)
fixupTypeOf(c, prev, t)
result = t.typ
if result.kind == tyFromExpr:
result.flags.incl tfNonConstExpr

proc semTypeIdent(c: PContext, n: PNode): PSym =
if n.kind == nkSym:
Expand Down Expand Up @@ -2116,6 +2120,12 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType =
of mRef: result = semAnyRef(c, n, tyRef, prev)
of mPtr: result = semAnyRef(c, n, tyPtr, prev)
of mTuple: result = semTuple(c, n, prev)
of mBuiltinType:
case s.name.s
of "lent": result = semAnyRef(c, n, tyLent, prev)
of "sink": result = semAnyRef(c, n, tySink, prev)
of "owned": result = semAnyRef(c, n, tyOwned, prev)
else: result = semGeneric(c, n, s, prev)
else: result = semGeneric(c, n, s, prev)
of nkDotExpr:
let typeExpr = semExpr(c, n)
Expand Down
12 changes: 12 additions & 0 deletions tests/generics/tuninstantiatedgenericcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,18 @@ block: # issue #24044
type MyBuf[I] = ArrayBuf[maxLen(I)]
var v: MyBuf[int]

block: # issue #15959
proc byLent2[T](a: T): lent type(a[0]) = a[0] # Error: type mismatch: got <T, int literal(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 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

block: # issue #22342, type section version of #22607
type GenAlias[isInt: static bool] = (
when isInt:
Expand Down

0 comments on commit eeeb068

Please sign in to comment.