-
Notifications
You must be signed in to change notification settings - Fork 107
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
base: main
Are you sure you want to change the base?
Changes from all commits
4e99d21
8cf2173
72ebd37
569de25
d8f035d
7bb1ef4
4b9e402
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -158,7 +158,8 @@ Tact { | |||||||
|
||||||||
StatementForEach = foreach "(" id "," id "in" Expression ")" "{" Statement* "}" | ||||||||
|
||||||||
StatementDestruct = let typeId "{" ListOf<DestructItem, ","> ","? "}" "=" Expression (";" | &"}") | ||||||||
StatementDestruct = let typeId "{" ListOf<DestructItem, ","> ","? "}" "=" Expression (";" | &"}") --noRest | ||||||||
| let typeId "{" ListOf<DestructItem, ","> "," ".." ","? "}" "=" Expression (";" | &"}") --withRest | ||||||||
Comment on lines
+161
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say that allowing trailing comma after
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better do Or just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
|
||||||||
DestructItem = id ":" id --regular | ||||||||
| id --punned | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,12 @@ message M { | |
fun testFunc(): Int { | ||
let s = S{ a: 1, b: 2, c: 3 }; | ||
let S { a, b, c } = s; | ||
let S { a: a1 } = s; | ||
let S { b: b1 } = s; | ||
let S { c: c1 } = s; | ||
let S { a: a2, b: b2 } = s; | ||
let S { a: a3, c: c3 } = s; | ||
let S { b: b4, c: c4 } = s; | ||
let S { a: a1, .. } = s; | ||
let S { b: b1, .. } = s; | ||
let S { c: c1, .., } = s; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should fail There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep |
||
let S { a: a2, b: b2, .. } = s; | ||
let S { a: a3, c: c3, .., } = s; | ||
let S { b: b4, c: c4, .. } = s; | ||
|
||
let m = M{ a: 1, b: 2 }; | ||
let M { a: a_m, b: b_m } = m; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1465,14 +1465,6 @@ export class Interpreter { | |
ast.expression.loc, | ||
); | ||
} | ||
if (ast.identifiers.size !== Object.keys(val).length - 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( The interpreter is only called when attempting to reduce an expression to a value. So, for example, in this code:
Line At compile-time, there is nothing in the code triggering a
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
Comming back to the code in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I think when the check is added to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so should we also go with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, let's go for it |
||
throwErrorConstEval( | ||
`destructuring assignment expected ${Object.keys(val).length - 1} fields, but got ${ | ||
ast.identifiers.size | ||
}`, | ||
ast.loc, | ||
); | ||
} | ||
|
||
for (const [field, name] of ast.identifiers.values()) { | ||
if (name.text === "_") { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.