Skip to content

Suggest to borrow after failing to cast from T to *const/mut T #84228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2021
Merged
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
15 changes: 15 additions & 0 deletions compiler/rustc_typeck/src/check/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/cast/issue-84213.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run-rustfix

struct Something {
pub field: u32,
}

fn main() {
let mut something = Something { field: 1337 };

let _pointer_to_something = &something as *const Something;
//~^ ERROR: non-primitive cast

let _mut_pointer_to_something = &mut something as *mut Something;
//~^ ERROR: non-primitive cast
}
15 changes: 15 additions & 0 deletions src/test/ui/cast/issue-84213.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run-rustfix

struct Something {
pub field: u32,
}

fn main() {
let mut something = Something { field: 1337 };

let _pointer_to_something = something as *const Something;
//~^ ERROR: non-primitive cast

let _mut_pointer_to_something = something as *mut Something;
//~^ ERROR: non-primitive cast
}
25 changes: 25 additions & 0 deletions src/test/ui/cast/issue-84213.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0605]: non-primitive cast: `Something` as `*const Something`
--> $DIR/issue-84213.rs:10:33
|
LL | let _pointer_to_something = something as *const Something;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
|
help: borrow the value for the cast to be valid
|
LL | let _pointer_to_something = &something as *const Something;
| ^

error[E0605]: non-primitive cast: `Something` as `*mut Something`
--> $DIR/issue-84213.rs:13:37
|
LL | let _mut_pointer_to_something = something as *mut Something;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
|
help: borrow the value for the cast to be valid
|
LL | let _mut_pointer_to_something = &mut something as *mut Something;
| ^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0605`.