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 incorrect typing in closure-iterator C code-gen #1355

Merged

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Jun 19, 2024

Summary

Fix the C code generator emitting incorrectly typed code for closure
closure-iterators, which recent, stricter C compilers report errors
for.

Fixes #1353.

Details

When computing the environment for a closure iterator where the
iterator closes over some outer locals, there's already a symbol in
the hidden parameter slot, using the environment type of the enclosing
routine.

The symbol is retyped during closure iterator environment computation,
but the node referencing it was not updated. cgen used the MIR
local's type for the definition, but the node's type (which was still
the enclosing procedure's environment type) for the cast, resulting in
a pointer type mismatch.

The node referencing the symbol is now also updated, and an assertion
to make sure the types match is added in cgen.

Summary
=======

Fix the C code generator emitting incorrectly typed code for closure
closure-iterators, which recent stricter C compilers report errors for.

Details
=======

When computing the environment for closure iterators, and the closure
iterator closes over some outer locals, there's already a symbol in the
hidden parameter slot, using the environment type of the enclosing
routine.

The symbol is retyped during closure iterator environment computation,
but the *node* referencing it was not updated. `cgen` used the MIR
locals type for the definition, but the *node's* type (which was still
the enclosing procedure's environment type) for the cast, resulting in
a pointer type mismatch.

The node referencing the symbol is now also updated, and an assertion
to make sure the types match is added in `cgen`.
@zerbina zerbina added bug Something isn't working compiler/backend Related to backend system of the compiler labels Jun 19, 2024
@zerbina zerbina added this to the C backend rework milestone Jun 19, 2024
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.

Sorry, if I'm missing something, but wouldn't adding the previously broken code (per the bug), with some modification, as a test case be sufficient? It's not perfect, but if we had that scenario tested then we would have caught this, is my thinking.

@zerbina
Copy link
Collaborator Author

zerbina commented Jun 20, 2024

Sorry, if I'm missing something, but wouldn't adding the previously broken code (per the bug), with some modification, as a test case be sufficient?

No worries, it's somewhat non-obvious. The problem affects all closure closure-iterators (regardless of their complexity), so there's already test coverage for the bug (e.g., in tests/closure/tclosure.nim). Incompatible pointer assignments being reported as errors was only introduced in GCC 14 (previously those resulted in just warnings), which CI is seemingly not using yet, so no C compiler errors are reported by CI at the moment.

Now, adding a test where the GCC or Clang warning is promoted to an error is possible, but GCC, at least, has the quirk that promoting a warning to an error only works if C compiler warnings are not disabled across the board (which the NimSkull compiler does by default, by passing -w), so the test has to override the NimSkull C compiler options.

This is exactly what I did in this PR, but said approach has the significant downside that as soon as the NimSkull compiler starts requiring some mission-critical flags to be passed along to the C compiler, the tests will cease to work, so I'd rather not embrace this approach further.

@saem
Copy link
Collaborator

saem commented Jun 20, 2024

Oh wow, that's a much bigger problem -- thanks for the detailed explanation.

@saem
Copy link
Collaborator

saem commented Jun 20, 2024

/merge

Copy link

Merge requested by: @saem

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


Notes for Reviewers

  • hard to add a regression test for without resorting to brittle C compiler argument overrides

@chore-runner chore-runner bot added this pull request to the merge queue Jun 20, 2024
Merged via the queue into nim-works:devel with commit de5ff57 Jun 20, 2024
31 checks passed
@zerbina zerbina deleted the fix-closure-env-param-node-type branch June 22, 2024 13:41
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/backend Related to backend system of the compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

closure iterator that captures the outer proc environment generates incompatible pointer assignments
2 participants