Skip to content

Commit 6d1f654

Browse files
committed
Sema: Better diagnostic when parameterized protocol is used in an unsupported context
1 parent 8cc0ffb commit 6d1f654

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3788,8 +3788,6 @@ ERROR(protocol_declares_unknown_primary_assoc_type,none,
37883788
(Identifier, Type))
37893789
ERROR(protocol_declares_duplicate_primary_assoc_type,none,
37903790
"duplicate primary associated type name %0", (Identifier))
3791-
ERROR(parameterized_protocol_not_supported,none,
3792-
"protocol type with type arguments can only be used as a generic constraint", ())
37933791
ERROR(protocol_does_not_have_primary_assoc_type,none,
37943792
"cannot specialize protocol type %0", (Type))
37953793
ERROR(parameterized_protocol_type_argument_count_mismatch,none,

lib/Sema/TypeCheckType.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -629,15 +629,8 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
629629
auto &diags = ctx.Diags;
630630

631631
if (auto *protoType = type->getAs<ProtocolType>()) {
632-
// Build ParameterizedProtocolType if the protocol has a primary associated
633-
// type and we're in a supported context (for now just generic requirements,
634-
// inheritance clause, extension binding).
635-
if (!resolution.getOptions().isParameterizedProtocolSupported()) {
636-
diags.diagnose(loc, diag::parameterized_protocol_not_supported);
637-
return ErrorType::get(ctx);
638-
}
639-
640632
auto *protoDecl = protoType->getDecl();
633+
641634
auto assocTypes = protoDecl->getPrimaryAssociatedTypes();
642635
if (assocTypes.empty()) {
643636
diags.diagnose(loc, diag::protocol_does_not_have_primary_assoc_type,
@@ -657,6 +650,18 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
657650
return ErrorType::get(ctx);
658651
}
659652

653+
// Build ParameterizedProtocolType if the protocol has a primary associated
654+
// type and we're in a supported context (for now just generic requirements,
655+
// inheritance clause, extension binding).
656+
if (!resolution.getOptions().isParameterizedProtocolSupported()) {
657+
diags.diagnose(loc, diag::existential_requires_any,
658+
protoDecl->getDeclaredInterfaceType(),
659+
protoDecl->getExistentialType(),
660+
/*isAlias=*/isa<TypeAliasType>(type.getPointer()));
661+
662+
return ErrorType::get(ctx);
663+
}
664+
660665
// Disallow opaque types anywhere in the structure of the generic arguments
661666
// to a parameterized existential type.
662667
if (options.is(TypeResolverContext::ExistentialConstraint))

test/type/parameterized_existential.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ protocol Sequence<Element> { // expected-note {{'Sequence' declared here}}
77
// 'any' is required here
88

99
func takesSequenceOfInt1(_: Sequence<Int>) {}
10-
// expected-error@-1 {{protocol type with type arguments can only be used as a generic constraint}}
10+
// expected-error@-1 {{use of protocol 'Sequence' as a type must be written 'any Sequence'}}
1111

1212
func returnsSequenceOfInt1() -> Sequence<Int> {}
13-
// expected-error@-1 {{protocol type with type arguments can only be used as a generic constraint}}
13+
// expected-error@-1 {{use of protocol 'Sequence' as a type must be written 'any Sequence'}}
1414

1515
struct ConcreteSequence<Element> : Sequence {}
1616

@@ -83,7 +83,7 @@ func splay(_ x: some Pair<Int, String>) -> (Int, String) { fatalError() }
8383

8484
func typeExpr() {
8585
_ = Sequence<Int>.self
86-
// expected-error@-1 {{protocol type with type arguments can only be used as a generic constraint}}
86+
// expected-error@-1 {{use of protocol 'Sequence' as a type must be written 'any Sequence'}}
8787

8888
_ = any Sequence<Int>.self
8989
// expected-error@-1 {{'self' is not a member type of protocol 'parameterized_existential.Sequence<Swift.Int>'}}

0 commit comments

Comments
 (0)