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

semFor propagate iterator position error nodes #932

Merged
merged 8 commits into from
Oct 5, 2023

Conversation

bung87
Copy link
Contributor

@bung87 bung87 commented Sep 30, 2023

Summary

semFor propagates errors that occur in the iterator position. They
are
now properly reported and semFor produces errors as required.

Details

Updated semFor to propagate error nodes in the iterator position, as
well as appropriately wrap the semantically analysed nkFor if an
error
is present.

Added adSemForExpectedIterator to AstDiagKind to capture the error
as a node instead of using legacy reports localReport.

Copy link
Collaborator

@saem saem left a comment

Choose a reason for hiding this comment

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

this change needs some tests.

reach out if you get stuck.

@saem saem added bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler labels Sep 30, 2023
@zerbina
Copy link
Collaborator

zerbina commented Sep 30, 2023

Hm, but there's no IndexDefect happening? When semExprNoDeref returns an nkError, isCallExpr will be 'false', and call is thus never indexed into.

I've tried with both your test case, and the reduced:

for x in doesntExist: # provokes an error
  discard

and neither fails.

If you're encountering an error IndexDefect as part of the LSP work, then it's not related to the error handling in semFor. Changing var hasError = false in semForVars to var hasError = n[^2].kind == nkError should be enough to propagate errors in the iterable expression slot.

@saem
Copy link
Collaborator

saem commented Sep 30, 2023

I tried the following, which is a variant of what the macro did, and it failed appropriately without a crash:

for i in (if true: discard else: discard):
  discard

@saem
Copy link
Collaborator

saem commented Sep 30, 2023

@bung87 do you have a stacktrace or something that shows the variant field access error?

@bung87
Copy link
Contributor Author

bung87 commented Sep 30, 2023

Hm, but there's no IndexDefect happening? When semExprNoDeref returns an nkError, isCallExpr will be 'false', and call is thus never indexed into.

I've tried with both your test case, and the reduced:

for x in doesntExist: # provokes an error
  discard

and neither fails.

If you're encountering an error IndexDefect as part of the LSP work, then it's not related to the error handling in semFor. Changing var hasError = false in semForVars to var hasError = n[^2].kind == nkError should be enough to propagate errors in the iterable expression slot.

it's in not isCallExpr or call[0].kind != nkSym or call[0].sym.kind != skIterator

@zerbina
Copy link
Collaborator

zerbina commented Sep 30, 2023

it's in not isCallExpr or call[0].kind != nkSym or call[0].sym.kind != skIterator

Yes, but if call is an nkError, then isCallExpr is 'false', and the entire rest of the or chain is short-circuited.

@saem
Copy link
Collaborator

saem commented Sep 30, 2023

If it was an nkError then it would be a field not accessible error, if it's an index error that means it's a empty (invalid) call node. Furthermore, the isCallExpr would be false, and would not evaluate the rest of the boolean expression (McCarthy evaluation).

@bung87 can you do a astrepr.debug on call or n to see the shape of the ast?

@bung87
Copy link
Contributor Author

bung87 commented Sep 30, 2023

hmm, I can't reproduce but I encouter index error multiple times, I just remind of it is a call with empty children.

@bung87 bung87 changed the title fix semFor not handle semExprNoDeref returns error node fix semFor not handle call with empty children Sep 30, 2023
@bung87
Copy link
Contributor Author

bung87 commented Sep 30, 2023

As it's not clear for now, I'll investigate later

@bung87 bung87 marked this pull request as draft October 1, 2023 02:28
@bung87
Copy link
Contributor Author

bung87 commented Oct 1, 2023

Found this is because LSP not properly handle global error reports come from like checkMinSonsLen, checkMinSonsLen which raise error in command line mode prevent compiler continue compiles, this may affect nimsuggest as well.

@bung87 bung87 changed the title fix semFor not handle call with empty children semFor propagate error node Oct 3, 2023
@bung87 bung87 marked this pull request as ready for review October 3, 2023 14:53
@bung87
Copy link
Contributor Author

bung87 commented Oct 3, 2023

now changed as a refactoring not a bugfix anymore.

@saem saem added refactor Implementation refactor and removed bug Something isn't working labels Oct 3, 2023
compiler/sem/semstmts.nim Outdated Show resolved Hide resolved
@bung87 bung87 requested a review from saem October 4, 2023 19:07
compiler/sem/semstmts.nim Outdated Show resolved Hide resolved
compiler/sem/semstmts.nim Outdated Show resolved Hide resolved
Copy link
Collaborator

@saem saem left a comment

Choose a reason for hiding this comment

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

found a couple items

bung87 and others added 3 commits October 5, 2023 12:04
Co-authored-by: Saem Ghani <saemghani+github@gmail.com>
@bung87 bung87 requested a review from saem October 5, 2023 15:51
@saem saem changed the title semFor propagate error node semFor propagate iterator position error nodes Oct 5, 2023
Copy link
Collaborator

@saem saem left a comment

Choose a reason for hiding this comment

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

The new code is better, nice.

I did have to spend a fair bit of time updating the commit message. The formatting was off and the summary contained an implementation description, instead of a description of behavioural change.

@saem
Copy link
Collaborator

saem commented Oct 5, 2023

/merge

@github-actions
Copy link

github-actions bot commented Oct 5, 2023

Merge requested by: @saem

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


Notes for Reviewers

@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 07e6958 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
compiler/sem Related to semantic-analysis system of the compiler refactor Implementation refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants