Skip to content

Commit 1cb500c

Browse files
authored
Merge pull request #70573 from jckarter/moveonly-addressor-diagnostic-names
SIL: Get addressor property names for display in move checker diagnostics.
2 parents 3692f75 + ba7abd0 commit 1cb500c

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
@@ -82,44 +82,43 @@ func borrow(_ nc: borrowing NC) {}
8282
func mod(_ nc: inout NC) {}
8383
func take(_ nc: consuming NC) {}
8484

85-
// TODO: Resolve thing being consumed more precisely than 'unknown'.
8685
// TODO: Use more specific diagnostic than "reinitialization of inout parameter"
8786

8887
func test(c: C) {
8988
borrow(c.data)
90-
take(c.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
89+
take(c.data) // expected-error{{'c.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
9190

9291
borrow(c.mutableData)
9392
mod(&c.mutableData)
94-
take(c.mutableData) // expected-error{{missing reinitialization of inout parameter 'unknown' after consume}} expected-note{{consumed here}}
93+
take(c.mutableData) // expected-error{{missing reinitialization of inout parameter 'c.mutableData' after consume}} expected-note{{consumed here}}
9594
}
9695
func test(s: S) {
9796
borrow(s.data)
98-
take(s.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
97+
take(s.data) // expected-error{{'s.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
9998

10099
borrow(s.mutableData)
101-
take(s.mutableData) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
100+
take(s.mutableData) // expected-error{{'s.mutableData' is borrowed and cannot be consumed}} expected-note{{consumed here}}
102101
}
103102
func test(mut_s s: inout S) {
104103
borrow(s.data)
105-
take(s.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
104+
take(s.data) // expected-error{{'s.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
106105

107106
borrow(s.mutableData)
108107
mod(&s.mutableData)
109-
take(s.mutableData) // expected-error{{missing reinitialization of inout parameter 'unknown' after consume}} expected-note{{consumed here}}
108+
take(s.mutableData) // expected-error{{missing reinitialization of inout parameter 's.mutableData' after consume}} expected-note{{consumed here}}
110109
}
111110
func test(snc: borrowing SNC) {
112111
borrow(snc.data)
113-
take(snc.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
112+
take(snc.data) // expected-error{{'snc.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
114113

115114
borrow(snc.mutableData)
116-
take(snc.mutableData) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
115+
take(snc.mutableData) // expected-error{{'snc.mutableData' is borrowed and cannot be consumed}} expected-note{{consumed here}}
117116
}
118117
func test(mut_snc snc: inout SNC) {
119118
borrow(snc.data)
120-
take(snc.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}}
119+
take(snc.data) // expected-error{{'snc.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
121120

122121
borrow(snc.mutableData)
123122
mod(&snc.mutableData)
124-
take(snc.mutableData) // expected-error{{missing reinitialization of inout parameter 'unknown' after consume}} expected-note{{consumed here}}
123+
take(snc.mutableData) // expected-error{{missing reinitialization of inout parameter 'snc.mutableData' after consume}} expected-note{{consumed here}}
125124
}

0 commit comments

Comments
 (0)