Skip to content

Commit 1610444

Browse files
metagnnarimiran
authored andcommitted
cascade tyFromExpr in type conversions in generic bodies (#22499)
fixes #22490, fixes #22491, adapts #22029 to type conversions (cherry picked from commit 98c39e8)
1 parent 646c4d1 commit 1610444

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

compiler/semexprs.nim

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ proc isOwnedSym(c: PContext; n: PNode): bool =
311311
let s = qualifiedLookUp(c, n, {})
312312
result = s != nil and sfSystemModule in s.owner.flags and s.name.s == "owned"
313313

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

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

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

10731075
let nOrig = n.copyTree
10741076
semOpAux(c, n)
@@ -3110,7 +3112,7 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}, expectedType: PType
31103112
# XXX think about this more (``set`` procs)
31113113
let ambig = c.isAmbiguous
31123114
if not (n[0].kind in {nkClosedSymChoice, nkOpenSymChoice, nkIdent} and ambig) and n.len == 2:
3113-
result = semConv(c, n, expectedType)
3115+
result = semConv(c, n, flags, expectedType)
31143116
elif ambig and n.len == 1:
31153117
errorUseQualifier(c, n.info, s)
31163118
elif n.len == 1:

tests/statictypes/tgenericcomputedrange.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,11 @@ block: # issue #22187
115115
k: array[p(m(T, s)), int64]
116116
var x: F[int, 3]
117117
doAssert x.k is array[3, int64]
118+
119+
block: # issue #22490
120+
proc log2trunc(x: uint64): int =
121+
if x == 0: int(0) else: int(0)
122+
template maxChunkIdx(T: typedesc): int64 = 0'i64
123+
template layer(vIdx: int64): int = log2trunc(0'u64)
124+
type HashList[T] = object
125+
indices: array[int(layer(maxChunkIdx(T))), int]

0 commit comments

Comments
 (0)