Skip to content

fix: Detect E0804 when casting raw ptr-to-dyn adds auto traits#21699

Merged
ShoyuVanilla merged 1 commit intorust-lang:masterfrom
Albab-Hasan:fix/ptr-cast-add-auto-trait-not-detected
Feb 27, 2026
Merged

fix: Detect E0804 when casting raw ptr-to-dyn adds auto traits#21699
ShoyuVanilla merged 1 commit intorust-lang:masterfrom
Albab-Hasan:fix/ptr-cast-add-auto-trait-not-detected

Conversation

@Albab-Hasan
Copy link
Contributor

the check_ptr_ptr_cast function had an early return when the source and destination dyn trait objects shared the same principal trait def id:

if src_principal == dst_principal {
    return Ok(());
}

this bypassed all three remaining checks that the code comment explicitly listed as required: generic argument equality, projection equality and the auto-trait superset check. as a resultcasts like *mut dyn Trait as *mut (dyn Trait + Send) were silently accepted instead of emitting e0804.

the fix removes the early return. CastError::PtrPtrAddingAutoTraits and its diagnostic were already implemented they just couldnt be reached. the supertrait case (trait Trait: Send) continues to work correctly because the auto trait check already accounts for implied auto traits via supertrait elaboration.

the `check_ptr_ptr_cast` function had an early return when the source and
destination dyn trait objects shared the same principal trait def id:

    if src_principal == dst_principal {
        return Ok(());
    }

this bypassed all three remaining checks that the code comment explicitly
listed as required: generic argument equality, projection equality, and
the auto-trait superset check. as a result, casts like
`*mut dyn Trait as *mut (dyn Trait + Send)` were silently accepted
instead of emitting E0804.

the fix removes the early return. `CastError::PtrPtrAddingAutoTraits` and
its diagnostic were already implemented — they just couldn't be reached.
the supertrait case (`trait Trait: Send`) continues to work correctly
because the auto-trait check already accounts for implied auto traits via
supertrait elaboration.
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 24, 2026
@Albab-Hasan Albab-Hasan changed the title fix: detect E0804 when casting raw ptr-to-dyn adds auto traits fix: Detect E0804 when casting raw ptr-to-dyn adds auto traits Feb 24, 2026
@Albab-Hasan
Copy link
Contributor Author

@ChayimFriedman2 hey, can you check this out?

Copy link
Member

@ShoyuVanilla ShoyuVanilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, this was added deliberately to suppress the false positives, but yeah, I guess this would be good to be removed as our type inferences is mature enough by now.
Thanks!

View changes since this review

@ShoyuVanilla ShoyuVanilla added this pull request to the merge queue Feb 27, 2026
Merged via the queue into rust-lang:master with commit d3ea35e Feb 27, 2026
16 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants