Skip to content

offset_of! accepts unsized tuple field #112186

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

Closed
tmiasko opened this issue Jun 1, 2023 · 3 comments · Fixed by #112193
Closed

offset_of! accepts unsized tuple field #112186

tmiasko opened this issue Jun 1, 2023 · 3 comments · Fixed by #112193
Labels
C-bug Category: This is a bug. F-offset_of `#![feature(offset_of)]`

Comments

@tmiasko
Copy link
Contributor

tmiasko commented Jun 1, 2023

I tried this code:

#![feature(offset_of)]
use std::fmt::Debug;
fn main() {
    core::mem::offset_of!((u8, dyn Debug), 1);
}

I expected the compilation to fail since the offset cannot be statically determined. Instead, the compilation succeeds.

@tmiasko tmiasko added C-bug Category: This is a bug. F-offset_of `#![feature(offset_of)]` labels Jun 1, 2023
@scottmcm
Copy link
Member

scottmcm commented Jun 1, 2023

cc @clubby789 #112069

@clubby789
Copy link
Contributor

Technically this was possible before as there were no Sized checks on the tuple case, but it no longer ICEs when codegenning the value.
Should

struct S<T: ?Sized> {
    a: u64,
    b: T,
}
fn main() {
    let _a = core::mem::offset_of!(S<dyn std::fmt::Debug>, a);
}

from the linked issue be allowed - is this a problem specific to tuples?

@est31
Copy link
Member

est31 commented Jun 2, 2023

is this a problem specific to tuples?

I'd say so, yes. The example you gave fails to compile if you try to get the offset of the b field.

I'd say there is a missing call to require_type_is_sized in the match in compiler/rustc_hir_typeck/src/expr.rs. The arm for ADTs has it, the one for tuples doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. F-offset_of `#![feature(offset_of)]`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants