Skip to content

Commit

Permalink
cascade tyFromExpr in type conversions in generic bodies (#22499)
Browse files Browse the repository at this point in the history
fixes #22490, fixes #22491, adapts #22029 to type conversions

(cherry picked from commit 98c39e8)
  • Loading branch information
metagn authored and narimiran committed Apr 17, 2024
1 parent 646c4d1 commit 1610444
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 7 additions & 5 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ proc isOwnedSym(c: PContext; n: PNode): bool =
let s = qualifiedLookUp(c, n, {})
result = s != nil and sfSystemModule in s.owner.flags and s.name.s == "owned"

proc semConv(c: PContext, n: PNode; expectedType: PType = nil): PNode =
proc semConv(c: PContext, n: PNode; flags: TExprFlags = {}, expectedType: PType = nil): PNode =
if n.len != 2:
localError(c.config, n.info, "a type conversion takes exactly one argument")
return n
Expand Down Expand Up @@ -358,7 +358,7 @@ proc semConv(c: PContext, n: PNode; expectedType: PType = nil): PNode =
if n[1].kind == nkExprEqExpr and
targetType.skipTypes(abstractPtrs).kind == tyObject:
localError(c.config, n.info, "object construction uses ':', not '='")
var op = semExprWithType(c, n[1])
var op = semExprWithType(c, n[1], flags * {efDetermineType})
if op.kind == nkClosedSymChoice and op.len > 0 and
op[0].sym.kind == skEnumField: # resolves overloadedable enums
op = ambiguousSymChoice(c, n, op)
Expand All @@ -373,7 +373,9 @@ proc semConv(c: PContext, n: PNode; expectedType: PType = nil): PNode =
# here or needs to be overwritten too then.
result.add op

if targetType.kind == tyGenericParam:
if targetType.kind == tyGenericParam or
(op.typ != nil and op.typ.kind == tyFromExpr and c.inGenericContext > 0):
# expression is compiled early in a generic body
result.typ = makeTypeFromExpr(c, copyTree(result))
return result

Expand Down Expand Up @@ -1068,7 +1070,7 @@ proc semIndirectOp(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType
t = skipTypes(n[0].typ, abstractInst+{tyOwned}-{tyTypeDesc, tyDistinct})
if t != nil and t.kind == tyTypeDesc:
if n.len == 1: return semObjConstr(c, n, flags, expectedType)
return semConv(c, n)
return semConv(c, n, flags)

let nOrig = n.copyTree
semOpAux(c, n)
Expand Down Expand Up @@ -3110,7 +3112,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}, expectedType: PType
# XXX think about this more (``set`` procs)
let ambig = c.isAmbiguous
if not (n[0].kind in {nkClosedSymChoice, nkOpenSymChoice, nkIdent} and ambig) and n.len == 2:
result = semConv(c, n, expectedType)
result = semConv(c, n, flags, expectedType)
elif ambig and n.len == 1:
errorUseQualifier(c, n.info, s)
elif n.len == 1:
Expand Down
8 changes: 8 additions & 0 deletions tests/statictypes/tgenericcomputedrange.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,11 @@ block: # issue #22187
k: array[p(m(T, s)), int64]
var x: F[int, 3]
doAssert x.k is array[3, int64]

block: # issue #22490
proc log2trunc(x: uint64): int =
if x == 0: int(0) else: int(0)
template maxChunkIdx(T: typedesc): int64 = 0'i64
template layer(vIdx: int64): int = log2trunc(0'u64)
type HashList[T] = object
indices: array[int(layer(maxChunkIdx(T))), int]

0 comments on commit 1610444

Please sign in to comment.