@@ -467,10 +467,12 @@ namespace {
467
467
// / AST walker that infers requirements from type representations.
468
468
struct InferRequirementsWalker : public TypeWalker {
469
469
ModuleDecl *module ;
470
+ DeclContext *dc;
470
471
SmallVector<Requirement, 2 > reqs;
471
472
SmallVector<RequirementError, 2 > errors;
472
473
473
- explicit InferRequirementsWalker (ModuleDecl *module ) : module(module ) {}
474
+ explicit InferRequirementsWalker (ModuleDecl *module , DeclContext *dc)
475
+ : module(module ), dc(dc) {}
474
476
475
477
Action walkToTypePre (Type ty) override {
476
478
// Unbound generic types are the result of recovered-but-invalid code, and
@@ -482,11 +484,32 @@ struct InferRequirementsWalker : public TypeWalker {
482
484
}
483
485
484
486
Action walkToTypePost (Type ty) override {
487
+ // Skip `Sendable` conformance requirements that are inferred from
488
+ // `@preconcurrency` declarations.
489
+ auto skipRequirement = [&](Requirement req, Decl *fromDecl) {
490
+ if (!fromDecl->preconcurrency ())
491
+ return false ;
492
+
493
+ // If this decl is `@preconcurrency`, include concurrency
494
+ // requirements. The explicit annotation directly on the decl
495
+ // will still exclude `Sendable` requirements from ABI.
496
+ auto *decl = dc->getAsDecl ();
497
+ if (!decl || decl->preconcurrency ())
498
+ return false ;
499
+
500
+ return (req.getKind () == RequirementKind::Conformance &&
501
+ req.getSecondType ()->castTo <ProtocolType>()->getDecl ()
502
+ ->isSpecificProtocol (KnownProtocolKind::Sendable));
503
+ };
504
+
485
505
// Infer from generic typealiases.
486
506
if (auto typeAlias = dyn_cast<TypeAliasType>(ty.getPointer ())) {
487
507
auto decl = typeAlias->getDecl ();
488
508
auto subMap = typeAlias->getSubstitutionMap ();
489
509
for (const auto &rawReq : decl->getGenericSignature ().getRequirements ()) {
510
+ if (skipRequirement (rawReq, decl))
511
+ continue ;
512
+
490
513
desugarRequirement (rawReq.subst (subMap), SourceLoc (), reqs, errors);
491
514
}
492
515
@@ -567,6 +590,9 @@ struct InferRequirementsWalker : public TypeWalker {
567
590
// Handle the requirements.
568
591
// FIXME: Inaccurate TypeReprs.
569
592
for (const auto &rawReq : genericSig.getRequirements ()) {
593
+ if (skipRequirement (rawReq, decl))
594
+ continue ;
595
+
570
596
auto req = rawReq.subst (subMap);
571
597
desugarRequirement (req, SourceLoc (), reqs, errors);
572
598
}
@@ -585,12 +611,13 @@ struct InferRequirementsWalker : public TypeWalker {
585
611
// / We automatically infer 'T : Hashable' from the fact that 'struct Set'
586
612
// / declares a Hashable requirement on its generic parameter.
587
613
void swift::rewriting::inferRequirements (
588
- Type type, SourceLoc loc, ModuleDecl *module ,
614
+ Type type, SourceLoc loc,
615
+ ModuleDecl *module , DeclContext *dc,
589
616
SmallVectorImpl<StructuralRequirement> &result) {
590
617
if (!type)
591
618
return ;
592
619
593
- InferRequirementsWalker walker (module );
620
+ InferRequirementsWalker walker (module , dc );
594
621
type.walk (walker);
595
622
596
623
for (const auto &req : walker.reqs )
@@ -619,11 +646,11 @@ void swift::rewriting::realizeRequirement(
619
646
if (shouldInferRequirements) {
620
647
auto firstLoc = (reqRepr ? reqRepr->getSubjectRepr ()->getStartLoc ()
621
648
: SourceLoc ());
622
- inferRequirements (firstType, firstLoc, moduleForInference, result);
649
+ inferRequirements (firstType, firstLoc, moduleForInference, dc, result);
623
650
624
651
auto secondLoc = (reqRepr ? reqRepr->getConstraintRepr ()->getStartLoc ()
625
652
: SourceLoc ());
626
- inferRequirements (secondType, secondLoc, moduleForInference, result);
653
+ inferRequirements (secondType, secondLoc, moduleForInference, dc, result);
627
654
}
628
655
629
656
realizeTypeRequirement (dc, firstType, secondType, loc, result, errors);
@@ -634,7 +661,7 @@ void swift::rewriting::realizeRequirement(
634
661
if (shouldInferRequirements) {
635
662
auto firstLoc = (reqRepr ? reqRepr->getSubjectRepr ()->getStartLoc ()
636
663
: SourceLoc ());
637
- inferRequirements (firstType, firstLoc, moduleForInference, result);
664
+ inferRequirements (firstType, firstLoc, moduleForInference, dc, result);
638
665
}
639
666
640
667
SmallVector<Requirement, 2 > reqs;
@@ -652,11 +679,11 @@ void swift::rewriting::realizeRequirement(
652
679
if (shouldInferRequirements) {
653
680
auto firstLoc = (reqRepr ? reqRepr->getFirstTypeRepr ()->getStartLoc ()
654
681
: SourceLoc ());
655
- inferRequirements (firstType, firstLoc, moduleForInference, result);
682
+ inferRequirements (firstType, firstLoc, moduleForInference, dc, result);
656
683
657
684
auto secondLoc = (reqRepr ? reqRepr->getSecondTypeRepr ()->getStartLoc ()
658
685
: SourceLoc ());
659
- inferRequirements (secondType, secondLoc, moduleForInference, result);
686
+ inferRequirements (secondType, secondLoc, moduleForInference, dc, result);
660
687
}
661
688
662
689
SmallVector<Requirement, 2 > reqs;
@@ -700,7 +727,8 @@ void swift::rewriting::realizeInheritedRequirements(
700
727
auto *typeRepr = inheritedTypes[index].getTypeRepr ();
701
728
SourceLoc loc = (typeRepr ? typeRepr->getStartLoc () : SourceLoc ());
702
729
if (shouldInferRequirements) {
703
- inferRequirements (inheritedType, loc, moduleForInference, result);
730
+ inferRequirements (inheritedType, loc, moduleForInference,
731
+ decl->getInnermostDeclContext (), result);
704
732
}
705
733
706
734
realizeTypeRequirement (dc, type, inheritedType, loc, result, errors);
0 commit comments