Skip to content

Commit

Permalink
make borrow . work with aliases if not overriden (#22072)
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn authored Jun 11, 2023
1 parent 9b5f310 commit 5139a2e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 8 additions & 3 deletions compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion tests/distinct/tborrow.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 5139a2e

Please sign in to comment.