Skip to content

Conversation

@joshlf
Copy link
Member

@joshlf joshlf commented Sep 20, 2024

Closes #1708

@joshlf joshlf requested a review from jswrenn September 20, 2024 23:29
@joshlf joshlf enabled auto-merge September 20, 2024 23:29
@joshlf
Copy link
Member Author

joshlf commented Sep 20, 2024

@jswrenn if you're on board with this, I think this can supersede #1710.

@joshlf joshlf force-pushed the into-bytes-improve-unsized-error branch from e38839b to b42cfb6 Compare September 20, 2024 23:54
@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.79%. Comparing base (7eb4026) to head (b42cfb6).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1713   +/-   ##
=======================================
  Coverage   88.79%   88.79%           
=======================================
  Files          16       16           
  Lines        5843     5846    +3     
=======================================
+ Hits         5188     5191    +3     
  Misses        655      655           

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

#[inline(always)]
#[must_use]
#[allow(clippy::needless_maybe_sized)]
pub const fn size_of<T: Sized + ?core::marker::Sized>() -> usize {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is such a cursed bound.

Copy link

Choose a reason for hiding this comment

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

I just stumbled across this randomly: why is that bound there? Wouldn't any T automatically also be core::mem::Sized due to the trait bound of __size_of::Sized? Thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

From a correctness perspective, you're right. However, without ?core::marker::Sized, when Rust encounters a type that is unsized that is passed to this size_of, it emits a Sized error first and bails, so it never gets to the point of discovering that the Sized bound (our bespoke Sized trait) isn't satisfied, and so it never emits our custom diagnostic. Adding ?core::marker::Sized "uncovers" our unsatisfied trait bound so that the desired diagnostic actually gets emitted.

It is, as all things in proc macro land, mildly cursed 😛

Copy link

Choose a reason for hiding this comment

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

@joshlf thanks for the explanation!

Copy link
Member Author

Choose a reason for hiding this comment

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

np!

--> tests/ui-nightly/struct.rs:129:8
|
129 | b: [u8],
| ^^^^ `IntoBytes` needs all field types to be `Sized` in order to determine whether there is inter-field padding
Copy link
Collaborator

Choose a reason for hiding this comment

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

NICE!

@joshlf joshlf added this pull request to the merge queue Sep 21, 2024
Merged via the queue into main with commit 553996e Sep 21, 2024
86 checks passed
@joshlf joshlf deleted the into-bytes-improve-unsized-error branch September 21, 2024 14: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.

Improve ergonomics of IntoBytes on unsized types

4 participants