Skip to content

Commit ed533bf

Browse files
committed
[Concurrency] Downgrade @preconcurrency conformance requirement failures to
warnings in Swift 6 mode. (cherry picked from commit e21bf2f)
1 parent b5198c4 commit ed533bf

6 files changed

+49
-18
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3875,8 +3875,12 @@ bool ExprAvailabilityWalker::diagnoseDeclRefAvailability(
38753875
return true;
38763876

38773877
if (R.isValid()) {
3878-
if (diagnoseSubstitutionMapAvailability(R.Start, declRef.getSubstitutions(),
3879-
Where)) {
3878+
if (diagnoseSubstitutionMapAvailability(
3879+
R.Start, declRef.getSubstitutions(), Where,
3880+
Type(), Type(),
3881+
/*warnIfConformanceUnavailablePreSwift6*/false,
3882+
/*suppressParameterizationCheckForOptional*/false,
3883+
/*preconcurrency*/D->preconcurrency())) {
38803884
return true;
38813885
}
38823886
}
@@ -4348,7 +4352,8 @@ class ProblematicTypeFinder : public TypeDeclFinder {
43484352
/*depTy=*/Type(),
43494353
/*replacementTy=*/Type(),
43504354
/*warnIfConformanceUnavailablePreSwift6=*/false,
4351-
/*suppressParameterizationCheckForOptional=*/ty->isOptional());
4355+
/*suppressParameterizationCheckForOptional=*/ty->isOptional(),
4356+
/*preconcurrency*/ty->getAnyNominal()->preconcurrency());
43524357
return Action::Continue;
43534358
}
43544359

@@ -4403,17 +4408,19 @@ void swift::diagnoseTypeAvailability(const TypeRepr *TR, Type T, SourceLoc loc,
44034408
}
44044409

44054410
static void diagnoseMissingConformance(
4406-
SourceLoc loc, Type type, ProtocolDecl *proto, const DeclContext *fromDC) {
4411+
SourceLoc loc, Type type, ProtocolDecl *proto, const DeclContext *fromDC,
4412+
bool preconcurrency) {
44074413
assert(proto->isSpecificProtocol(KnownProtocolKind::Sendable));
4408-
diagnoseMissingSendableConformance(loc, type, fromDC);
4414+
diagnoseMissingSendableConformance(loc, type, fromDC, preconcurrency);
44094415
}
44104416

44114417
bool
44124418
swift::diagnoseConformanceAvailability(SourceLoc loc,
44134419
ProtocolConformanceRef conformance,
44144420
const ExportContext &where,
44154421
Type depTy, Type replacementTy,
4416-
bool warnIfConformanceUnavailablePreSwift6) {
4422+
bool warnIfConformanceUnavailablePreSwift6,
4423+
bool preconcurrency) {
44174424
assert(!where.isImplicit());
44184425

44194426
if (conformance.isInvalid() || conformance.isAbstract())
@@ -4425,7 +4432,8 @@ swift::diagnoseConformanceAvailability(SourceLoc loc,
44254432
for (auto patternConf : pack->getPatternConformances()) {
44264433
diagnosed |= diagnoseConformanceAvailability(
44274434
loc, patternConf, where, depTy, replacementTy,
4428-
warnIfConformanceUnavailablePreSwift6);
4435+
warnIfConformanceUnavailablePreSwift6,
4436+
preconcurrency);
44294437
}
44304438
return diagnosed;
44314439
}
@@ -4444,7 +4452,8 @@ swift::diagnoseConformanceAvailability(SourceLoc loc,
44444452
if (auto builtinConformance = dyn_cast<BuiltinProtocolConformance>(rootConf)){
44454453
if (builtinConformance->isMissing()) {
44464454
diagnoseMissingConformance(loc, builtinConformance->getType(),
4447-
builtinConformance->getProtocol(), DC);
4455+
builtinConformance->getProtocol(), DC,
4456+
preconcurrency);
44484457
}
44494458
}
44504459

@@ -4498,7 +4507,8 @@ swift::diagnoseConformanceAvailability(SourceLoc loc,
44984507
SubstitutionMap subConformanceSubs = concreteConf->getSubstitutionMap();
44994508
if (diagnoseSubstitutionMapAvailability(loc, subConformanceSubs, where,
45004509
depTy, replacementTy,
4501-
warnIfConformanceUnavailablePreSwift6))
4510+
warnIfConformanceUnavailablePreSwift6,
4511+
preconcurrency))
45024512
return true;
45034513

45044514
return false;
@@ -4510,12 +4520,14 @@ swift::diagnoseSubstitutionMapAvailability(SourceLoc loc,
45104520
const ExportContext &where,
45114521
Type depTy, Type replacementTy,
45124522
bool warnIfConformanceUnavailablePreSwift6,
4513-
bool suppressParameterizationCheckForOptional) {
4523+
bool suppressParameterizationCheckForOptional,
4524+
bool preconcurrency) {
45144525
bool hadAnyIssues = false;
45154526
for (ProtocolConformanceRef conformance : subs.getConformances()) {
45164527
if (diagnoseConformanceAvailability(loc, conformance, where,
45174528
depTy, replacementTy,
4518-
warnIfConformanceUnavailablePreSwift6))
4529+
warnIfConformanceUnavailablePreSwift6,
4530+
preconcurrency))
45194531
hadAnyIssues = true;
45204532
}
45214533

lib/Sema/TypeCheckAvailability.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ diagnoseConformanceAvailability(SourceLoc loc,
228228
const ExportContext &context,
229229
Type depTy=Type(),
230230
Type replacementTy=Type(),
231-
bool warnIfConformanceUnavailablePreSwift6 = false);
231+
bool warnIfConformanceUnavailablePreSwift6 = false,
232+
bool preconcurrency = false);
232233

233234
bool diagnoseSubstitutionMapAvailability(
234235
SourceLoc loc,
@@ -237,7 +238,8 @@ bool diagnoseSubstitutionMapAvailability(
237238
Type depTy = Type(),
238239
Type replacementTy = Type(),
239240
bool warnIfConformanceUnavailablePreSwift6 = false,
240-
bool suppressParameterizationCheckForOptional = false);
241+
bool suppressParameterizationCheckForOptional = false,
242+
bool preconcurrency = false);
241243

242244
/// Diagnose uses of unavailable declarations. Returns true if a diagnostic
243245
/// was emitted.

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,9 +1233,10 @@ bool swift::diagnoseNonSendableTypesInReference(
12331233
}
12341234

12351235
void swift::diagnoseMissingSendableConformance(
1236-
SourceLoc loc, Type type, const DeclContext *fromDC) {
1236+
SourceLoc loc, Type type, const DeclContext *fromDC, bool preconcurrency) {
1237+
SendableCheckContext sendableContext(fromDC, preconcurrency);
12371238
diagnoseNonSendableTypes(
1238-
type, fromDC, /*inDerivedConformance*/Type(),
1239+
type, sendableContext, /*inDerivedConformance*/Type(),
12391240
loc, diag::non_sendable_type);
12401241
}
12411242

lib/Sema/TypeCheckConcurrency.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ bool diagnoseNonSendableTypesInReference(
320320

321321
/// Produce a diagnostic for a missing conformance to Sendable.
322322
void diagnoseMissingSendableConformance(
323-
SourceLoc loc, Type type, const DeclContext *fromDC);
323+
SourceLoc loc, Type type, const DeclContext *fromDC, bool preconcurrency);
324324

325325
/// If the given nominal type is public and does not explicitly
326326
/// state whether it conforms to Sendable, provide a diagnostic.

test/Concurrency/predates_concurrency_swift6.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,19 @@ func conversionDowngrade() {
173173
withSendableClosure(ns)
174174
// expected-warning@-1 {{converting non-sendable function value to '@Sendable () -> Void' may introduce data races}}
175175
}
176+
177+
@preconcurrency
178+
func requireSendable<T: Sendable>(_: T) {}
179+
180+
@preconcurrency
181+
struct RequireSendable<T: Sendable> {}
182+
183+
class NotSendable {} // expected-note 2 {{class 'NotSendable' does not conform to the 'Sendable' protocol}}
184+
185+
typealias T = RequireSendable<NotSendable>
186+
// expected-warning@-1 {{type 'NotSendable' does not conform to the 'Sendable' protocol}}
187+
188+
func testRequirementDowngrade(ns: NotSendable) {
189+
requireSendable(ns)
190+
// expected-warning@-1 {{type 'NotSendable' does not conform to the 'Sendable' protocol}}
191+
}

test/Concurrency/sendable_objc_attr_in_type_context_swift6.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,13 @@ func test_sendable_attr_in_type_context(test: Test) {
104104

105105
// TOOD(diagnostics): Duplicate diagnostics
106106
TestWithSendableID().add(MyValue())
107-
// expected-error@-1 3 {{type 'MyValue' does not conform to the 'Sendable' protocol}}
107+
// expected-warning@-1 3 {{type 'MyValue' does not conform to the 'Sendable' protocol}}
108108

109109
TestWithSendableSuperclass().add(SendableMyValue()) // Ok
110110

111111
// TOOD(diagnostics): Duplicate diagnostics
112112
TestWithSendableSuperclass().add(MyValue())
113-
// expected-error@-1 3 {{type 'MyValue' does not conform to the 'Sendable' protocol}}
113+
// expected-warning@-1 3 {{type 'MyValue' does not conform to the 'Sendable' protocol}}
114114
}
115115

116116
class TestConformanceWithStripping : InnerSendableTypes {

0 commit comments

Comments
 (0)