From 0d23419e681c4ce561ec43dc4a66a0ed40b8009e Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Thu, 6 Oct 2022 17:08:41 +0200 Subject: DAA and 'out' parameters (#20506) * DAA and 'out' parameters * progress * documented strictDefs and out parameters * docs, tests and a bugfix * fixes silly regression --- compiler/ast.nim | 9 ++++++--- compiler/condsyms.nim | 2 ++ compiler/dfa.nim | 7 +++---- compiler/jsgen.nim | 4 ++-- compiler/lineinfos.nim | 2 +- compiler/nim.nim | 2 +- compiler/options.nim | 3 ++- compiler/parser.nim | 5 +---- compiler/renderer.nim | 12 +++++++++--- compiler/sempass2.nim | 38 ++++++++++++++++++++++---------------- compiler/semtypes.nim | 10 ++++++---- compiler/sigmatch.nim | 27 +++++++++++++++++++-------- compiler/types.nim | 4 ++-- 13 files changed, 76 insertions(+), 49 deletions(-) (limited to 'compiler') diff --git a/compiler/ast.nim b/compiler/ast.nim index 3f567cc58..2a9556693 100644 --- a/compiler/ast.nim +++ b/compiler/ast.nim @@ -207,7 +207,7 @@ type nkPtrTy, # ``ptr T`` nkVarTy, # ``var T`` nkConstTy, # ``const T`` - nkMutableTy, # ``mutable T`` + nkOutTy, # ``out T`` nkDistinctTy, # distinct type nkProcTy, # proc type nkIteratorTy, # iterator type @@ -513,7 +513,7 @@ type nfUseDefaultField # node has a default value (object constructor) TNodeFlags* = set[TNodeFlag] - TTypeFlag* = enum # keep below 32 for efficiency reasons (now: 45) + TTypeFlag* = enum # keep below 32 for efficiency reasons (now: 46) tfVarargs, # procedure has C styled varargs # tyArray type represeting a varargs list tfNoSideEffect, # procedure type does not allow side effects @@ -582,6 +582,7 @@ type tfExplicitCallConv tfIsConstructor tfEffectSystemWorkaround + tfIsOutParam TTypeFlags* = set[TTypeFlag] @@ -632,7 +633,7 @@ const skError* = skUnknown var - eqTypeFlags* = {tfIterator, tfNotNil, tfVarIsPtr, tfGcSafe, tfNoSideEffect} + eqTypeFlags* = {tfIterator, tfNotNil, tfVarIsPtr, tfGcSafe, tfNoSideEffect, tfIsOutParam} ## type flags that are essential for type equality. ## This is now a variable because for emulation of version:1.0 we ## might exclude {tfGcSafe, tfNoSideEffect}. @@ -2129,6 +2130,8 @@ proc isNewStyleConcept*(n: PNode): bool {.inline.} = assert n.kind == nkTypeClassTy result = n[0].kind == nkEmpty +proc isOutParam*(t: PType): bool {.inline.} = tfIsOutParam in t.flags + const nodesToIgnoreSet* = {nkNone..pred(nkSym), succ(nkSym)..nkNilLit, nkTypeSection, nkProcDef, nkConverterDef, diff --git a/compiler/condsyms.nim b/compiler/condsyms.nim index c4f4d8290..bab8ff5e3 100644 --- a/compiler/condsyms.nim +++ b/compiler/condsyms.nim @@ -145,3 +145,5 @@ proc initDefines*(symbols: StringTableRef) = defineSymbol("nimHasCstringCase") defineSymbol("nimHasCallsitePragma") defineSymbol("nimHasAmbiguousEnumHint") + + defineSymbol("nimHasOutParams") diff --git a/compiler/dfa.nim b/compiler/dfa.nim index 57fd3ae2b..001fcdbe7 100644 --- a/compiler/dfa.nim +++ b/compiler/dfa.nim @@ -381,10 +381,9 @@ proc genCall(c: var Con; n: PNode) = if t != nil: t = t.skipTypes(abstractInst) for i in 1.. 0 and canRaiseConservative(n[0]): # we generate the instruction sequence: diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index fd380e35c..5d90af920 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -597,7 +597,7 @@ template unaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) = proc arithAux(p: PProc, n: PNode, r: var TCompRes, op: TMagic) = var x, y: TCompRes - xLoc,yLoc: Rope + xLoc, yLoc: Rope let i = ord(optOverflowCheck notin p.options) useMagic(p, jsMagics[op][i]) if n.len > 2: @@ -614,7 +614,7 @@ proc arithAux(p: PProc, n: PNode, r: var TCompRes, op: TMagic) = template applyFormat(frmtA, frmtB) = if i == 0: applyFormat(frmtA) else: applyFormat(frmtB) - case op: + case op of mAddI: applyFormat("addInt($1, $2)", "($1 + $2)") of mSubI: applyFormat("subInt($1, $2)", "($1 - $2)") of mMulI: applyFormat("mulInt($1, $2)", "($1 * $2)") diff --git a/compiler/lineinfos.nim b/compiler/lineinfos.nim index 05f4eba5a..fcc15606d 100644 --- a/compiler/lineinfos.nim +++ b/compiler/lineinfos.nim @@ -234,7 +234,7 @@ type proc computeNotesVerbosity(): array[0..3, TNoteKinds] = result[3] = {low(TNoteKind)..high(TNoteKind)} - {warnObservableStores, warnResultUsed, warnAnyEnumConv} - result[2] = result[3] - {hintStackTrace, warnUninit, hintExtendedContext, hintDeclaredLoc, hintProcessingStmt} + result[2] = result[3] - {hintStackTrace, hintExtendedContext, hintDeclaredLoc, hintProcessingStmt} result[1] = result[2] - {warnProveField, warnProveIndex, warnGcUnsafe, hintPath, hintDependency, hintCodeBegin, hintCodeEnd, hintSource, hintGlobalVar, hintGCStats, hintMsgOrigin, hintPerformance} diff --git a/compiler/nim.nim b/compiler/nim.nim index b98a514d7..c2b70dda2 100644 --- a/compiler/nim.nim +++ b/compiler/nim.nim @@ -117,7 +117,7 @@ proc handleCmdLine(cache: IdentCache; conf: ConfigRef) = case conf.cmd of cmdBackends, cmdTcc: let nimRunExe = getNimRunExe(conf) - var cmdPrefix: string + var cmdPrefix = "" if nimRunExe.len > 0: cmdPrefix.add nimRunExe.quoteShell case conf.backend of backendC, backendCpp, backendObjc: discard diff --git a/compiler/options.nim b/compiler/options.nim index 94776cc59..ce3b33355 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -216,7 +216,8 @@ type overloadableEnums, # deadcode strictEffects, unicodeOperators, # deadcode - flexibleOptionalParams + flexibleOptionalParams, + strictDefs LegacyFeature* = enum allowSemcheckedAstModification, diff --git a/compiler/parser.nim b/compiler/parser.nim index 2c514ee74..990f7dfef 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -1340,10 +1340,7 @@ proc primary(p: var Parser, mode: PrimaryMode): PNode = optInd(p, result) result.add(primary(p, pmNormal)) of tkVar: result = parseTypeDescKAux(p, nkVarTy, mode) - of tkOut: - # I like this parser extension to be in 1.4 as it still might turn out - # useful in the long run. - result = parseTypeDescKAux(p, nkMutableTy, mode) + of tkOut: result = parseTypeDescKAux(p, nkOutTy, mode) of tkRef: result = parseTypeDescKAux(p, nkRefTy, mode) of tkPtr: result = parseTypeDescKAux(p, nkPtrTy, mode) of tkDistinct: result = parseTypeDescKAux(p, nkDistinctTy, mode) diff --git a/compiler/renderer.nim b/compiler/renderer.nim index 141240518..756513f8e 100644 --- a/compiler/renderer.nim +++ b/compiler/renderer.nim @@ -506,7 +506,7 @@ proc lsub(g: TSrcGen; n: PNode): int = of nkTypeOfExpr: result = (if n.len > 0: lsub(g, n[0]) else: 0)+len("typeof()") of nkRefTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("ref") of nkPtrTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("ptr") - of nkVarTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("var") + of nkVarTy, nkOutTy: result = (if n.len > 0: lsub(g, n[0])+1 else: 0) + len("var") of nkDistinctTy: result = len("distinct") + (if n.len > 0: lsub(g, n[0])+1 else: 0) if n.len > 1: @@ -607,8 +607,8 @@ proc gcommaAux(g: var TSrcGen, n: PNode, ind: int, start: int = 0, let inPragma = g.inPragma == 1 # just the top-level var inHideable = false for i in start..n.len + theEnd: - var c = i < n.len + theEnd - var sublen = lsub(g, n[i]) + ord(c) + let c = i < n.len + theEnd + let sublen = lsub(g, n[i]) + ord(c) if not fits(g, g.lineLen + sublen) and (ind + sublen < MaxLineLen): optNL(g, ind) let oldLen = g.tokens.len if inPragma: @@ -1384,6 +1384,12 @@ proc gsub(g: var TSrcGen, n: PNode, c: TContext, fromStmtList = false) = gsub(g, n[0]) else: put(g, tkVar, "var") + of nkOutTy: + if n.len > 0: + putWithSpace(g, tkOut, "out") + gsub(g, n[0]) + else: + put(g, tkOut, "out") of nkDistinctTy: if n.len > 0: putWithSpace(g, tkDistinct, "distinct") diff --git a/compiler/sempass2.nim b/compiler/sempass2.nim index 65cd77aca..a1887b20e 100644 --- a/compiler/sempass2.nim +++ b/compiler/sempass2.nim @@ -104,9 +104,8 @@ proc createTypeBoundOps(tracked: PEffects, typ: PType; info: TLineInfo) = tracked.owner.flags.incl sfInjectDestructors proc isLocalVar(a: PEffects, s: PSym): bool = - # and (s.kind != skParam or s.typ.kind == tyOut) - s.kind in {skVar, skResult} and sfGlobal notin s.flags and - s.owner == a.owner and s.typ != nil + s.typ != nil and (s.kind in {skVar, skResult} or (s.kind == skParam and isOutParam(s.typ))) and + sfGlobal notin s.flags and s.owner == a.owner proc getLockLevel(t: PType): TLockLevel = var t = t @@ -194,7 +193,11 @@ proc varDecl(a: PEffects; n: PNode) {.inline.} = if n.kind == nkSym: a.scopes[n.sym.id] = a.currentBlock +proc skipHiddenDeref(n: PNode): PNode {.inline.} = + result = if n.kind == nkHiddenDeref: n[0] else: n + proc initVar(a: PEffects, n: PNode; volatileCheck: bool) = + let n = skipHiddenDeref(n) if n.kind != nkSym: return let s = n.sym if isLocalVar(a, s): @@ -221,6 +224,7 @@ proc initVar(a: PEffects, n: PNode; volatileCheck: bool) = n.flags.incl nfFirstWrite proc initVarViaNew(a: PEffects, n: PNode) = + let n = skipHiddenDeref(n) if n.kind != nkSym: return let s = n.sym if {tfRequiresInit, tfNotNil} * s.typ.flags <= {tfNotNil}: @@ -348,7 +352,8 @@ proc useVar(a: PEffects, n: PNode) = if s.typ.requiresInit: message(a.config, n.info, warnProveInit, s.name.s) elif a.leftPartOfAsgn <= 0: - message(a.config, n.info, warnUninit, s.name.s) + if strictDefs in a.c.features: + message(a.config, n.info, warnUninit, s.name.s) # prevent superfluous warnings about the same variable: a.init.add s.id useVarNoInitCheck(a, n, s) @@ -945,17 +950,18 @@ proc trackCall(tracked: PEffects; n: PNode) = if op != nil and op.kind == tyProc: for i in 1.. 0: inc(c.inheritancePenalty, depth) @@ -1461,7 +1472,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, elif aAsObject.kind == fKind: aAsObject = aAsObject.base - if aAsObject.kind == tyObject: + if aAsObject.kind == tyObject and trIsOutParam notin flags: let baseType = aAsObject.base if baseType != nil: c.inheritancePenalty += 1 @@ -1637,7 +1648,7 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, elif a.len > 0 and a.lastSon == f: # Needed for checking `Y` == `Addable` in the following #[ - type + type Addable = concept a, type A a + a is A MyType[T: Addable; Y: static T] = object diff --git a/compiler/types.nim b/compiler/types.nim index 0d98becda..1737fd48b 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -685,7 +685,7 @@ proc typeToString(typ: PType, prefer: TPreferedDesc = preferName): string = elif t.len == 1: result.add(",") result.add(')') of tyPtr, tyRef, tyVar, tyLent: - result = typeToStr[t.kind] + result = if isOutParam(t): "out " else: typeToStr[t.kind] if t.len >= 2: setLen(result, result.len-1) result.add '[' @@ -1211,7 +1211,7 @@ proc sameTypeAux(x, y: PType, c: var TSameTypeClosure): bool = result = sameChildrenAux(a, b, c) if result: if IgnoreTupleFields in c.flags: - result = a.flags * {tfVarIsPtr} == b.flags * {tfVarIsPtr} + result = a.flags * {tfVarIsPtr, tfIsOutParam} == b.flags * {tfVarIsPtr, tfIsOutParam} else: result = sameFlags(a, b) if result and ExactGcSafety in c.flags: -- cgit 1.4.1-2-gfad0