Skip to content

Commit afd65fa

Browse files
authored
Merge pull request #59849 from DougGregor/preconcurrency-sendable-params
2 parents f1207ff + 43399ba commit afd65fa

13 files changed

+129
-92
lines changed

include/swift/Sema/CSFix.h

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,14 @@ class ConstraintFix {
409409
FixKind Kind;
410410
ConstraintLocator *Locator;
411411

412-
/// Determines whether this fix is simplify a warning which doesn't
413-
/// require immediate source changes.
414-
bool IsWarning;
412+
/// The behavior limit to apply to the diagnostics emitted.
413+
DiagnosticBehavior behaviorLimit;
415414

416415
public:
417416
ConstraintFix(ConstraintSystem &cs, FixKind kind, ConstraintLocator *locator,
418-
bool warning = false)
419-
: CS(cs), Kind(kind), Locator(locator), IsWarning(warning) {}
417+
DiagnosticBehavior behaviorLimit =
418+
DiagnosticBehavior::Unspecified)
419+
: CS(cs), Kind(kind), Locator(locator), behaviorLimit(behaviorLimit) {}
420420

421421
virtual ~ConstraintFix();
422422

@@ -427,7 +427,14 @@ class ConstraintFix {
427427

428428
FixKind getKind() const { return Kind; }
429429

430-
bool isWarning() const { return IsWarning; }
430+
bool isWarning() const {
431+
return behaviorLimit == DiagnosticBehavior::Warning ||
432+
behaviorLimit == DiagnosticBehavior::Ignore;
433+
}
434+
435+
/// The diagnostic behavior limit that will be applied to any emitted
436+
/// diagnostics.
437+
DiagnosticBehavior diagBehaviorLimit() const { return behaviorLimit; }
431438

432439
virtual std::string getName() const = 0;
433440

@@ -672,14 +679,17 @@ class ContextualMismatch : public ConstraintFix {
672679
Type LHS, RHS;
673680

674681
ContextualMismatch(ConstraintSystem &cs, Type lhs, Type rhs,
675-
ConstraintLocator *locator, bool warning)
676-
: ConstraintFix(cs, FixKind::ContextualMismatch, locator, warning),
682+
ConstraintLocator *locator,
683+
DiagnosticBehavior behaviorLimit)
684+
: ConstraintFix(cs, FixKind::ContextualMismatch, locator, behaviorLimit),
677685
LHS(lhs), RHS(rhs) {}
678686

679687
protected:
680688
ContextualMismatch(ConstraintSystem &cs, FixKind kind, Type lhs, Type rhs,
681-
ConstraintLocator *locator, bool warning = false)
682-
: ConstraintFix(cs, kind, locator, warning), LHS(lhs), RHS(rhs) {}
689+
ConstraintLocator *locator,
690+
DiagnosticBehavior behaviorLimit =
691+
DiagnosticBehavior::Unspecified)
692+
: ConstraintFix(cs, kind, locator, behaviorLimit), LHS(lhs), RHS(rhs) {}
683693

684694
public:
685695
std::string getName() const override { return "fix contextual mismatch"; }
@@ -766,9 +776,10 @@ class MarkExplicitlyEscaping final : public ContextualMismatch {
766776
/// Mark function type as being part of a global actor.
767777
class MarkGlobalActorFunction final : public ContextualMismatch {
768778
MarkGlobalActorFunction(ConstraintSystem &cs, Type lhs, Type rhs,
769-
ConstraintLocator *locator, bool warning)
779+
ConstraintLocator *locator,
780+
DiagnosticBehavior behaviorLimit)
770781
: ContextualMismatch(cs, FixKind::MarkGlobalActorFunction, lhs, rhs,
771-
locator, warning) {
782+
locator, behaviorLimit) {
772783
}
773784

774785
public:
@@ -778,7 +789,7 @@ class MarkGlobalActorFunction final : public ContextualMismatch {
778789

779790
static MarkGlobalActorFunction *create(ConstraintSystem &cs, Type lhs,
780791
Type rhs, ConstraintLocator *locator,
781-
bool warning);
792+
DiagnosticBehavior behaviorLimit);
782793

783794
static bool classof(ConstraintFix *fix) {
784795
return fix->getKind() == FixKind::MarkGlobalActorFunction;
@@ -814,9 +825,9 @@ class ForceOptional final : public ContextualMismatch {
814825
class AddSendableAttribute final : public ContextualMismatch {
815826
AddSendableAttribute(ConstraintSystem &cs, FunctionType *fromType,
816827
FunctionType *toType, ConstraintLocator *locator,
817-
bool warning)
828+
DiagnosticBehavior behaviorLimit)
818829
: ContextualMismatch(cs, FixKind::AddSendableAttribute, fromType, toType,
819-
locator, warning) {
830+
locator, behaviorLimit) {
820831
assert(fromType->isSendable() != toType->isSendable());
821832
}
822833

@@ -829,7 +840,7 @@ class AddSendableAttribute final : public ContextualMismatch {
829840
FunctionType *fromType,
830841
FunctionType *toType,
831842
ConstraintLocator *locator,
832-
bool warning);
843+
DiagnosticBehavior behaviorLimit);
833844

834845
static bool classof(ConstraintFix *fix) {
835846
return fix->getKind() == FixKind::AddSendableAttribute;
@@ -1395,7 +1406,8 @@ class AllowInvalidPartialApplication final : public ConstraintFix {
13951406
AllowInvalidPartialApplication(bool isWarning, ConstraintSystem &cs,
13961407
ConstraintLocator *locator)
13971408
: ConstraintFix(cs, FixKind::AllowInvalidPartialApplication, locator,
1398-
isWarning) {}
1409+
isWarning ? DiagnosticBehavior::Warning
1410+
: DiagnosticBehavior::Unspecified) {}
13991411

14001412
public:
14011413
std::string getName() const override {
@@ -2129,8 +2141,10 @@ class AllowArgumentMismatch : public ContextualMismatch {
21292141

21302142
AllowArgumentMismatch(ConstraintSystem &cs, FixKind kind, Type argType,
21312143
Type paramType, ConstraintLocator *locator,
2132-
bool warning = false)
2133-
: ContextualMismatch(cs, kind, argType, paramType, locator, warning) {}
2144+
DiagnosticBehavior behaviorLimit =
2145+
DiagnosticBehavior::Unspecified)
2146+
: ContextualMismatch(
2147+
cs, kind, argType, paramType, locator, behaviorLimit) {}
21342148

21352149
public:
21362150
std::string getName() const override {
@@ -2278,9 +2292,9 @@ class TreatEphemeralAsNonEphemeral final : public AllowArgumentMismatch {
22782292
TreatEphemeralAsNonEphemeral(ConstraintSystem &cs, ConstraintLocator *locator,
22792293
Type srcType, Type dstType,
22802294
ConversionRestrictionKind conversionKind,
2281-
bool downgradeToWarning)
2295+
DiagnosticBehavior behaviorLimit)
22822296
: AllowArgumentMismatch(cs, FixKind::TreatEphemeralAsNonEphemeral,
2283-
srcType, dstType, locator, downgradeToWarning),
2297+
srcType, dstType, locator, behaviorLimit),
22842298
ConversionKind(conversionKind) {}
22852299

22862300
public:
@@ -2444,7 +2458,7 @@ class AllowCoercionToForceCast final : public ContextualMismatch {
24442458
AllowCoercionToForceCast(ConstraintSystem &cs, Type fromType, Type toType,
24452459
ConstraintLocator *locator)
24462460
: ContextualMismatch(cs, FixKind::AllowCoercionToForceCast, fromType,
2447-
toType, locator, /*warning*/ true) {}
2461+
toType, locator, DiagnosticBehavior::Warning) {}
24482462

24492463
public:
24502464
std::string getName() const override {
@@ -2558,7 +2572,7 @@ class SpecifyLabelToAssociateTrailingClosure final : public ConstraintFix {
25582572
SpecifyLabelToAssociateTrailingClosure(ConstraintSystem &cs,
25592573
ConstraintLocator *locator)
25602574
: ConstraintFix(cs, FixKind::SpecifyLabelToAssociateTrailingClosure,
2561-
locator, /*isWarning=*/true) {}
2575+
locator, DiagnosticBehavior::Warning) {}
25622576

25632577
public:
25642578
std::string getName() const override {
@@ -2728,7 +2742,7 @@ class SpecifyBaseTypeForOptionalUnresolvedMember final : public ConstraintFix {
27282742
DeclNameRef memberName,
27292743
ConstraintLocator *locator)
27302744
: ConstraintFix(cs, FixKind::SpecifyBaseTypeForOptionalUnresolvedMember,
2731-
locator, /*isWarning=*/true),
2745+
locator, DiagnosticBehavior::Warning),
27322746
MemberName(memberName) {}
27332747
DeclNameRef MemberName;
27342748

@@ -2759,7 +2773,7 @@ class CheckedCastContextualMismatchWarning : public ContextualMismatch {
27592773
CheckedCastKind kind,
27602774
ConstraintLocator *locator)
27612775
: ContextualMismatch(cs, fixKind, fromType, toType, locator,
2762-
/*isWarning*/ true),
2776+
DiagnosticBehavior::Warning),
27632777
CastKind(kind) {}
27642778
CheckedCastKind CastKind;
27652779
};
@@ -2918,7 +2932,7 @@ class AllowTupleLabelMismatch final : public ContextualMismatch {
29182932
AllowTupleLabelMismatch(ConstraintSystem &cs, Type fromType, Type toType,
29192933
ConstraintLocator *locator)
29202934
: ContextualMismatch(cs, FixKind::AllowTupleLabelMismatch, fromType,
2921-
toType, locator, /*warning*/ true) {}
2935+
toType, locator, DiagnosticBehavior::Warning) {}
29222936

29232937
public:
29242938
std::string getName() const override { return "allow tuple label mismatch"; }

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,24 +1846,13 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
18461846
SILFunctionConventions substConv(substTy, F.getModule());
18471847
unsigned appliedArgStartIdx =
18481848
substConv.getNumSILArguments() - PAI->getNumArguments();
1849-
bool isSendableAndStageIsCanonical =
1850-
PAI->getFunctionType()->isSendable() &&
1851-
F.getModule().getStage() >= SILStage::Canonical;
18521849
for (auto p : llvm::enumerate(PAI->getArguments())) {
18531850
requireSameType(
18541851
p.value()->getType(),
18551852
substConv.getSILArgumentType(appliedArgStartIdx + p.index(),
18561853
F.getTypeExpansionContext()),
18571854
"applied argument types do not match suffix of function type's "
18581855
"inputs");
1859-
1860-
// TODO: Expand this to also be true for address only types.
1861-
if (isSendableAndStageIsCanonical)
1862-
require(
1863-
!p.value()->getType().getASTType()->is<SILBoxType>() ||
1864-
p.value()->getType().getSILBoxFieldType(&F).isAddressOnly(F),
1865-
"Concurrent partial apply in canonical SIL with a loadable box "
1866-
"type argument?!");
18671856
}
18681857

18691858
// The arguments to the result function type must match the prefix of the

lib/Sema/CSApply.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5701,8 +5701,17 @@ ArgumentList *ExprRewriter::coerceCallArguments(
57015701
// Quickly test if any further fix-ups for the argument types are necessary.
57025702
auto matches = args->matches(params, [&](Expr *E) { return cs.getType(E); });
57035703
if (matches && !shouldInjectWrappedValuePlaceholder &&
5704-
!paramInfo.anyContextualInfo())
5704+
!paramInfo.anyContextualInfo()) {
5705+
// Propagate preconcurrency to any closure arguments.
5706+
if (preconcurrency) {
5707+
for (const auto &arg : *args) {
5708+
Expr *argExpr = arg.getExpr();
5709+
applyContextualClosureFlags(argExpr, false, false, preconcurrency);
5710+
}
5711+
}
5712+
57055713
return args;
5714+
}
57065715

57075716
// Determine the parameter bindings that were applied.
57085717
auto *locatorPtr = cs.getConstraintLocator(locator);

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ template <typename... ArgTypes>
103103
InFlightDiagnostic
104104
FailureDiagnostic::emitDiagnosticAt(ArgTypes &&... Args) const {
105105
auto &DE = getASTContext().Diags;
106-
auto behavior = isWarning ? DiagnosticBehavior::Warning
107-
: DiagnosticBehavior::Unspecified;
108106
return std::move(DE.diagnose(std::forward<ArgTypes>(Args)...)
109-
.limitBehavior(behavior));
107+
.limitBehavior(behaviorLimit));
110108
}
111109

112110
Expr *FailureDiagnostic::findParentExpr(const Expr *subExpr) const {

lib/Sema/CSDiagnostics.h

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,19 @@ class FunctionArgApplyInfo;
4242
class FailureDiagnostic {
4343
const Solution &S;
4444
ConstraintLocator *Locator;
45-
bool isWarning;
45+
DiagnosticBehavior behaviorLimit;
4646

4747
public:
4848
FailureDiagnostic(const Solution &solution, ConstraintLocator *locator,
49-
bool isWarning = false)
50-
: S(solution), Locator(locator), isWarning(isWarning) {}
49+
DiagnosticBehavior behaviorLimit =
50+
DiagnosticBehavior::Unspecified)
51+
: S(solution), Locator(locator), behaviorLimit(behaviorLimit) {}
5152

5253
FailureDiagnostic(const Solution &solution, ASTNode anchor,
53-
bool isWarning = false)
54+
DiagnosticBehavior behaviorLimit =
55+
DiagnosticBehavior::Unspecified)
5456
: FailureDiagnostic(solution, solution.getConstraintLocator(anchor),
55-
isWarning) { }
57+
behaviorLimit) { }
5658

5759
virtual ~FailureDiagnostic();
5860

@@ -605,20 +607,23 @@ class ContextualFailure : public FailureDiagnostic {
605607

606608
public:
607609
ContextualFailure(const Solution &solution, Type lhs, Type rhs,
608-
ConstraintLocator *locator, bool isWarning = false)
610+
ConstraintLocator *locator,
611+
DiagnosticBehavior behaviorLimit =
612+
DiagnosticBehavior::Unspecified)
609613
: ContextualFailure(
610614
solution,
611615
locator->isForContextualType()
612616
? locator->castLastElementTo<LocatorPathElt::ContextualType>()
613617
.getPurpose()
614618
: solution.getConstraintSystem().getContextualTypePurpose(
615619
locator->getAnchor()),
616-
lhs, rhs, locator, isWarning) {}
620+
lhs, rhs, locator, behaviorLimit) {}
617621

618622
ContextualFailure(const Solution &solution, ContextualTypePurpose purpose,
619623
Type lhs, Type rhs, ConstraintLocator *locator,
620-
bool isWarning = false)
621-
: FailureDiagnostic(solution, locator, isWarning), CTP(purpose),
624+
DiagnosticBehavior behaviorLimit =
625+
DiagnosticBehavior::Unspecified)
626+
: FailureDiagnostic(solution, locator, behaviorLimit), CTP(purpose),
622627
RawFromType(lhs), RawToType(rhs) {
623628
assert(lhs && "Expected a valid 'from' type");
624629
assert(rhs && "Expected a valid 'to' type");
@@ -759,8 +764,9 @@ class AttributedFuncToTypeConversionFailure final : public ContextualFailure {
759764
AttributedFuncToTypeConversionFailure(const Solution &solution, Type fromType,
760765
Type toType, ConstraintLocator *locator,
761766
AttributeKind attributeKind,
762-
bool isWarning = false)
763-
: ContextualFailure(solution, fromType, toType, locator, isWarning),
767+
DiagnosticBehavior behaviorLimit =
768+
DiagnosticBehavior::Unspecified)
769+
: ContextualFailure(solution, fromType, toType, locator, behaviorLimit),
764770
attributeKind(attributeKind) {}
765771

766772
bool diagnoseAsError() override;
@@ -784,8 +790,8 @@ class DroppedGlobalActorFunctionAttr final : public ContextualFailure {
784790
public:
785791
DroppedGlobalActorFunctionAttr(const Solution &solution, Type fromType,
786792
Type toType, ConstraintLocator *locator,
787-
bool isWarning)
788-
: ContextualFailure(solution, fromType, toType, locator, isWarning) { }
793+
DiagnosticBehavior behaviorLimit)
794+
: ContextualFailure(solution, fromType, toType, locator, behaviorLimit) { }
789795

790796
bool diagnoseAsError() override;
791797
};
@@ -1955,8 +1961,9 @@ class ArgumentMismatchFailure : public ContextualFailure {
19551961
public:
19561962
ArgumentMismatchFailure(const Solution &solution, Type argType,
19571963
Type paramType, ConstraintLocator *locator,
1958-
bool warning = false)
1959-
: ContextualFailure(solution, argType, paramType, locator, warning),
1964+
DiagnosticBehavior behaviorLimit =
1965+
DiagnosticBehavior::Unspecified)
1966+
: ContextualFailure(solution, argType, paramType, locator, behaviorLimit),
19601967
Info(*getFunctionArgApplyInfo(getLocator())) {}
19611968

19621969
bool diagnoseAsError() override;
@@ -2135,8 +2142,9 @@ class NonEphemeralConversionFailure final : public ArgumentMismatchFailure {
21352142
ConstraintLocator *locator, Type fromType,
21362143
Type toType,
21372144
ConversionRestrictionKind conversionKind,
2138-
bool warning)
2139-
: ArgumentMismatchFailure(solution, fromType, toType, locator, warning),
2145+
DiagnosticBehavior behaviorLimit)
2146+
: ArgumentMismatchFailure(
2147+
solution, fromType, toType, locator, behaviorLimit),
21402148
ConversionKind(conversionKind) {
21412149
}
21422150

0 commit comments

Comments
 (0)