Skip to content

Commit 8a1a194

Browse files
authored
[SR-12460] Don't crash when missing a type in a Diagnostic message. (#30979)
* Don't crash when missing a type in a Diagnostic message. Print an empty string for the missing type. Resolves SR-12460 * Added new diagnostic message, eg. "operator '==' declared in extension must be 'static'"
1 parent 4fd19a8 commit 8a1a194

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,8 +902,8 @@ ERROR(nonstatic_operator_in_nominal,none,
902902
"operator %0 declared in type %1 must be 'static'",
903903
(Identifier, DeclName))
904904
ERROR(nonstatic_operator_in_extension,none,
905-
"operator %0 declared in extension of %1 must be 'static'",
906-
(Identifier, TypeRepr*))
905+
"operator %0 declared in extension%select{| of %2}1 must be 'static'",
906+
(Identifier, bool, TypeRepr*))
907907
ERROR(nonfinal_operator_in_class,none,
908908
"operator %0 declared in non-final class %1 must be 'final'",
909909
(Identifier, Type))

lib/AST/DiagnosticEngine.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,11 +583,14 @@ static void formatDiagnosticArgument(StringRef Modifier,
583583
}
584584
break;
585585
}
586+
586587
case DiagnosticArgumentKind::TypeRepr:
587588
assert(Modifier.empty() && "Improper modifier for TypeRepr argument");
589+
assert(Arg.getAsTypeRepr() && "TypeRepr argument is null");
588590
Out << FormatOpts.OpeningQuotationMark << Arg.getAsTypeRepr()
589591
<< FormatOpts.ClosingQuotationMark;
590592
break;
593+
591594
case DiagnosticArgumentKind::PatternKind:
592595
assert(Modifier.empty() && "Improper modifier for PatternKind argument");
593596
Out << Arg.getAsPatternKind();

lib/Sema/TypeCheckDecl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,9 @@ IsStaticRequest::evaluate(Evaluator &evaluator, FuncDecl *decl) const {
676676
dc->isTypeContext()) {
677677
const auto operatorName = decl->getBaseIdentifier();
678678
if (auto ED = dyn_cast<ExtensionDecl>(dc->getAsDecl())) {
679-
decl->diagnose(diag::nonstatic_operator_in_extension,
680-
operatorName, ED->getExtendedTypeRepr())
679+
decl->diagnose(diag::nonstatic_operator_in_extension, operatorName,
680+
ED->getExtendedTypeRepr() != nullptr,
681+
ED->getExtendedTypeRepr())
681682
.fixItInsert(decl->getAttributeInsertionLoc(/*forModifier=*/true),
682683
"static ");
683684
} else {

test/decl/ext/sr_12460.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s 2>&1 | %FileCheck -check-prefix CHECK %s
2+
3+
// Test that we don't crash when validating members inside an extension with no type name.
4+
5+
// CHECK: :[[@LINE+1]]:11: error: expected type name in extension declaration
6+
extension {
7+
// CHECK: :[[@LINE+1]]:8: error: operator '==' declared in extension must be 'static'
8+
func ==(lhs: Any, rhs: Any) -> Bool {}
9+
}

0 commit comments

Comments
 (0)