Skip to content

Commit 465fa2a

Browse files
committed
NCGenerics: omit inverses in stub diagnostic
fixes rdar://121077877
1 parent 312ac1b commit 465fa2a

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

include/swift/AST/Requirement.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
namespace swift {
3030
class GenericContext;
31+
class GenericTypeParamType;
3132

3233
/// Return type of Requirement::checkRequirement().
3334
enum class CheckRequirementResult : uint8_t {
@@ -264,6 +265,9 @@ struct InverseRequirement {
264265
static void expandDefaults(ASTContext &ctx,
265266
ArrayRef<Type> gps,
266267
SmallVectorImpl<StructuralRequirement> &result);
268+
static void expandDefaults(ASTContext &ctx,
269+
ArrayRef<GenericTypeParamType *> gps,
270+
SmallVectorImpl<Requirement> &result);
267271
};
268272

269273
} // end namespace swift

lib/AST/Requirement.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,17 @@ void InverseRequirement::expandDefaults(
366366
}
367367
}
368368
}
369+
370+
void InverseRequirement::expandDefaults(ASTContext &ctx,
371+
ArrayRef<GenericTypeParamType *> gps,
372+
SmallVectorImpl<Requirement> &result) {
373+
SmallVector<Type, 4> gpsAsType;
374+
for (auto *gp : gps)
375+
gpsAsType.push_back(gp);
376+
377+
SmallVector<StructuralRequirement, 8> structReqs;
378+
expandDefaults(ctx, gpsAsType, structReqs);
379+
380+
for (auto sreq : structReqs)
381+
result.push_back(sreq.req);
382+
}

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,10 +2495,15 @@ static Type getTypeForDisplay(ModuleDecl *module, ValueDecl *decl) {
24952495

24962496
GenericSignature sigWithoutReqts;
24972497
if (auto genericFn = type->getAs<GenericFunctionType>()) {
2498-
// For generic functions, build a new generic function... but strip off
2499-
// the requirements. They don't add value.
2498+
// For generic functions, build a new generic function... but without
2499+
// any explicit requirements. They don't add value. We need to provide the
2500+
// implicit requirements so that inverses like ~Copyable do not appear.
2501+
auto genericParams = genericFn->getGenericParams();
2502+
SmallVector<Requirement> implicitReqs;
2503+
InverseRequirement::expandDefaults(genericFn->getASTContext(),
2504+
genericParams, implicitReqs);
25002505
sigWithoutReqts
2501-
= GenericSignature::get(genericFn->getGenericParams(), {});
2506+
= GenericSignature::get(genericFn->getGenericParams(), implicitReqs);
25022507
}
25032508

25042509
// For functions, strip off the 'Self' parameter clause.

test/Generics/inverse_misc.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-typecheck-verify-swift \
2+
// RUN: -parse-stdlib -module-name Swift \
3+
// RUN: -enable-experimental-feature NoncopyableGenerics
4+
5+
// NOTE: -parse-stdlib is a transitional workaround and should not be required.
6+
7+
// rdar://121077877 (wrong generic signature in protocol stub diagnostic)
8+
protocol Subscriber {}
9+
protocol P {
10+
func receive<T>(subscriber: T) where T : Subscriber
11+
// expected-note@-1 {{protocol requires function 'receive(subscriber:)' with type '<T> (subscriber: T) -> ()'; add a stub for conformance}}
12+
}
13+
struct S: P { // expected-error {{type 'S' does not conform to protocol 'P'}}
14+
}

0 commit comments

Comments
 (0)