Skip to content

Commit 2899b58

Browse files
committed
Add suggestion to borrow when casting T to *const/mut T
1 parent 60158f4 commit 2899b58

File tree

1 file changed

+15
-0
lines changed
  • compiler/rustc_typeck/src/check

1 file changed

+15
-0
lines changed

compiler/rustc_typeck/src/check/cast.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,21 @@ impl<'a, 'tcx> CastCheck<'tcx> {
359359
{
360360
sugg = Some(format!("&{}", mutbl.prefix_str()));
361361
}
362+
} else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind() {
363+
if fcx
364+
.try_coerce(
365+
self.expr,
366+
fcx.tcx.mk_ref(
367+
&ty::RegionKind::ReErased,
368+
TypeAndMut { ty: self.expr_ty, mutbl },
369+
),
370+
self.cast_ty,
371+
AllowTwoPhase::No,
372+
)
373+
.is_ok()
374+
{
375+
sugg = Some(format!("&{}", mutbl.prefix_str()));
376+
}
362377
}
363378
if let Some(sugg) = sugg {
364379
err.span_label(self.span, "invalid cast");

0 commit comments

Comments
 (0)