From 33458894da13da7134604639c129273bfb0cb1c1 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 9 Oct 2018 19:58:23 +0200 Subject: Fix overload resolution for pragmas evaluation (#8902) Fixes #6448 Fixes #4384 --- compiler/pragmas.nim | 4 ++-- compiler/semcall.nim | 13 ++++++------ compiler/semdata.nim | 5 ++++- compiler/semstmts.nim | 53 +++++++++++++++++++++++++++-------------------- tests/macros/tmsginfo.nim | 2 +- tests/pragmas/foobar.nim | 3 +++ tests/pragmas/t4384.nim | 3 +++ tests/pragmas/t6448.nim | 16 ++++++++++++++ 8 files changed, 66 insertions(+), 33 deletions(-) create mode 100644 tests/pragmas/foobar.nim create mode 100644 tests/pragmas/t4384.nim create mode 100644 tests/pragmas/t6448.nim diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index c4308fc6f..eda52ab02 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -723,13 +723,13 @@ proc semCustomPragma(c: PContext, n: PNode): PNode = elif n.kind == nkExprColonExpr: # pragma: arg -> pragma(arg) result = newTree(nkCall, n[0], n[1]) - elif n.kind in nkPragmaCallKinds + {nkIdent}: + elif n.kind in nkPragmaCallKinds: result = n else: invalidPragma(c, n) return n - let r = c.semOverloadedCall(c, result, n, {skTemplate}, {}) + let r = c.semOverloadedCall(c, result, n, {skTemplate}, {efNoUndeclared}) if r.isNil or sfCustomPragma notin r[0].sym.flags: invalidPragma(c, n) else: diff --git a/compiler/semcall.nim b/compiler/semcall.nim index 53f7045dd..71a9197b7 100644 --- a/compiler/semcall.nim +++ b/compiler/semcall.nim @@ -330,10 +330,11 @@ proc resolveOverloads(c: PContext, n, orig: PNode, pickBest(callOp) if overloadsState == csEmpty and result.state == csEmpty: - if nfDotField in n.flags and nfExplicitCall notin n.flags: - localError(c.config, n.info, errUndeclaredField % considerQuotedIdent(c, f, n).s) - else: - localError(c.config, n.info, errUndeclaredRoutine % considerQuotedIdent(c, f, n).s) + if efNoUndeclared notin flags: + if nfDotField in n.flags and nfExplicitCall notin n.flags: + localError(c.config, n.info, errUndeclaredField % considerQuotedIdent(c, f, n).s) + else: + localError(c.config, n.info, errUndeclaredRoutine % considerQuotedIdent(c, f, n).s) return elif result.state != csMatch: if nfExprCall in n.flags: @@ -500,14 +501,14 @@ proc semOverloadedCall(c: PContext, n, nOrig: PNode, # repeat the overload resolution, # this time enabling all the diagnostic output (this should fail again) discard semOverloadedCall(c, n, nOrig, filter, flags + {efExplain}) - else: + elif efNoUndeclared notin flags: notFoundError(c, n, errors) else: if efExplain notin flags: # repeat the overload resolution, # this time enabling all the diagnostic output (this should fail again) discard semOverloadedCall(c, n, nOrig, filter, flags + {efExplain}) - else: + elif efNoUndeclared notin flags: notFoundError(c, n, errors) proc explicitGenericInstError(c: PContext; n: PNode): PNode = diff --git a/compiler/semdata.nim b/compiler/semdata.nim index 6d6627690..bec4a59a4 100644 --- a/compiler/semdata.nim +++ b/compiler/semdata.nim @@ -65,7 +65,10 @@ type # to the user. efWantStmt, efAllowStmt, efDetermineType, efExplain, efAllowDestructor, efWantValue, efOperand, efNoSemCheck, - efNoEvaluateGeneric, efInCall, efFromHlo + efNoEvaluateGeneric, efInCall, efFromHlo, + efNoUndeclared + # Use this if undeclared identifiers should not raise an error during + # overload resolution. TExprFlags* = set[TExprFlag] diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 2817b5ef9..e02a38d8a 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -1218,13 +1218,6 @@ proc copyExcept(n: PNode, i: int): PNode = for j in 0..= 1: it.sons[0] else: it - let m = lookupMacro(c, key) - if m == nil: - if key.kind == nkIdent and key.ident.id == ord(wDelegator): - if considerQuotedIdent(c, prc.sons[namePos]).s == "()": - prc.sons[namePos] = newIdentNode(c.cache.idDelegator, prc.info) - prc.sons[pragmasPos] = copyExcept(n, i) - else: - localError(c.config, prc.info, "only a call operator can be a delegator") + + if whichPragma(it) != wInvalid: + # Not a custom pragma + continue + elif strTableGet(c.userPragmas, considerQuotedIdent(c, key)) != nil: + # User-defined pragma continue - elif sfCustomPragma in m.flags: - continue # semantic check for custom pragma happens later in semProcAux # we transform ``proc p {.m, rest.}`` into ``m(do: proc p {.rest.})`` and # let the semantic checker deal with it: - var x = newNodeI(nkCall, n.info) - x.add(newSymNode(m)) - prc.sons[pragmasPos] = copyExcept(n, i) - if prc[pragmasPos].kind != nkEmpty and prc[pragmasPos].len == 0: - prc.sons[pragmasPos] = c.graph.emptyNode + var x = newNodeI(nkCall, key.info) + x.add(key) if it.kind in nkPragmaCallKinds and it.len > 1: # pass pragma arguments to the macro too: for i in 1..