Skip to content

[FixIt]: Diagnostic for use of plain protocol name in type position suggests some or any #74197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5842,6 +5842,15 @@ ERROR(incorrect_optional_any,none,
ERROR(existential_requires_any,none,
"use of %select{protocol |}2%0 as a type must be written %1",
(Type, Type, bool))
ERROR(existential_requires_any_or_some,none,
"use of %select{protocol |}3%0 as a type must be written %1 or %2",
(Type, Type, StringRef, bool))
NOTE(replace_with_any,none,
"replace %0 with %1",
(Type, Type))
NOTE(replace_with_some,none,
"replace %0 with %1",
(Type, StringRef))
ERROR(inverse_requires_any,none,
"constraint that suppresses conformance requires 'any'", ())

Expand Down
19 changes: 15 additions & 4 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6402,11 +6402,22 @@ class ExistentialTypeSyntaxChecker : public ASTWalker {

if (auto *proto = dyn_cast<ProtocolDecl>(decl)) {
if (proto->existentialRequiresAny() && isAnyOrSomeMissing()) {
//Emit fixit to add any keyword
Ctx.Diags.diagnose(T->getNameLoc(), diag::replace_with_any,
proto->getDeclaredInterfaceType(),
proto->getDeclaredExistentialType())
.fixItReplace(T->getSourceRange(), "any " + proto->getDeclaredInterfaceType()->getString());
//Emit fixit to add some keyword
Ctx.Diags.diagnose(T->getNameLoc(), diag::replace_with_some,
proto->getDeclaredInterfaceType(),
("'some " + proto->getDeclaredInterfaceType()->getString() + "'"))
.fixItReplace(T->getSourceRange(), "some " + proto->getDeclaredInterfaceType()->getString());
auto diag =
Ctx.Diags.diagnose(T->getNameLoc(), diag::existential_requires_any,
proto->getDeclaredInterfaceType(),
proto->getDeclaredExistentialType(),
/*isAlias=*/false);
Ctx.Diags.diagnose(T->getNameLoc(), diag::existential_requires_any_or_some,
proto->getDeclaredInterfaceType(),
proto->getDeclaredExistentialType(),
("'some " + proto->getDeclaredInterfaceType()->getString() + "'"),
/*isAlias=*/false);
diag.warnUntilSwiftVersionIf(warnUntilSwift7, 7);
emitInsertAnyFixit(diag, T);
}
Expand Down
8 changes: 8 additions & 0 deletions test/decl/protocol/protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -575,3 +575,11 @@ struct S0_53813 {
c0.foo3 = self // expected-error {{cannot assign value of type 'S0_53813' to type '(any P4_53813)?'}}
}
}

// https://github.com/apple/swift/issues/68284
protocol P {
associatedtype A
}
func generic(value: P) {} // expected-error {{use of protocol 'P' as a type must be written 'any P' or 'some P'}}
// expected-note@-1 {{replace 'P' with 'any P'}} {{21-22=any P}}
// expected-note@-2 {{replace 'P' with 'some P'}} {{21-22=some P}}