Skip to content

Rust JSG: check the fields of a jsg_traced struct for GC references - #8465

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-14-rjsg-traced-fields
Nov 17, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-14-rjsg-traced-fields

Conversation

@littledivy

Copy link
Copy Markdown
Member

jsg_traced!(Foo) gives a concrete type a no-op trace and HoldsNoGcReferences without looking at its fields. If the memory cache's Fill gained a TracedGlobal field, jsg_traced!(Fill, ...) would still compile and claim no GC references. The handle would never be visited, and IoContext::add_object would accept it, which is the leak #8401 closed for the generic form.

In this patch:

  • A new #[jsg_traced] attribute implements the same no-op Traced and HoldsNoGcReferences for a struct, but bounds each field's type by HoldsNoGcReferences. A jsg::Rc or TracedGlobal field fails to compile at the field. Unlike #[jsg_struct], the bounds are not higher-ranked, since a trace that visits nothing must not quietly drop the marker instead.
  • Fill, SharedFill, NoData and two io-context test types move onto it. jsg_traced!(Foo) stays for types whose fields can't implement the marker because they come from outside JSG (FallbackPermit, SpanBuilder, memory_cache::Binding), and its docs now say the claim is unchecked.
  • A new compile test checks that a TracedGlobal or jsg::Rc field is rejected, and that nested plain data still compiles. Without the bounds it fails because the code compiles. The change is compile-time only.

@littledivy
littledivy requested review from a team as code owners November 14, 2026 05:16

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of d773d46a.

This review, of ade244a3

✅ Approved — This compile-time-only change tightens GC-reference checking for traced structs; no issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns. The change strengthens a compile-time guarantee that a no-op trace can't hide a GC reference.


📖 AGENTS.md: ✅ LGTM

src/rust/AGENTS.md documents #[jsg_traced] and when to use the unchecked jsg::jsg_traced! instead.


📋 Code Quality: ✅ LGTM

The new #[jsg_traced] expansion is correct: it bounds every field type by jsg::HoldsNoGcReferences, keeps existing where-clause predicates, and handles unit, tuple and named-field structs. Every migrated type (Fill, SharedFill, NoData, Reaction, DropFlag) holds only fields that satisfy the marker, and jsg_macros is already a proc-macro dependency of every crate that now uses the attribute.


⚡ Performance: ✅ LGTM

Compile-time-only change; no runtime performance impact.


📝 Documentation: ✅ LGTM

The macro README and the jsg_traced! docs describe the new checked attribute and say the macro form is unchecked.


Reviewed commit: ade244a3

@littledivy
littledivy force-pushed the littledivy/2026-11-14-rjsg-traced-fields branch from ade244a to d773d46 Compare November 16, 2026 15:33

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 20dad059.

This review, of d773d46a

✅ Approved — This change is compile-time only. It makes the #[jsg_traced] attribute check every field of a traced struct for GC references, and no issues were found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns. The change only adds compile-time checks, and they are stricter than before: a no-op trace can no longer hide a GC reference in a struct's fields.


📖 AGENTS.md: ✅ LGTM

src/rust/AGENTS.md documents #[jsg_traced] and when to use the unchecked jsg::jsg_traced! instead.


📋 Code Quality: ✅ LGTM

The #[jsg_traced] expansion requires every field type to implement jsg::HoldsNoGcReferences. It keeps the struct's existing where-clause predicates and works for unit, tuple and named-field structs. Every type moved onto the attribute holds only fields that implement the marker.


⚡ Performance: ✅ LGTM

Compile-time-only change with no runtime performance impact.


📝 Documentation: ✅ LGTM

The macro README and the jsg_traced! docs describe the new checked attribute, and they say the macro form does not check fields.


Reviewed commit: d773d46a

@guybedford guybedford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM.

Comment thread src/rust/jsg/macros.rs Resolved
Comment thread src/rust/jsg-macros/README.md Resolved
`jsg_traced!(Foo)` gives a concrete type a no-op `trace` and `HoldsNoGcReferences` without looking at its fields. If the memory cache's `Fill` gained a `TracedGlobal` field, `jsg_traced!(Fill, ...)` would still compile and claim no GC references. The handle would never be visited, and `IoContext::add_object` would accept it, which is the leak #8401 closed for the generic form.

In this patch:

- A new `#[jsg_traced]` attribute implements the same no-op `Traced` and `HoldsNoGcReferences` for a struct, but bounds each field's type by `HoldsNoGcReferences`. A `jsg::Rc` or `TracedGlobal` field fails to compile at the field. Unlike `#[jsg_struct]`, the bounds are not higher-ranked, since a trace that visits nothing must not quietly drop the marker instead.
- `Fill`, `SharedFill`, `NoData`, io-context's `IoOwnedSpan` and two io-context test types move onto it. `jsg_traced!(Foo)` stays for types whose fields can't implement the marker because they come from outside JSG (`FallbackPermit`, `SpanBuilder`, `memory_cache::Binding`), and its docs now say the claim is unchecked.
- A new compile test checks that a `TracedGlobal` or `jsg::Rc` field is rejected, and that nested plain data still compiles. Without the bounds it fails because the code compiles. The change is compile-time only.
@littledivy
littledivy force-pushed the littledivy/2026-11-14-rjsg-traced-fields branch from d773d46 to 20dad05 Compare November 17, 2026 03:27

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — This change only affects compile time and makes the checks stricter. No issues were found, and the doc and IoOwnedSpan follow-ups from the earlier discussion are done.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

This only adds compile-time checks, and they are stricter than before: a struct whose trace visits nothing can no longer hide a GC reference in its fields.


📖 AGENTS.md: ✅ LGTM

src/rust/AGENTS.md documents the checked attribute and when to use the unchecked macro instead.


📋 Code Quality: ✅ LGTM

IoOwnedSpan on #[jsg_traced] meets the field bound: IoOwned<T> gets the marker for T: HoldsNoGcReferences + Send, and ThreadBoundSpan has both. The new proc_macro_deps entry lets the attribute resolve in the io-context crate.


⚡ Performance: ✅ LGTM

This change only affects compile time and adds no runtime cost.


📝 Documentation: ✅ LGTM

The review checklist and the JSG README now recommend #[jsg_traced] and describe jsg::jsg_traced! as the unchecked fallback.


Reviewed commit: 20dad059

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 66.42%. Comparing base (e2ab064) to head (20dad05).
⚠️ Report is 20 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8465      +/-   ##
==========================================
- Coverage   66.53%   66.42%   -0.11%     
==========================================
  Files         783      794      +11     
  Lines      201160   202837    +1677     
  Branches    31480    31503      +23     
==========================================
+ Hits       133841   134741     +900     
- Misses      48875    49644     +769     
- Partials    18444    18452       +8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@littledivy
littledivy merged commit 2061be6 into main Nov 17, 2026
36 of 37 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-14-rjsg-traced-fields branch November 17, 2026 04:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants