-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
offset_of_slice
exposes whether a custom DST has a private slice field
#138327
Comments
offset_of_slice
exposes the type of private fieldsoffset_of_slice
exposes whether a custom DST has a private slice field
I would expect both of those cases to compile, as the user is not attempting to observe anything about |
I’m sorry, I should have given more context for the issue. The entire point of the base restriction which use std::sync::atomic::*;
use std::fmt::Debug;
use std::mem::align_of_val;
#[repr(C)]
pub struct Dst<T: ?Sized> {
head: u8,
tail: T,
}
fn dynamic_offset_of_tail<T: ?Sized>(value: &Dst<T>) -> usize {
(&raw const value.tail).addr() - (&raw const *value).addr()
}
fn main() {
let dst1: &Dst<dyn Debug> = &Dst { head: 0, tail: AtomicU8::new(0) };
let dst8: &Dst<dyn Debug> = &Dst { head: 0, tail: AtomicU64::new(0) };
assert_eq!(dynamic_offset_of_tail(dst1), 1);
assert_eq!(dynamic_offset_of_tail(dst8), 8);
assert_eq!(align_of_val(dst1), 1);
assert_eq!(align_of_val(dst8), 8);
} Two values of identical type, |
Ah right, if |
I think "because tail is a private field, and therefore information about its type should not leak to places where it is not visible" might not necessarily be true (even ignoring |
Personally, I would argue it’s a flaw in the language that that is already observable, and there shouldn’t be even more occurrences of the same flaw. But I understand that’s much more debatable than revealing not-previously-revealed information. |
The following program compiles:
It does not compile if the type of
tail
is changed to adyn
type. I believe neither version should compile, becausetail
is a private field, and therefore information about its type should not leak to places where it is not visible.rustc version: 1.87.0-nightly (2025-03-09 3ea711f)
@rustbot label F-offset_of_slice C-bug
The text was updated successfully, but these errors were encountered: