diff --git a/changelog.md b/changelog.md index 8e8b0a908ccb7..63ba58f4ff233 100644 --- a/changelog.md +++ b/changelog.md @@ -402,6 +402,7 @@ for an object type in the current scope. - `typeof(voidStmt)` now works and returns `void`. +- `const` now works with types containing `ref`, see `tests/vm/tconstrefs.nim` - The `gc:orc` algorithm was refined so that custom container types can participate in the cycle collection process. diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 79c73b602c138..162f64753cb21 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -2724,7 +2724,9 @@ proc expr(p: BProc, n: PNode, d: var TLoc) = internalError(p.config, n.info, "expr: proc not init " & sym.name.s) putLocIntoDest(p, d, sym.loc) of skConst: - if isSimpleConst(sym.typ): + if containsTyRef(sym.typ.skipTypes(abstractVar)): + expr(p, sym.ast, d) + elif isSimpleConst(sym.typ): putIntoDest(p, d, n, genLiteral(p, sym.ast, sym.typ), OnStatic) elif useAliveDataFromDce in p.module.flags: genConstHeader(p.module, p.module, p, sym) diff --git a/compiler/isolation_check.nim b/compiler/isolation_check.nim index 777e7f6ce80f9..63e47ddef3187 100644 --- a/compiler/isolation_check.nim +++ b/compiler/isolation_check.nim @@ -78,7 +78,7 @@ proc canAlias*(arg, ret: PType): bool = result = canAlias(arg, ret, marker) proc checkIsolate*(n: PNode): bool = - if types.containsTyRef(n.typ): + if types.containsTyRefOrClosure(n.typ): # XXX Maybe require that 'n.typ' is acyclic. This is not much # worse than the already exisiting inheritance and closure restrictions. case n.kind @@ -95,7 +95,7 @@ proc checkIsolate*(n: PNode): bool = discard "fine, it is isolated already" else: let argType = n[i].typ - if argType != nil and not isCompileTimeOnly(argType) and containsTyRef(argType): + if argType != nil and not isCompileTimeOnly(argType) and containsTyRefOrClosure(argType): if argType.canAlias(n.typ): return false result = true diff --git a/compiler/spawn.nim b/compiler/spawn.nim index e57686cb4c8dd..ad16bfba51eaf 100644 --- a/compiler/spawn.nim +++ b/compiler/spawn.nim @@ -215,7 +215,7 @@ proc setupArgsForConcurrency(g: ModuleGraph; n: PNode; objType: PType; if formals[i].typ.kind in {tyTypeDesc, tyStatic}: continue - #elif containsTyRef(argType): + #elif containsTyRefOrClosure(argType): # localError(n[i].info, "'spawn'ed function cannot refer to 'ref'/closure") let fieldname = if i < formals.len: formals[i].sym.name else: tmpName @@ -246,7 +246,7 @@ proc setupArgsForParallelism(g: ModuleGraph; n: PNode; objType: PType; let argType = skipTypes(if i < formals.len: formals[i].typ else: n.typ, abstractInst) - #if containsTyRef(argType): + #if containsTyRefOrClosure(argType): # localError(n.info, "'spawn'ed function cannot refer to 'ref'/closure") let fieldname = if i < formals.len: formals[i].sym.name else: tmpName diff --git a/compiler/typeallowed.nim b/compiler/typeallowed.nim index 2f0c039a4842f..b39d48a113087 100644 --- a/compiler/typeallowed.nim +++ b/compiler/typeallowed.nim @@ -161,8 +161,8 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind, elif kind in {skVar, skLet}: result = t[1] of tyRef: - if kind == skConst: result = t - else: result = typeAllowedAux(marker, t.lastSon, kind, c, flags+{taHeap}) + # skConst is now allowed + result = typeAllowedAux(marker, t.lastSon, kind, c, flags+{taHeap}) of tyPtr: result = typeAllowedAux(marker, t.lastSon, kind, c, flags+{taHeap}) of tySet: @@ -170,8 +170,8 @@ proc typeAllowedAux(marker: var IntSet, typ: PType, kind: TSymKind, result = typeAllowedAux(marker, t[i], kind, c, flags) if result != nil: break of tyObject, tyTuple: - if kind in {skProc, skFunc, skConst} and - t.kind == tyObject and t[0] != nil: + # skConst is now allowed + if kind in {skProc, skFunc} and t.kind == tyObject and t[0] != nil: result = t else: let flags = flags+{taField} diff --git a/compiler/types.nim b/compiler/types.nim index f0bd99a17a14b..a413ed3c4b212 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -271,7 +271,7 @@ proc searchTypeForAux(t: PType, predicate: TTypePredicate, if not result: result = searchTypeNodeForAux(t.n, predicate, marker) of tyGenericInst, tyDistinct, tyAlias, tySink: result = searchTypeForAux(lastSon(t), predicate, marker) - of tyArray, tySet, tyTuple: + of tyArray, tySet, tyTuple, tySequence: for i in 0..