From 401d297ee2ffb3a2b97b6c393be8398f0492abb4 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 16 Jun 2025 15:41:53 -0400 Subject: [PATCH 01/11] AST: Fix crash on invalid in isVanishingTupleConformance() --- lib/AST/ProtocolConformance.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/AST/ProtocolConformance.cpp b/lib/AST/ProtocolConformance.cpp index da9f1adc7d0ac..56519a83bdfa6 100644 --- a/lib/AST/ProtocolConformance.cpp +++ b/lib/AST/ProtocolConformance.cpp @@ -990,9 +990,12 @@ static bool isVanishingTupleConformance( auto replacementTypes = substitutions.getReplacementTypes(); assert(replacementTypes.size() == 1); - auto packType = replacementTypes[0]->castTo(); - return (packType->getNumElements() == 1 && + // This might not be an actual pack type with an invalid tuple conformance. + auto packType = replacementTypes[0]->getAs(); + + return (packType && + packType->getNumElements() == 1 && !packType->getElementTypes()[0]->is()); } From 908c9368ed31b889c19dc0e83970ea02cd760093 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 16 Jun 2025 14:59:28 -0400 Subject: [PATCH 02/11] Parse: Only accept certain literals as enum case raw values Just checking for LiteralExpr is too broad, because Sema doesn't know what to do with RegexLiteralExpr for example. --- include/swift/AST/DiagnosticsSema.def | 3 -- lib/Parse/ParseDecl.cpp | 17 +++++++++-- lib/Sema/TypeCheckDecl.cpp | 9 ------ test/decl/enum/invalid_raw_value.swift | 7 +++++ .../441e44e28042d5a3.swift | 2 +- .../issue-55443.swift | 28 +++++++++---------- 6 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 test/decl/enum/invalid_raw_value.swift rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/441e44e28042d5a3.swift (66%) diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index f12bd7a6c03cf..bcad61af75d58 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -3781,9 +3781,6 @@ ERROR(enum_non_integer_convertible_raw_type_no_value,none, "expressible by integer or string literal", ()) ERROR(enum_raw_value_not_unique,none, "raw value for enum case is not unique", ()) -ERROR(enum_raw_value_magic_literal,none, - "use of '%0' literal as raw value for enum case is not supported", - (StringRef)) NOTE(enum_raw_value_used_here,none, "raw value previously used here", ()) NOTE(enum_raw_value_incrementing_from_here,none, diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 478c58fd34889..678f2fa789b27 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -9229,6 +9229,20 @@ ParserResult Parser::parseDeclEnum(ParseDeclOptions Flags, return DCC.fixupParserResult(Status, ED); } +static bool isValidEnumRawValueLiteral(LiteralExpr *expr) { + if (expr == nullptr) + return false; + + if (!isa(expr) && + !isa(expr) && + !isa(expr) && + !isa(expr) && + !isa(expr)) + return false; + + return true; +} + /// Parse a 'case' of an enum. /// /// \verbatim @@ -9346,8 +9360,7 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags, } // The raw value must be syntactically a simple literal. LiteralRawValueExpr = dyn_cast(RawValueExpr.getPtrOrNull()); - if (!LiteralRawValueExpr - || isa(LiteralRawValueExpr)) { + if (!isValidEnumRawValueLiteral(LiteralRawValueExpr)) { diagnose(RawValueExpr.getPtrOrNull()->getLoc(), diag::nonliteral_enum_case_raw_value); LiteralRawValueExpr = nullptr; diff --git a/lib/Sema/TypeCheckDecl.cpp b/lib/Sema/TypeCheckDecl.cpp index abce1b3299457..7dd540e408c6e 100644 --- a/lib/Sema/TypeCheckDecl.cpp +++ b/lib/Sema/TypeCheckDecl.cpp @@ -1304,15 +1304,6 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED, SourceLoc diagLoc = uncheckedRawValueOf(elt)->isImplicit() ? elt->getLoc() : uncheckedRawValueOf(elt)->getLoc(); - if (auto magicLiteralExpr = - dyn_cast(prevValue)) { - auto kindString = - magicLiteralExpr->getKindString(magicLiteralExpr->getKind()); - Diags.diagnose(diagLoc, diag::enum_raw_value_magic_literal, kindString); - elt->setInvalid(); - continue; - } - // Check that the raw value is unique. RawValueKey key{prevValue}; RawValueSource source{elt, lastExplicitValueElt}; diff --git a/test/decl/enum/invalid_raw_value.swift b/test/decl/enum/invalid_raw_value.swift new file mode 100644 index 0000000000000..31ef46e50a5c7 --- /dev/null +++ b/test/decl/enum/invalid_raw_value.swift @@ -0,0 +1,7 @@ +// RUN: %target-typecheck-verify-swift + +_ = a.init +_ = b.init + +enum a : Int { case x = #/ /# } // expected-error {{raw value for enum case must be a literal}} +enum b : String { case x = #file } // expected-error {{raw value for enum case must be a literal}} diff --git a/validation-test/compiler_crashers_2/441e44e28042d5a3.swift b/validation-test/compiler_crashers_2_fixed/441e44e28042d5a3.swift similarity index 66% rename from validation-test/compiler_crashers_2/441e44e28042d5a3.swift rename to validation-test/compiler_crashers_2_fixed/441e44e28042d5a3.swift index 69369f6de1291..5b6e0e7abafed 100644 --- a/validation-test/compiler_crashers_2/441e44e28042d5a3.swift +++ b/validation-test/compiler_crashers_2_fixed/441e44e28042d5a3.swift @@ -1,3 +1,3 @@ // {"signature":"deriveBodyRawRepresentable_init(swift::AbstractFunctionDecl*, void*)"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s a enum a : Int { case = #/ diff --git a/validation-test/compiler_crashers_2_fixed/issue-55443.swift b/validation-test/compiler_crashers_2_fixed/issue-55443.swift index 928c1def248d1..0a25bcf9fe080 100644 --- a/validation-test/compiler_crashers_2_fixed/issue-55443.swift +++ b/validation-test/compiler_crashers_2_fixed/issue-55443.swift @@ -2,20 +2,20 @@ // https://github.com/apple/swift/issues/55443 -enum FooString: String { // expected-error {{'FooString' declares raw type 'String', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}} - case bar1 = #file // expected-error {{use of '#file' literal as raw value for enum case is not supported}} - case bar2 = #function // expected-error {{use of '#function' literal as raw value for enum case is not supported}} - case bar3 = #filePath // expected-error {{use of '#filePath' literal as raw value for enum case is not supported}} - case bar4 = #line // expected-error {{cannot convert value of type 'Int' to raw type 'String'}} - case bar5 = #column // expected-error {{cannot convert value of type 'Int' to raw type 'String'}} - case bar6 = #dsohandle // expected-error {{cannot convert value of type 'UnsafeRawPointer' to raw type 'String'}} +enum FooString: String { + case bar1 = #file // expected-error {{raw value for enum case must be a literal}} + case bar2 = #function // expected-error {{raw value for enum case must be a literal}} + case bar3 = #filePath // expected-error {{raw value for enum case must be a literal}} + case bar4 = #line // expected-error {{raw value for enum case must be a literal}} + case bar5 = #column // expected-error {{raw value for enum case must be a literal}} + case bar6 = #dsohandle // expected-error {{raw value for enum case must be a literal}} } -enum FooInt: Int { // expected-error {{'FooInt' declares raw type 'Int', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-note {{add stubs for conformance}} - case bar1 = #file // expected-error {{cannot convert value of type 'String' to raw type 'Int'}} - case bar2 = #function // expected-error {{cannot convert value of type 'String' to raw type 'Int'}} - case bar3 = #filePath // expected-error {{cannot convert value of type 'String' to raw type 'Int'}} - case bar4 = #line // expected-error {{use of '#line' literal as raw value for enum case is not supported}} - case bar5 = #column // expected-error {{use of '#column' literal as raw value for enum case is not supported}} - case bar6 = #dsohandle // expected-error {{cannot convert value of type 'UnsafeRawPointer' to raw type 'Int'}} +enum FooInt: Int { + case bar1 = #file // expected-error {{raw value for enum case must be a literal}} + case bar2 = #function // expected-error {{raw value for enum case must be a literal}} + case bar3 = #filePath // expected-error {{raw value for enum case must be a literal}} + case bar4 = #line // expected-error {{raw value for enum case must be a literal}} + case bar5 = #column // expected-error {{raw value for enum case must be a literal}} + case bar6 = #dsohandle // expected-error {{raw value for enum case must be a literal}} } From 4fa2e979fad6f245e8b05a3d3abd2d7985fd074b Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 16 Jun 2025 16:09:02 -0400 Subject: [PATCH 03/11] RequirementMachine: Don't crash if we cannot desugar a same-shape requirement --- lib/AST/RequirementMachine/RequirementLowering.cpp | 1 + .../d48def542970cfa.swift | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/d48def542970cfa.swift (85%) diff --git a/lib/AST/RequirementMachine/RequirementLowering.cpp b/lib/AST/RequirementMachine/RequirementLowering.cpp index cb3cfcdb25583..7fa5cec5beef3 100644 --- a/lib/AST/RequirementMachine/RequirementLowering.cpp +++ b/lib/AST/RequirementMachine/RequirementLowering.cpp @@ -451,6 +451,7 @@ static void desugarSameShapeRequirement( !req.getSecondType()->isParameterPack()) { errors.push_back(RequirementError::forInvalidShapeRequirement( req, loc)); + return; } result.emplace_back(RequirementKind::SameShape, diff --git a/validation-test/compiler_crashers_2/d48def542970cfa.swift b/validation-test/compiler_crashers_2_fixed/d48def542970cfa.swift similarity index 85% rename from validation-test/compiler_crashers_2/d48def542970cfa.swift rename to validation-test/compiler_crashers_2_fixed/d48def542970cfa.swift index ffbeac8876178..33d9b14c41143 100644 --- a/validation-test/compiler_crashers_2/d48def542970cfa.swift +++ b/validation-test/compiler_crashers_2_fixed/d48def542970cfa.swift @@ -1,4 +1,4 @@ // {"signature":"swift::rewriting::performConcreteContraction(llvm::ArrayRef, llvm::SmallVectorImpl&, llvm::SmallVectorImpl&, bool)"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s func a < each b, each c where(repeat(each c each b)) : { typealias d = () func e where d == From 9a01e872da2caf797e99175f3f16e1ed7e4f863c Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 16 Jun 2025 14:45:50 -0400 Subject: [PATCH 04/11] Sema: Address FIXME resulting in a crash in filterProtocolRequirements() --- lib/Sema/TypeCheckProtocol.cpp | 23 +++++++++++++------ .../conforms/fixit_stub_generic.swift | 13 +++++++++++ .../2617a198505b47e4.swift | 2 +- 3 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 test/decl/protocol/conforms/fixit_stub_generic.swift rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/2617a198505b47e4.swift (82%) diff --git a/lib/Sema/TypeCheckProtocol.cpp b/lib/Sema/TypeCheckProtocol.cpp index 86fe85b94b69f..8121b40eb8097 100644 --- a/lib/Sema/TypeCheckProtocol.cpp +++ b/lib/Sema/TypeCheckProtocol.cpp @@ -3888,10 +3888,19 @@ filterProtocolRequirements( return Filtered; } - const auto getProtocolSubstitutionMap = [&](ValueDecl *Req) { - auto *const PD = cast(Req->getDeclContext()); - auto Conformance = lookupConformance(Adoptee, PD); - return SubstitutionMap::getProtocolSubstitutions(Conformance); + const auto getProtocolSubstitutionMap = [&](ValueDecl *req) { + ASSERT(isa(req->getDeclContext())); + auto genericSig = req->getInnermostDeclContext() + ->getGenericSignatureOfContext(); + SmallVector args; + for (auto paramTy : genericSig.getGenericParams()) { + if (args.empty()) + args.push_back(Adoptee); + else + args.push_back(paramTy); + } + return SubstitutionMap::get(genericSig, args, + LookUpConformanceInModule()); }; llvm::SmallDenseMap, 4> @@ -3907,11 +3916,11 @@ filterProtocolRequirements( auto OverloadTy = Req->getOverloadSignatureType(); if (OverloadTy) { auto Subs = getProtocolSubstitutionMap(Req); - // FIXME: This is wrong if the overload has its own generic parameters - if (auto GenericFnTy = dyn_cast(OverloadTy)) + if (auto GenericFnTy = dyn_cast(OverloadTy)) { OverloadTy = GenericFnTy.substGenericArgs(Subs); - else + } else { OverloadTy = OverloadTy.subst(Subs)->getCanonicalType(); + } } if (llvm::any_of(DeclsByName[Req->getName()], [&](ValueDecl *OtherReq) { auto OtherOverloadTy = OtherReq->getOverloadSignatureType(); diff --git a/test/decl/protocol/conforms/fixit_stub_generic.swift b/test/decl/protocol/conforms/fixit_stub_generic.swift new file mode 100644 index 0000000000000..b47ef8c65f6fa --- /dev/null +++ b/test/decl/protocol/conforms/fixit_stub_generic.swift @@ -0,0 +1,13 @@ +// RUN: %target-typecheck-verify-swift + +protocol P1 { + associatedtype A +} + +protocol P2 { + func f(_: T, _: T) // expected-note {{protocol requires function 'f' with type ' (T, T) -> ()'}} + func f(_: T, _: T.A) // expected-note {{protocol requires function 'f' with type ' (T, T.A) -> ()'}} +} + +struct S: P2 {} // expected-error {{type 'S' does not conform to protocol 'P2'}} +// expected-note@-1 {{add stubs for conformance}} diff --git a/validation-test/compiler_crashers_2/2617a198505b47e4.swift b/validation-test/compiler_crashers_2_fixed/2617a198505b47e4.swift similarity index 82% rename from validation-test/compiler_crashers_2/2617a198505b47e4.swift rename to validation-test/compiler_crashers_2_fixed/2617a198505b47e4.swift index 4a03fe302445c..9147add3fa74f 100644 --- a/validation-test/compiler_crashers_2/2617a198505b47e4.swift +++ b/validation-test/compiler_crashers_2_fixed/2617a198505b47e4.swift @@ -1,5 +1,5 @@ // {"signature":"swift::rewriting::RequirementMachine::getReducedShape(swift::Type, llvm::ArrayRef) const"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s protocol a{ b < c > (c, _ : c} protocol d : a{ b(c, c.c) protocol e { From eec924b5052c23bf11b48526b1e8cb3f4f2c52e1 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 16 Jun 2025 14:47:35 -0400 Subject: [PATCH 05/11] Sema: Fix crash in diagnoseIfSynthesisUnsupportedForDecl() with tuple extension --- lib/Sema/DerivedConformance/DerivedConformance.cpp | 3 +++ test/Generics/tuple-conformances-synthesize-crash.swift | 5 +++++ .../259dac55924ef426.swift | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 test/Generics/tuple-conformances-synthesize-crash.swift rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/259dac55924ef426.swift (65%) diff --git a/lib/Sema/DerivedConformance/DerivedConformance.cpp b/lib/Sema/DerivedConformance/DerivedConformance.cpp index 8d3f5b62d0b09..7df7919af315a 100644 --- a/lib/Sema/DerivedConformance/DerivedConformance.cpp +++ b/lib/Sema/DerivedConformance/DerivedConformance.cpp @@ -272,6 +272,9 @@ void DerivedConformance::diagnoseIfSynthesisUnsupportedForDecl( shouldDiagnose = !isa(nominal); } + if (isa(nominal)) + shouldDiagnose = false; + if (shouldDiagnose) { auto &ctx = nominal->getASTContext(); ctx.Diags.diagnose(nominal->getLoc(), diff --git a/test/Generics/tuple-conformances-synthesize-crash.swift b/test/Generics/tuple-conformances-synthesize-crash.swift new file mode 100644 index 0000000000000..8d4fdfafdfb2f --- /dev/null +++ b/test/Generics/tuple-conformances-synthesize-crash.swift @@ -0,0 +1,5 @@ +// RUN: not %target-swift-frontend -typecheck %s + +// Just don't crash. +extension () : Comparable {} + diff --git a/validation-test/compiler_crashers_2/259dac55924ef426.swift b/validation-test/compiler_crashers_2_fixed/259dac55924ef426.swift similarity index 65% rename from validation-test/compiler_crashers_2/259dac55924ef426.swift rename to validation-test/compiler_crashers_2_fixed/259dac55924ef426.swift index a3f4faaa87333..beaa9da85e046 100644 --- a/validation-test/compiler_crashers_2/259dac55924ef426.swift +++ b/validation-test/compiler_crashers_2_fixed/259dac55924ef426.swift @@ -1,3 +1,3 @@ // {"signature":"swift::GenericContext::getGenericParams() const"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s typealias a = () extension a : Comparable From 8b12f8cb8e18980698e5197a09ff11942c39c008 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 16 Jun 2025 14:49:38 -0400 Subject: [PATCH 06/11] Sema: Fix null pointer dereference in LazyStoragePropertyRequest::evaluate() --- lib/Sema/TypeCheckStorage.cpp | 7 ++++--- .../1f1b9ed16e283e76.swift | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/1f1b9ed16e283e76.swift (69%) diff --git a/lib/Sema/TypeCheckStorage.cpp b/lib/Sema/TypeCheckStorage.cpp index afc616c560e33..b001206557629 100644 --- a/lib/Sema/TypeCheckStorage.cpp +++ b/lib/Sema/TypeCheckStorage.cpp @@ -2996,9 +2996,10 @@ LazyStoragePropertyRequest::evaluate(Evaluator &evaluator, addMemberToContextIfNeeded(PBD, VD->getDeclContext(), Storage); // Make sure the original init is marked as subsumed. - auto *originalPBD = VD->getParentPatternBinding(); - auto originalIndex = originalPBD->getPatternEntryIndexForVarDecl(VD); - originalPBD->setInitializerSubsumed(originalIndex); + if (auto *originalPBD = VD->getParentPatternBinding()) { + auto originalIndex = originalPBD->getPatternEntryIndexForVarDecl(VD); + originalPBD->setInitializerSubsumed(originalIndex); + } return Storage; } diff --git a/validation-test/compiler_crashers_2/1f1b9ed16e283e76.swift b/validation-test/compiler_crashers_2_fixed/1f1b9ed16e283e76.swift similarity index 69% rename from validation-test/compiler_crashers_2/1f1b9ed16e283e76.swift rename to validation-test/compiler_crashers_2_fixed/1f1b9ed16e283e76.swift index 8c2da215f9391..1365c7e0535ea 100644 --- a/validation-test/compiler_crashers_2/1f1b9ed16e283e76.swift +++ b/validation-test/compiler_crashers_2_fixed/1f1b9ed16e283e76.swift @@ -1,4 +1,4 @@ // {"signature":"swift::LazyStoragePropertyRequest::evaluate(swift::Evaluator&, swift::VarDecl*) const"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s class a { lazy(b, c) { From ef5e2861a2d4ede1af7c369e7084e966dd110d99 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 16 Jun 2025 15:21:55 -0400 Subject: [PATCH 07/11] Sema: Fix crash on invalid code in diagnoseDictionaryLiteralDuplicateKeyEntries() --- lib/Sema/MiscDiagnostics.cpp | 4 ++-- .../93eaba9fd0e76cdf.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/93eaba9fd0e76cdf.swift (75%) diff --git a/lib/Sema/MiscDiagnostics.cpp b/lib/Sema/MiscDiagnostics.cpp index 547092d7033ec..4228f526e6a8e 100644 --- a/lib/Sema/MiscDiagnostics.cpp +++ b/lib/Sema/MiscDiagnostics.cpp @@ -6268,9 +6268,9 @@ diagnoseDictionaryLiteralDuplicateKeyEntries(const Expr *E, note.fixItRemove(duplicated.first->getSourceRange()); if (duplicatedEltIdx < commanLocs.size()) { note.fixItRemove(commanLocs[duplicatedEltIdx]); - } else { + } else if (!commanLocs.empty()) { // For the last element remove the previous comma. - note.fixItRemove(commanLocs[duplicatedEltIdx - 1]); + note.fixItRemove(commanLocs[commanLocs.size() - 1]); } }; diff --git a/validation-test/compiler_crashers_2/93eaba9fd0e76cdf.swift b/validation-test/compiler_crashers_2_fixed/93eaba9fd0e76cdf.swift similarity index 75% rename from validation-test/compiler_crashers_2/93eaba9fd0e76cdf.swift rename to validation-test/compiler_crashers_2_fixed/93eaba9fd0e76cdf.swift index 3b4535b3be622..97cda574e4c59 100644 --- a/validation-test/compiler_crashers_2/93eaba9fd0e76cdf.swift +++ b/validation-test/compiler_crashers_2_fixed/93eaba9fd0e76cdf.swift @@ -1,3 +1,3 @@ // {"signature":"diagnoseDictionaryLiteralDuplicateKeyEntries(swift::Expr const*, swift::DeclContext const*)::DiagnoseWalker::walkToExprPre(swift::Expr*)"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s [ 1.01: "" 1.01: "" From 0d6470345b9cbce638ef4ecfec55cd421f0554fc Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 16 Jun 2025 15:22:05 -0400 Subject: [PATCH 08/11] Sema: Fix crash on invalid code in isOverrideBasedOnType() --- lib/Sema/TypeCheckDeclOverride.cpp | 3 +++ .../90773c979d435f7.swift | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/90773c979d435f7.swift (80%) diff --git a/lib/Sema/TypeCheckDeclOverride.cpp b/lib/Sema/TypeCheckDeclOverride.cpp index ff61134aadd51..77b47d3d2ce67 100644 --- a/lib/Sema/TypeCheckDeclOverride.cpp +++ b/lib/Sema/TypeCheckDeclOverride.cpp @@ -217,6 +217,9 @@ bool swift::isOverrideBasedOnType(const ValueDecl *decl, Type declTy, return false; } + if (declTy->is()) + return false; + auto fnType1 = declTy->castTo(); auto fnType2 = parentDeclTy->castTo(); return AnyFunctionType::equalParams(fnType1->getParams(), diff --git a/validation-test/compiler_crashers_2/90773c979d435f7.swift b/validation-test/compiler_crashers_2_fixed/90773c979d435f7.swift similarity index 80% rename from validation-test/compiler_crashers_2/90773c979d435f7.swift rename to validation-test/compiler_crashers_2_fixed/90773c979d435f7.swift index 6b160e9f6cde6..dccfdaecd70c6 100644 --- a/validation-test/compiler_crashers_2/90773c979d435f7.swift +++ b/validation-test/compiler_crashers_2_fixed/90773c979d435f7.swift @@ -1,5 +1,5 @@ // {"signature":"swift::isOverrideBasedOnType(swift::ValueDecl const*, swift::Type, swift::ValueDecl const*)"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s struct a < b { protocol c { associatedtype d init(e : d } From b79f2817f92e8cbcd66f74a5b7c72aa17c628ad0 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Mon, 16 Jun 2025 15:41:38 -0400 Subject: [PATCH 09/11] Sema: Fix MissingCallFailure::diagnoseAsError() crash with special base names --- include/swift/AST/DiagnosticsSema.def | 4 ++-- lib/Sema/CSDiagnostics.cpp | 6 +++--- .../a9f541ae5c40ef35.swift | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/a9f541ae5c40ef35.swift (71%) diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index bcad61af75d58..64378e2ae4c67 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -1436,10 +1436,10 @@ ERROR(did_not_call_function_value,none, ()) ERROR(did_not_call_function,none, "function %0 was used as a property; add () to call it", - (Identifier)) + (DeclBaseName)) ERROR(did_not_call_method,none, "method %0 was used as a property; add () to call it", - (Identifier)) + (DeclBaseName)) ERROR(init_not_instance_member_use_assignment,none, "'init' is a member of the type; use assignment " diff --git a/lib/Sema/CSDiagnostics.cpp b/lib/Sema/CSDiagnostics.cpp index 40c22af154615..6af4e4bfc995e 100644 --- a/lib/Sema/CSDiagnostics.cpp +++ b/lib/Sema/CSDiagnostics.cpp @@ -3940,14 +3940,14 @@ bool MissingCallFailure::diagnoseAsError() { if (auto *DRE = getAsExpr(anchor)) { emitDiagnostic(diag::did_not_call_function, - DRE->getDecl()->getBaseIdentifier()) + DRE->getDecl()->getBaseName()) .fixItInsertAfter(insertLoc, "()"); return true; } if (auto *UDE = getAsExpr(anchor)) { emitDiagnostic(diag::did_not_call_method, - UDE->getName().getBaseIdentifier()) + UDE->getName().getBaseName()) .fixItInsertAfter(insertLoc, "()"); return true; } @@ -3955,7 +3955,7 @@ bool MissingCallFailure::diagnoseAsError() { if (auto *DSCE = getAsExpr(anchor)) { if (auto *DRE = dyn_cast(DSCE->getFn())) { emitDiagnostic(diag::did_not_call_method, - DRE->getDecl()->getBaseIdentifier()) + DRE->getDecl()->getBaseName()) .fixItInsertAfter(insertLoc, "()"); return true; } diff --git a/validation-test/compiler_crashers_2/a9f541ae5c40ef35.swift b/validation-test/compiler_crashers_2_fixed/a9f541ae5c40ef35.swift similarity index 71% rename from validation-test/compiler_crashers_2/a9f541ae5c40ef35.swift rename to validation-test/compiler_crashers_2_fixed/a9f541ae5c40ef35.swift index e988fba99f1dc..e74acdceb6a01 100644 --- a/validation-test/compiler_crashers_2/a9f541ae5c40ef35.swift +++ b/validation-test/compiler_crashers_2_fixed/a9f541ae5c40ef35.swift @@ -1,3 +1,3 @@ // {"signature":"(anonymous namespace)::ABIDependencyEvaluator::computeABIDependenciesForModule(swift::ModuleDecl*)"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s struct a { init? { init! From cfa9de8f0a62f53e46f994f55fefbbb4fd7500bf Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 17 Jun 2025 10:14:34 -0400 Subject: [PATCH 10/11] Parse: Address an llvm_unreachable that is actually reachable --- lib/Parse/ParseDecl.cpp | 7 +++---- .../1c4f5ed8bb743453.swift | 2 +- .../1fb63f2dc87bc9b9.swift | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/1c4f5ed8bb743453.swift (75%) rename validation-test/{compiler_crashers_2 => compiler_crashers_2_fixed}/1fb63f2dc87bc9b9.swift (70%) diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 678f2fa789b27..bf6541726f4ce 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -6337,8 +6337,8 @@ ParserStatus Parser::parseDecl(bool IsAtStartOfLineOrPreviousHadSemi, DescriptiveKind = DescriptiveDeclKind::StaticProperty; break; case StaticSpellingKind::KeywordClass: - llvm_unreachable("kw_class is only parsed as a modifier if it's " - "followed by a keyword"); + DescriptiveKind = DescriptiveDeclKind::ClassProperty; + break; } diagnose(Tok.getLoc(), diag::expected_keyword_in_decl, "var", @@ -6366,8 +6366,7 @@ ParserStatus Parser::parseDecl(bool IsAtStartOfLineOrPreviousHadSemi, DescriptiveKind = DescriptiveDeclKind::StaticMethod; break; case StaticSpellingKind::KeywordClass: - llvm_unreachable("kw_class is only parsed as a modifier if it's " - "followed by a keyword"); + DescriptiveKind = DescriptiveDeclKind::ClassMethod; } } diff --git a/validation-test/compiler_crashers_2/1c4f5ed8bb743453.swift b/validation-test/compiler_crashers_2_fixed/1c4f5ed8bb743453.swift similarity index 75% rename from validation-test/compiler_crashers_2/1c4f5ed8bb743453.swift rename to validation-test/compiler_crashers_2_fixed/1c4f5ed8bb743453.swift index 67cc303dee5ac..567cb0fb4f627 100644 --- a/validation-test/compiler_crashers_2/1c4f5ed8bb743453.swift +++ b/validation-test/compiler_crashers_2_fixed/1c4f5ed8bb743453.swift @@ -1,4 +1,4 @@ // {"signature":"swift::Parser::parseNewDeclAttribute(swift::DeclAttributes&, swift::SourceLoc, swift::DeclAttrKind, bool)::$_4::operator()() const"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s class a { class override b diff --git a/validation-test/compiler_crashers_2/1fb63f2dc87bc9b9.swift b/validation-test/compiler_crashers_2_fixed/1fb63f2dc87bc9b9.swift similarity index 70% rename from validation-test/compiler_crashers_2/1fb63f2dc87bc9b9.swift rename to validation-test/compiler_crashers_2_fixed/1fb63f2dc87bc9b9.swift index 6192c4bd17615..763f8bacf18b1 100644 --- a/validation-test/compiler_crashers_2/1fb63f2dc87bc9b9.swift +++ b/validation-test/compiler_crashers_2_fixed/1fb63f2dc87bc9b9.swift @@ -1,3 +1,3 @@ // {"signature":"swift::Parser::parseDecl(bool, bool, llvm::function_ref, bool)"} -// RUN: not --crash %target-swift-frontend -typecheck %s +// RUN: not %target-swift-frontend -typecheck %s class a { class override ( override From 1b4178ff48eadd8795c679796bbe526c20240213 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Tue, 17 Jun 2025 10:15:20 -0400 Subject: [PATCH 11/11] ASTScope: Fix llvm_unreachable when printing scopes before extension binding --- lib/AST/ASTScope.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/AST/ASTScope.cpp b/lib/AST/ASTScope.cpp index 4583ac892c517..c49c6c49756f5 100644 --- a/lib/AST/ASTScope.cpp +++ b/lib/AST/ASTScope.cpp @@ -356,6 +356,8 @@ SourceRange NominalTypeScope::getBraces() const { return decl->getBraces(); } NullablePtr ExtensionScope::getCorrespondingNominalTypeDecl() const { + if (!decl->hasBeenBound()) + return nullptr; return decl->getExtendedNominal(); }