Skip to content

Commit 215c4e1

Browse files
committed
NCGenerics: restrict conditional Copyable reqs
It doesn't really make sense for a conditional conformance requirement for `Copyable` to depend on any other requirement other than other `Copyable` conformance requirements. resolves rdar://124967739
1 parent 3bc570f commit 215c4e1

File tree

7 files changed

+92
-6
lines changed

7 files changed

+92
-6
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2791,7 +2791,7 @@ ERROR(protocol_has_missing_requirements_versioned,none,
27912791
(Type, Type, llvm::VersionTuple, llvm::VersionTuple))
27922792
ERROR(requirement_restricts_self,none,
27932793
"%kindonly0 requirement %0 cannot add constraint "
2794-
"'%1%select{:|:| ==|:}2 %3' on 'Self'",
2794+
"'%1%select{:|:| ==|:| has same shape as}2 %3' on 'Self'",
27952795
(const ValueDecl *, StringRef, unsigned, StringRef))
27962796
ERROR(witness_argument_name_mismatch,none,
27972797
"%kind0 has different argument labels "
@@ -7644,6 +7644,10 @@ ERROR(inverse_extension, none,
76447644
ERROR(copyable_illegal_deinit, none,
76457645
"deinitializer cannot be declared in %kind0 that conforms to 'Copyable'",
76467646
(const ValueDecl *))
7647+
ERROR(inverse_cannot_be_conditional_on_requirement, none,
7648+
"conditional conformance to suppressible %kind0 cannot depend on "
7649+
"'%1%select{:|:| ==|:| has same shape as}2 %3'",
7650+
(const ProtocolDecl *, StringRef, unsigned, StringRef))
76477651
ERROR(inverse_type_member_in_conforming_type,none,
76487652
"%select{stored property %2|associated value %2}1 of "
76497653
"'%4'-conforming %kind3 has non-%4 type %0",

include/swift/AST/RequirementKind.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ enum class RequirementKind : unsigned {
3636
Layout,
3737
/// A same-shape requirement shape(T) == shape(U), where T and U are pack
3838
/// parameters.
39-
SameShape
39+
SameShape,
4040

4141
// Note: there is code that packs this enum in a 3-bit bitfield. Audit users
4242
// when adding enumerators.
43+
LAST_KIND=SameShape
4344
};
4445

4546
} // namespace swift

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ void TypeChecker::checkProtocolSelfRequirements(ValueDecl *decl) {
302302
req.getFirstType()->is<GenericTypeParamType>())
303303
continue;
304304

305+
static_assert((unsigned)RequirementKind::LAST_KIND == 4,
306+
"update %select in diagnostic!");
305307
ctx.Diags.diagnose(decl, diag::requirement_restricts_self, decl,
306308
req.getFirstType().getString(),
307309
static_cast<unsigned>(req.getKind()),

lib/Sema/TypeCheckInvertible.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,50 @@ static void checkInvertibleConformanceCommon(DeclContext *dc,
140140
if (conformance.isConcrete()) {
141141
auto concrete = conformance.getConcrete();
142142
if (auto *normalConf = dyn_cast<NormalProtocolConformance>(concrete)) {
143-
hasUnconditionalConformance =
144-
normalConf->getConditionalRequirements().empty();
145143
conformanceLoc = normalConf->getLoc();
146144
assert(conformanceLoc);
145+
146+
auto condReqs = normalConf->getConditionalRequirements();
147+
hasUnconditionalConformance = condReqs.empty();
148+
auto *thisProto = normalConf->getProtocol();
149+
150+
// Ensure that conditional conformance to an invertible protocol IP only
151+
// depends conformance requirements involving IP, and its subject is not
152+
// a dependent member type.
153+
//
154+
// In theory, it could depend on any invertible protocol, but it may be
155+
// confusing if we permitted that and this simplifies the model a bit.
156+
for (auto req : condReqs) {
157+
std::optional<StringRef> illegalSecondTypeStr;
158+
159+
// If we are diagnosing, fill-in the second-type string of this req.
160+
switch (req.getKind()) {
161+
case RequirementKind::Layout:
162+
illegalSecondTypeStr = req.getLayoutConstraint().getString();
163+
break;
164+
case RequirementKind::Conformance:
165+
if (req.getProtocolDecl() == thisProto
166+
&& !req.getFirstType()->is<DependentMemberType>())
167+
break; // permitted, don't fill-in.
168+
LLVM_FALLTHROUGH;
169+
case RequirementKind::Superclass:
170+
case RequirementKind::SameType:
171+
case RequirementKind::SameShape:
172+
illegalSecondTypeStr = req.getSecondType().getString();
173+
break;
174+
}
175+
176+
static_assert((unsigned)RequirementKind::LAST_KIND == 4,
177+
"update %select in diagnostic!");
178+
if (illegalSecondTypeStr) {
179+
ctx.Diags.diagnose(conformanceLoc,
180+
diag::inverse_cannot_be_conditional_on_requirement,
181+
thisProto,
182+
req.getFirstType().getString(),
183+
static_cast<unsigned>(req.getKind()),
184+
*illegalSecondTypeStr);
185+
}
186+
}
147187
}
148188
}
149189
assert(!conformance.isPack() && "not handled");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %target-typecheck-verify-swift \
2+
// RUN: -enable-experimental-feature NoncopyableGenerics \
3+
// RUN: -enable-experimental-feature NonescapableTypes \
4+
// RUN: -enable-experimental-feature SuppressedAssociatedTypes
5+
6+
protocol P {}
7+
protocol Q {}
8+
class DoggoClass {}
9+
10+
struct Blah<T: ~Copyable & ~Escapable>: ~Copyable, ~Escapable {}
11+
extension Blah: Copyable {} // expected-error {{conditional conformance to suppressible protocol 'Copyable' cannot depend on 'T: Escapable'}}
12+
extension Blah: Escapable {} // expected-error {{conditional conformance to suppressible protocol 'Escapable' cannot depend on 'T: Copyable'}}
13+
14+
struct Yuck<T: ~Copyable & ~Escapable>: ~Copyable, ~Escapable {}
15+
extension Yuck: Copyable where T: ~Escapable {}
16+
extension Yuck: Escapable where T: ~Copyable {}
17+
18+
struct TryConformance<Whatever: ~Copyable>: ~Copyable {}
19+
extension TryConformance: Copyable
20+
where Whatever: P, Whatever: Q, Whatever: Sendable {}
21+
// expected-error@-2 {{conditional conformance to suppressible protocol 'Copyable' cannot depend on 'Whatever: P'}}
22+
// expected-error@-3 {{conditional conformance to suppressible protocol 'Copyable' cannot depend on 'Whatever: Q'}}
23+
// expected-error@-4 {{conditional conformance to suppressible protocol 'Copyable' cannot depend on 'Whatever: Sendable'}}
24+
25+
struct TrySameType<Whatever: ~Copyable>: ~Copyable {}
26+
extension TrySameType: Copyable
27+
where Whatever == Int {}
28+
// expected-error@-2 {{conditional conformance to suppressible protocol 'Copyable' cannot depend on 'Whatever == Int'}}
29+
30+
struct TryClassAndLayoutConstraints<Whatever: ~Copyable, Heckin>: ~Copyable {}
31+
extension TryClassAndLayoutConstraints: Copyable
32+
where Heckin: DoggoClass, Whatever: AnyObject {}
33+
// expected-error@-2 {{conditional conformance to suppressible protocol 'Copyable' cannot depend on 'Whatever: AnyObject'}}
34+
// expected-error@-3 {{conditional conformance to suppressible protocol 'Copyable' cannot depend on 'Heckin: DoggoClass'}}
35+
36+
protocol Queue: ~Copyable { associatedtype Job: ~Copyable }
37+
struct Scheduler<Q: Queue>: ~Copyable {}
38+
extension Scheduler: Copyable where Q.Job: Copyable {}
39+
// expected-error@-1 {{conditional conformance to suppressible protocol 'Copyable' cannot depend on 'Q.Job: Copyable'}}

test/ModuleInterface/Inputs/NoncopyableGenerics_Misc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ extension Outer: Copyable {}
9898
extension Outer.InnerStruct: Copyable {}
9999

100100
extension Outer.InnerVariation1: Copyable {}
101-
extension Outer.InnerVariation2: Escapable {}
101+
extension Outer.InnerVariation2: Escapable where A: ~Copyable {}
102102

103103
extension Outer.InnerStruct {
104104
public func hello<T: ~Escapable>(_ t: T) {}

test/ModuleInterface/noncopyable_generics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ import NoncopyableGenerics_Misc
134134
// CHECK-MISC: #endif
135135

136136
// CHECK-MISC: #if compiler(>=5.3) && $NoncopyableGenerics
137-
// CHECK-MISC-NEXT: extension {{.*}}.Outer.InnerVariation2 : Swift.Escapable {
137+
// CHECK-MISC-NEXT: extension {{.*}}.Outer.InnerVariation2 : Swift.Escapable where A : ~Copyable {
138138
// CHECK-MISC: #endif
139139

140140
// CHECK-MISC: #if compiler(>=5.3) && $NoncopyableGenerics

0 commit comments

Comments
 (0)