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 JSON deserialization of AssignExpr #5179

Merged
merged 6 commits into from
Aug 9, 2022

Conversation

overlookmotel
Copy link
Contributor

Description:

Fix for #5168. However:

  1. Surely there's an easier way to do this?
  2. I imagine there is a more idiomatic way to use match { } here:

https://github.com/overlookmotel/swc/blob/assign-expr-deserialize/crates/swc_ecma_ast/src/expr.rs#L568-L576

I would appreciate any pointers as to how to improve this.

Related issue (if exists):

#5168

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Please remove images

@overlookmotel overlookmotel force-pushed the assign-expr-deserialize branch from 7106287 to 8881aa9 Compare July 11, 2022 14:01
@overlookmotel
Copy link
Contributor Author

Oops sorry I based this on wrong branch. Have pushed again with just the relevant commit.

@magic-akari
Copy link
Member

magic-akari commented Jul 11, 2022

Is there a case where PatOrExpr::Expr is not Expr::Ident? @kdy1
I wonder if PatOrExpr is redundant?


Oh, it may be Expr::Member.

@kdy1
Copy link
Member

kdy1 commented Jul 11, 2022

PatOrExpr::Expr stores MemberExpr and some more, and more (I think) with operator assignments, but I'm not sure about it at the moment.

I think Pat::Expr can be used instead of it.

@magic-akari
Copy link
Member

It seems a lot of work to do if I remove the PatOrExpr, I will try it later.
And I don't have strong opinions on this pull request change. It's ok to fix #5168.

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thank you!

crates/swc_ecma_ast/src/expr.rs Outdated Show resolved Hide resolved
crates/swc_ecma_ast/src/expr.rs Outdated Show resolved Hide resolved
crates/swc_ecma_ast/src/expr.rs Outdated Show resolved Hide resolved
crates/swc_ecma_ast/src/expr.rs Outdated Show resolved Hide resolved
crates/swc_ecma_ast/src/expr.rs Outdated Show resolved Hide resolved
@kdy1 kdy1 added this to the Planned milestone Jul 11, 2022
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thanks!


swc-bump:

  • swc_ecma_ast

@kdy1 kdy1 enabled auto-merge (squash) August 9, 2022 13:40
Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script

@overlookmotel
Copy link
Contributor Author

overlookmotel commented Aug 9, 2022

I've made the changes requested and tidied up some other bits.

I'm still not completely happy with this:

if let PatOrExpr::Pat(ref pat) = left {
  if let Pat::Ident(ident) = &**pat {
    left = PatOrExpr::Expr(Box::new(Expr::Ident(ident.id.clone())));
  }
}

The .clone() call should be unnecessary - ident.id can just be moved from the old structure to the new. But I couldn't find a way to get the compiler to accept this, since pat is borrowed.

My first attempt was:

left = match left {
  PatOrExpr::Pat(pat) => match *pat {
    Pat::Ident(ident) => PatOrExpr::Expr(Box::new(Expr::Ident(ident.id))),
    pat => PatOrExpr::Pat(Box::new(pat)),
  },
  left => left,
}

This avoids cloning as it takes ownership, but has the worse consequences of unclear code and re-creating PatOrExpr::Pat(Box::new(pat)) unnecessarily in the majority of cases.

Or am I worrying too much? Will the compiler compile away the .clone() call?

@kdy1
Copy link
Member

kdy1 commented Aug 9, 2022

I think so?
Even if the compiler fails to optimize it, it will not affect performance in almost all cases.
clone() of Ident is typically very cheap.

@overlookmotel
Copy link
Contributor Author

Is there no way to tell compiler "give me ownership of ident, and make left and pat uninitialized"? Sorry, this has nothing to do with SWC specifically, I'm just still learning Rust. This feels like it'd be a common situation.

@kdy1 kdy1 merged commit 53627af into swc-project:main Aug 9, 2022
@overlookmotel overlookmotel deleted the assign-expr-deserialize branch August 9, 2022 18:20
@kdy1 kdy1 added this to the v1.2.226 milestone Aug 12, 2022
@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants