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(ast_macros): raise compile error on invalid generate_derive input. #4766

Conversation

rzvxa
Copy link
Contributor

@rzvxa rzvxa commented Aug 8, 2024

It checks 2 things. 1) The input is a supported derive 2) The given identifier is the same as the fully qualified target trait.

The latter makes sure that the trait for derive is included in the scope.

Part of oxc-project/backlog#124

Here's an expanded example of how we assert traits:

const _:() = {
    {
        trait AssertionTrait: ::oxc_allocator::CloneIn<'static> {}
        impl<T: CloneIn<'static>> AssertionTrait for T {}
    };
};

It makes sure CloneIn is the same as ::oxc_allocator::CloneIn and more importantly requires the user to include the trait if they wish to use it with generate_derive.

It also provides LSP jump to definition.

Copy link
Contributor Author

rzvxa commented Aug 8, 2024

@github-actions github-actions bot added the A-ast Area - AST label Aug 8, 2024
@rzvxa rzvxa force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch 2 times, most recently from 34fe924 to 62ee689 Compare August 8, 2024 16:18
@rzvxa rzvxa force-pushed the 08-08-fix_ast_ast_codegen_use_generate_derive_instead_of_visitable_for_generating_span_derives branch from 8440cdf to 862fe61 Compare August 8, 2024 16:23
@rzvxa rzvxa force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch from 62ee689 to 4125aad Compare August 8, 2024 16:23
@rzvxa rzvxa requested a review from overlookmotel August 8, 2024 16:27
@rzvxa rzvxa force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch from 4125aad to a61a01f Compare August 8, 2024 16:28
Copy link

codspeed-hq bot commented Aug 8, 2024

CodSpeed Performance Report

Merging #4766 will not alter performance

Comparing 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input (f5eeebd) with main (7ea058d)

Summary

✅ 29 untouched benchmarks

@rzvxa rzvxa force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch from a61a01f to 6f4c6bf Compare August 8, 2024 16:37
@rzvxa rzvxa marked this pull request as ready for review August 8, 2024 16:40
@overlookmotel overlookmotel force-pushed the 08-08-fix_ast_ast_codegen_use_generate_derive_instead_of_visitable_for_generating_span_derives branch from 862fe61 to 60b6cf9 Compare August 8, 2024 16:43
@overlookmotel overlookmotel force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch from 6f4c6bf to 15c92ab Compare August 8, 2024 16:43
@rzvxa rzvxa force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch from 15c92ab to 3bff36e Compare August 8, 2024 17:01
@overlookmotel overlookmotel force-pushed the 08-08-fix_ast_ast_codegen_use_generate_derive_instead_of_visitable_for_generating_span_derives branch from 60b6cf9 to e711be3 Compare August 8, 2024 17:02
@overlookmotel overlookmotel changed the base branch from 08-08-fix_ast_ast_codegen_use_generate_derive_instead_of_visitable_for_generating_span_derives to graphite-base/4766 August 8, 2024 17:06
@overlookmotel overlookmotel force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch from 3bff36e to 2b7f89e Compare August 8, 2024 17:10
@overlookmotel overlookmotel changed the base branch from graphite-base/4766 to main August 8, 2024 17:11
@overlookmotel overlookmotel force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch from 2b7f89e to df19742 Compare August 8, 2024 17:39
@rzvxa rzvxa force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch from df19742 to 2c0b146 Compare August 8, 2024 18:20
@overlookmotel overlookmotel force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch 2 times, most recently from ca2b441 to 3850558 Compare August 8, 2024 18:41
@overlookmotel
Copy link
Contributor

This is absolutely ingenious!

You can fool it with:

use oxc_span::GetSpan as GetSpanReal;
trait GetSpan: GetSpanReal {}

#[ast]
#[generate_derive(GetSpan)]
struct Foo { /* blah */ }

But I can't see any way to catch that one, and anyone who does such a bizarre thing probably deserves everything they get!

I've pushed 2 more commits.

  1. Adding comments. I've removed the stuff about "all computation is cached... without any complex operation" because I don't think we can reasonably claim there's no complexity any more.
  2. A nit - just removing an unnecessary ; from generated code.

If you're happy with those changes, please go ahead and merge.

@rzvxa
Copy link
Contributor Author

rzvxa commented Aug 8, 2024

You can fool it with:

use oxc_span::GetSpan as GetSpanReal;
trait GetSpan: GetSpanReal {}

#[ast]
#[generate_derive(GetSpan)]
struct Foo { /* blah */ }

But I can't see any way to catch that one, and anyone who does such a bizarre thing probably deserves everything they get!

Crap, You are right; And I can't figure out how exactly we can prevent it. If I can think of one I'll follow it up with another PR.

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

graphite-app bot commented Aug 8, 2024

Merge activity

  • Aug 8, 3:05 PM EDT: The merge label 'merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Aug 8, 3:06 PM EDT: rzvxa added this pull request to the Graphite merge queue.
  • Aug 8, 3:09 PM EDT: rzvxa merged this pull request with the Graphite merge queue.

…ut. (#4766)

It checks 2 things. 1) The input is a supported derive 2) The given identifier is the same as the fully qualified target trait.

The latter makes sure that the trait for derive is included in the scope.

Part of #4704

Here's an expanded example of how we assert traits:

```rust
const _:() = {
    {
        trait AssertionTrait: ::oxc_allocator::CloneIn<'static> {}
        impl<T: CloneIn<'static>> AssertionTrait for T {}
    };
};
```

It makes sure `CloneIn` is the same as `::oxc_allocator::CloneIn` and more importantly requires the user to include the trait if they wish to use it with `generate_derive`.

It also provides LSP jump to definition.
@rzvxa rzvxa force-pushed the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch from 3850558 to f5eeebd Compare August 8, 2024 19:06
@graphite-app graphite-app bot merged commit f5eeebd into main Aug 8, 2024
24 checks passed
@graphite-app graphite-app bot deleted the 08-08-fix_ast_macros_raise_compile_error_on_invalid_generate_derive_input branch August 8, 2024 19:09
@overlookmotel
Copy link
Contributor

Crap, You are right; And I can't figure out how exactly we can prevent it. If I can think of one I'll follow it up with another PR.

Just to say, this is a tiny point. For all practical purposes, this works. And I noticed this morning that "jump to definition" now works too, which is brilliant.

@rzvxa
Copy link
Contributor Author

rzvxa commented Aug 9, 2024

Crap, You are right; And I can't figure out how exactly we can prevent it. If I can think of one I'll follow it up with another PR.

Just to say, this is a tiny point. For all practical purposes, this works. And I noticed this morning that "jump to definition" now works too, which is brilliant.

Yeah, the jump to definition is a nice plus.

@oxc-bot oxc-bot mentioned this pull request Aug 10, 2024
Boshen added a commit that referenced this pull request Aug 10, 2024
## [0.24.1] - 2024-08-10

### Features

- b3c3125 linter: Overhaul unicorn/no-useless-spread (#4791) (DonIsaac)
- c519295 minifier: Add `InjectGlobalVariables` plugin
(`@rollup/plugin-inject`) (#4759) (Boshen)

### Bug Fixes

- fff9da3 ast, ast_codegen: Use `generate_derive` instead of visitable
for generating span derives. (#4747) (rzvxa)
- f5eeebd ast_macros: Raise compile error on invalid `generate_derive`
input. (#4766) (rzvxa)
- 4d0b40a napi/transform: Fix wrong isolated declarations emit (Boshen)

### Refactor

- daa0b2e ast: `oxc_ast` crate re-export AST types from other crates
(#4773) (overlookmotel)
- d4a3be8 ast_codegen: Line breaks between types in layout assertions
(#4781) (overlookmotel)
- dbb5f4c ast_codegen: Remove unnecessary imports from generated files
(#4774) (overlookmotel)
- 7ea058d ast_codegen: Replace Windows-style line breaks with Unix-style
(#4769) (overlookmotel)
- 2dea0ca ast_codegen: Consistent import order (#4761) (overlookmotel)

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