Skip to content

Commit bb461e8

Browse files
jckartermeg-gupta
authored andcommitted
Look through copy_addr and opened existentials to diagnose move-only sources.
1 parent 2b78ccd commit bb461e8

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyDiagnostics.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ static void getVariableNameForValue(SILValue value2,
9393
value2->getFunction());
9494
while (true) {
9595
if (auto *allocInst = dyn_cast<AllocationInst>(searchValue)) {
96+
// If the instruction itself doesn't carry any variable info, see whether
97+
// it's copied from another place that does.
98+
if (!allocInst->getDecl()) {
99+
if (auto copy = allocInst->getSingleUserOfType<CopyAddrInst>()) {
100+
if (copy->getDest() == allocInst
101+
&& !copy->isTakeOfSrc()
102+
&& copy->isInitializationOfDest()) {
103+
searchValue = copy->getSrc();
104+
continue;
105+
}
106+
}
107+
}
108+
96109
variableNamePath.push_back(allocInst);
97110
break;
98111
}
@@ -102,6 +115,11 @@ static void getVariableNameForValue(SILValue value2,
102115
break;
103116
}
104117

118+
if (auto *oeInst = dyn_cast<OpenExistentialAddrInst>(searchValue)) {
119+
searchValue = oeInst->getOperand();
120+
continue;
121+
}
122+
105123
if (auto *rei = dyn_cast<RefElementAddrInst>(searchValue)) {
106124
variableNamePath.push_back(rei);
107125
searchValue = rei->getOperand();

test/SILOptimizer/moveonly_modify_coroutine.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,21 @@ func test(m_snc snc: inout SNC) {
8080

8181
func test<T: P>(t: T) {
8282
borrow(t.data)
83-
take(t.data) // expected-error{{'unknown.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
83+
take(t.data) // expected-error{{'t.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
8484
}
8585
func test<T: P>(m_t t: inout T) {
8686
borrow(t.data)
8787
take(t.data) // expected-error{{missing reinitialization of inout parameter 't.data' after consume}} expected-note{{consumed here}}
8888
mod(&t.data)
8989
}
9090

91-
// FIXME: get the actual variable name instead of 'unknown' in these cases.
9291
func test(p: P) {
9392
borrow(p.data)
94-
take(p.data) // expected-error{{'unknown.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
93+
take(p.data) // expected-error{{'p.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
9594
}
9695
func test(m_p p: inout P) {
9796
borrow(p.data)
98-
take(p.data) // expected-error{{missing reinitialization of inout parameter 'unknown' after consume}} expected-note{{consumed here}}
97+
take(p.data) // expected-error{{missing reinitialization of inout parameter 'p.data' after consume}} expected-note{{consumed here}}
9998
mod(&p.data)
10099
}
101100

test/SILOptimizer/moveonly_read_coroutine.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,12 @@ func test(snc: borrowing SNC) {
6262
take(snc.data) // expected-error{{'snc.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
6363
}
6464

65-
// FIXME: get the actual variable name instead of 'unknown' in these cases.
6665
func test<T: P>(t: T) {
6766
borrow(t.data)
68-
take(t.data) // expected-error{{'unknown.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
67+
take(t.data) // expected-error{{'t.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
6968
}
7069
func test(p: P) {
7170
borrow(p.data)
72-
take(p.data) // expected-error{{'unknown.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
71+
take(p.data) // expected-error{{'p.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
7372
}
7473

0 commit comments

Comments
 (0)