special typing rules for owned pointers
This commit is contained in:
parent
0fd5417aff
commit
a6d49c3a81
6 changed files with 52 additions and 19 deletions
|
|
@ -738,7 +738,8 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
|
||||||
expectNoArg(conf, switch, arg, pass, info)
|
expectNoArg(conf, switch, arg, pass, info)
|
||||||
doAssert(conf != nil)
|
doAssert(conf != nil)
|
||||||
incl(conf.features, destructor)
|
incl(conf.features, destructor)
|
||||||
defineSymbol(conf.symbols, "nimNewRuntime")
|
incl(conf.globalOptions, optNimV2)
|
||||||
|
defineSymbol(conf.symbols, "nimV2")
|
||||||
of "stylecheck":
|
of "stylecheck":
|
||||||
case arg.normalize
|
case arg.normalize
|
||||||
of "off": conf.globalOptions = conf.globalOptions - {optStyleHint, optStyleError}
|
of "off": conf.globalOptions = conf.globalOptions - {optStyleHint, optStyleError}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ const
|
||||||
useEffectSystem* = true
|
useEffectSystem* = true
|
||||||
useWriteTracking* = false
|
useWriteTracking* = false
|
||||||
hasFFI* = defined(nimHasLibFFI)
|
hasFFI* = defined(nimHasLibFFI)
|
||||||
copyrightYear* = "2018"
|
copyrightYear* = "2019"
|
||||||
|
|
||||||
type # please make sure we have under 32 options
|
type # please make sure we have under 32 options
|
||||||
# (improves code efficiency a lot!)
|
# (improves code efficiency a lot!)
|
||||||
|
|
@ -82,6 +82,7 @@ type # please make sure we have under 32 options
|
||||||
optNoNimblePath
|
optNoNimblePath
|
||||||
optHotCodeReloading
|
optHotCodeReloading
|
||||||
optDynlibOverrideAll
|
optDynlibOverrideAll
|
||||||
|
optNimV2
|
||||||
|
|
||||||
TGlobalOptions* = set[TGlobalOption]
|
TGlobalOptions* = set[TGlobalOption]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1516,6 +1516,10 @@ proc asgnToResultVar(c: PContext, n, le, ri: PNode) {.inline.} =
|
||||||
n.sons[1] = takeImplicitAddr(c, ri, x.typ.kind == tyLent)
|
n.sons[1] = takeImplicitAddr(c, ri, x.typ.kind == tyLent)
|
||||||
x.typ.flags.incl tfVarIsPtr
|
x.typ.flags.incl tfVarIsPtr
|
||||||
#echo x.info, " setting it for this type ", typeToString(x.typ), " ", n.info
|
#echo x.info, " setting it for this type ", typeToString(x.typ), " ", n.info
|
||||||
|
# Special typing rule: do not allow to pass 'owned T' to 'T' in 'result = x':
|
||||||
|
if ri.typ != nil and ri.typ.skipTypes(abstractInst).kind == tyOwned and
|
||||||
|
le.typ != nil and le.typ.skipTypes(abstractInst).kind != tyOwned:
|
||||||
|
localError(c.config, n.info, "cannot return an owned pointer as an unowned pointer")
|
||||||
|
|
||||||
template resultTypeIsInferrable(typ: PType): untyped =
|
template resultTypeIsInferrable(typ: PType): untyped =
|
||||||
typ.isMetaType and typ.kind != tyTypeDesc
|
typ.isMetaType and typ.kind != tyTypeDesc
|
||||||
|
|
|
||||||
|
|
@ -263,8 +263,11 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||||
localError(c.config, n.info, errGenerated, "object constructor needs an object type")
|
localError(c.config, n.info, errGenerated, "object constructor needs an object type")
|
||||||
return
|
return
|
||||||
|
|
||||||
t = skipTypes(t, {tyGenericInst, tyAlias, tySink})
|
t = skipTypes(t, {tyGenericInst, tyAlias, tySink, tyOwned})
|
||||||
if t.kind == tyRef: t = skipTypes(t.sons[0], {tyGenericInst, tyAlias, tySink})
|
if t.kind == tyRef:
|
||||||
|
t = skipTypes(t.sons[0], {tyGenericInst, tyAlias, tySink, tyOwned})
|
||||||
|
if optNimV2 in c.config.globalOptions:
|
||||||
|
result.typ = makeVarType(c, result.typ, tyOwned)
|
||||||
if t.kind != tyObject:
|
if t.kind != tyObject:
|
||||||
localError(c.config, n.info, errGenerated, "object constructor needs an object type")
|
localError(c.config, n.info, errGenerated, "object constructor needs an object type")
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -474,6 +474,10 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode =
|
||||||
($typ.kind).substr(2).toLowerAscii)
|
($typ.kind).substr(2).toLowerAscii)
|
||||||
elif typ.kind == tyProc and tfUnresolved in typ.flags:
|
elif typ.kind == tyProc and tfUnresolved in typ.flags:
|
||||||
localError(c.config, def.info, errProcHasNoConcreteType % def.renderTree)
|
localError(c.config, def.info, errProcHasNoConcreteType % def.renderTree)
|
||||||
|
elif symkind == skVar and typ.kind == tyOwned and def.kind notin nkCallKinds:
|
||||||
|
# special type inference rule: 'var it = ownedPointer' is turned
|
||||||
|
# into an unowned pointer.
|
||||||
|
typ = typ.lastSon
|
||||||
else:
|
else:
|
||||||
if symkind == skLet: localError(c.config, a.info, errLetNeedsInit)
|
if symkind == skLet: localError(c.config, a.info, errLetNeedsInit)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1341,6 +1341,26 @@ proc `is`*[T, S](x: T, y: S): bool {.magic: "Is", noSideEffect.}
|
||||||
template `isnot`*(x, y: untyped): untyped = not (x is y)
|
template `isnot`*(x, y: untyped): untyped = not (x is y)
|
||||||
## Negated version of `is`. Equivalent to ``not(x is y)``.
|
## Negated version of `is`. Equivalent to ``not(x is y)``.
|
||||||
|
|
||||||
|
when defined(nimV2):
|
||||||
|
type owned*[T]{.magic: "BuiltinType".}
|
||||||
|
|
||||||
|
proc new*[T](a: var owned(ref T)) {.magic: "New", noSideEffect.}
|
||||||
|
## creates a new object of type ``T`` and returns a safe (traced)
|
||||||
|
## reference to it in ``a``.
|
||||||
|
|
||||||
|
proc new*(t: typedesc): auto =
|
||||||
|
## creates a new object of type ``T`` and returns a safe (traced)
|
||||||
|
## reference to it as result value.
|
||||||
|
##
|
||||||
|
## When ``T`` is a ref type then the resulting type will be ``T``,
|
||||||
|
## otherwise it will be ``ref T``.
|
||||||
|
when (t is ref):
|
||||||
|
var r: owned t
|
||||||
|
else:
|
||||||
|
var r: owned(ref t)
|
||||||
|
new(r)
|
||||||
|
return r
|
||||||
|
else:
|
||||||
proc new*[T](a: var ref T) {.magic: "New", noSideEffect.}
|
proc new*[T](a: var ref T) {.magic: "New", noSideEffect.}
|
||||||
## creates a new object of type ``T`` and returns a safe (traced)
|
## creates a new object of type ``T`` and returns a safe (traced)
|
||||||
## reference to it in ``a``.
|
## reference to it in ``a``.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue