Skip to content

[6.1] TypeCheckType: Unconditionally warn about missing existential any until a future language mode #78389

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

Merged
merged 2 commits into from
Feb 19, 2025
Merged
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
1 change: 1 addition & 0 deletions include/swift/AST/DiagnosticGroups.def
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ GROUP(no_group, "")
GROUP(DeprecatedDeclaration, "DeprecatedDeclaration.md")
GROUP(Unsafe, "Unsafe.md")
GROUP(UnknownWarningGroup, "UnknownWarningGroup.md")
GROUP(ExistentialAny, "ExistentialAny.md")

#define UNDEFINE_DIAGNOSTIC_GROUPS_MACROS
#include "swift/AST/DefineDiagnosticGroupsMacros.h"
11 changes: 6 additions & 5 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5817,11 +5817,12 @@ ERROR(any_not_existential,none,
ERROR(incorrect_optional_any,none,
"optional 'any' type must be written %0",
(Type))
ERROR(existential_requires_any,none,
"use of %select{protocol |}2%0 as a type must be written %1",
(Type, Type, bool))
ERROR(inverse_requires_any,none,
"constraint that suppresses conformance requires 'any'", ())

GROUPED_ERROR(existential_requires_any,ExistentialAny,none,
"use of %select{protocol |}2%0 as a type must be written %1",
(Type, Type, bool))
GROUPED_ERROR(inverse_requires_any,ExistentialAny,none,
"constraint that suppresses conformance requires 'any'", ())

ERROR(nonisolated_mutable_storage,none,
"'nonisolated' cannot be applied to mutable stored properties",
Expand Down
32 changes: 13 additions & 19 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,11 +895,15 @@ static Type applyGenericArguments(Type type,
auto parameterized =
ParameterizedProtocolType::get(ctx, protoType, argTys);

// FIXME: Can this not be done in ExistentialTypeSyntaxChecker?
if (resolution.getOptions().isConstraintImplicitExistential() &&
!ctx.LangOpts.hasFeature(Feature::ImplicitSome)) {
diags.diagnose(loc, diag::existential_requires_any, parameterized,
ExistentialType::get(parameterized),
/*isAlias=*/isa<TypeAliasType>(type.getPointer()));
diags
.diagnose(loc, diag::existential_requires_any, parameterized,
ExistentialType::get(parameterized),
/*isAlias=*/isa<TypeAliasType>(type.getPointer()))
.warnUntilSwiftVersion(7);

return ErrorType::get(ctx);
}

Expand Down Expand Up @@ -6115,16 +6119,13 @@ class ExistentialTypeSyntaxChecker : public ASTWalker {
ASTContext &Ctx;
bool checkStatements;
bool hitTopStmt;
bool warnUntilSwift7;

unsigned exprCount = 0;
llvm::SmallVector<TypeRepr *, 4> reprStack;

public:
ExistentialTypeSyntaxChecker(ASTContext &ctx, bool checkStatements,
bool warnUntilSwift7 = false)
: Ctx(ctx), checkStatements(checkStatements), hitTopStmt(false),
warnUntilSwift7(warnUntilSwift7) {}
ExistentialTypeSyntaxChecker(ASTContext &ctx, bool checkStatements)
: Ctx(ctx), checkStatements(checkStatements), hitTopStmt(false) {}

MacroWalking getMacroWalkingBehavior() const override {
return MacroWalking::ArgumentsAndExpansion;
Expand Down Expand Up @@ -6338,7 +6339,7 @@ class ExistentialTypeSyntaxChecker : public ASTWalker {
inverse && isAnyOrSomeMissing()) {
auto diag = Ctx.Diags.diagnose(inverse->getTildeLoc(),
diag::inverse_requires_any);
diag.warnUntilSwiftVersionIf(warnUntilSwift7, 7);
diag.warnUntilSwiftVersion(7);
emitInsertAnyFixit(diag, T);
return;
}
Expand All @@ -6356,7 +6357,7 @@ class ExistentialTypeSyntaxChecker : public ASTWalker {
proto->getDeclaredInterfaceType(),
proto->getDeclaredExistentialType(),
/*isAlias=*/false);
diag.warnUntilSwiftVersionIf(warnUntilSwift7, 7);
diag.warnUntilSwiftVersion(7);
emitInsertAnyFixit(diag, T);
}
} else if (auto *alias = dyn_cast<TypeAliasDecl>(decl)) {
Expand Down Expand Up @@ -6387,7 +6388,7 @@ class ExistentialTypeSyntaxChecker : public ASTWalker {
alias->getDeclaredInterfaceType(),
ExistentialType::get(alias->getDeclaredInterfaceType()),
/*isAlias=*/true);
diag.warnUntilSwiftVersionIf(warnUntilSwift7, 7);
diag.warnUntilSwiftVersion(7);
emitInsertAnyFixit(diag, T);
}
}
Expand Down Expand Up @@ -6467,14 +6468,7 @@ void TypeChecker::checkExistentialTypes(ASTContext &ctx, Stmt *stmt,
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface)
return;

// Previously we missed this diagnostic on 'catch' statements, downgrade
// to a warning until Swift 7.
auto downgradeUntilSwift7 = false;
if (auto *CS = dyn_cast<CaseStmt>(stmt))
downgradeUntilSwift7 = CS->getParentKind() == CaseParentKind::DoCatch;

ExistentialTypeSyntaxChecker checker(ctx, /*checkStatements=*/true,
downgradeUntilSwift7);
ExistentialTypeSyntaxChecker checker(ctx, /*checkStatements=*/true);
stmt->walk(checker);
}

Expand Down
4 changes: 2 additions & 2 deletions test/Generics/inverse_generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@ func checkExistentials() {

typealias NotCopyable = ~Copyable
typealias EmptyComposition = ~Copyable & ~Escapable
func test(_ t: borrowing NotCopyable) {} // expected-error {{use of 'NotCopyable' (aka '~Copyable') as a type must be written 'any NotCopyable'}}
func test(_ t: borrowing EmptyComposition) {} // expected-error {{use of 'EmptyComposition' (aka '~Copyable & ~Escapable') as a type must be written 'any EmptyComposition' (aka 'any ~Copyable & ~Escapable')}}
func test(_ t: borrowing NotCopyable) {} // expected-warning {{use of 'NotCopyable' (aka '~Copyable') as a type must be written 'any NotCopyable'}}
func test(_ t: borrowing EmptyComposition) {} // expected-warning {{use of 'EmptyComposition' (aka '~Copyable & ~Escapable') as a type must be written 'any EmptyComposition' (aka 'any ~Copyable & ~Escapable')}}

typealias Copy = Copyable
func test(_ z1: Copy, _ z2: Copyable) {}
Expand Down
4 changes: 2 additions & 2 deletions test/Macros/macros_diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ struct MyStruct<T: MyProto> {

struct SomeType {
#genericUnary<Equatable>(0 as Hashable)
// expected-error@-1{{use of protocol 'Equatable' as a type must be written 'any Equatable'}}
// expected-error@-2{{use of protocol 'Hashable' as a type must be written 'any Hashable'}}
// expected-warning@-1{{use of protocol 'Equatable' as a type must be written 'any Equatable'}}
// expected-warning@-2{{use of protocol 'Hashable' as a type must be written 'any Hashable'}}
// expected-error@-3{{external macro implementation type}}
}

Expand Down
6 changes: 3 additions & 3 deletions test/Macros/top_level_freestanding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ func testGlobalVariable() {

// expected-note @+1 6 {{in expansion of macro 'anonymousTypes' here}}
#anonymousTypes(causeErrors: true) { "foo" }
// DIAG_BUFFERS-DAG: @__swiftmacro_9MacroUser0033top_level_freestandingswift_DbGHjfMX108_0_33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf0_{{.*}}: error: use of protocol 'Equatable' as a type must be written 'any Equatable'
// DIAG_BUFFERS-DAG: @__swiftmacro_9MacroUser00142___swiftmacro_9MacroUser0033top_level_freestandingswift_DbGHjfMX108_0_33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf0_swift_DAIABdjIbfMX23_2_33_082AE7CFEFA6960C804A9FE7366EB5A0Ll27introduceTypeCheckingErrorsfMf_{{.*}}: error: use of protocol 'Hashable' as a type must be written 'any Hashable'
// DIAG_BUFFERS-DAG: @__swiftmacro_9MacroUser0033top_level_freestandingswift_DbGHjfMX108_0_33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf0_{{.*}}: warning: use of protocol 'Equatable' as a type must be written 'any Equatable'
// DIAG_BUFFERS-DAG: @__swiftmacro_9MacroUser00142___swiftmacro_9MacroUser0033top_level_freestandingswift_DbGHjfMX108_0_33_082AE7CFEFA6960C804A9FE7366EB5A0Ll14anonymousTypesfMf0_swift_DAIABdjIbfMX23_2_33_082AE7CFEFA6960C804A9FE7366EB5A0Ll27introduceTypeCheckingErrorsfMf_{{.*}}: warning: use of protocol 'Hashable' as a type must be written 'any Hashable'

// expected-note @+1 2 {{in expansion of macro 'anonymousTypes' here}}
#anonymousTypes { () -> String in
// expected-error @+1 {{use of protocol 'Equatable' as a type must be written 'any Equatable'}}
// expected-warning @+1 {{use of protocol 'Equatable' as a type must be written 'any Equatable'}}
_ = 0 as Equatable
return "foo"
}
Expand Down
2 changes: 1 addition & 1 deletion test/decl/nested/protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ extension OuterProtocol {

struct ConformsToOuterProtocol : OuterProtocol {
typealias Hen = Int
func f() { let _ = InnerProtocol.self } // expected-error {{use of protocol 'InnerProtocol' as a type must be written 'any InnerProtocol'}}
func f() { let _ = InnerProtocol.self } // expected-warning {{use of protocol 'InnerProtocol' as a type must be written 'any InnerProtocol'}}
}

extension OuterProtocol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ protocol P {
protocol Q {}

do {
func test(p: P) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
func test(p: P) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
p.method(false) // expected-error {{member 'method' cannot be used on value of type 'any P'; consider using a generic constraint instead}} {{-1:16--1:17=some P}} {{none}}
}
}
do {
func test(p: ((P))) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
func test(p: ((P))) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
p.method(false) // expected-error {{member 'method' cannot be used on value of type 'any P'; consider using a generic constraint instead}} {{-1:18--1:19=some P}} {{none}}
}
}
Expand Down Expand Up @@ -57,12 +57,12 @@ do {
}
}
do {
func test(p: P.Type) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
func test(p: P.Type) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
p.staticMethod(false) // expected-error {{member 'staticMethod' cannot be used on value of type 'any P.Type'; consider using a generic constraint instead}} {{-1:16--1:17=(some P)}} {{none}}
}
}
do {
func test(p: (P).Type) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
func test(p: (P).Type) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
p.staticMethod(false) // expected-error {{member 'staticMethod' cannot be used on value of type 'any P.Type'; consider using a generic constraint instead}} {{-1:17--1:18=some P}} {{none}}
}
}
Expand All @@ -78,12 +78,12 @@ do {
}

do {
func test(p: P & Q) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
func test(p: P & Q) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
p.method(false) // expected-error {{member 'method' cannot be used on value of type 'any P & Q'; consider using a generic constraint instead}} {{-1:16--1:21=some P & Q}} {{none}}
}
}
do {
func test(p: ((P & Q))) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
func test(p: ((P & Q))) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
p.method(false) // expected-error {{member 'method' cannot be used on value of type 'any P & Q'; consider using a generic constraint instead}} {{-1:18--1:23=some P & Q}} {{none}}
}
}
Expand Down Expand Up @@ -123,12 +123,12 @@ do {
}
}
do {
func test(p: (P & Q).Type) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
func test(p: (P & Q).Type) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
p.staticMethod(false) // expected-error {{member 'staticMethod' cannot be used on value of type 'any (P & Q).Type'; consider using a generic constraint instead}} {{-1:17--1:22=some P & Q}} {{none}}
}
}
do {
func test(p: ((P & Q)).Type) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
func test(p: ((P & Q)).Type) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
p.staticMethod(false) // expected-error {{member 'staticMethod' cannot be used on value of type 'any (P & Q).Type'; consider using a generic constraint instead}} {{-1:18--1:23=some P & Q}} {{none}}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/decl/protocol/protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func i<T : C3>(_ x : T?) -> Bool {
// expected-warning@-1 {{checking a value with optional type 'T?' against type 'any P1' succeeds whenever the value is non-nil; did you mean to use '!= nil'?}}
}
func j(_ x : C1) -> Bool {
return x is P1 // expected-error {{use of protocol 'P1' as a type must be written 'any P1'}}
return x is P1 // expected-warning {{use of protocol 'P1' as a type must be written 'any P1'}}
}
func k(_ x : C1?) -> Bool {
return x is any P1
Expand Down
6 changes: 3 additions & 3 deletions test/decl/protocol/recursive_requirement.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %target-typecheck-verify-swift
// RUN: not %target-swift-frontend -typecheck %s -debug-generic-signatures 2>&1 | %FileCheck %s
// RUN: %target-swift-frontend -typecheck %s -debug-generic-signatures 2>&1 | %FileCheck %s

// -----

Expand Down Expand Up @@ -95,7 +95,7 @@ protocol AsExistentialB {
}

protocol AsExistentialAssocTypeA {
var delegate : AsExistentialAssocTypeB? { get } // expected-error {{use of protocol 'AsExistentialAssocTypeB' as a type must be written 'any AsExistentialAssocTypeB'}}
var delegate : (any AsExistentialAssocTypeB)? { get }
}
protocol AsExistentialAssocTypeB {
func aMethod(_ object : AsExistentialAssocTypeA)
Expand All @@ -107,7 +107,7 @@ protocol AsExistentialAssocTypeAgainA {
associatedtype Bar
}
protocol AsExistentialAssocTypeAgainB {
func aMethod(_ object : AsExistentialAssocTypeAgainA) // expected-error {{use of protocol 'AsExistentialAssocTypeAgainA' as a type must be written 'any AsExistentialAssocTypeAgainA'}}
func aMethod(_ object : any AsExistentialAssocTypeAgainA)
}

// https://github.com/apple/swift/issues/43164
Expand Down
Loading