Skip to content

Commit

Permalink
Auto merge of #66259 - JohnTitor:rollup-x9nk1e2, r=JohnTitor
Browse files Browse the repository at this point in the history
Rollup of 7 pull requests

Successful merges:

 - #65719 (Refactor sync::Once)
 - #65831 (Don't cast directly from &[T; N] to *const T)
 - #66048 (Correct error in documentation for Ipv4Addr method)
 - #66058 (Correct deprecated `is_global` IPv6 documentation)
 - #66216 ([mir-opt] Handle return place in ConstProp and improve SimplifyLocals pass)
 - #66217 (invalid_value lint: use diagnostic items)
 - #66235 (rustc_metadata: don't let LLVM confuse rmeta blobs for COFF object files.)

Failed merges:

r? @ghost
  • Loading branch information
bors committed Nov 10, 2019
2 parents c296b2d + 0fec5ab commit 57a5f92
Show file tree
Hide file tree
Showing 20 changed files with 423 additions and 226 deletions.
2 changes: 2 additions & 0 deletions src/libcore/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ impl<T> MaybeUninit<T> {
/// [type]: union.MaybeUninit.html
#[stable(feature = "maybe_uninit", since = "1.36.0")]
#[inline(always)]
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "maybe_uninit_uninit")]
pub const fn uninit() -> MaybeUninit<T> {
MaybeUninit { uninit: () }
}
Expand Down Expand Up @@ -339,6 +340,7 @@ impl<T> MaybeUninit<T> {
/// ```
#[stable(feature = "maybe_uninit", since = "1.36.0")]
#[inline]
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "maybe_uninit_zeroed")]
pub fn zeroed() -> MaybeUninit<T> {
let mut u = MaybeUninit::<T>::uninit();
unsafe {
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ pub const fn needs_drop<T>() -> bool {
#[stable(feature = "rust1", since = "1.0.0")]
#[allow(deprecated_in_future)]
#[allow(deprecated)]
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "mem_zeroed")]
pub unsafe fn zeroed<T>() -> T {
intrinsics::panic_if_uninhabited::<T>();
intrinsics::init()
Expand Down Expand Up @@ -496,6 +497,7 @@ pub unsafe fn zeroed<T>() -> T {
#[stable(feature = "rust1", since = "1.0.0")]
#[allow(deprecated_in_future)]
#[allow(deprecated)]
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "mem_uninitialized")]
pub unsafe fn uninitialized<T>() -> T {
intrinsics::panic_if_uninhabited::<T>();
intrinsics::uninit()
Expand Down
3 changes: 3 additions & 0 deletions src/librustc/ty/adjustment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub enum PointerCast {
/// Go from a mut raw pointer to a const raw pointer.
MutToConstPointer,

/// Go from `*const [T; N]` to `*const T`
ArrayToPointer,

/// Unsize a pointer/reference value, e.g., `&[T; n]` to
/// `&[T]`. Note that the source could be a thin or fat pointer.
/// This will do things like convert thin pointers to fat
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_codegen_ssa/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ pub struct ModuleCodegen<M> {
pub kind: ModuleKind,
}

pub const METADATA_FILENAME: &str = "rust.metadata.bin";
// FIXME(eddyb) maybe include the crate name in this?
pub const METADATA_FILENAME: &str = "lib.rmeta";
pub const RLIB_BYTECODE_EXTENSION: &str = "bc.z";


Expand Down
1 change: 1 addition & 0 deletions src/librustc_codegen_ssa/mir/rvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
}
mir::CastKind::Pointer(PointerCast::MutToConstPointer)
| mir::CastKind::Pointer(PointerCast::ArrayToPointer)
| mir::CastKind::Misc => {
assert!(bx.cx().is_backend_immediate(cast));
let ll_t_out = bx.cx().immediate_backend_type(cast);
Expand Down
21 changes: 8 additions & 13 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1903,29 +1903,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {

/// Determine if this expression is a "dangerous initialization".
fn is_dangerous_init(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<InitKind> {
const ZEROED_PATH: &[Symbol] = &[sym::core, sym::mem, sym::zeroed];
const UININIT_PATH: &[Symbol] = &[sym::core, sym::mem, sym::uninitialized];
// `transmute` is inside an anonymous module (the `extern` block?);
// `Invalid` represents the empty string and matches that.
// FIXME(#66075): use diagnostic items. Somehow, that does not seem to work
// on intrinsics right now.
const TRANSMUTE_PATH: &[Symbol] =
&[sym::core, sym::intrinsics, kw::Invalid, sym::transmute];
const MU_ZEROED_PATH: &[Symbol] =
&[sym::core, sym::mem, sym::maybe_uninit, sym::MaybeUninit, sym::zeroed];
const MU_UNINIT_PATH: &[Symbol] =
&[sym::core, sym::mem, sym::maybe_uninit, sym::MaybeUninit, sym::uninit];

if let hir::ExprKind::Call(ref path_expr, ref args) = expr.kind {
// Find calls to `mem::{uninitialized,zeroed}` methods.
if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
let def_id = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;

if cx.match_def_path(def_id, ZEROED_PATH) {
if cx.tcx.is_diagnostic_item(sym::mem_zeroed, def_id) {
return Some(InitKind::Zeroed);
}
if cx.match_def_path(def_id, UININIT_PATH) {
} else if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, def_id) {
return Some(InitKind::Uninit);
}
if cx.match_def_path(def_id, TRANSMUTE_PATH) {
} else if cx.match_def_path(def_id, TRANSMUTE_PATH) {
if is_zero(&args[0]) {
return Some(InitKind::Zeroed);
}
Expand All @@ -1940,9 +1934,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
if let hir::ExprKind::Call(ref path_expr, _) = args[0].kind {
if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
let def_id = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
if cx.match_def_path(def_id, MU_ZEROED_PATH) {

if cx.tcx.is_diagnostic_item(sym::maybe_uninit_zeroed, def_id) {
return Some(InitKind::Zeroed);
} else if cx.match_def_path(def_id, MU_UNINIT_PATH) {
} else if cx.tcx.is_diagnostic_item(sym::maybe_uninit_uninit, def_id) {
return Some(InitKind::Uninit);
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/librustc_metadata/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,15 @@ crate fn rustc_version() -> String {
/// Metadata encoding version.
/// N.B., increment this if you change the format of metadata such that
/// the rustc version can't be found to compare with `rustc_version()`.
const METADATA_VERSION: u8 = 4;
const METADATA_VERSION: u8 = 5;

/// Metadata header which includes `METADATA_VERSION`.
/// To get older versions of rustc to ignore this metadata,
/// there are 4 zero bytes at the start, which are treated
/// as a length of 0 by old compilers.
///
/// This header is followed by the position of the `CrateRoot`,
/// which is encoded as a 32-bit big-endian unsigned integer,
/// and further followed by the rustc version string.
crate const METADATA_HEADER: &[u8; 12] =
&[0, 0, 0, 0, b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
crate const METADATA_HEADER: &[u8; 8] =
&[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];

/// Additional metadata for a `Lazy<T>` where `T` may not be `Sized`,
/// e.g. for `Lazy<[T]>`, this is the length (count of `T` values).
Expand Down
162 changes: 108 additions & 54 deletions src/librustc_mir/borrow_check/nll/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use rustc::traits::query::type_op::custom::CustomTypeOp;
use rustc::traits::query::{Fallible, NoSolution};
use rustc::traits::{self, ObligationCause, PredicateObligations};
use rustc::ty::adjustment::{PointerCast};
use rustc::ty::cast::CastTy;
use rustc::ty::fold::TypeFoldable;
use rustc::ty::subst::{Subst, SubstsRef, GenericArgKind, UserSubsts};
use rustc::ty::{
Expand Down Expand Up @@ -2177,72 +2178,125 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
ty_from,
ty_to,
terr
)
);
}
}

CastKind::Misc => {
if let ty::Ref(_, mut ty_from, _) = op.ty(body, tcx).kind {
let (mut ty_to, mutability) = if let ty::RawPtr(ty::TypeAndMut {
ty: ty_to,
mutbl,
}) = ty.kind {
(ty_to, mutbl)
} else {
CastKind::Pointer(PointerCast::ArrayToPointer) => {
let ty_from = op.ty(body, tcx);

let opt_ty_elem = match ty_from.kind {
ty::RawPtr(
ty::TypeAndMut { mutbl: hir::MutImmutable, ty: array_ty }
) => {
match array_ty.kind {
ty::Array(ty_elem, _) => Some(ty_elem),
_ => None,
}
}
_ => None,
};

let ty_elem = match opt_ty_elem {
Some(ty_elem) => ty_elem,
None => {
span_mirbug!(
self,
rvalue,
"invalid cast types {:?} -> {:?}",
op.ty(body, tcx),
"ArrayToPointer cast from unexpected type {:?}",
ty_from,
);
return;
}
};

let ty_to = match ty.kind {
ty::RawPtr(
ty::TypeAndMut { mutbl: hir::MutImmutable, ty: ty_to }
) => {
ty_to
}
_ => {
span_mirbug!(
self,
rvalue,
"ArrayToPointer cast to unexpected type {:?}",
ty,
);
return;
};

// Handle the direct cast from `&[T; N]` to `*const T` by unwrapping
// any array we find.
while let ty::Array(ty_elem_from, _) = ty_from.kind {
ty_from = ty_elem_from;
if let ty::Array(ty_elem_to, _) = ty_to.kind {
ty_to = ty_elem_to;
} else {
break;
}
}
};

if let hir::MutMutable = mutability {
if let Err(terr) = self.eq_types(
ty_from,
ty_to,
location.to_locations(),
ConstraintCategory::Cast,
) {
span_mirbug!(
self,
rvalue,
"equating {:?} with {:?} yields {:?}",
ty_from,
ty_to,
terr
)
}
} else {
if let Err(terr) = self.sub_types(
ty_from,
ty_to,
location.to_locations(),
ConstraintCategory::Cast,
) {
span_mirbug!(
self,
rvalue,
"relating {:?} with {:?} yields {:?}",
ty_from,
ty_to,
terr
)
if let Err(terr) = self.sub_types(
ty_elem,
ty_to,
location.to_locations(),
ConstraintCategory::Cast,
) {
span_mirbug!(
self,
rvalue,
"relating {:?} with {:?} yields {:?}",
ty_elem,
ty_to,
terr
)
}
}

CastKind::Misc => {
let ty_from = op.ty(body, tcx);
let cast_ty_from = CastTy::from_ty(ty_from);
let cast_ty_to = CastTy::from_ty(ty);
match (cast_ty_from, cast_ty_to) {
(Some(CastTy::RPtr(ref_tm)), Some(CastTy::Ptr(ptr_tm))) => {
if let hir::MutMutable = ptr_tm.mutbl {
if let Err(terr) = self.eq_types(
ref_tm.ty,
ptr_tm.ty,
location.to_locations(),
ConstraintCategory::Cast,
) {
span_mirbug!(
self,
rvalue,
"equating {:?} with {:?} yields {:?}",
ref_tm.ty,
ptr_tm.ty,
terr
)
}
} else {
if let Err(terr) = self.sub_types(
ref_tm.ty,
ptr_tm.ty,
location.to_locations(),
ConstraintCategory::Cast,
) {
span_mirbug!(
self,
rvalue,
"relating {:?} with {:?} yields {:?}",
ref_tm.ty,
ptr_tm.ty,
terr
)
}
}
}
},
(None, _)
| (_, None)
| (_, Some(CastTy::FnPtr))
| (Some(CastTy::Float), Some(CastTy::Ptr(_)))
| (Some(CastTy::Ptr(_)), Some(CastTy::Float))
| (Some(CastTy::FnPtr), Some(CastTy::Float)) => span_mirbug!(
self,
rvalue,
"Invalid cast {:?} -> {:?}",
ty_from,
ty,
),
_ => (),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_mir/hair/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,11 @@ fn make_mirror_unadjusted<'a, 'tcx>(
let cast = if cx.tables().is_coercion_cast(source.hir_id) {
// Convert the lexpr to a vexpr.
ExprKind::Use { source: source.to_ref() }
} else if cx.tables().expr_ty(source).is_region_ptr() {
// Special cased so that we can type check that the element
// type of the source matches the pointed to type of the
// destination.
ExprKind::Pointer { source: source.to_ref(), cast: PointerCast::ArrayToPointer }
} else {
// check whether this is casting an enum variant discriminant
// to prevent cycles, we refer to the discriminant initializer
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_mir/interpret/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
self.unsize_into(src, dest)?;
}

Misc | Pointer(PointerCast::MutToConstPointer) => {
Misc
| Pointer(PointerCast::MutToConstPointer)
| Pointer(PointerCast::ArrayToPointer) => {
let src = self.read_immediate(src)?;
let res = self.cast_immediate(src, dest.layout)?;
self.write_immediate(res, dest)?;
Expand Down
Loading

0 comments on commit 57a5f92

Please sign in to comment.