@@ -4284,20 +4284,32 @@ TypeResolver::resolveOwnershipTypeRepr(OwnershipTypeRepr *repr,
4284
4284
if (result->hasError ())
4285
4285
return result;
4286
4286
4287
- // Unless we have the experimental no-implicit-copy feature enabled, Copyable
4288
- // types can't use 'consuming' or 'borrowing' ownership specifiers.
4289
- if (!getASTContext ().LangOpts .hasFeature (Feature::NoImplicitCopy)) {
4290
- if (!result->isPureMoveOnly ()) {
4291
- // Prevent copyable types from using the non-underscored ownership parameter
4292
- // specifiers, other than 'inout'.
4293
- switch (ownershipRepr->getSpecifier ()) {
4294
- case ParamSpecifier::Default:
4295
- case ParamSpecifier::InOut:
4296
- case ParamSpecifier::LegacyShared:
4297
- case ParamSpecifier::LegacyOwned:break ;
4298
-
4299
- case ParamSpecifier::Borrowing:
4300
- case ParamSpecifier::Consuming:
4287
+ // Check for illegal combinations of ownership specifiers and types.
4288
+ switch (ownershipRepr->getSpecifier ()) {
4289
+ case ParamSpecifier::Default:
4290
+ case ParamSpecifier::InOut:
4291
+ case ParamSpecifier::LegacyShared:
4292
+ case ParamSpecifier::LegacyOwned:
4293
+ break ;
4294
+
4295
+ case ParamSpecifier::Consuming:
4296
+ // consuming cannot be applied to parameters of nonescaping closure type,
4297
+ // which by their nature are always borrowed
4298
+ if (auto fnTy = result->getAs <AnyFunctionType>()) {
4299
+ if (fnTy->isNoEscape ()) {
4300
+ diagnoseInvalid (ownershipRepr,
4301
+ ownershipRepr->getLoc (),
4302
+ diag::ownership_specifier_illegal_nonscape_closure,
4303
+ result, ownershipRepr->getSpecifierSpelling ());
4304
+ return ErrorType::get (getASTContext ());
4305
+ }
4306
+ }
4307
+ SWIFT_FALLTHROUGH;
4308
+ case ParamSpecifier::Borrowing:
4309
+ // Unless we have the experimental no-implicit-copy feature enabled,
4310
+ // Copyable types can't use 'consuming' or 'borrowing' ownership specifiers.
4311
+ if (!getASTContext ().LangOpts .hasFeature (Feature::NoImplicitCopy)) {
4312
+ if (!result->isPureMoveOnly ()) {
4301
4313
diagnoseInvalid (ownershipRepr,
4302
4314
ownershipRepr->getLoc (),
4303
4315
diag::ownership_specifier_copyable);
0 commit comments