Skip to content

[SR-12460] Don't crash when missing a type in a Diagnostic message. #30979

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 13 commits into from
Apr 14, 2020
Merged
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,8 @@ ERROR(nonstatic_operator_in_nominal,none,
"operator %0 declared in type %1 must be 'static'",
(Identifier, DeclName))
ERROR(nonstatic_operator_in_extension,none,
"operator %0 declared in extension of %1 must be 'static'",
(Identifier, TypeRepr*))
"operator %0 declared in extension%select{| of %2}1 must be 'static'",
(Identifier, bool, TypeRepr*))
ERROR(nonfinal_operator_in_class,none,
"operator %0 declared in non-final class %1 must be 'final'",
(Identifier, Type))
Expand Down
3 changes: 3 additions & 0 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,14 @@ static void formatDiagnosticArgument(StringRef Modifier,
}
break;
}

case DiagnosticArgumentKind::TypeRepr:
assert(Modifier.empty() && "Improper modifier for TypeRepr argument");
assert(Arg.getAsTypeRepr() && "TypeRepr argument is null");
Out << FormatOpts.OpeningQuotationMark << Arg.getAsTypeRepr()
<< FormatOpts.ClosingQuotationMark;
break;

case DiagnosticArgumentKind::PatternKind:
assert(Modifier.empty() && "Improper modifier for PatternKind argument");
Out << Arg.getAsPatternKind();
Expand Down
5 changes: 3 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,9 @@ IsStaticRequest::evaluate(Evaluator &evaluator, FuncDecl *decl) const {
dc->isTypeContext()) {
const auto operatorName = decl->getBaseIdentifier();
if (auto ED = dyn_cast<ExtensionDecl>(dc->getAsDecl())) {
decl->diagnose(diag::nonstatic_operator_in_extension,
operatorName, ED->getExtendedTypeRepr())
decl->diagnose(diag::nonstatic_operator_in_extension, operatorName,
ED->getExtendedTypeRepr() != nullptr,
ED->getExtendedTypeRepr())
.fixItInsert(decl->getAttributeInsertionLoc(/*forModifier=*/true),
"static ");
} else {
Expand Down
9 changes: 9 additions & 0 deletions test/decl/ext/sr_12460.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s 2>&1 | %FileCheck -check-prefix CHECK %s

// Test that we don't crash when validating members inside an extension with no type name.

// CHECK: :[[@LINE+1]]:11: error: expected type name in extension declaration
extension {
// CHECK: :[[@LINE+1]]:8: error: operator '==' declared in extension must be 'static'
func ==(lhs: Any, rhs: Any) -> Bool {}
}