Skip to content

Conversation

@Dunqing
Copy link
Member

@Dunqing Dunqing commented Jul 10, 2025

Added some comments to describe CSSMinifier, and clarify why quasis.set_len(expressions.len() + 1); is safe.

Also, there is a assert in codegen to ensure that quasis is always one greater than expressions

debug_assert_eq!(self.quasis.len(), self.expressions.len() + 1);

In addition, I found that we can use truncate() instead of set_len. Both are the same, but truncate is a safe function.

@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-docs Category - Documentation. Related to user-facing or internal documentation labels Jul 10, 2025
Copy link
Member Author

Dunqing commented Jul 10, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed-hq
Copy link

codspeed-hq bot commented Jul 10, 2025

CodSpeed Instrumentation Performance Report

Merging #12197 will not alter performance

Comparing 07-10-docs_transformer_styled-components_add_comments_about_cssminifier (86eb108) with main (daf6087)

Summary

✅ 34 untouched benchmarks

@Dunqing Dunqing marked this pull request as ready for review July 10, 2025 15:45
@Dunqing Dunqing requested a review from overlookmotel as a code owner July 10, 2025 15:45
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Jul 10, 2025
@overlookmotel overlookmotel removed the 0-merge Merge with Graphite Merge Queue label Jul 10, 2025
@graphite-app
Copy link
Contributor

graphite-app bot commented Jul 10, 2025

Merge activity

  • Jul 10, 4:27 PM UTC: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Jul 10, 5:03 PM UTC: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Jul 10, 5:03 PM UTC: overlookmotel added this pull request to the Graphite merge queue.
  • Jul 10, 5:08 PM UTC: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Jul 10, 5:09 PM UTC: The merge label '0-merge' was removed. This PR will no longer be merged by the Graphite merge queue
  • Jul 10, 5:12 PM UTC: Merged by the Graphite merge queue.

Copy link
Member

@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.

Apart from the typo Graphite's spotted (it finally got something right), all looks good. Thank you for documenting this.

Comment on lines +415 to +421
// SAFETY:
// This is safe because template literal has ensured that `quasis` always
// has one more element than `expressions`, and the `CSSMinifier` guarantees that
Copy link
Member

@overlookmotel overlookmotel Jul 10, 2025

Choose a reason for hiding this comment

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

What makes it true that "template literal has ensured that quasis always has one more element than expressions"? How is that ensured?

If in some other transform, I do expressions.push(...) without corresponding quasis.push(...) by accident, this invariant is broken, and quasis.set_len(expressions.len() + 1) is UB. Safety invariants should be local and provable. "SAFETY: There are no bugs in the rest of the codebase" isn't very convincing!

Also, there is a assert in codegen to ensure that quasis is always one greater than expressions

debug_assert_eq!(self.quasis.len(), self.expressions.len() + 1);

  1. It's debug_assert! not assert!. That's no good as a safety guarantee.
  2. It's in codegen. Even if it was assert!, it's too late. We's already invoke UB before we get to that.

In addition, I found that we can use truncate() instead of set_len. Both are the same, but truncate is a safe function.

truncate would be fine - it might panic if some other code has broken the rule, but won't cause UB.

Or we could add an assertion at top of the function assert!(quasis.len() == expressions.len() + 1), and then use unsafe set_len.

But... this PR is mainly about adding comments. Let's leave it as is here, and address it in a separate PR.

Copy link
Member

Choose a reason for hiding this comment

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

Personally, I'd quite like to make this a type system invariant by changing TemplateLiteral to:

pub struct TemplateLiteral<'a> {
    pub span: Span,
    pub lead: Vec<'a, TemplateElementAndExpression<'a>>,
    pub tail: TemplateElement<'a>,
}

pub struct TemplateElementAndExpression<'a> {
    quasi: TemplateElement<'a>,
    expression: Expression<'a>,
}

Then we can stop worrying about this! Rust will prevent us making mistakes.

Copy link
Member

Choose a reason for hiding this comment

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

@Dunqing Ping just to make sure you see this. I had to mark it as "resolved" to merge the PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

The new type looks good, the current one is probably to align with the ESTree. I will add that assertion later.

Copy link
Member

Choose a reason for hiding this comment

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

@Dunqing Dunqing force-pushed the 07-10-docs_transformer_styled-components_add_comments_about_cssminifier branch 2 times, most recently from b8b68dd to e22c81f Compare July 10, 2025 16:50
@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Jul 10, 2025
…#12197)

Added some comments to describe `CSSMinifier`, and clarify why `quasis.set_len(expressions.len() + 1);` is safe.

Also, there is a `assert` in codegen to ensure that `quasis` is always one greater than `expressions`

https://github.com/oxc-project/oxc/blob/c003ebad60970d27aae48b488ff361ef2b612379/crates/oxc_codegen/src/gen.rs#L2091-L2092

In addition, I found that we can use `truncate()` instead of `set_len`. Both are the same, but `truncate` is a safe function.
@graphite-app graphite-app bot force-pushed the 07-10-docs_transformer_styled-components_add_comments_about_cssminifier branch from e22c81f to 86eb108 Compare July 10, 2025 17:04
@graphite-app graphite-app bot added 0-merge Merge with Graphite Merge Queue and removed 0-merge Merge with Graphite Merge Queue labels Jul 10, 2025
@graphite-app graphite-app bot merged commit 86eb108 into main Jul 10, 2025
25 checks passed
@graphite-app graphite-app bot deleted the 07-10-docs_transformer_styled-components_add_comments_about_cssminifier branch July 10, 2025 17:12
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Jul 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler C-docs Category - Documentation. Related to user-facing or internal documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants