diff --git a/changelog.md b/changelog.md index e905dd941b..3144c7cf54 100644 --- a/changelog.md +++ b/changelog.md @@ -304,10 +304,13 @@ proc mydiv(a, b): int {.raises: [].} = - `system.deepcopy` has to be enabled explicitly for `--gc:arc` and `--gc:orc` via `--deepcopy:on`. -- Added a `std/effecttraits` module for introspection of the inferred `.raise` effects. +- Added a `std/effecttraits` module for introspection of the inferred effects. We hope this enables `async` macros that are precise about the possible exceptions that can be raised. - Added `critbits.toCritBitTree`, similar to `tables.toTable`, creates a new `CritBitTree` with given arguments. +- The pragma blocks `{.gcsafe.}: ...` and `{.noSideEffect.}: ...` can now also be + written as `{.cast(gcsafe).}: ...` and `{.cast(noSideEffect).}: ...`. This is the new + preferred way of writing these, emphasizing their unsafe nature. ## Compiler changes diff --git a/compiler/condsyms.nim b/compiler/condsyms.nim index 7dc1d183eb..9771a48d3d 100644 --- a/compiler/condsyms.nim +++ b/compiler/condsyms.nim @@ -121,3 +121,5 @@ proc initDefines*(symbols: StringTableRef) = defineSymbol("nimHasLentIterators") defineSymbol("nimHasDeclaredMagic") defineSymbol("nimHasStacktracesModule") + defineSymbol("nimHasEffectTraitsModule") + defineSymbol("nimHasCastPragmaBlocks") diff --git a/compiler/parser.nim b/compiler/parser.nim index 1ec7080573..e3cf54b38d 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -485,17 +485,24 @@ proc setOrTableConstr(p: var Parser): PNode = eat(p, tkCurlyRi) # skip '}' proc parseCast(p: var Parser): PNode = - #| castExpr = 'cast' '[' optInd typeDesc optPar ']' '(' optInd expr optPar ')' + #| castExpr = 'cast' ('[' optInd typeDesc optPar ']' '(' optInd expr optPar ')') / + # ('(' optInd exprColonEqExpr optPar ')') result = newNodeP(nkCast, p) getTok(p) - eat(p, tkBracketLe) - optInd(p, result) - result.add(parseTypeDesc(p)) - optPar(p) - eat(p, tkBracketRi) - eat(p, tkParLe) - optInd(p, result) - result.add(parseExpr(p)) + if p.tok.tokType == tkBracketLe: + getTok(p) + optInd(p, result) + result.add(parseTypeDesc(p)) + optPar(p) + eat(p, tkBracketRi) + eat(p, tkParLe) + optInd(p, result) + result.add(parseExpr(p)) + else: + result.add p.emptyNode + eat(p, tkParLe) + optInd(p, result) + result.add(exprColonEqExpr(p)) optPar(p) eat(p, tkParRi) diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 67e574d522..fe8cd3cfe6 100644 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -771,6 +771,15 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int, if key.kind == nkBracketExpr: processNote(c, it) return + elif key.kind == nkCast: + if comesFromPush: + localError(c.config, n.info, "a 'cast' pragma cannot be pushed") + elif not isStatement: + localError(c.config, n.info, "'cast' pragma only allowed in a statement context") + case whichPragma(key[1]) + of wRaises, wTags: pragmaRaisesOrTags(c, key[1]) + else: discard + return elif key.kind notin nkIdentKinds: n[i] = semCustomPragma(c, it) return diff --git a/compiler/renderer.nim b/compiler/renderer.nim index 88b9adf27a..08012bb55d 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -1057,9 +1057,10 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext) = put(g, tkSymbol, "(wrong conv)") of nkCast: put(g, tkCast, "cast") - put(g, tkBracketLe, "[") - gsub(g, n, 0) - put(g, tkBracketRi, "]") + if n.len > 0 and n[0].kind != nkEmpty: + put(g, tkBracketLe, "[") + gsub(g, n, 0) + put(g, tkBracketRi, "]") put(g, tkParLe, "(") gsub(g, n, 1) put(g, tkParRi, ")") diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index cda156224a..19223b88fa 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -325,7 +325,7 @@ proc createTag(g: ModuleGraph; n: PNode): PNode = result.typ = g.sysTypeFromName(n.info, "RootEffect") if not n.isNil: result.info = n.info -proc addEffect(a: PEffects, e, comesFrom: PNode) = +proc addRaiseEffect(a: PEffects, e, comesFrom: PNode) = assert e.kind != nkRaiseStmt var aa = a.exc for i in a.bottom.. 0: n[0] else: n - if key.kind == nkIdent: result = whichKeyword(key.ident) + case key.kind + of nkIdent: result = whichKeyword(key.ident) + of nkSym: result = whichKeyword(key.sym.name) + of nkCast: result = wCast + of nkClosedSymChoice, nkOpenSymChoice: + result = whichPragma(key[0]) + else: result = wInvalid + +proc isNoSideEffectPragma*(n: PNode): bool = + var k = whichPragma(n) + if k == wCast: + k = whichPragma(n[1]) + result = k == wNoSideEffect proc findPragma*(n: PNode, which: TSpecialWord): PNode = if n.kind == nkPragma: diff --git a/compiler/varpartitions.nim b/compiler/varpartitions.nim index e59c5cb751..a172a906e2 100644 --- a/compiler/varpartitions.nim +++ b/compiler/varpartitions.nim @@ -29,8 +29,7 @@ ## for a high-level description of how borrow checking works. import ast, types, lineinfos, options, msgs, renderer, typeallowed -from trees import getMagic, whichPragma, stupidStmtListExpr -from wordrecg import wNoSideEffect +from trees import getMagic, isNoSideEffectPragma, stupidStmtListExpr from isolation_check import canAlias type @@ -713,7 +712,7 @@ proc traverse(c: var Partitions; n: PNode) = let pragmaList = n[0] var enforceNoSideEffects = 0 for i in 0..= effectListLen and - fn.typ.n[0][exceptionEffects] != nil: + fn.typ.n[0][effectIndex] != nil: var list = newNodeI(nkBracket, fn.info) - for e in fn.typ.n[0][exceptionEffects]: + for e in fn.typ.n[0][effectIndex]: list.add opMapTypeInstToAst(c.cache, e.typ.skipTypes({tyRef}), e.info) setResult(a, list) + + registerCallback c, "stdlib.effecttraits.getRaisesListImpl", proc (a: VmArgs) = + getEffectList(c, a, exceptionEffects) + registerCallback c, "stdlib.effecttraits.getTagsListImpl", proc (a: VmArgs) = + getEffectList(c, a, tagEffects) + + registerCallback c, "stdlib.effecttraits.isGcSafeImpl", proc (a: VmArgs) = + let fn = getNode(a, 0) + setResult(a, fn.typ != nil and tfGcSafe in fn.typ.flags) + + registerCallback c, "stdlib.effecttraits.hasNoSideEffectsImpl", proc (a: VmArgs) = + let fn = getNode(a, 0) + setResult(a, (fn.typ != nil and tfNoSideEffect in fn.typ.flags) or + (fn.kind == nkSym and fn.sym.kind == skFunc)) diff --git a/doc/grammar.txt b/doc/grammar.txt index 9d952d3721..f5e619ebcd 100644 --- a/doc/grammar.txt +++ b/doc/grammar.txt @@ -6,7 +6,7 @@ colon = ':' COMMENT? colcom = ':' COMMENT? operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9 | 'or' | 'xor' | 'and' - | 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'from' | + | 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'from' | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..' prefixOperator = operator optInd = COMMENT? IND? @@ -31,15 +31,15 @@ dotExpr = expr '.' optInd (symbol | '[:' exprList ']') explicitGenericInstantiation = '[:' exprList ']' ( '(' exprColonEqExpr ')' )? qualifiedIdent = symbol ('.' optInd symbol)? setOrTableConstr = '{' ((exprColonEqExpr comma)* | ':' ) '}' -castExpr = 'cast' '[' optInd typeDesc optPar ']' '(' optInd expr optPar ')' +castExpr = 'cast' ('[' optInd typeDesc optPar ']' '(' optInd expr optPar ')') / parKeyw = 'discard' | 'include' | 'if' | 'while' | 'case' | 'try' | 'finally' | 'except' | 'for' | 'block' | 'const' | 'let' | 'when' | 'var' | 'mixin' par = '(' optInd - ( &parKeyw complexOrSimpleStmt ^+ ';' - | ';' complexOrSimpleStmt ^+ ';' + ( &parKeyw (ifExpr \ complexOrSimpleStmt) ^+ ';' + | ';' (ifExpr \ complexOrSimpleStmt) ^+ ';' | pragmaStmt - | simpleExpr ( ('=' expr (';' complexOrSimpleStmt ^+ ';' )? ) + | simpleExpr ( ('=' expr (';' (ifExpr \ complexOrSimpleStmt) ^+ ';' )? ) | (':' expr (',' exprColonEqExpr ^+ ',' )? ) ) ) optPar ')' literal = | INT_LIT | INT8_LIT | INT16_LIT | INT32_LIT | INT64_LIT @@ -59,11 +59,6 @@ primarySuffix = '(' (exprColonEqExpr comma?)* ')' | '[' optInd exprColonEqExprList optPar ']' | '{' optInd exprColonEqExprList optPar '}' | &( '`'|IDENT|literal|'cast'|'addr'|'type') expr # command syntax -condExpr = expr colcom expr optInd - ('elif' expr colcom expr optInd)* - 'else' colcom expr -ifExpr = 'if' condExpr -whenExpr = 'when' condExpr pragma = '{.' optInd (exprColonEqExpr comma?)* optPar ('.}' | '}') identVis = symbol OPR? # postfix position identVisDot = symbol '.' optInd symbol OPR? @@ -130,6 +125,11 @@ condStmt = expr colcom stmt COMMENT? (IND{=} 'else' colcom stmt)? ifStmt = 'if' condStmt whenStmt = 'when' condStmt +condExpr = expr colcom expr optInd + ('elif' expr colcom expr optInd)* + 'else' colcom expr +ifExpr = 'if' condExpr +whenExpr = 'when' condExpr whileStmt = 'while' expr colcom stmt ofBranch = 'of' exprList colcom stmt ofBranches = ofBranch (IND{=} ofBranch)* @@ -193,8 +193,8 @@ complexOrSimpleStmt = (ifStmt | whenStmt | whileStmt | tryStmt | forStmt | blockStmt | staticStmt | deferStmt | asmStmt | 'proc' routine - | 'func' routine | 'method' routine + | 'func' routine | 'iterator' routine | 'macro' routine | 'template' routine diff --git a/doc/manual.rst b/doc/manual.rst index 11ea6b99cc..494a009cbd 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -6031,12 +6031,12 @@ so that it can be used for debugging routines marked as ``noSideEffect``. To override the compiler's side effect analysis a ``{.noSideEffect.}`` -pragma block can be used: +``cast`` pragma block can be used: .. code-block:: nim func f() = - {.noSideEffect.}: + {.cast(noSideEffect).}: echo "test" @@ -7501,7 +7501,7 @@ To disable the GC-safety checking the ``--threadAnalysis:off`` command line switch can be used. This is a temporary workaround to ease the porting effort from old code to the new threading model. -To override the compiler's gcsafety analysis a ``{.gcsafe.}`` pragma block can +To override the compiler's gcsafety analysis a ``{.cast(gcsafe).}`` pragma block can be used: .. code-block:: nim @@ -7511,7 +7511,7 @@ be used: perThread {.threadvar.}: string proc setPerThread() = - {.gcsafe.}: + {.cast(gcsafe).}: deepCopy(perThread, someGlobal) diff --git a/lib/std/effecttraits.nim b/lib/std/effecttraits.nim index a5d7997866..0f0a244926 100644 --- a/lib/std/effecttraits.nim +++ b/lib/std/effecttraits.nim @@ -1,7 +1,7 @@ # # # Nim's Runtime Library -# (c) Copyright 2018 Nim contributors +# (c) Copyright 2020 Nim contributors # # See the file "copying.txt", included in this # distribution, for details about the copyright. @@ -9,11 +9,46 @@ ## This module provides access to the inferred .raises effects ## for Nim's macro system. +## **Since**: Version 1.4. +## +## One can test for the existance of this standard module +## via ``defined(nimHasEffectTraitsModule)``. import macros proc getRaisesListImpl(n: NimNode): NimNode = discard "see compiler/vmops.nim" +proc getTagsListImpl(n: NimNode): NimNode = discard "see compiler/vmops.nim" +proc isGcSafeImpl(n: NimNode): bool = discard "see compiler/vmops.nim" +proc hasNoSideEffectsImpl(n: NimNode): bool = discard "see compiler/vmops.nim" -proc getRaisesList*(call: NimNode): NimNode = - expectKind call, nnkCallKinds - result = getRaisesListImpl(call[0]) +proc getRaisesList*(fn: NimNode): NimNode = + ## Extracts the ``.raises`` list of the func/proc/etc ``fn``. + ## ``fn`` has to be a resolved symbol of kind ``nnkSym``. This + ## implies that the macro that calls this proc should accept ``typed`` + ## arguments and not ``untyped`` arguments. + expectKind fn, nnkSym + result = getRaisesListImpl(fn) + +proc getTagsList*(fn: NimNode): NimNode = + ## Extracts the ``.tags`` list of the func/proc/etc ``fn``. + ## ``fn`` has to be a resolved symbol of kind ``nnkSym``. This + ## implies that the macro that calls this proc should accept ``typed`` + ## arguments and not ``untyped`` arguments. + expectKind fn, nnkSym + result = getTagsListImpl(fn) + +proc isGcSafe*(fn: NimNode): bool = + ## Return true if the func/proc/etc ``fn`` is `gcsafe`. + ## ``fn`` has to be a resolved symbol of kind ``nnkSym``. This + ## implies that the macro that calls this proc should accept ``typed`` + ## arguments and not ``untyped`` arguments. + expectKind fn, nnkSym + result = isGcSafeImpl(fn) + +proc hasNoSideEffects*(fn: NimNode): bool = + ## Return true if the func/proc/etc ``fn`` has `noSideEffect`. + ## ``fn`` has to be a resolved symbol of kind ``nnkSym``. This + ## implies that the macro that calls this proc should accept ``typed`` + ## arguments and not ``untyped`` arguments. + expectKind fn, nnkSym + result = hasNoSideEffectsImpl(fn) diff --git a/tests/effects/tcast_as_pragma.nim b/tests/effects/tcast_as_pragma.nim new file mode 100644 index 0000000000..1de61333e2 --- /dev/null +++ b/tests/effects/tcast_as_pragma.nim @@ -0,0 +1,18 @@ +discard """ + cmd: "nim c $file" + action: "compile" +""" + +proc taggy() {.tags: RootEffect.} = discard + +proc m {.raises: [], tags: [].} = + {.cast(noSideEffect).}: + echo "hi" + + {.cast(raises: []).}: + raise newException(ValueError, "bah") + + {.cast(tags: []).}: + taggy() + +m() diff --git a/tests/macros/tgetraiseslist.nim b/tests/macros/tgetraiseslist.nim index de7ba06c8f..79694a66f7 100644 --- a/tests/macros/tgetraiseslist.nim +++ b/tests/macros/tgetraiseslist.nim @@ -1,5 +1,7 @@ discard """ - nimout: '''##[ValueError, Gen[string]]##''' + nimout: '''##[ValueError, Gen[string]]## +%%[RootEffect]%% +true true''' """ import macros @@ -10,13 +12,18 @@ type x: T macro m(call: typed): untyped = - echo "##", repr getRaisesList(call), "##" + echo "##", repr getRaisesList(call[0]), "##" + echo "%%", repr getTagsList(call[0]), "%%" + echo isGcSafe(call[0]), " ", hasNoSideEffects(call[0]) result = call +proc gutenTag() {.tags: RootEffect.} = discard + proc r(inp: int) = if inp == 0: raise newException(ValueError, "bah") elif inp == 1: raise newException(Gen[string], "bahB") + gutenTag() m r(2)