Skip to content

Commit

Permalink
[SR-12460] Don't crash when missing a type in a Diagnostic message. (s…
Browse files Browse the repository at this point in the history
…wiftlang#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'"
  • Loading branch information
blueeor authored Apr 14, 2020
1 parent 4fd19a8 commit 8a1a194
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
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 {}
}

0 comments on commit 8a1a194

Please sign in to comment.