Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
*entry.get_mut() = adj;
}

(
&mut [.., Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. }],
&[Adjustment { kind: Adjust::Deref(_), .. }, ..],
) => {
// A reborrow has no effect before a dereference, so we can safely merge adjustments.
// A more general version of the above case.
let entry_mut = entry.get_mut();
entry_mut.pop();
entry_mut.extend_from_slice(&adj[1..]);
}

_ => {
// FIXME: currently we never try to compose autoderefs
// and ReifyFnPointer/UnsafeFnPointer, but we could.
// FIXME: currently we never try to compose ReifyFnPointer/UnsafeFnPointer, but we could.
self.dcx().span_delayed_bug(
expr.span,
format!(
Expand Down
32 changes: 32 additions & 0 deletions tests/ui/coercion/compose_autoderefs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//@ edition:2021
//@ check-pass

use core::ops::Deref;

struct A;
struct B;
struct C;

impl Deref for C {
type Target = B;
fn deref(&self) -> &Self::Target {
&B
}
}

impl Deref for B {
type Target = A;
fn deref(&self) -> &Self::Target {
&A
}
}

fn f(v: u8) {
let _ = match v {
0 => &C,
1 => &B,
_ => &A,
};
}

fn main() {}
Loading