Repository navigation
feat(sv)!: support shallow copies of class objects #1810
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -50,6 +50,10 @@ pub(super) enum Job { | |
| minimum: u8, | ||
| member: Name, | ||
| }, | ||
| Copy { | ||
| start: usize, | ||
| minimum: u8, | ||
| }, | ||
| GroupTypical { | ||
| start: usize, | ||
| minimum: u8, | ||
|
|
@@ -312,6 +316,7 @@ impl Parser<'_> { | |
| pending.push(*expression); | ||
| } | ||
| ExprKind::Group(value) | ||
| | ExprKind::Copy(value) | ||
| | ExprKind::Member { | ||
| expression: value, .. | ||
| } => pending.push(*value), | ||
|
|
@@ -582,6 +587,22 @@ impl Parser<'_> { | |
| jobs.push(Job::Expression(0)); | ||
| } | ||
| } | ||
| // `new h` copies the object of `h`; `new` followed | ||
| // by `(`, or by nothing, constructs one (8.7, 8.12). | ||
| "new" | ||
| if self.indices.get(self.at + 1).is_some_and(|&index| { | ||
| let token = &self.tokens[index]; | ||
| matches!( | ||
| token.kind, | ||
| TokenKind::Identifier | TokenKind::EscapedIdentifier | ||
| ) && (!self.reserved_identifier(token) | ||
| || matches!(token.text.as_str(), "this" | "super")) | ||
|
Comment on lines
+597
to
+599
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.
The lookahead recognizes a shallow copy only when the token after Useful? React with 👍 / 👎.
Contributor
Author
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. Tracked in #1891 (shallow copies of class-valued expressions, in any context and of |
||
| }) => | ||
| { | ||
| self.at += 1; | ||
| jobs.push(Job::Copy { start, minimum }); | ||
| jobs.push(Job::Expression(0)); | ||
| } | ||
| "(" => { | ||
| self.at += 1; | ||
| jobs.push(Job::Group { start, minimum }); | ||
|
|
@@ -924,6 +945,10 @@ impl Parser<'_> { | |
| ExprKind::Tagged { member, value }, | ||
| )?; | ||
| } | ||
| Job::Copy { start, minimum } => { | ||
| let value = values.pop().unwrap().expression(); | ||
| self.tail(&mut jobs, start, minimum, ExprKind::Copy(value))?; | ||
| } | ||
| Job::GroupTypical { | ||
| start, | ||
| minimum, | ||
|
|
||
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.
A
Copynode is lowered only whenconstructoris called for the complete right-hand side of an assignment, declaration initializer, or property initializer. In other valid contextual positions such astake(new h),return new h, or a conditional branch, normalbind_valuetraversal reaches the new node and rejects it as an unsupported primary expression, even when the formal, return, or enclosing assignment type supplies the required class. HandleExprKind::Copywherever a class-valued expression is bound, rather than only at top-level assignment sites.Useful? React with 👍 / 👎.
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.
Tracked in #1891 (shallow copies of class-valued expressions, in any context and of
null): these forms are rejected with a diagnostic today, not lowered wrongly.