Skip to content

‘reference to packed field is unaligned’ in nested field of match pattern #137250

Closed
@Sirraide

Description

@Sirraide

Consider (https://godbolt.org/z/x39GzMKz4):

#[repr(packed)]
#[derive(PartialEq)]
pub struct Packed(i32);

enum Y {
    Foo { id: Packed, },
    Bar(i32)
}

fn pred(_: &i32) -> bool {
    return true;
}

fn f(x: Y) {
    match &x {
        Y::Foo { id: Packed(4), .. } => {},
        Y::Bar(s) if pred(s) => {},
        _ => {}
    }
}

This results in an error:

error[E0793]: reference to packed field is unaligned
  --> <source>:14:11
   |
14 |     match &x {
   |           ^^
   |

I don’t really know what the problem is here, but it might be the match on the id field in the first match arm that triggers the error. Moving Packed(4) into a local variable works, but if I declare it as a const variable instead the error resurfaces. Alternatively, removing the Y::Bar match arm for some reason also makes the error go away.

This currently errors on nightly and some testing on godbolt shows that this code results in an error since Rust 1.62. Before that, we still get the same diagnostic; it’s just treated as a warning instead.

Metadata

Metadata

Labels

A-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.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