@@ -221,12 +221,41 @@ static ConstructorDecl *findDefaultInit(ASTContext &ctx,
221
221
init->getParameters ()->getArray (),
222
222
[](const ParamDecl *decl) { return decl->isDefaultArgument (); });
223
223
224
- // Only add non-synthesized initializers.
225
- if (allParamsHaveDefaultArg && !init->isImplicit ()) {
224
+ if (allParamsHaveDefaultArg) {
226
225
defaultValueInitializers.push_back (init);
227
226
}
228
227
}
229
228
229
+ if (defaultValueInitializers.size () > 1 ) {
230
+ // Let's find the single most specialized init. If we don't have one, then
231
+ // we'll diagnose later.
232
+ if (auto TC = static_cast <TypeChecker *>(ctx.getLazyResolver ())) {
233
+ Optional<unsigned > bestIdx;
234
+ for (unsigned i = 1 , n = defaultValueInitializers.size (); i != n; ++i) {
235
+ auto bestOrPrevIdx = bestIdx ? bestIdx.getValue () : i - 1 ;
236
+ auto firstInit =
237
+ cast<ValueDecl>(defaultValueInitializers[bestOrPrevIdx]);
238
+ auto secondInit = cast<ValueDecl>(defaultValueInitializers[i]);
239
+
240
+ switch (TC->compareDeclarations (nominal, firstInit, secondInit)) {
241
+ case Comparison::Better:
242
+ bestIdx = bestOrPrevIdx;
243
+ break ;
244
+ case Comparison::Worse:
245
+ bestIdx = i;
246
+ break ;
247
+ case Comparison::Unordered:
248
+ break ;
249
+ }
250
+ }
251
+
252
+ if (bestIdx.hasValue ()) {
253
+ auto bestInit = defaultValueInitializers[bestIdx.getValue ()];
254
+ defaultValueInitializers = {bestInit};
255
+ }
256
+ }
257
+ }
258
+
230
259
switch (defaultValueInitializers.size ()) {
231
260
case 0 :
232
261
return nullptr ;
@@ -235,7 +264,7 @@ static ConstructorDecl *findDefaultInit(ASTContext &ctx,
235
264
break ;
236
265
237
266
default :
238
- // Diagnose ambiguous init() initializers.
267
+ // Diagnose ambiguous default value initializers.
239
268
nominal->diagnose (diag::property_wrapper_ambiguous_default_value_init,
240
269
nominal->getDeclaredType ());
241
270
for (auto init : defaultValueInitializers) {
@@ -245,7 +274,7 @@ static ConstructorDecl *findDefaultInit(ASTContext &ctx,
245
274
return nullptr ;
246
275
}
247
276
248
- // 'init()' must be as accessible as the nominal type.
277
+ // The initializer must be as accessible as the nominal type.
249
278
auto init = defaultValueInitializers.front ();
250
279
if (init->getFormalAccess () < nominal->getFormalAccess ()) {
251
280
init->diagnose (diag::property_wrapper_type_requirement_not_accessible,
0 commit comments