Fix member accesses for nested structs returned from funcs#537
Closed
Fix member accesses for nested structs returned from funcs#537
Conversation
…s in codegen_idenntifier
guipublic
requested changes
Nov 30, 2022
Contributor
guipublic
left a comment
There was a problem hiding this comment.
Considering the comments explaining that this does not solve all the cases, I thought it would be simpler to start from scratch, so I implemented nested structures from functions in PR #542 and I suggest to close this one.
5 tasks
Contributor
Author
|
This PR is deprecated by PR #542 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related issue(s)
Resolves #529
Trying to resolve #492, however, there is still an issue.
Description
This PR aims to fix how nested tuples are handled in SSA. When returning a nested struct from a function there is a high chance of a mismatch in the expected field member node actually being accessed and the one that should be accessed. The error is elaborated upon in issue #529.
Summary of changes
I added extra logic to the
codegen_identifiermethod. We determine whether the ident being accessed is a nested struct/tuple that was returned from a function.When creating an SSA function we flatten the return values. This can lead to a mismatch in the NodeId that is fetched. We recreate a struct/tuple with an unflattened structure in order to enable accurate indexing. If the Value::Tuple has a longer list of NodeIds (meaning it has been flattened) we know to use our recreated value which possesses the correct structure.NOTE I had an issue with stack overflows that I previously mentioned in this PR description, but I just partially fixed it. However, it broke this change I mention in the paragraph below. Reference Additional Context section for more details.
I also added a conditional to
into_field_memberincode_gen.rs. In #492 it looks like when we had a struct with a singular element, which when returned from a function is also flattened. However, it is flattened into a singular value causing it to hit unreachable code. I simply check whether thefield_indexbeing accessed is 0 and then return the value object itself if this is the case.Dependency additions / changes
N/A
Test additions / changes
I added a test showing the usage of nested structs being returned from functions. I also added a test for the example provided in issue #492. Look to # Additional Context for more information about the comments.
Checklist
cargo fmtwith default settings.Additional context
THIS ISSUE PARTIALLY FIXED
There is something missing still in SSA when handling the nested struct inside of the main function. In my test I highlight the snippets that can cause this error. Currently, I must specify the return value in the
Prover.tomlif that return value comes from a nested struct returned by a function in main. If I attempt to have Noir auto-fill I will get a stack overflow when attempting to solve. However, auto-fill still works as expected if I access the nested struct member in a separate function (non-main func). I can then return from main the value returned from this separate function as expected. This is shown in my test in this PR. I have a suspicion this could be related to accessing the correct SSA nodes when I construct a new Value::Tuple.Fields from a nested struct that have been constrained elsewhere in
maincan bemainreturn values, however, I am still getting a stack overflow when trying to return nested struct fields that have not been used. It is looking like I may have to add extra constraints for nested structs return by a func that are then accessed inside ofmain.STILL BROKEN
This PR is still not feature complete. Fixing the fixed handling of individual nested structs seems to not be working as well for tuples. The issue that was occurs in #492 has been replaced with this bug:
I did not have a chance to investigate this further, but I do not think it should be a hard fix and want to get it into this PR.