Skip to content

Const eval does alignment checks #61952

Closed
@oli-obk

Description

@oli-obk

The const eval engine takes quite a few steps to ensure that it bails out of evaluation if any steps would cause unaligned reads. E.g.

#[repr(packed)]
struct Foo {
    a: i32,
}

static A: Foo = Foo { a: 42 };
static B: i32 = {
    let x: &i32 = unsafe { &A.a };
    *x
};

will fail to compile with

error[E0080]: could not evaluate static initializer
 --> src/main.rs:9:5
  |
9 |     *x
  |     ^^ tried to access memory with alignment 1, but alignment 4 is required

The question is, do we need to be this restrictive during const eval. The checks are everywhere, so we may even see perf gains from removing them, but the main gain would be that we can remove the additional code that just exists to ensure we fiddle the right alignment through the pattern-matching-on-constants logic (ConstValue::ByRef's Align field is unused for anything else).

I think there's little use in keeping this restriction in const eval, since it only exists in miri because some hardware platforms actually do UB on unaligned reads, while other platforms are simply slower on unaligned reads.

cc @RalfJung

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language 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