Dotborrow now works with generic distincts (#18848)
This commit is contained in:
parent
ef390e6a68
commit
172253cb55
4 changed files with 37 additions and 7 deletions
|
|
@ -1280,7 +1280,7 @@ proc semSym(c: PContext, n: PNode, sym: PSym, flags: TExprFlags): PNode =
|
||||||
if p != nil and p.selfSym != nil:
|
if p != nil and p.selfSym != nil:
|
||||||
var ty = skipTypes(p.selfSym.typ, {tyGenericInst, tyVar, tyLent, tyPtr, tyRef,
|
var ty = skipTypes(p.selfSym.typ, {tyGenericInst, tyVar, tyLent, tyPtr, tyRef,
|
||||||
tyAlias, tySink, tyOwned})
|
tyAlias, tySink, tyOwned})
|
||||||
while tfBorrowDot in ty.flags: ty = ty.skipTypes({tyDistinct})
|
while tfBorrowDot in ty.flags: ty = ty.skipTypes({tyDistinct, tyGenericInst})
|
||||||
var check: PNode = nil
|
var check: PNode = nil
|
||||||
if ty.kind == tyObject:
|
if ty.kind == tyObject:
|
||||||
while true:
|
while true:
|
||||||
|
|
@ -1412,7 +1412,7 @@ proc builtinFieldAccess(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||||
if ty.kind in tyUserTypeClasses and ty.isResolvedUserTypeClass:
|
if ty.kind in tyUserTypeClasses and ty.isResolvedUserTypeClass:
|
||||||
ty = ty.lastSon
|
ty = ty.lastSon
|
||||||
ty = skipTypes(ty, {tyGenericInst, tyVar, tyLent, tyPtr, tyRef, tyOwned, tyAlias, tySink})
|
ty = skipTypes(ty, {tyGenericInst, tyVar, tyLent, tyPtr, tyRef, tyOwned, tyAlias, tySink})
|
||||||
while tfBorrowDot in ty.flags: ty = ty.skipTypes({tyDistinct})
|
while tfBorrowDot in ty.flags: ty = ty.skipTypes({tyDistinct, tyGenericInst})
|
||||||
var check: PNode = nil
|
var check: PNode = nil
|
||||||
if ty.kind == tyObject:
|
if ty.kind == tyObject:
|
||||||
while true:
|
while true:
|
||||||
|
|
|
||||||
|
|
@ -1296,7 +1296,8 @@ proc typeSectionRightSidePass(c: PContext, n: PNode) =
|
||||||
incl a[2].flags, nfSem # bug #10548
|
incl a[2].flags, nfSem # bug #10548
|
||||||
if sfExportc in s.flags and s.typ.kind == tyAlias:
|
if sfExportc in s.flags and s.typ.kind == tyAlias:
|
||||||
localError(c.config, name.info, "{.exportc.} not allowed for type aliases")
|
localError(c.config, name.info, "{.exportc.} not allowed for type aliases")
|
||||||
if tfBorrowDot in s.typ.flags and s.typ.kind != tyDistinct:
|
|
||||||
|
if tfBorrowDot in s.typ.flags and s.typ.skipTypes({tyGenericBody}).kind != tyDistinct:
|
||||||
excl s.typ.flags, tfBorrowDot
|
excl s.typ.flags, tfBorrowDot
|
||||||
localError(c.config, name.info, "only a 'distinct' type can borrow `.`")
|
localError(c.config, name.info, "only a 'distinct' type can borrow `.`")
|
||||||
let aa = a[2]
|
let aa = a[2]
|
||||||
|
|
|
||||||
|
|
@ -33,3 +33,23 @@ let b = Radians(1.0)
|
||||||
a -= b
|
a -= b
|
||||||
|
|
||||||
echo a.float64
|
echo a.float64
|
||||||
|
|
||||||
|
block: #14449
|
||||||
|
type
|
||||||
|
Foo[T] = object
|
||||||
|
foo: T
|
||||||
|
|
||||||
|
Bar[T] {.borrow:`.`.} = distinct Foo[T]
|
||||||
|
SomeThing {.borrow:`.`.} = distinct Foo[float]
|
||||||
|
OtherThing {.borrow:`.`.} = distinct SomeThing
|
||||||
|
|
||||||
|
var
|
||||||
|
a: Bar[int]
|
||||||
|
b: SomeThing
|
||||||
|
c: OtherThing
|
||||||
|
a.foo = 300
|
||||||
|
b.foo = 400
|
||||||
|
c.foo = 42
|
||||||
|
assert a.foo == 300
|
||||||
|
assert b.foo == 400d
|
||||||
|
assert c.foo == 42d
|
||||||
|
|
@ -1,16 +1,25 @@
|
||||||
discard """
|
discard """
|
||||||
errormsg: "no symbol to borrow from found"
|
cmd: "nim check --hints:off --warnings:off $file"
|
||||||
line: 11
|
action: "reject"
|
||||||
|
nimout:'''
|
||||||
|
tinvalidborrow.nim(18, 3) Error: only a 'distinct' type can borrow `.`
|
||||||
|
tinvalidborrow.nim(19, 3) Error: only a 'distinct' type can borrow `.`
|
||||||
|
tinvalidborrow.nim(20, 1) Error: no symbol to borrow from found
|
||||||
|
'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# bug #516
|
# bug #516
|
||||||
|
|
||||||
type
|
type
|
||||||
TAtom = culong
|
TAtom = culong
|
||||||
|
Test {.borrow:`.`.} = distinct int
|
||||||
|
Foo[T] = object
|
||||||
|
a: int
|
||||||
|
Bar[T] {.borrow:`.`.} = Foo[T]
|
||||||
|
OtherFoo {.borrow:`.`.} = Foo[int]
|
||||||
proc `==`*(a, b: TAtom): bool {.borrow.}
|
proc `==`*(a, b: TAtom): bool {.borrow.}
|
||||||
|
|
||||||
var
|
var
|
||||||
d, e: TAtom
|
d, e: TAtom
|
||||||
|
|
||||||
echo( $(d == e) )
|
discard( $(d == e) )
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue