Skip to content

Commit ca6f3c4

Browse files
authored
Merge pull request #59967 from CodaFi/prime-cuts
Improve Diagnostics for Over-Constrained Protocol Types
2 parents b9afb79 + f51b4a3 commit ca6f3c4

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3762,7 +3762,8 @@ ERROR(protocol_declares_unknown_primary_assoc_type,none,
37623762
ERROR(protocol_declares_duplicate_primary_assoc_type,none,
37633763
"duplicate primary associated type name %0", (Identifier))
37643764
ERROR(protocol_does_not_have_primary_assoc_type,none,
3765-
"cannot specialize protocol type %0", (Type))
3765+
"protocol %0 does not have primary associated types that can be "
3766+
"constrained", (Type))
37663767
ERROR(parameterized_protocol_type_argument_count_mismatch,none,
37673768
"protocol type %0 specialized with %select{too many|too few}3 type arguments "
37683769
"(got %1, but expected %2)",

lib/Sema/TypeCheckType.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,12 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
621621
auto assocTypes = protoDecl->getPrimaryAssociatedTypes();
622622
if (assocTypes.empty()) {
623623
diags.diagnose(loc, diag::protocol_does_not_have_primary_assoc_type,
624-
protoType);
625-
624+
protoType)
625+
.fixItRemove(generic->getAngleBrackets());
626+
if (!protoDecl->isImplicit()) {
627+
diags.diagnose(protoDecl, diag::decl_declared_here,
628+
protoDecl->getName());
629+
}
626630
return ErrorType::get(ctx);
627631
}
628632

test/Generics/generic_types.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %target-typecheck-verify-swift
22

3-
protocol MyFormattedPrintable {
3+
protocol MyFormattedPrintable { // expected-note 3 {{declared here}}
44
func myFormat() -> String
55
}
66

@@ -258,3 +258,10 @@ extension X8 where T == MetatypeTypeResolutionProto {
258258
static var property2: T.Type { property1 } // ok, still .Protocol
259259
static func method2() -> T.Type { method1() } // ok, still .Protocol
260260
}
261+
262+
func bogusProtocolConstraint1(_ : any MyFormattedPrintable<String>) {}
263+
// expected-error@-1 {{protocol 'MyFormattedPrintable' does not have primary associated types that can be constrained}}{{59-67=}}
264+
func bogusProtocolConstraint2(_ : some MyFormattedPrintable<String>) {}
265+
// expected-error@-1 {{protocol 'MyFormattedPrintable' does not have primary associated types that can be constrained}}{{60-68=}}
266+
func bogusProtocolConstraint3(_ : MyFormattedPrintable<String>) {}
267+
// expected-error@-1 {{protocol 'MyFormattedPrintable' does not have primary associated types that can be constrained}}{{55-63=}}

0 commit comments

Comments
 (0)