From 5139a2ec37960f7d16a284369e0497efa331cd87 Mon Sep 17 00:00:00 2001 From: metagn Date: Sun, 11 Jun 2023 20:19:48 +0300 Subject: [PATCH] make borrow `.` work with aliases if not overriden (#22072) --- compiler/semstmts.nim | 11 ++++++++--- tests/distinct/tborrow.nim | 11 ++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index b39361434b3b..48b075570b52 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -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: diff --git a/tests/distinct/tborrow.nim b/tests/distinct/tborrow.nim index 35652e2e0ac6..8a40982a8dc7 100644 --- a/tests/distinct/tborrow.nim +++ b/tests/distinct/tborrow.nim @@ -88,4 +88,13 @@ block: # Borrow from generic alias c = default(C) e = default(E) assert c.i == int(0) - assert e.i == 0d \ No newline at end of file + 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]