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

fix(sem): nimsuggest crashing for ill-formed AST #939

Merged
merged 20 commits into from
Oct 5, 2023

Conversation

bung87
Copy link
Contributor

@bung87 bung87 commented Oct 3, 2023

Summary

Fix nimsuggest, and other tooling that uses a custom structured
report hook, crashing when encountering ill-formed AST that has the
wrong number of nodes.

Details

Many of the semantic analysis procedures first verify that the input
PNode has the right number of child nodes. This is done via the
checkSonsLen and checkMinSonsLen procedures, with the expectation
being that both raise an ERecoverableError (through globalReport)
when the check fails.

However, since reporting the error happens via handleReport, it's
possible that the error handling strategy is overridden with a mode
(e.g., doNothing) that results in execution to continue without any
exception being raised. nimsuggest sets the error handling mode to
doNothing, which then resulted in checkSonsLen and
checkMinSonsLen not ending the active routine, usually leading to
IndexDefects or other crashes.

As an interim solution, the reportAndFail procedure is introduced.
reportAndForceRaise works similar to similar to handleReport, but
it ensures that at least an exception is raised -- both
checkSonsLen and checkMinSonsLen use it to ensure that the calling
routine is exited.

@bung87 bung87 mentioned this pull request Oct 3, 2023
@bung87 bung87 marked this pull request as ready for review October 3, 2023 11:40
Copy link
Collaborator

@zerbina zerbina left a comment

Choose a reason for hiding this comment

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

Thank you. Can the other PR be closed now?

The details section in the PR message is good, but the title and summary need to be adjusted. This PR represents a significant fix for nimsuggest, but it also affects compiles, both which needs to be highlighted.

Regarding the implementation, I've left some requests regarding code style, documentation, and test improvements.

compiler/front/msgs.nim Outdated Show resolved Hide resolved
compiler/front/msgs.nim Outdated Show resolved Hide resolved
nimsuggest/tests/terror_handling.nim Outdated Show resolved Hide resolved
nimsuggest/tests/terror_handling.nim Outdated Show resolved Hide resolved
compiler/front/msgs.nim Outdated Show resolved Hide resolved
bung87 and others added 6 commits October 4, 2023 01:03
Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com>
Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com>
compiler/front/msgs.nim Outdated Show resolved Hide resolved
compiler/front/msgs.nim Outdated Show resolved Hide resolved
Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com>
@zerbina zerbina added bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler compiler/msgs Compiler output and diagnostic subsystem: error and warnig reporting, information, debugging labels Oct 3, 2023
@bung87 bung87 changed the title add reportAndForceRaise fix checkSonsLen, checkSonsMinLen not fast fails in suggest mode Oct 4, 2023
@bung87 bung87 changed the title fix checkSonsLen, checkSonsMinLen not fast fails in suggest mode fix checkSonsLen, checkSonsMinLen not fast fail in suggest mode Oct 4, 2023
bung87 and others added 2 commits October 4, 2023 20:59
Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com>
compiler/front/msgs.nim Outdated Show resolved Hide resolved
compiler/front/msgs.nim Outdated Show resolved Hide resolved
compiler/front/msgs.nim Outdated Show resolved Hide resolved
compiler/front/msgs.nim Show resolved Hide resolved
bung87 and others added 4 commits October 4, 2023 21:04
Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com>
Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com>
Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com>
Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com>
Copy link
Collaborator

@zerbina zerbina left a comment

Choose a reason for hiding this comment

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

Sorry for not noticing this earlier. As is, the implementation ignores the default actions, which means that --errorMax is ignored by reportAndForceRaise.

You need to change reportAndForceRaise to first go through errorActions (refer to handleReport for how this should look like), before forcing doNothing to doRaise.

At this point, I think that reportAndFail would be a better name for the procedure.

@bung87
Copy link
Contributor Author

bung87 commented Oct 4, 2023

like this?

case conf.report(rep)
of doAbort:
  quit 1
of doDefault:
  let (action, trace) = errorActions(conf, rep, eh)
  case action
  of doAbort:
    quit 1
  of doRaise, doNothing:
    raiseRecoverableError("report")
of doRaise, doNothing:
  raiseRecoverableError("report")

compiler/front/msgs.nim Outdated Show resolved Hide resolved
@zerbina
Copy link
Collaborator

zerbina commented Oct 4, 2023

Hmm, I just noticed a much simpler and also more broad fix that also fixes internalError being turned into a no-op by the hook. The reporting subsystem is quite convoluted, and I have some trouble navigating it.

However, I don't want to block this PR any longer, and would thus make a follow-up PR myself. In the meantime, this fix works well enough. Sorry for all the detours, and thank you for working on this.

@zerbina zerbina changed the title fix checkSonsLen, checkSonsMinLen not fast fail in suggest mode fix(sem): nimsuggest crashing for ill-formed AST Oct 4, 2023
@zerbina
Copy link
Collaborator

zerbina commented Oct 4, 2023

@bung87 I've updated the PR message and title, please take a look at it:

  • I reworded the title so that it's a bit shorter
  • the summary gives a broad overview of what failed and what works now. For a tooling-related fix, this should focus on user-facing behaviour. The previous summary listed the implementation details, but didn't summarize the fixed issue

Once the remaining suggestion is applied, this PR is good to be merged.

Co-authored-by: zerbina <100542850+zerbina@users.noreply.github.com>
@bung87
Copy link
Contributor Author

bung87 commented Oct 5, 2023

Hmm, I just noticed a much simpler and also more broad fix that also fixes internalError being turned into a no-op by the hook. The reporting subsystem is quite convoluted, and I have some trouble navigating it.

However, I don't want to block this PR any longer, and would thus make a follow-up PR myself. In the meantime, this fix works well enough. Sorry for all the detours, and thank you for working on this.

I also think this can be simpler, eg. the condition can depends on config.cmd that give a default handling that doesn't need TErrorHandling returns from user side. but I didn't think further.

@zerbina
Copy link
Collaborator

zerbina commented Oct 5, 2023

/merge

@github-actions
Copy link

github-actions bot commented Oct 5, 2023

Merge requested by: @zerbina

Contents after the first section break of the PR description has been removed and preserved below:


Notes for Reviewers

this is made for following @zerbina suggestion.

@chore-runner chore-runner bot added this pull request to the merge queue Oct 5, 2023
Merged via the queue into nim-works:devel with commit 38b50a9 Oct 5, 2023
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler/msgs Compiler output and diagnostic subsystem: error and warnig reporting, information, debugging compiler/sem Related to semantic-analysis system of the compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants