make borrow . work with aliases if not overriden (#22072)

This commit is contained in:
metagn
2023-06-11 20:19:48 +03:00
committed by GitHub
parent 9b5f310b9e
commit 5139a2ec37
2 changed files with 18 additions and 4 deletions

View File

@@ -1456,9 +1456,14 @@ proc typeSectionRightSidePass(c: PContext, n: PNode) =
if sfExportc in s.flags and s.typ.kind == tyAlias:
localError(c.config, name.info, "{.exportc.} not allowed for type aliases")
if tfBorrowDot in s.typ.flags and s.typ.skipTypes({tyGenericBody}).kind != tyDistinct:
excl s.typ.flags, tfBorrowDot
localError(c.config, name.info, "only a 'distinct' type can borrow `.`")
if tfBorrowDot in s.typ.flags:
let body = s.typ.skipTypes({tyGenericBody})
if body.kind != tyDistinct:
# flag might be copied from alias/instantiation:
let t = body.skipTypes({tyAlias, tyGenericInst})
if not (t.kind == tyDistinct and tfBorrowDot in t.flags):
excl s.typ.flags, tfBorrowDot
localError(c.config, name.info, "only a 'distinct' type can borrow `.`")
let aa = a[2]
if aa.kind in {nkRefTy, nkPtrTy} and aa.len == 1 and
aa[0].kind == nkObjectTy:

View File

@@ -88,4 +88,13 @@ block: # Borrow from generic alias
c = default(C)
e = default(E)
assert c.i == int(0)
assert e.i == 0d
assert e.i == 0d
block: # issue #22069
type
Vehicle[C: static[int]] = object
color: array[C, int]
Car[C: static[int]] {.borrow: `.`.} = distinct Vehicle[C]
MuscleCar = Car[128]
var x: MuscleCar
doAssert x.color is array[128, int]