Skip to content

Conversation

@theemathas
Copy link
Contributor

@theemathas theemathas commented Nov 22, 2025

This modifies the &mut -> &mut CoerceUnsized impl so that, it can shorten the lifetime.

Note that there are already two impls that allow shortening the lifetime like this (the &mut T -> &U and the &T -> &U impls). So this change makes the impls consistent with each other.

I initially tried to also do the same to the CoerceUnsized impl for core::cell::{Ref, RefMut}. However, this can't be done because Ref and RefMut "store" the lifetime and the data in different fields, and CoerceUnsized can only coerce one field.

I don't know if there is a visible effect in stable code or not.

@theemathas theemathas added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. A-coercions Area: implicit and explicit `expr as Type` coercions F-coerce_unsized The `CoerceUnsized` trait T-types Relevant to the types team, which will review and decide on the PR/issue. labels Nov 22, 2025
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 22, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 22, 2025

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rust-log-analyzer

This comment has been minimized.

@theemathas
Copy link
Contributor Author

I don't quite understand the errors here...

@zachs18
Copy link
Contributor

zachs18 commented Nov 23, 2025

IIUC, the errors are because when implementing CoerceUnsized for ADTs, the non-coerced fields (i.e. the fields other than the pointer field) must be exactly the same type between the source and destination instantiations in the current implementation.

https://github.com/rust-lang/rust/blob/94b49fd998d6723e0a9240a7cff5f9df37b84dd8/compiler/rustc_hir_analysis/src/coherence/builtin.rs#L485-502 (the error is produced on line 557, lines 485-492 describe the check)

Since BorrowRefMut<'a> is not exactly the same type as BorrowRefMut<'b> (as per Ty::eq), this isn't allowed. The comment starting on line 552 (just before the equality check) is also probably relevant

// Ignore fields that aren't changed; it may
// be that we could get away with subtyping or
// something more accepting, but we use
// equality because we want to be able to
// perform this check without computing
// variance or constraining opaque types' hidden types.
// (This is because we may have to evaluate constraint
// expressions in the course of execution.)
// See e.g., #41936.
if a == b {
return None;
}

This modifies the &mut -> &mut CoerceUnsized impl so that, it can
shorten the lifetime.

Note that there are already two impls that allow shortening the lifetime
like this (the &mut T -> &U and the &T -> &U impls). So this change
makes the impls consistent with each other.

I initially tried to also do the same to the CoerceUnsized impl for
core::cell::{Ref, RefMut}. However, this can't be done because
Ref and RefMut "store" the lifetime and the data in different fields,
and CoerceUnsized can only coerce one field.

I don't know if there is a visible effect in stable code or not.
@theemathas theemathas force-pushed the coerce-unsized-shorten branch from 6bfef63 to cfcd9bf Compare November 28, 2025 14:25
@theemathas theemathas changed the title Allow shortening lifetimes in CoerceUnsized impls Allow shortening lifetime in CoerceUnsized for &mut Nov 28, 2025
@theemathas
Copy link
Contributor Author

I changed the PR to just edit the impl for &mut, and leave the impls for Ref and RefMut alone. I don't know if this is the right way to go though...

@scottmcm scottmcm added the I-types-nominated Nominated for discussion during a types team meeting. label Dec 6, 2025
@scottmcm
Copy link
Member

scottmcm commented Dec 6, 2025

TBH, I feel quite unqualified to review the soundness of this. Maybe someone from types feels more confident?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-coercions Area: implicit and explicit `expr as Type` coercions F-coerce_unsized The `CoerceUnsized` trait I-types-nominated Nominated for discussion during a types team meeting. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants