Skip to content

Commit 5b86323

Browse files
Auto merge of #142973 - compiler-errors:quick-coerce-2, r=<try>
[perf] Fast path for coercions of TY == TY
2 parents 36b2163 + 5d1b220 commit 5b86323

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,30 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
206206
return self.coerce_from_inference_variable(a, b);
207207
}
208208

209+
// No coercion needed; types are identical. The only exception is when we have
210+
// a mutable reference, which needs a pair of reborrow adjustments inserted.
211+
if a == b {
212+
if let ty::Ref(_, pointee, ty::Mutability::Mut) = *a.kind() {
213+
return Ok(InferOk {
214+
value: (
215+
vec![
216+
Adjustment { kind: Adjust::Deref(None), target: pointee },
217+
Adjustment {
218+
kind: Adjust::Borrow(AutoBorrow::Ref(AutoBorrowMutability::Mut {
219+
allow_two_phase_borrow: AllowTwoPhase::No,
220+
})),
221+
target: a,
222+
},
223+
],
224+
a,
225+
),
226+
obligations: Default::default(),
227+
});
228+
} else {
229+
return Ok(InferOk { value: (vec![], a), obligations: Default::default() });
230+
}
231+
}
232+
209233
// Consider coercing the subtype to a DST
210234
//
211235
// NOTE: this is wrapped in a `commit_if_ok` because it creates

tests/ui/nll/issue-52663-trait-object.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ LL | let tmp0 = 3;
66
LL | let tmp1 = &tmp0;
77
| ^^^^^ borrowed value does not live long enough
88
LL | Box::new(tmp1) as Box<dyn Foo + '_>
9-
| ----------------------------------- borrow later captured here by trait object
109
LL | };
1110
| - `tmp0` dropped here while still borrowed
1211

tests/ui/span/regions-close-over-type-parameter-2.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ LL | let tmp0 = 3;
66
LL | let tmp1 = &tmp0;
77
| ^^^^^ borrowed value does not live long enough
88
LL | repeater3(tmp1)
9-
| --------------- borrow later captured here by trait object
109
LL | };
1110
| - `tmp0` dropped here while still borrowed
1211

0 commit comments

Comments
 (0)