Skip to content

Ensure ErasedData only implements appropriate auto traits#154229

Open
Zoxc wants to merge 1 commit intorust-lang:mainfrom
Zoxc:erase-auto-traits
Open

Ensure ErasedData only implements appropriate auto traits#154229
Zoxc wants to merge 1 commit intorust-lang:mainfrom
Zoxc:erase-auto-traits

Conversation

@Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Mar 22, 2026

This uses an external type to prevent auto traits to be inferred on ErasedData. That inference is incorrect since it may not store the type it declares.

This also implements DynSend and DynSync on ErasedData which are checked by bounds on erase_val.

Some diagnostics bounds were missing DynSync, which is also added here.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 22, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 22, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 12 candidates

@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 22, 2026

cc @petrochenkov @Zalathar

@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 22, 2026

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Mar 22, 2026
Ensure `ErasedData` only implements appropriate auto traits
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 22, 2026
Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

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

Seems ok to me. I'm not super familiar with the type erasing and DynSend/DynSync stuff so I will wait a bit in case others have anything to add.

cc @zetanumbers

View changes since this review

#[inline(always)]
#[define_opaque(Erased)]
pub fn erase_val<T: Erasable>(value: T) -> Erased<T> {
pub fn erase_val<T: Erasable + DynSend + DynSync>(value: T) -> Erased<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a comment here referring back to the unsafe DynSend/DynSync impls for ErasedData.

data: MaybeUninit<Storage>,
/// `Storage` is an erased type, so we use an external type here to opt-out of auto traits
/// as those would be incorrect.
hide: PhantomData<Hide>,
Copy link
Contributor

Choose a reason for hiding this comment

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

A verb is strange for a field name/type name. hidden/Hidden would be better than hide/Hide, I think.

Copy link
Member

Choose a reason for hiding this comment

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

I find the name puzzling either way; I don't see what is being “hidden” here.

If the goal is to not implement auto-traits, having something like _no_auto_traits: PhantomData<NoAutoTraits> seems a lot more straightforward to me.

@rust-bors
Copy link
Contributor

rust-bors bot commented Mar 23, 2026

☀️ Try build successful (CI)
Build commit: 87b89de (87b89de55445312cd3c1039b604a707c97613829, parent: 562dee4820c458d823175268e41601d4c060588a)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (87b89de): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.3%] 3
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -6.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-6.3% [-6.3%, -6.3%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -6.3% [-6.3%, -6.3%] 1

Cycles

Results (secondary -2.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.6% [-2.8%, -2.3%] 3
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 485.484s -> 484.192s (-0.27%)
Artifact size: 394.88 MiB -> 396.95 MiB (0.53%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Mar 23, 2026
Comment on lines +38 to +40
// SAFETY: The bounds on `erase_val` ensure the types we erase are `DynSync` and `DynSend`
unsafe impl<Storage: Copy> DynSync for ErasedData<Storage> {}
unsafe impl<Storage: Copy> DynSend for ErasedData<Storage> {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Storage should be DynSync and DynSend too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't store Storage or create any instance of that type.

@Zoxc Zoxc force-pushed the erase-auto-traits branch from b0ccfe9 to 951f715 Compare March 23, 2026 20:36
@nnethercote
Copy link
Contributor

I just noticed that this overlaps with the experiment in #151715, interesting.

@Zalathar
Copy link
Member

I just noticed that this overlaps with the experiment in #151715, interesting.

Yeah, experimentally removing value erasure exposed all the places where we accidentally erase things that aren’t actually DynSend/DynSync, though in practice it’s all just dyn trait types that can be updated to include the appropriate bounds.

@nnethercote
Copy link
Contributor

I see, this PR is basically fixing the FIXME comment on erase_val.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants