diff options
| author | Giacomo Stevanato <giaco.stevanato@gmail.com> | 2021-04-15 23:17:15 +0200 |
|---|---|---|
| committer | Giacomo Stevanato <giaco.stevanato@gmail.com> | 2021-04-15 23:49:42 +0200 |
| commit | 2899b58832fec1fd3570e65e231920d2a2e937ae (patch) | |
| tree | 3f73ed854439f520b92dc16bfdfe2ba82ed04b18 | |
| parent | 60158f4a7cf3e3063df6127d3f0d206921d285b0 (diff) | |
| download | rust-2899b58832fec1fd3570e65e231920d2a2e937ae.tar.gz rust-2899b58832fec1fd3570e65e231920d2a2e937ae.zip | |
Add suggestion to borrow when casting T to *const/mut T
| -rw-r--r-- | compiler/rustc_typeck/src/check/cast.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/compiler/rustc_typeck/src/check/cast.rs b/compiler/rustc_typeck/src/check/cast.rs index 16c344e8e2b..b760a54f08c 100644 --- a/compiler/rustc_typeck/src/check/cast.rs +++ b/compiler/rustc_typeck/src/check/cast.rs @@ -359,6 +359,21 @@ impl<'a, 'tcx> CastCheck<'tcx> { { sugg = Some(format!("&{}", mutbl.prefix_str())); } + } else if let ty::RawPtr(TypeAndMut { mutbl, .. }) = *self.cast_ty.kind() { + if fcx + .try_coerce( + self.expr, + fcx.tcx.mk_ref( + &ty::RegionKind::ReErased, + TypeAndMut { ty: self.expr_ty, mutbl }, + ), + self.cast_ty, + AllowTwoPhase::No, + ) + .is_ok() + { + sugg = Some(format!("&{}", mutbl.prefix_str())); + } } if let Some(sugg) = sugg { err.span_label(self.span, "invalid cast"); |
