Skip to content

Commit 86adffa

Browse files
committed
diag-sema: plain protocol suggests some or any
1 parent f787e8f commit 86adffa

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5792,6 +5792,15 @@ ERROR(incorrect_optional_any,none,
57925792
ERROR(existential_requires_any,none,
57935793
"use of %select{protocol |}2%0 as a type must be written %1",
57945794
(Type, Type, bool))
5795+
ERROR(existential_requires_any_or_some,none,
5796+
"use of %select{protocol |}3%0 as a type must be written %1 or %2",
5797+
(Type, Type, StringRef, bool))
5798+
NOTE(replace_with_any,none,
5799+
"replace %0 with %1",
5800+
(Type, Type))
5801+
NOTE(replace_with_some,none,
5802+
"replace %0 with %1",
5803+
(Type, StringRef))
57955804
ERROR(inverse_requires_any,none,
57965805
"constraint that suppresses conformance requires 'any'", ())
57975806

lib/Sema/TypeCheckType.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6284,6 +6284,25 @@ class ExistentialTypeSyntaxChecker : public ASTWalker {
62846284
}
62856285

62866286
if (auto *proto = dyn_cast<ProtocolDecl>(decl)) {
6287+
if (proto->existentialRequiresAny()) {
6288+
//Emit fixit to add any keyword
6289+
Ctx.Diags.diagnose(T->getNameLoc(), diag::replace_with_any,
6290+
proto->getDeclaredInterfaceType(),
6291+
proto->getDeclaredExistentialType())
6292+
.fixItReplace(T->getSourceRange(), "any " + proto->getDeclaredInterfaceType()->getString());
6293+
//Emit fixit to add some keyword
6294+
Ctx.Diags.diagnose(T->getNameLoc(), diag::replace_with_some,
6295+
proto->getDeclaredInterfaceType(),
6296+
("'some " + proto->getDeclaredInterfaceType()->getString() + "'"))
6297+
.fixItReplace(T->getSourceRange(), "some " + proto->getDeclaredInterfaceType()->getString());
6298+
auto diag =
6299+
Ctx.Diags.diagnose(T->getNameLoc(), diag::existential_requires_any_or_some,
6300+
proto->getDeclaredInterfaceType(),
6301+
proto->getDeclaredExistentialType(),
6302+
("'some " + proto->getDeclaredInterfaceType()->getString() + "'"),
6303+
/*isAlias=*/false);
6304+
emitInsertAnyFixit(diag, T);
6305+
}
62876306
if (proto->existentialRequiresAny() && isAnyOrSomeMissing()) {
62886307
auto diag =
62896308
Ctx.Diags.diagnose(T->getNameLoc(), diag::existential_requires_any,

test/decl/nested/protocol.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,11 @@ struct Outer {
356356
}
357357
}
358358
}
359+
360+
// https://github.com/apple/swift/issues/68284
361+
protocol P {
362+
associatedtype A
363+
}
364+
func generic(value: P) {} // expected-error {{use of protocol 'P' as a type must be written 'any P' or 'some P'}}
365+
// expected-note@-1 {{replace 'P' with 'any P'}} {{21-22=any P}}
366+
// expected-note@-2 {{replace 'P' with 'some P'}} {{21-22=some P}}

0 commit comments

Comments
 (0)