Skip to content
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

Avoid diagnostic message forcing crashing the compiler #19113

Merged
merged 3 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ trait HideNonSensicalMessages extends Reporter {
*/
override def isHidden(dia: Diagnostic)(using Context): Boolean =
super.isHidden(dia) || {
dia.msg.isNonSensical &&
(if !errorsReported && dia.isInstanceOf[Diagnostic.Error] then
// Bump up errorCount so hasErrors is true while forcing the message.
// We use errorsReported as a predicate for broken code.
// So now any forcing won't cause, for instance,
// assertion errors and thus compiler crashes.
// Some messages, once forced, run more code
// to generate useful hints for the user.
try
_errorCount += 1
dia.msg.isNonSensical
finally _errorCount -= 1 // decrease rather than reset the value so we only ever decrease by 1
else dia.msg.isNonSensical) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we simply test hasErrors first? Then the isNonSensical test is assured to run with errorCount > 0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, but I had to make another change too.

hasErrors && // if there are no errors yet, report even if diagnostic is non-sensical
!ctx.settings.YshowSuppressedErrors.value
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/reporting/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ abstract class Reporter extends interfaces.ReporterResult {

private def isIncompleteChecking = incompleteHandler ne defaultIncompleteHandler

private var _errorCount = 0
private var _warningCount = 0
protected var _errorCount = 0
protected var _warningCount = 0

/** The number of errors reported by this reporter (ignoring outer reporters) */
def errorCount: Int = _errorCount
Expand Down
Loading