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: remove the destructuring fields quantity check in interpreter #969

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

Gusarich
Copy link
Member

Issue

Closes #968.

Checklist

  • I have updated CHANGELOG.md
  • [ ] I have added tests to demonstrate the contribution is correctly implemented: this usually includes both positive and negative tests, showing the happy path(s) and featuring intentionally broken cases
  • I have run all the tests locally and no test failure was reported
  • I have run the linter, formatter and spellchecker
  • I did not do unrelated and/or undiscussed refactorings

@Gusarich Gusarich added this to the v1.6.0 milestone Oct 16, 2024
@Gusarich Gusarich requested a review from a team as a code owner October 16, 2024 22:18
@Gusarich Gusarich changed the title fix: destructuring fix: remove the destructuring fields quantity check in interpreter Oct 16, 2024
@anton-trunov anton-trunov self-assigned this Oct 17, 2024
Copy link
Member

@anton-trunov anton-trunov left a comment

Choose a reason for hiding this comment

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

Let's add the previously failing test case from the linked issue

Copy link
Contributor

@jeshecdom jeshecdom left a comment

Choose a reason for hiding this comment

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

Regarding issue

@@ -1465,14 +1465,6 @@ export class Interpreter {
ast.expression.loc,
);
}
if (ast.identifiers.size !== Object.keys(val).length - 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the solution is not to remove this code from the interpreter, but to add this number check in the typechecker (resolveStatements.ts in the statement_destruct case of the switch). Let me explain why.

The interpreter is only called when attempting to reduce an expression to a value. So, for example, in this code:

struct Two { first: Int; second: String }

fun discard(s: Two) {
    let Two { first } = s;  // A
}

contract TestContract {

    get fun test1(): Int {
        discard(Two {first: 5, second: "10"});   // B
        return 0;
    }
.............

Line B will trigger the interpreter, because test1 is calling the discard function. In other words, the interpreter will execute the code of discard, detecting the error at line A. If line B is removed, the interpreter is never called, hence it will not detect the error at line A in that case. Therefore, the error at line A should be detected by the typechecker (i.e., the interpreter is just an optimization phase).

At compile-time, there is nothing in the code triggering a receive, which means that the interpreter will never be called in the receive example (again, this should be caught by the typechecker):

struct Two { first: Int; second: String }
message HasTwo { s: Two }

contract TestContract {

    receive(msg: HasTwo) {
       // Almost the same line as in the previous example
       let Two { first } = msg.s;
       
       // No errors before or here
       dump(first);
   }

Just to give an example that the interpreter does catch an example similar to the previous one, in the following example it says the correct error at line A (just changed message HasTwo to a struct):

struct Two { first: Int; second: String }
struct HasTwo { s: Two }

fun discard2(m: HasTwo) {
    let Two { first } = m.s;   // A
}

contract TestContract {

    get fun test2(): Int {
        discard2(HasTwo {s: Two {first: 5, second: "10"}});
        return 0;
    }
.............

Comming back to the code in resolveStatements.ts, I see that the code at line 736 will only detect that the left side of a destructuring statement is contained in the right-hand side. But it will not detect the case when the right-hand side has more fields that the left-hand side, as in the examples above. The number check needs to be added before line 736.

Copy link
Member

@novusnota novusnota Oct 17, 2024

Choose a reason for hiding this comment

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

To add to your point — doesn't removing such check defeat the purpose of having field: _ syntax in the first place? Like, if there's no requirement to unpack all fields, the only purpose of _ is to temporarily stop binding the certain field to a certain variable name. But in this case, it would have been easier to just remove the field from the destructuring statement altogether, wouldn't it?

Because the type and number of elements in any Struct/Message are known at compile-time, I'm guessing it should be possible to add that check when resolving receive() functions and alike too :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. I think when the check is added to resolveStatements.ts, it will handle all the cases, including those in receive declarations.

Copy link
Member Author

Choose a reason for hiding this comment

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

so do you suggest adding this check back (to typechecker)? @anton-trunov what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

I would say we should make the user specify all the fields of a struct unless they explicitly say they do not want to do that. For instance, Rust has the .. syntax for this case, so for the example above this will be let Two { first, .. } = struct;

Copy link
Member Author

Choose a reason for hiding this comment

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

so should we also go with the .. syntax?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, let's go for it

@anton-trunov
Copy link
Member

@Gusarich Let's finish this PR asap, please

Copy link
Member

@novusnota novusnota left a comment

Choose a reason for hiding this comment

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

Comment on lines +161 to +162
StatementDestruct = let typeId "{" ListOf<DestructItem, ","> ","? "}" "=" Expression (";" | &"}") --noRest
| let typeId "{" ListOf<DestructItem, ","> "," ".." ","? "}" "=" Expression (";" | &"}") --withRest
Copy link
Member

Choose a reason for hiding this comment

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

I'd say that allowing trailing comma after .. makes little sense, since you cannot add more stuff later

Suggested change
StatementDestruct = let typeId "{" ListOf<DestructItem, ","> ","? "}" "=" Expression (";" | &"}") --noRest
| let typeId "{" ListOf<DestructItem, ","> "," ".." ","? "}" "=" Expression (";" | &"}") --withRest
StatementDestruct = let typeId "{" ListOf<DestructItem, ","> (","? | "," "..") }" "=" Expression (";" | &"}")

Copy link
Member

@novusnota novusnota Oct 28, 2024

Choose a reason for hiding this comment

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

Better do ("," ".." | ","?), otherwise the ".." will always be omitted in the parse.

Or just ("," ".."?)?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

("," ".."?)? -- this is harder to understand

("," ".." | ","?) -- this means the list of field patterns ends either ends with an optional trailing comma or the ignore unspecified fields pattern

@@ -326,6 +326,7 @@ export type AstStatementDestruct = {
type: AstTypeId;
/** field name -> [field id, local id] */
identifiers: Map<string, [AstId, AstId]>;
rest: boolean;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
rest: boolean;
ignoreUnspecifiedFields: boolean;

let S { b: b4, c: c4 } = s;
let S { a: a1, .. } = s;
let S { b: b1, .. } = s;
let S { c: c1, .., } = s;
Copy link
Member

Choose a reason for hiding this comment

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

this should fail

Copy link
Member Author

Choose a reason for hiding this comment

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

because of .., on line 17?

Copy link
Member

Choose a reason for hiding this comment

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

yep

Copy link
Contributor

@jeshecdom jeshecdom left a comment

Choose a reason for hiding this comment

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

Tests LGTM. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

destructuring of less fields than are in struct throws an error from interpreter
4 participants