-
Notifications
You must be signed in to change notification settings - Fork 0
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
Better way to replace Statements + make AstBuilder::move_* cheaper #103
Comments
These will propagate confusion to all other tools. I think what I intended with "replace with It would be a none issue once this is coupled with the |
Yes, I can see there are downsides to this. I will try again to come up with a We can also solve the problems to a degree by other means: Removing statements
Tricky, but probably doable, and would have other benefits (lower memory usage, ability to keep AST and scopes tree in sync at all times in transformer, ability to also insert statements from a child visitor). Make
|
Expression::None
and Statement::None
?
For some reason, this hasn't transferred from Oxc repo correctly. Closing and will try to transfer it again. |
I wonder if we should add
Expression::None
andStatement::None
variants to AST?Use cases
1. Removing
Statement
sRemoving a
Statement
from aVec<Statement>
(as various transforms do) would be very easy and would not require "shuffling up" theVec
. Just replace the statement withStatement::None
.Make
AstBuilder::move_expression
cheaperCurrently
move_expression
substitutes a dummyBox<NullExpression>
into the AST. This involves allocating space for it in the arena and writing it.In contrast,
Expression::None
would be an enum variant with no "payload" and so would not require allocation. Probably then the compiler will be able to see code like this:and understand that it can skip writing
Expression::None
completely, because it's pointless - it's overwritten again shortly after. So likely it'll be as efficient asAstBuilder::copy
, while being completely safe.Right now it can't do that, because
move_expression
currently has an observable side effect of advancing the bump allocator's pointer.Codegen
Codegen would need to skip over
Statement::None
as if its not there.It would panic on
Expression::None
. If any transforms are malfunctioning and leavingExpression::None
s in the AST, we'd discover that very quickly as we'd get panics in CI.Alternatives
I've tried to dream up other APIs to replace
move_expression
. e.g. some kind ofAstBuilder::replace_expression
which uses unsafe code to avoid writing anything to the AST until it gets the replacement to put back in, and uses the type system to ensure you can't commit UB. But it seems pretty intractable to me. Simple cases like a one-for-one replacement are easy enough, but once you get into pulling out child nodes, combining them in a new struct, and pushing that back into the AST (which transformer does a lot), it gets... hard.So, while I'm not that keen on adding "fake" types into the AST, I can't see a more viable solution to alleviating these pain points at present.
The text was updated successfully, but these errors were encountered: