Skip to content

Commit 5cecd81

Browse files
metagnnarimiran
authored andcommitted
fix semFinishOperands for bracket expressions [backport:2.0] (#23571)
fixes #23568, fixes #23310 In #23091 `semFinishOperands` was changed to not be called for `mArrGet` and `mArrPut`, presumably in preparation for #23188 (not sure why it was needed in #23091, maybe they got mixed together), since the compiler handles these later and needs the first argument to not be completely "typed" since brackets can serve as explicit generic instantiations in which case the first argument would have to be an unresolved generic proc (not accepted by `finishOperand`). In this PR we just make it so `mArrGet` and `mArrPut` specifically skip calling `finishOperand` on the first argument. This way the generic arguments in the explicit instantiation get typed, but not the unresolved generic proc. (cherry picked from commit 09bd9d0)
1 parent 526e48b commit 5cecd81

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

compiler/semexprs.nim

+8-7
Original file line numberDiff line numberDiff line change
@@ -1012,10 +1012,14 @@ proc finishOperand(c: PContext, a: PNode): PNode =
10121012
localError(c.config, a.info, err)
10131013
considerGenSyms(c, result)
10141014

1015-
proc semFinishOperands(c: PContext; n: PNode) =
1015+
proc semFinishOperands(c: PContext; n: PNode; isBracketExpr = false) =
10161016
# this needs to be called to ensure that after overloading resolution every
1017-
# argument has been sem'checked:
1018-
for i in 1..<n.len:
1017+
# argument has been sem'checked
1018+
1019+
# skip the first argument for operands of `[]` since it may be an unresolved
1020+
# generic proc, which is handled in semMagic
1021+
let start = 1 + ord(isBracketExpr)
1022+
for i in start..<n.len:
10191023
n[i] = finishOperand(c, n[i])
10201024

10211025
proc afterCallActions(c: PContext; n, orig: PNode, flags: TExprFlags; expectedType: PType = nil): PNode =
@@ -1038,10 +1042,7 @@ proc afterCallActions(c: PContext; n, orig: PNode, flags: TExprFlags; expectedTy
10381042
of skMacro: result = semMacroExpr(c, result, orig, callee, flags, expectedType)
10391043
of skTemplate: result = semTemplateExpr(c, result, callee, flags, expectedType)
10401044
else:
1041-
if callee.magic notin {mArrGet, mArrPut, mNBindSym}:
1042-
# calls to `[]` can be explicit generic instantiations,
1043-
# don't sem every operand now, leave it to semmagic
1044-
semFinishOperands(c, result)
1045+
semFinishOperands(c, result, isBracketExpr = callee.magic in {mArrGet, mArrPut})
10451046
activate(c, result)
10461047
fixAbstractType(c, result)
10471048
analyseIfAddressTakenInCall(c, result)

tests/generics/tnestedissues.nim

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
block: # issue #23568
2+
type G[T] = object
3+
j: T
4+
proc s[T](u: int) = discard
5+
proc s[T]() = discard
6+
proc c(e: int | int): G[G[G[int]]] = s[G[G[int]]]()
7+
discard c(0)
8+
9+
import std/options
10+
11+
block: # issue #23310
12+
type
13+
BID = string or uint64
14+
Future[T] = ref object of RootObj
15+
internalValue: T
16+
InternalRaisesFuture[T] = ref object of Future[T]
17+
proc newInternalRaisesFutureImpl[T](): InternalRaisesFuture[T] =
18+
let fut = InternalRaisesFuture[T]()
19+
template newFuture[T](): auto =
20+
newInternalRaisesFutureImpl[T]()
21+
proc problematic(blockId: BID): Future[Option[seq[int]]] =
22+
let resultFuture = newFuture[Option[seq[int]]]()
23+
return resultFuture
24+
let x = problematic("latest")

0 commit comments

Comments
 (0)