rust-lang / rust-lang/rust

`CoerceShared` unsoundness due to not checking `repr`.

Open
#156,435 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-repr C-bug F-reborrow I-unsound T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The following code causes a segmentation fault at run time. The compiler seems to be incorrectly assuming that MyMut and MyRef have the same size.

#![feature(reborrow)]

use std::marker::{Reborrow, CoerceShared};

#[repr(align(4096))]
struct MyMut<'a>(&'a i32);

impl Reborrow for MyMut<'_> {}

#[derive(Clone, Copy)]
#[repr(align(65536))]
struct MyRef<'a>(&'a i32);

impl<'a> CoerceShared<MyRef<'a>> for MyMut<'a> {}

fn main() {
    foo(MyMut(&1));
}

fn foo(x: MyMut<'_>) {
    bar(x);
}

fn bar(x: MyRef<'_>) {}

cc @aapoalas

Meta

Reproducible on the playground with version 1.97.0-nightly (2026-05-10 4b0c9d76ae7d387229ca)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided minimized example with the stated nightly compiler and trace how the compiler handles the CoerceShared implementation and the two differently aligned types. Identify the missing repr compatibility check, then add a regression test showing that the unsound coercion no longer compiles or runs unsafely.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.