Skip to content

Commit

Permalink
fixes #15221 (#15230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq authored Aug 27, 2020
1 parent a9a9860 commit ccccd30
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/impure/nre.nim
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func contains*(pattern: CaptureBounds, name: string): bool =
func contains*(pattern: Captures, name: string): bool =
name in CaptureBounds(pattern)

func checkNamedCaptured(pattern: RegexMatch, name: string): void =
func checkNamedCaptured(pattern: RegexMatch, name: string) =
if not (name in pattern.captureBounds):
raise newException(KeyError, "Group '" & name & "' was not captured")

Expand Down
90 changes: 44 additions & 46 deletions lib/pure/typetraits.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,54 +115,52 @@ macro genericParamsImpl(T: typedesc): untyped =
impl = impl[1]
while true:
case impl.kind
of nnkSym:
impl = impl.getImpl
continue
of nnkTypeDef:
impl = impl[2]
continue
of nnkTypeOfExpr:
impl = getTypeInst(impl[0])
continue
of nnkBracketExpr:
for i in 1..<impl.len:
let ai = impl[i]
var ret: NimNode = nil
case ai.typeKind
of ntyTypeDesc:
of nnkSym:
impl = impl.getImpl
of nnkTypeDef:
impl = impl[2]
of nnkTypeOfExpr:
impl = getTypeInst(impl[0])
of nnkBracketExpr:
for i in 1..<impl.len:
let ai = impl[i]
var ret: NimNode = nil
case ai.typeKind
of ntyTypeDesc:
ret = ai
of ntyStatic: doAssert false
else:
# getType from a resolved symbol might return a typedesc symbol.
# If so, use it directly instead of wrapping it in StaticParam.
if (ai.kind == nnkSym and ai.symKind == nskType) or
(ai.kind == nnkBracketExpr and ai[0].kind == nnkSym and
ai[0].symKind == nskType) or ai.kind in {nnkRefTy, nnkVarTy, nnkPtrTy, nnkProcTy}:
ret = ai
of ntyStatic: doAssert false
elif ai.kind == nnkInfix and ai[0].kind == nnkIdent and
ai[0].strVal == "..":
# For built-in array types, the "2" is translated to "0..1" then
# automagically translated to "range[0..1]". However this is not
# reflected in the AST, thus requiring manual transformation here.
#
# We will also be losing some context here:
# var a: array[10, int]
# will be translated to:
# var a: array[0..9, int]
# after typecheck. This means that we can't get the exact
# definition as typed by the user, which will cause confusion for
# users expecting:
# genericParams(typeof(a)) is (StaticParam(10), int)
# to be true while in fact the result will be:
# genericParams(typeof(a)) is (range[0..9], int)
ret = newTree(nnkBracketExpr, @[bindSym"range", ai])
else:
# getType from a resolved symbol might return a typedesc symbol.
# If so, use it directly instead of wrapping it in StaticParam.
if (ai.kind == nnkSym and ai.symKind == nskType) or
(ai.kind == nnkBracketExpr and ai[0].kind == nnkSym and
ai[0].symKind == nskType):
ret = ai
elif ai.kind == nnkInfix and ai[0].kind == nnkIdent and
ai[0].strVal == "..":
# For built-in array types, the "2" is translated to "0..1" then
# automagically translated to "range[0..1]". However this is not
# reflected in the AST, thus requiring manual transformation here.
#
# We will also be losing some context here:
# var a: array[10, int]
# will be translated to:
# var a: array[0..9, int]
# after typecheck. This means that we can't get the exact
# definition as typed by the user, which will cause confusion for
# users expecting:
# genericParams(typeof(a)) is (StaticParam(10), int)
# to be true while in fact the result will be:
# genericParams(typeof(a)) is (range[0..9], int)
ret = newTree(nnkBracketExpr, @[bindSym"range", ai])
else:
since (1, 1):
ret = newTree(nnkBracketExpr, @[bindSym"StaticParam", ai])
result.add ret
break
else:
error "wrong kind: " & $impl.kind, impl
since (1, 1):
echo ai.typeKind
ret = newTree(nnkBracketExpr, @[bindSym"StaticParam", ai])
result.add ret
break
else:
error "wrong kind: " & $impl.kind, impl

since (1, 1):
template genericParams*(T: typedesc): untyped =
Expand Down

0 comments on commit ccccd30

Please sign in to comment.