-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Forbid casting to/from a pointer of unknown kind #45735
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
src/librustc_typeck/check/cast.rs
Outdated
@@ -105,6 +105,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |||
// We should really try to normalize here. | |||
ty::TyProjection(ref pi) => PointerKind::OfProjection(pi), | |||
ty::TyParam(ref p) => PointerKind::OfParam(p), | |||
// Insufficient type information; this will be caught later, so just report that this | |||
// is a thin pointer. | |||
ty::TyInfer(_) => PointerKind::Thin, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this case occur only by lack of type information? I'm not pretty sure.
It's not a good idea to assume things when encountering a use std::fmt;
fn main() {
let x: *const _ = 0 as *const _; // we don't want to allow this if...
let y: Option<*const fmt::Debug> = Some(x) as _;
// ^ later, we have x = *const fmt::Debug
}
fn not_ok() {
let x = 0 as *const i32 as *const _ as *mut _; //~ ERROR ?
} Instead, you should make |
Added a new error code for casting with unknown pointer kind. r? @arielb1 |
src/librustc_typeck/check/cast.rs
Outdated
@@ -457,14 +478,20 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> { | |||
debug!("check_ptr_ptr_cast m_expr={:?} m_cast={:?}", m_expr, m_cast); | |||
// ptr-ptr cast. vtables must match. | |||
|
|||
let (expr_kind, cast_kind) = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This forbids casting an *const _
to an *const u32
, please move the check after the following if to avoid breaking user code.
Updated, but I couldn't trigger the |
Can't you have |
@bors r+ |
📌 Commit 99ada04 has been approved by |
It just compiles. I'm not sure when inference occurs, but maybe |
Forbid casting to/from a pointer of unknown kind Fixes #45730. Before, it ICE'd when `pointer_kind` encountered `TyInfer`.
☀️ Test successful - status-appveyor, status-travis |
Fixes #45730.
Before, it ICE'd when
pointer_kind
encounteredTyInfer
.