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

feat(ast_codegen, span): process SourceType through ast_codegen. #4696

Conversation

rzvxa
Copy link
Contributor

@rzvxa rzvxa commented Aug 6, 2024

No description provided.

@rzvxa rzvxa force-pushed the 08-06-feat_ast_codegen_span_process_sourcetype_through_ast_codegen branch from 914bf87 to 5aff20e Compare August 6, 2024 15:42
@rzvxa rzvxa marked this pull request as ready for review August 6, 2024 15:43
@rzvxa rzvxa requested a review from overlookmotel August 6, 2024 15:46
@rzvxa
Copy link
Contributor Author

rzvxa commented Aug 6, 2024

@overlookmotel this stack should be ready for review.

Copy link

codspeed-hq bot commented Aug 6, 2024

CodSpeed Performance Report

Merging #4696 will not alter performance

Comparing 08-06-feat_ast_codegen_span_process_sourcetype_through_ast_codegen (125c5fd) with main (3f3cb62)

Summary

✅ 29 untouched benchmarks

Copy link
Contributor

@overlookmotel overlookmotel left a comment

Choose a reason for hiding this comment

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

Great to have these types included in schema.

Personally I wouldn't have split the types into a separate file. Is the rationale for that to give the codegen less code to chew through looking for items with an #[ast] attr? If so, I wouldn't worry too much about that - the file is not so large, and it's no big deal if the codegen isn't quite as quick as it could be.

It's annoying not to be able to assert that field offsets are correct because the fields are private. Perhaps at some point we should add a generated folder to oxc_syntax and oxc_span crates, and generate the assertions there so they can access the private fields. We'd need to do that for CloneIn and Serialize traits if we want to codegen their impls for SourceType.

@overlookmotel
Copy link
Contributor

One last thing: Since oxc_span crate now depends on oxc_ast_macros, should we put #[ast] on Span as well?

@overlookmotel overlookmotel force-pushed the 08-06-feat_ast_codegen_process_ast-related_syntax_types branch from d0cb56a to 82e2f6b Compare August 6, 2024 17:15
@overlookmotel overlookmotel changed the base branch from 08-06-feat_ast_codegen_process_ast-related_syntax_types to main August 6, 2024 17:22
@overlookmotel overlookmotel force-pushed the 08-06-feat_ast_codegen_span_process_sourcetype_through_ast_codegen branch from 5aff20e to c807b7b Compare August 6, 2024 17:23
@rzvxa
Copy link
Contributor Author

rzvxa commented Aug 6, 2024

Great to have these types included in schema.

Personally I wouldn't have split the types into a separate file. Is the rationale for that to give the codegen less code to chew through looking for items with an #[ast] attr? If so, I wouldn't worry too much about that - the file is not so large, and it's no big deal if the codegen isn't quite as quick as it could be.

I intentionally panic on unrelated items in the source read by codegen. It isn't for performance purposes, I don't care for performance in the codegen.

It lowers the clutter in the rust definition files, I would even suggest going as far as changing the file extension to something like XXX.d.rs. Otherwise, we have no way of reasoning about what is and isn't included in the codegen(unless you read the source code).

It's annoying not to be able to assert that field offsets are correct because the fields are private. Perhaps at some point we should add a generated folder to oxc_syntax and oxc_span crates, and generate the assertions there so they can access the private fields. We'd need to do that for CloneIn and Serialize traits if we want to codegen their impls for SourceType.

Yeah, Currently I'm working on the generate_derive marker and the CloneIn trait. One limitation of our current approach is that we output one file per generator. That means we get only one output derive_clone_in.rs file.

We used to have multiple outputs from a single generator but the implementation was no good. We need a way to have one generator per crate or one output per type and fold it into crate/module boundaries.

We might not even need such a thing. In general, I prefer we don't invest time on cross-crate compilation, We shouldn't expand codegen far out of AST. Otherwise, we start generating code all over the place and bloat the project.

I can extract the pre-expanded derives as its own thing if we see a reasonable improvement over vanilla derives.

@rzvxa
Copy link
Contributor Author

rzvxa commented Aug 6, 2024

One last thing: Since oxc_span crate now depends on oxc_ast_macros, should we put #[ast] on Span as well?

Sure.

@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Aug 6, 2024
Copy link

graphite-app bot commented Aug 6, 2024

Merge activity

  • Aug 6, 1:40 PM EDT: The merge label 'merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Aug 6, 1:41 PM EDT: overlookmotel added this pull request to the Graphite merge queue.
  • Aug 6, 1:46 PM EDT: overlookmotel merged this pull request with the Graphite merge queue.

@overlookmotel
Copy link
Contributor

overlookmotel commented Aug 6, 2024

Otherwise, we have no way of reasoning about what is and isn't included in the codegen(unless you read the source code).

Personally, I think it's fine to just have #[ast] to inform the reader what it being altered by the codegen. I also like your idea of using #[generate_derive(CloneIn)] - again, that makes it explicit that some code is being generated for the type, rather than it just happening invisibly.

I'll make a PR to add #[ast] to Span.

@overlookmotel overlookmotel force-pushed the 08-06-feat_ast_codegen_span_process_sourcetype_through_ast_codegen branch from c807b7b to 125c5fd Compare August 6, 2024 17:42
@graphite-app graphite-app bot merged commit 125c5fd into main Aug 6, 2024
24 checks passed
@graphite-app graphite-app bot deleted the 08-06-feat_ast_codegen_span_process_sourcetype_through_ast_codegen branch August 6, 2024 17:46
@rzvxa
Copy link
Contributor Author

rzvxa commented Aug 6, 2024

Otherwise, we have no way of reasoning about what is and isn't included in the codegen(unless you read the source code).

Personally, I think it's fine to just have #[ast] to inform the reader what it being altered by the codegen. I also like your idea of using #[generate_derive(CloneIn)] - again, that makes it explicit that some code is being generated for the type, rather than it just happening invisibly.

The problem is when you use #[ast] on something expecting it to work similarly to all places you've seen this marker, But it doesn't; The #[ast] itself isn't the whole picture. we don't know if it is even included in the codegen or not as long as we haven't checked the main file of the codegen.

In the #[ast] macro we can panic if the file isn't already processed by the codegen, which just complicates the compilation, However, it is possible to use it to raise compile errors on illegal usage.

It means when you add #[ast] to a new type you can't build the crate as long as you haven't run the just ast command.

I'll make a PR to add #[ast] to Span.

Thank you!

rzvxa pushed a commit that referenced this pull request Aug 6, 2024
Similar to #4375 and #4698. #4696 added `#[ast]` attribute to types in `oxc_span`, so these types can use `#[serde]` attrs without the `#[cfg_attr(feature = "serialize", ...)]` guard.
@overlookmotel
Copy link
Contributor

In the #[ast] macro we can panic if the file isn't already processed by the codegen, which just complicates the compilation, However, it is possible to use it to raise compile errors on illegal usage.

I think this would be a good idea. Opened oxc-project/backlog#124 to discuss.

rzvxa pushed a commit that referenced this pull request Aug 6, 2024
Add `#[ast]` attr to `Span`.

Due to how AST codegen works, this necessitates putting the type def in a separate file. #4696 (comment)
@oxc-bot oxc-bot mentioned this pull request Aug 8, 2024
Boshen added a commit that referenced this pull request Aug 8, 2024
## [0.24.0] - 2024-08-08

- 75f2207 traverse: [**BREAKING**] Replace `find_scope` with
`ancestor_scopes` returning iterator (#4693) (overlookmotel)

- 506709f traverse: [**BREAKING**] Replace `find_ancestor` with
`ancestors` returning iterator (#4692) (overlookmotel)

### Features

- 23b0040 allocator: Introduce `CloneIn` trait. (#4726) (rzvxa)
- 51c1ca0 ast: Derive `CloneIn` for AST types, using `generate_derive`.
(#4732) (rzvxa)
- e12bd1e ast: Allow conversion from TSAccessibility into &'static str
(#4711) (DonIsaac)
- fd2d9da ast: Improve `AstKind::debug_name` (#4553) (DonIsaac)
- b3b7028 ast: Implement missing Clone, Hash, and Display traits for
literals (#4552) (DonIsaac)
- 54047e0 ast: `GetSpanMut` trait (#4609) (overlookmotel)
- eae401c ast, ast_macros: Apply stable repr to all `#[ast]` enums
(#4373) (rzvxa)
- ec0b4cb ast_codegen: Add `derive_clone_in` generator. (#4731) (rzvxa)
- 2e91ad6 ast_codegen: Support for `generate_derive` marker. (#4728)
(rzvxa)
- 82e2f6b ast_codegen: Process AST-related `syntax` types. (#4694)
(rzvxa)
- 0c52c0d ast_codegen: Add alignment and size data to the schema.
(#4615) (rzvxa)
- 07607d3 ast_codegen, span: Process `Span` through ast_codegen (#4703)
(overlookmotel)
- 125c5fd ast_codegen, span: Process `SourceType` through ast_codegen.
(#4696) (rzvxa)
- eaddc8f linter: Add fixer for eslint/func_names (#4714) (DonIsaac)
- 229a0e9 minifier: Implement dot define for member expressions (#3959)
(camc314)
- 33f1312 semantic: Impl GetSpan for AstNode (#4717) (DonIsaac)
- e42ac3a sourcemap: Add `ConcatSourceMapBuilder::from_sourcemaps`
(#4639) (overlookmotel)
- 2e63618 span: Implement `CloneIn` for the AST-related items. (#4729)
(rzvxa)
- 6a36616 syntax: Derive `CloneIn` for the AST-related items. (#4730)
(rzvxa)

### Bug Fixes

- 4a56954 codegen: Print raw if value is number is Infinity (#4676)
(Boshen)
- 94d3c31 minifier: Avoid removing function declaration from `KeepVar`
(#4722) (Boshen)
- bf43148 minifier: Do not `remove_syntax` in dead_code_elimination
(Boshen)
- bf48c7f minifier: Fix `keep_var` keeping vars from arrow functions
(#4680) (Boshen)
- 9be29af minifier: Temporarily fix shadowed `undefined` variable
(#4678) (Boshen)
- e8b662a minifier: Various fixes to pass minifier conformance (#4667)
(Boshen)
- 01d85de napi/transform: Update napi files (Boshen)
- f290191 oxc_ast_macros: Fix `syn` lacking features to build (Boshen)
- a40a217 parser: Parse `assert` keyword in `TSImportAttributes` (#4610)
(Boshen)
- 03c643a semantic: Incorrect `scope_id` for catch parameter symbols
(#4659) (Dunqing)
- 6c612d1 semantic/jsdoc: Handle whitespace absence (#4642) (leaysgur)
- 0d2c41a semantic/jsdoc: Panic on parsing `type_name_comment`. (#4632)
(rzvxa)
- 9f8f299 syntax: Prevent creating invalid u32 IDs (#4675)
(overlookmotel)
- 4797eaa transformer: Strip TS statements from for in/of statement
bodies (#4686) (overlookmotel)
- 5327acd transformer/react: The `require` IdentifierReference does not
have a `reference_id` (#4658) (Dunqing)
- 3987665 transformer/typescript: Incorrect enum-related
`symbol_id`/`reference_id` (#4660) (Dunqing)
- 4efd54b transformer/typescript: Incorrect `SymbolFlags` for jsx
imports (#4549) (Dunqing)

### Performance

- 8dd76e4 codegen: Reduce size of `LineOffsetTable` (#4643)
(overlookmotel)
- b8e6753 codegen: `u32` indexes in `LineOffsetTable` for source maps
(#4641) (overlookmotel)
- 6ff200d linter: Change react rules and utils to use `Cow` and
`CompactStr` instead of `String` (#4603) (DonIsaac)
- 0f5e982 minifier: Only visit arrow expression after dropping
`console.log` (#4677) (Boshen)
- ff43dff sourcemap: Speed up VLQ encoding (#4633) (overlookmotel)
- a330773 sourcemap: Reduce string copying in `ConcatSourceMapBuilder`
(#4638) (overlookmotel)
- 372316b sourcemap: `ConcatSourceMapBuilder` extend `source_contents`
in separate loop (#4634) (overlookmotel)
- c7f1d48 sourcemap: Keep local copy of previous token in VLQ encode
(#4596) (overlookmotel)
- 590d795 sourcemap: Shorten main loop encoding VLQ (#4586)
(overlookmotel)

### Documentation

- c69ada4 ast: Improve AST node documentation (#4051) (Rintaro Itokawa)

### Refactor

- 579b797 ast: Use type identifier instead of `CloneIn::Cloned` GAT.
(#4738) (rzvxa)
- 475266d ast: Use correct lifetimes for name-related methods (#4712)
(DonIsaac)
- 83b6ca9 ast: Add explicit enum discriminants. (#4689) (rzvxa)
- ba70001 ast: Put `assert_layouts.rs` behind `debug_assertions` (#4621)
(rzvxa)
- 3f53b6f ast: Make AST structs `repr(C)`. (#4614) (rzvxa)
- 452e0ee ast: Remove defunct `visit_as` + `visit_args` attrs from
`#[ast]` macro (#4599) (overlookmotel)
- 2218340 ast, ast_codegen: Use `generate_derive` for implementing
`GetSpan` and `GetSpanMut` traits. (#4735) (rzvxa)
- fbfd852 minifier: Add `NodeUtil` trait for accessing symbols on ast
nodes (#4734) (Boshen)
- e0832f8 minifier: Use `oxc_traverse` for AST passes (#4725) (Boshen)
- 17602db minifier: Move tests and files around (Boshen)
- 3289477 minifier: Clean up tests (#4724) (Boshen)
- e78cba6 minifier: Ast passes infrastructure (#4625) (Boshen)
- d25dea7 parser: Use `ast_builder` in more places. (#4612) (rzvxa)
- 09d9822 semantic: Simplify setting scope flags (#4674) (overlookmotel)
- 6e453db semantic: Simplify inherit scope flags from parent scope
(#4664) (Dunqing)
- e1429e5 span: Reduce #[cfg_attr] boilerplate in type defs (#4702)
(overlookmotel)
- e24fb5b syntax: Add explicit enum discriminants to AST related types.
(#4691) (rzvxa)
- 3f3cb62 syntax, span: Reduce #[cfg_attr] boilerplate in type defs
(#4698) (overlookmotel)
- 54f9897 traverse: Simpler code for entering/exiting unconditional
scopes (#4685) (overlookmotel)
- 83546d3 traverse: Enter node before entering scope (#4684)
(overlookmotel)- 9b51e04 Overhaul napi transformer package (#4592)
(DonIsaac)

### Testing

- 49d5196 ast: Fix `assert_layouts.rs` offset tests on 32bit platforms.
(#4620) (rzvxa)

Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0-merge Merge with Graphite Merge Queue A-ast Area - AST
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants