Skip to content

Commit 7ac96f8

Browse files
authored
Merge pull request #63851 from angela-laar/fix-existential-parameterized-protocol-diagnostic
Fix existential parameterized protocol diagnostic
2 parents 184b514 + 4b64dc9 commit 7ac96f8

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -758,14 +758,32 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
758758
}
759759

760760
// Build ParameterizedProtocolType if the protocol has a primary associated
761-
// type and we're in a supported context (for now just generic requirements,
762-
// inheritance clause, extension binding).
761+
// type and we're in a supported context.
763762
if (resolution.getOptions().isConstraintImplicitExistential() &&
764763
!ctx.LangOpts.hasFeature(Feature::ImplicitSome)) {
765-
diags.diagnose(loc, diag::existential_requires_any,
766-
protoDecl->getDeclaredInterfaceType(),
767-
protoDecl->getDeclaredExistentialType(),
768-
/*isAlias=*/isa<TypeAliasType>(type.getPointer()));
764+
765+
if (!genericArgs.empty()) {
766+
767+
SmallVector<Type, 2> argTys;
768+
for (auto *genericArg : genericArgs) {
769+
Type argTy = resolution.resolveType(genericArg);
770+
if (!argTy || argTy->hasError())
771+
return ErrorType::get(ctx);
772+
773+
argTys.push_back(argTy);
774+
}
775+
776+
auto parameterized =
777+
ParameterizedProtocolType::get(ctx, protoType, argTys);
778+
diags.diagnose(loc, diag::existential_requires_any, parameterized,
779+
ExistentialType::get(parameterized),
780+
/*isAlias=*/isa<TypeAliasType>(type.getPointer()));
781+
} else {
782+
diags.diagnose(loc, diag::existential_requires_any,
783+
protoDecl->getDeclaredInterfaceType(),
784+
protoDecl->getDeclaredExistentialType(),
785+
/*isAlias=*/isa<TypeAliasType>(type.getPointer()));
786+
}
769787

770788
return ErrorType::get(ctx);
771789
}

test/type/explicit_existential_swift6.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,16 @@ protocol Collection<T> {
145145
associatedtype T
146146
}
147147

148+
struct TestParameterizedProtocol<T> : Collection {
149+
typealias T = T
150+
151+
let x : Collection<T> // expected-error {{use of protocol 'Collection<T>' as a type must be written 'any Collection<T>'}}
152+
}
153+
148154
func acceptAny(_: Collection<Int>) {}
149-
// expected-error@-1 {{use of protocol 'Collection' as a type must be written 'any Collection'}}
155+
// expected-error@-1 {{use of protocol 'Collection<Int>' as a type must be written 'any Collection<Int>'}}
150156
func returnsAny() -> Collection<Int> {}
151-
// expected-error@-1 {{use of protocol 'Collection' as a type must be written 'any Collection'}}
157+
// expected-error@-1 {{use of protocol 'Collection<Int>' as a type must be written 'any Collection<Int>'}}
152158

153159
func testInvalidAny() {
154160
struct S: HasAssoc {

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 {{use of protocol 'Sequence' as a type must be written 'any Sequence'}}
10+
// expected-error@-1 {{use of protocol 'Sequence<Int>' as a type must be written 'any Sequence<Int>'}}
1111

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

1515
struct ConcreteSequence<Element> : Sequence {}
1616

@@ -74,7 +74,7 @@ func saturation(_ dry: any Sponge, _ wet: any Sponge<Int, Int>) {
7474

7575
func typeExpr() {
7676
_ = Sequence<Int>.self
77-
// expected-error@-1 {{use of protocol 'Sequence' as a type must be written 'any Sequence'}}
77+
// expected-error@-1 {{use of protocol 'Sequence<Int>' as a type must be written 'any Sequence<Int>'}}
7878

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

0 commit comments

Comments
 (0)