macros: make hasCustomPragma more permissive (#19747)

Make hasCustomPragma return false rather than fail for invalid
parameters.
This commit is contained in:
ehmry 2022-04-25 15:16:11 -05:00 • committed by GitHub
commit 82680a12a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1521,7 +1521,7 @@ proc extractTypeImpl(n: NimNode): NimNode =
else: error("Invalid node to retrieve type implementation of: " & $n.kind) else: error("Invalid node to retrieve type implementation of: " & $n.kind)
proc customPragmaNode(n: NimNode): NimNode = proc customPragmaNode(n: NimNode): NimNode =
expectKind(n, {nnkSym, nnkDotExpr, nnkBracketExpr, nnkTypeOfExpr, nnkCheckedFieldExpr}) expectKind(n, {nnkSym, nnkDotExpr, nnkBracketExpr, nnkTypeOfExpr, nnkType, nnkCheckedFieldExpr})
let let
typ = n.getTypeInst() typ = n.getTypeInst()
@ -1532,7 +1532,9 @@ proc customPragmaNode(n: NimNode): NimNode =
if kind(typ[1]) == nnkBracketExpr: typ[1][0] if kind(typ[1]) == nnkBracketExpr: typ[1][0]
else: typ[1] else: typ[1]
) )
if impl[0].kind == nnkPragmaExpr: if impl.kind == nnkNilLit:
return impl
elif impl[0].kind == nnkPragmaExpr:
return impl[0][1] return impl[0][1]
else: else:
return impl[0] # handle types which don't have macro at all return impl[0] # handle types which don't have macro at all
@ -1560,7 +1562,7 @@ proc customPragmaNode(n: NimNode): NimNode =
while typDef != nil: while typDef != nil:
typDef.expectKind(nnkTypeDef) typDef.expectKind(nnkTypeDef)
let typ = typDef[2].extractTypeImpl() let typ = typDef[2].extractTypeImpl()
typ.expectKind({nnkRefTy, nnkPtrTy, nnkObjectTy}) if typ.kind notin {nnkRefTy, nnkPtrTy, nnkObjectTy}: break
let isRef = typ.kind in {nnkRefTy, nnkPtrTy} let isRef = typ.kind in {nnkRefTy, nnkPtrTy}
if isRef and typ[0].kind in {nnkSym, nnkBracketExpr}: # defines ref type for another object(e.g. X = ref X) if isRef and typ[0].kind in {nnkSym, nnkBracketExpr}: # defines ref type for another object(e.g. X = ref X)
typDef = getImpl(typ[0]) typDef = getImpl(typ[0])