From 627f41ccbcdaeb43c11c911e0a3e132831bc5e55 Mon Sep 17 00:00:00 2001 From: Hamish Knight Date: Thu, 28 Aug 2025 23:48:14 +0100 Subject: [PATCH] [CS] Fix an overly strict assert `matchExistentialTypes` can handle existential metatypes, fix the assert to handle that. --- lib/Sema/Constraint.cpp | 2 +- test/Constraints/existential_metatypes.swift | 14 ++++++++++++++ .../c5955772856ea0fa.swift | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/c5955772856ea0fa.swift (90%) diff --git a/lib/Sema/Constraint.cpp b/lib/Sema/Constraint.cpp index 6d6765f56b460..551bf8bc446fe 100644 --- a/lib/Sema/Constraint.cpp +++ b/lib/Sema/Constraint.cpp @@ -784,7 +784,7 @@ Constraint *Constraint::create(ConstraintSystem &cs, ConstraintKind kind, assert((kind != ConstraintKind::ConformsTo && kind != ConstraintKind::NonisolatedConformsTo && kind != ConstraintKind::TransitivelyConformsTo) || - second->isExistentialType()); + second->isAnyExistentialType()); // Literal protocol conformances expect a protocol. assert((kind != ConstraintKind::LiteralConformsTo) || diff --git a/test/Constraints/existential_metatypes.swift b/test/Constraints/existential_metatypes.swift index 35aca6b1e021f..759f5fb69ec91 100644 --- a/test/Constraints/existential_metatypes.swift +++ b/test/Constraints/existential_metatypes.swift @@ -119,3 +119,17 @@ func parameterizedExistentials() { var ppt: any PP4.Type pt = ppt // expected-error {{cannot assign value of type 'any PP4.Type' to type 'any P4.Type'}} } + +func testNestedMetatype() { + struct S: P {} + + func bar(_ x: T) -> T.Type { type(of: x) } + func foo(_ x: P.Type.Type) { } + + // Make sure we don't crash. + foo(bar(S.self)) + + // FIXME: Bad diagnostic + // https://github.com/swiftlang/swift/issues/83991 + foo(bar(0)) // expected-error {{failed to produce diagnostic for expression}} +} diff --git a/validation-test/compiler_crashers_2/c5955772856ea0fa.swift b/validation-test/compiler_crashers_2_fixed/c5955772856ea0fa.swift similarity index 90% rename from validation-test/compiler_crashers_2/c5955772856ea0fa.swift rename to validation-test/compiler_crashers_2_fixed/c5955772856ea0fa.swift index de033ad993f9f..89e02af41e4c2 100644 --- a/validation-test/compiler_crashers_2/c5955772856ea0fa.swift +++ b/validation-test/compiler_crashers_2_fixed/c5955772856ea0fa.swift @@ -1,3 +1,3 @@ // {"kind":"typecheck","signature":"swift::constraints::Constraint::create(swift::constraints::ConstraintSystem&, swift::constraints::ConstraintKind, swift::Type, swift::Type, swift::constraints::ConstraintLocator*, llvm::ArrayRef)","signatureAssert":"Assertion failed: ((kind != ConstraintKind::ConformsTo && kind != ConstraintKind::NonisolatedConformsTo && kind != ConstraintKind::TransitivelyConformsTo) || second->isExistentialType()), function create"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s protocol a let : a.Type.Type = b->c