@@ -2038,7 +2038,7 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2038
2038
2039
2039
// Recursively collect transitive captures from captured local functions.
2040
2040
llvm::DenseSet<AnyFunctionRef> visitedFunctions;
2041
- llvm::SetVector< CapturedValue> captures;
2041
+ llvm::MapVector<ValueDecl*, CapturedValue> captures;
2042
2042
2043
2043
// If there is a capture of 'self' with dynamic 'Self' type, it goes last so
2044
2044
// that IRGen can pass dynamic 'Self' metadata.
@@ -2139,30 +2139,43 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2139
2139
2140
2140
// We're capturing a 'self' value with dynamic 'Self' type;
2141
2141
// handle it specially.
2142
- if (!selfCapture &&
2143
- captureType->getClassOrBoundGenericClass ()) {
2144
- selfCapture = capture;
2142
+ if (captureType->getClassOrBoundGenericClass ()) {
2143
+ if (selfCapture)
2144
+ selfCapture = selfCapture->mergeFlags (capture);
2145
+ else
2146
+ selfCapture = capture;
2145
2147
continue ;
2146
2148
}
2147
2149
}
2148
2150
2149
2151
// Fall through to capture the storage.
2150
2152
}
2151
-
2153
+
2152
2154
// Collect non-function captures.
2153
- captures.insert (capture);
2155
+ ValueDecl *value = capture.getDecl ();
2156
+ auto existing = captures.find (value);
2157
+ if (existing != captures.end ()) {
2158
+ existing->second = existing->second .mergeFlags (capture);
2159
+ } else {
2160
+ captures.insert (std::pair<ValueDecl *, CapturedValue>(value, capture));
2161
+ }
2154
2162
}
2155
2163
};
2156
2164
collectFunctionCaptures (fn);
2157
2165
2166
+ SmallVector<CapturedValue, 4 > resultingCaptures;
2167
+ for (auto capturePair : captures) {
2168
+ resultingCaptures.push_back (capturePair.second );
2169
+ }
2170
+
2158
2171
// If we captured the dynamic 'Self' type and we have a 'self' value also,
2159
2172
// add it as the final capture. Otherwise, add a fake hidden capture for
2160
2173
// the dynamic 'Self' metatype.
2161
2174
if (selfCapture.hasValue ()) {
2162
- captures. insert (*selfCapture);
2175
+ resultingCaptures. push_back (*selfCapture);
2163
2176
} else if (capturesDynamicSelf) {
2164
2177
selfCapture = CapturedValue::getDynamicSelfMetadata ();
2165
- captures. insert (*selfCapture);
2178
+ resultingCaptures. push_back (*selfCapture);
2166
2179
}
2167
2180
2168
2181
// Cache the uniqued set of transitive captures.
@@ -2171,7 +2184,7 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2171
2184
auto &cachedCaptures = inserted.first ->second ;
2172
2185
cachedCaptures.setGenericParamCaptures (capturesGenericParams);
2173
2186
cachedCaptures.setDynamicSelfType (capturesDynamicSelf);
2174
- cachedCaptures.setCaptures (Context.AllocateCopy (captures ));
2187
+ cachedCaptures.setCaptures (Context.AllocateCopy (resultingCaptures ));
2175
2188
2176
2189
return cachedCaptures;
2177
2190
}
0 commit comments