Skip to content

ICE: offset of unsized field (type S) cannot be computed statically #123152

Closed
@cushionbadak

Description

Code

(reduced)

use std::mem::offset_of;

struct C<T> {
    v: T,
    w: T,
}

struct S {
    v: str,
    w: u16,
}

impl S {
    fn offs_in_c_colon() -> usize {
        offset_of!(C::<Self>, w)
    }
}

fn main() {}

(original)

Original Code

#![feature(offset_of_nested)]

use std::mem::offset_of;

struct C<T> {
    v: T,
    w: T,
}

struct S {
    v: str,
    w: u16,
}

impl S {
    fn v_offs() -> usize {
        offset_of!(Self, v)
    }
    fn v_offs_wrong_syntax() {
        offset_of!(Self, Self::v); //~ offset_of expects dot-separated field and variant names
        offset_of!(S, Self); //~ no field `Self` on type `S`
    }
    fn offs_in_c() -> usize {
        offset_of!(C<Self>, w)
    }
    fn offs_in_c_colon() -> usize {
        offset_of!(C::<Self>, w)
    }
}

mod m {
    use std::mem::offset_of;
    fn off() {
        offset_of!(self::S, v); //~ ERROR cannot find type `S` in module
        offset_of!(super::S, v);
        offset_of!(crate::S, v);
    }
    impl super::n::T {
        fn v_offs_self() -> usize {
            offset_of!(Self, v) //~ ERROR field `v` of struct `T` is private
        }
    }
}

mod n {
    pub struct T {
        v: u8,
    }
}

fn main() {
    offset_of!(self::S, v);
    offset_of!(Self, v); //~ ERROR cannot find type `Self` in this scope

    offset_of!(S, self); //~ no field `self` on type `S`
    offset_of!(S, v.self); //~ no field `self` on type `u8`
}

Note

I guess the original code is mutated from tests/ui/offset-of/offset-of-self.rs,
where the type of field v of struct S is tweaked from u8 to str.

Meta

rustc --version --verbose:

rustc 1.79.0-nightly (c9f8f3438 2024-03-27)
binary: rustc
commit-hash: c9f8f3438a8134a413aa5d4903e0196e44e37bbc
commit-date: 2024-03-27
host: x86_64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.2

Command

rustc with no other options

Error output

error[E0277]: the size for values of type `str` cannot be known at compilation time
 --> 0327/reduced_125C0267.rs:9:8
  |
9 |     v: str,
  |        ^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `str`
  = note: only the last field of a struct may have a dynamically sized type
  = help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
  |
9 |     v: &str,
  |        +
help: the `Box` type always has a statically known size and allocates its contents in the heap
  |
9 |     v: Box<str>,
  |        ++++   +

Backtrace

thread 'rustc' panicked at /rustc/c9f8f3438a8134a413aa5d4903e0196e44e37bbc/compiler/rustc_target/src/abi/mod.rs:270:13:
offset of unsized field (type S) cannot be computed statically
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: <rustc_target::abi::TyAndLayout<rustc_middle::ty::Ty>>::offset_of_subfield::<rustc_mir_transform::known_panics_lint::ConstPropagator, core::iter::adapters::copied::Copied<core::slice::iter::Iter<(rustc_target::abi::VariantIdx, rustc_target::abi::FieldIdx)>>>
   3: <rustc_mir_transform::known_panics_lint::ConstPropagator as rustc_middle::mir::visit::Visitor>::visit_assign
   4: <rustc_mir_transform::known_panics_lint::ConstPropagator as rustc_middle::mir::visit::Visitor>::visit_basic_block_data
   5: <rustc_mir_transform::known_panics_lint::ConstPropagator as rustc_middle::mir::visit::Visitor>::visit_body
   6: <rustc_mir_transform::known_panics_lint::KnownPanicsLint as rustc_mir_transform::pass_manager::MirLint>::run_lint
   7: rustc_mir_transform::pass_manager::run_passes_inner
   8: rustc_mir_transform::run_analysis_to_runtime_passes
   9: rustc_mir_transform::mir_drops_elaborated_and_const_checked
      [... omitted 1 frame ...]
  10: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  11: <rustc_interface::queries::QueryResult<&rustc_middle::ty::context::GlobalCtxt>>::enter::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}::{closure#1}::{closure#3}>
  12: rustc_interface::interface::run_compiler::<core::result::Result<(), rustc_span::ErrorGuaranteed>, rustc_driver_impl::run_compiler::{closure#0}>::{closure#0}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please attach the file at `/Volumes/T7/workspace/placeholder_rustexec/rustc-ice-2024-03-28T05_00_14-68115.txt` to your bug report

query stack during panic:
#0 [mir_drops_elaborated_and_const_checked] elaborating drops for `<impl at 0327/reduced_125C0267.rs:13:1: 13:7>::offs_in_c_colon`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.

Related Issues

I can find various offset_of related ICEs, but none of them has the same ICE location as this one.

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions