mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-18 13:30:33 +00:00
make borrow . work with aliases if not overriden (#22072)
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user