Skip to content

Commit 65a59a5

Browse files
committed
Update borrow accessor diagnostics
1 parent 5e3ff1e commit 65a59a5

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5707,15 +5707,20 @@ SILGenFunction::tryEmitProjectedLValue(SILLocation loc, LValue &&src,
57075707
TSanKind tsanKind) {
57085708
assert(src.getAccessKind() == SGFAccessKind::BorrowedAddressRead ||
57095709
src.getAccessKind() == SGFAccessKind::BorrowedObjectRead);
5710+
5711+
for (auto component = src.begin(); component != src.end(); component++) {
5712+
if (component->get()->getKind() != PathComponent::BorrowMutateKind &&
5713+
component->get()->getKind() != PathComponent::StructElementKind &&
5714+
component->get()->getKind() != PathComponent::TupleElementKind &&
5715+
component->get()->getKind() != PathComponent::ValueKind) {
5716+
return std::nullopt;
5717+
}
5718+
}
5719+
57105720
ManagedValue base;
57115721
PathComponent &&component =
57125722
drillToLastComponent(loc, std::move(src), base, tsanKind);
57135723

5714-
if (component.getKind() != PathComponent::BorrowMutateKind &&
5715-
component.getKind() != PathComponent::StructElementKind &&
5716-
component.getKind() != PathComponent::TupleElementKind) {
5717-
return std::nullopt;
5718-
}
57195724
auto value =
57205725
drillIntoComponent(*this, loc, std::move(component), base, tsanKind);
57215726
ASSERT(!value.hasCleanup());

test/SILGen/borrow_accessor.swift

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public struct NC : ~Copyable {}
1414

1515
func use(_ t: borrowing NC) {}
1616

17+
func consume<T : ~Copyable>(_ t: consuming T) {}
18+
1719
public struct S {
1820
var _k: Klass
1921

@@ -44,30 +46,62 @@ public struct Wrapper {
4446
}
4547
}
4648

49+
var s_get: S {
50+
get {
51+
return _s
52+
}
53+
}
54+
55+
var s_read: S {
56+
_read {
57+
yield _s
58+
}
59+
}
60+
4761
var k: Klass {
4862
borrow {
4963
return _k
5064
}
5165
}
5266

67+
var k_complex: Klass {
68+
borrow {
69+
consume(self)
70+
consume(_k)
71+
return _k
72+
}
73+
}
74+
5375
var nested_borrow: Klass {
5476
borrow {
5577
return _s.borrowKlass
5678
}
5779
}
5880

59-
var nested_get: Klass {
81+
var nested_get1: Klass {
6082
borrow {
6183
return _s.getKlass // expected-error{{invalid return value from borrow accessor}} // expected-note{{borrow accessors can return either literals, stored properties or computed properties that have borrow accessors}}
6284
}
6385
}
6486

65-
var nested_read: Klass {
87+
var nested_get2: Klass {
88+
borrow {
89+
return s_get.borrowKlass // expected-error{{invalid return value from borrow accessor}} // expected-note{{borrow accessors can return either literals, stored properties or computed properties that have borrow accessors}}
90+
}
91+
}
92+
93+
var nested_read1: Klass {
6694
borrow {
6795
return _s.readKlass // expected-error{{invalid return value from borrow accessor}} // expected-note{{borrow accessors can return either literals, stored properties or computed properties that have borrow accessors}}
6896
}
6997
}
7098

99+
var nested_read2: Klass {
100+
borrow {
101+
return s_read.readKlass // expected-error{{invalid return value from borrow accessor}} // expected-note{{borrow accessors can return either literals, stored properties or computed properties that have borrow accessors}}
102+
}
103+
}
104+
71105
var owned_value_direct: Klass {
72106
borrow {
73107
return getKlass() // expected-error{{invalid return value from borrow accessor}} // expected-note{{borrow accessors can return either literals, stored properties or computed properties that have borrow accessors}}
@@ -352,6 +386,7 @@ public struct NCWrapper: ~Copyable {
352386
return _nc
353387
}
354388
}
389+
355390
var nested1: NC {
356391
borrow {
357392
return _s.nc

0 commit comments

Comments
 (0)