Skip to content

Commit ba7abd0

Browse files
committed
SIL: Get addressor property names for display in move checker diagnostics.
1 parent 0280004 commit ba7abd0

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyDiagnostics.cpp

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ static void getVariableNameForValue(SILValue value2,
9191
// operand.
9292
StackList<llvm::PointerUnion<SILInstruction *, SILValue>> variableNamePath(
9393
value2->getFunction());
94-
while (true) {
94+
while (searchValue) {
9595
if (auto *allocInst = dyn_cast<AllocationInst>(searchValue)) {
9696
// If the instruction itself doesn't carry any variable info, see whether
9797
// it's copied from another place that does.
@@ -130,18 +130,44 @@ static void getVariableNameForValue(SILValue value2,
130130
variableNamePath.push_back({fArg});
131131
break;
132132
}
133-
134-
if (auto bai = dyn_cast_or_null<BeginApplyInst>(searchValue->getDefiningInstruction())) {
133+
134+
auto getNamePathComponentFromCallee = [&](FullApplySite call) -> llvm::Optional<SILValue> {
135135
// Use the name of the property being accessed if we can get to it.
136-
if (isa<FunctionRefBaseInst>(bai->getCallee())
137-
|| isa<MethodInst>(bai->getCallee())) {
138-
variableNamePath.push_back(bai->getCallee()->getDefiningInstruction());
136+
if (isa<FunctionRefBaseInst>(call.getCallee())
137+
|| isa<MethodInst>(call.getCallee())) {
138+
variableNamePath.push_back(call.getCallee()->getDefiningInstruction());
139139
// Try to name the base of the property if this is a method.
140-
if (bai->getSubstCalleeType()->hasSelfParam()) {
141-
searchValue = bai->getSelfArgument();
142-
continue;
140+
if (call.getSubstCalleeType()->hasSelfParam()) {
141+
return call.getSelfArgument();
143142
} else {
144-
break;
143+
return SILValue();
144+
}
145+
}
146+
return llvm::None;
147+
};
148+
149+
// Read or modify accessor.
150+
if (auto bai = dyn_cast_or_null<BeginApplyInst>(searchValue->getDefiningInstruction())) {
151+
if (auto selfParam = getNamePathComponentFromCallee(bai)) {
152+
searchValue = *selfParam;
153+
continue;
154+
}
155+
}
156+
157+
// Addressor accessor.
158+
if (auto ptrToAddr = dyn_cast<PointerToAddressInst>(stripAccessMarkers(searchValue))) {
159+
// The addressor can either produce the raw pointer itself or an `UnsafePointer` stdlib type wrapping it.
160+
ApplyInst *addressorInvocation;
161+
if (auto structExtract = dyn_cast<StructExtractInst>(ptrToAddr->getOperand())) {
162+
addressorInvocation = dyn_cast<ApplyInst>(structExtract->getOperand());
163+
} else {
164+
addressorInvocation = dyn_cast<ApplyInst>(ptrToAddr->getOperand());
165+
}
166+
167+
if (addressorInvocation) {
168+
if (auto selfParam = getNamePathComponentFromCallee(addressorInvocation)) {
169+
searchValue = *selfParam;
170+
continue;
145171
}
146172
}
147173
}

test/SILOptimizer/moveonly_addressors.swift

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,44 +80,43 @@ func borrow(_ nc: borrowing NC) {}
8080
func mod(_ nc: inout NC) {}
8181
func take(_ nc: consuming NC) {}
8282

83-
// TODO: Resolve thing being consumed more precisely than 'unknown'.
8483
// TODO: Use more specific diagnostic than "reinitialization of inout parameter"
8584

8685
func test(c: C) {
8786
borrow(c.data)
88-
take(c.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
87+
take(c.data) // expected-error{{'c.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
8988

9089
borrow(c.mutableData)
9190
mod(&c.mutableData)
92-
take(c.mutableData) // expected-error{{missing reinitialization of inout parameter 'unknown' after consume}} expected-note{{consumed here}}
91+
take(c.mutableData) // expected-error{{missing reinitialization of inout parameter 'c.mutableData' after consume}} expected-note{{consumed here}}
9392
}
9493
func test(s: S) {
9594
borrow(s.data)
96-
take(s.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
95+
take(s.data) // expected-error{{'s.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
9796

9897
borrow(s.mutableData)
99-
take(s.mutableData) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
98+
take(s.mutableData) // expected-error{{'s.mutableData' is borrowed and cannot be consumed}} expected-note{{consumed here}}
10099
}
101100
func test(mut_s s: inout S) {
102101
borrow(s.data)
103-
take(s.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
102+
take(s.data) // expected-error{{'s.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
104103

105104
borrow(s.mutableData)
106105
mod(&s.mutableData)
107-
take(s.mutableData) // expected-error{{missing reinitialization of inout parameter 'unknown' after consume}} expected-note{{consumed here}}
106+
take(s.mutableData) // expected-error{{missing reinitialization of inout parameter 's.mutableData' after consume}} expected-note{{consumed here}}
108107
}
109108
func test(snc: borrowing SNC) {
110109
borrow(snc.data)
111-
take(snc.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
110+
take(snc.data) // expected-error{{'snc.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
112111

113112
borrow(snc.mutableData)
114-
take(snc.mutableData) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
113+
take(snc.mutableData) // expected-error{{'snc.mutableData' is borrowed and cannot be consumed}} expected-note{{consumed here}}
115114
}
116115
func test(mut_snc snc: inout SNC) {
117116
borrow(snc.data)
118-
take(snc.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
117+
take(snc.data) // expected-error{{'snc.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
119118

120119
borrow(snc.mutableData)
121120
mod(&snc.mutableData)
122-
take(snc.mutableData) // expected-error{{missing reinitialization of inout parameter 'unknown' after consume}} expected-note{{consumed here}}
121+
take(snc.mutableData) // expected-error{{missing reinitialization of inout parameter 'snc.mutableData' after consume}} expected-note{{consumed here}}
123122
}

0 commit comments

Comments
 (0)