Skip to content

Commit 57a5f92

Browse files
committed
Auto merge of #66259 - JohnTitor:rollup-x9nk1e2, r=JohnTitor
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
2 parents c296b2d + 0fec5ab commit 57a5f92

File tree

20 files changed

+423
-226
lines changed

20 files changed

+423
-226
lines changed

Diff for: src/libcore/mem/maybe_uninit.rs

+2
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ impl<T> MaybeUninit<T> {
256256
/// [type]: union.MaybeUninit.html
257257
#[stable(feature = "maybe_uninit", since = "1.36.0")]
258258
#[inline(always)]
259+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "maybe_uninit_uninit")]
259260
pub const fn uninit() -> MaybeUninit<T> {
260261
MaybeUninit { uninit: () }
261262
}
@@ -339,6 +340,7 @@ impl<T> MaybeUninit<T> {
339340
/// ```
340341
#[stable(feature = "maybe_uninit", since = "1.36.0")]
341342
#[inline]
343+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "maybe_uninit_zeroed")]
342344
pub fn zeroed() -> MaybeUninit<T> {
343345
let mut u = MaybeUninit::<T>::uninit();
344346
unsafe {

Diff for: src/libcore/mem/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ pub const fn needs_drop<T>() -> bool {
468468
#[stable(feature = "rust1", since = "1.0.0")]
469469
#[allow(deprecated_in_future)]
470470
#[allow(deprecated)]
471+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "mem_zeroed")]
471472
pub unsafe fn zeroed<T>() -> T {
472473
intrinsics::panic_if_uninhabited::<T>();
473474
intrinsics::init()
@@ -496,6 +497,7 @@ pub unsafe fn zeroed<T>() -> T {
496497
#[stable(feature = "rust1", since = "1.0.0")]
497498
#[allow(deprecated_in_future)]
498499
#[allow(deprecated)]
500+
#[cfg_attr(all(not(bootstrap)), rustc_diagnostic_item = "mem_uninitialized")]
499501
pub unsafe fn uninitialized<T>() -> T {
500502
intrinsics::panic_if_uninhabited::<T>();
501503
intrinsics::uninit()

Diff for: src/librustc/ty/adjustment.rs

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub enum PointerCast {
2020
/// Go from a mut raw pointer to a const raw pointer.
2121
MutToConstPointer,
2222

23+
/// Go from `*const [T; N]` to `*const T`
24+
ArrayToPointer,
25+
2326
/// Unsize a pointer/reference value, e.g., `&[T; n]` to
2427
/// `&[T]`. Note that the source could be a thin or fat pointer.
2528
/// This will do things like convert thin pointers to fat

Diff for: src/librustc_codegen_ssa/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ pub struct ModuleCodegen<M> {
5959
pub kind: ModuleKind,
6060
}
6161

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

6566

Diff for: src/librustc_codegen_ssa/mir/rvalue.rs

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
269269
}
270270
}
271271
mir::CastKind::Pointer(PointerCast::MutToConstPointer)
272+
| mir::CastKind::Pointer(PointerCast::ArrayToPointer)
272273
| mir::CastKind::Misc => {
273274
assert!(bx.cx().is_backend_immediate(cast));
274275
let ll_t_out = bx.cx().immediate_backend_type(cast);

Diff for: src/librustc_lint/builtin.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -1903,29 +1903,23 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
19031903

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

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

1922-
if cx.match_def_path(def_id, ZEROED_PATH) {
1918+
if cx.tcx.is_diagnostic_item(sym::mem_zeroed, def_id) {
19231919
return Some(InitKind::Zeroed);
1924-
}
1925-
if cx.match_def_path(def_id, UININIT_PATH) {
1920+
} else if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, def_id) {
19261921
return Some(InitKind::Uninit);
1927-
}
1928-
if cx.match_def_path(def_id, TRANSMUTE_PATH) {
1922+
} else if cx.match_def_path(def_id, TRANSMUTE_PATH) {
19291923
if is_zero(&args[0]) {
19301924
return Some(InitKind::Zeroed);
19311925
}
@@ -1940,9 +1934,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue {
19401934
if let hir::ExprKind::Call(ref path_expr, _) = args[0].kind {
19411935
if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
19421936
let def_id = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id()?;
1943-
if cx.match_def_path(def_id, MU_ZEROED_PATH) {
1937+
1938+
if cx.tcx.is_diagnostic_item(sym::maybe_uninit_zeroed, def_id) {
19441939
return Some(InitKind::Zeroed);
1945-
} else if cx.match_def_path(def_id, MU_UNINIT_PATH) {
1940+
} else if cx.tcx.is_diagnostic_item(sym::maybe_uninit_uninit, def_id) {
19461941
return Some(InitKind::Uninit);
19471942
}
19481943
}

Diff for: src/librustc_metadata/rmeta/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,15 @@ crate fn rustc_version() -> String {
3737
/// Metadata encoding version.
3838
/// N.B., increment this if you change the format of metadata such that
3939
/// the rustc version can't be found to compare with `rustc_version()`.
40-
const METADATA_VERSION: u8 = 4;
40+
const METADATA_VERSION: u8 = 5;
4141

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

5350
/// Additional metadata for a `Lazy<T>` where `T` may not be `Sized`,
5451
/// e.g. for `Lazy<[T]>`, this is the length (count of `T` values).

Diff for: src/librustc_mir/borrow_check/nll/type_check/mod.rs

+108-54
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use rustc::traits::query::type_op::custom::CustomTypeOp;
3636
use rustc::traits::query::{Fallible, NoSolution};
3737
use rustc::traits::{self, ObligationCause, PredicateObligations};
3838
use rustc::ty::adjustment::{PointerCast};
39+
use rustc::ty::cast::CastTy;
3940
use rustc::ty::fold::TypeFoldable;
4041
use rustc::ty::subst::{Subst, SubstsRef, GenericArgKind, UserSubsts};
4142
use rustc::ty::{
@@ -2177,72 +2178,125 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21772178
ty_from,
21782179
ty_to,
21792180
terr
2180-
)
2181+
);
21812182
}
21822183
}
21832184

2184-
CastKind::Misc => {
2185-
if let ty::Ref(_, mut ty_from, _) = op.ty(body, tcx).kind {
2186-
let (mut ty_to, mutability) = if let ty::RawPtr(ty::TypeAndMut {
2187-
ty: ty_to,
2188-
mutbl,
2189-
}) = ty.kind {
2190-
(ty_to, mutbl)
2191-
} else {
2185+
CastKind::Pointer(PointerCast::ArrayToPointer) => {
2186+
let ty_from = op.ty(body, tcx);
2187+
2188+
let opt_ty_elem = match ty_from.kind {
2189+
ty::RawPtr(
2190+
ty::TypeAndMut { mutbl: hir::MutImmutable, ty: array_ty }
2191+
) => {
2192+
match array_ty.kind {
2193+
ty::Array(ty_elem, _) => Some(ty_elem),
2194+
_ => None,
2195+
}
2196+
}
2197+
_ => None,
2198+
};
2199+
2200+
let ty_elem = match opt_ty_elem {
2201+
Some(ty_elem) => ty_elem,
2202+
None => {
21922203
span_mirbug!(
21932204
self,
21942205
rvalue,
2195-
"invalid cast types {:?} -> {:?}",
2196-
op.ty(body, tcx),
2206+
"ArrayToPointer cast from unexpected type {:?}",
2207+
ty_from,
2208+
);
2209+
return;
2210+
}
2211+
};
2212+
2213+
let ty_to = match ty.kind {
2214+
ty::RawPtr(
2215+
ty::TypeAndMut { mutbl: hir::MutImmutable, ty: ty_to }
2216+
) => {
2217+
ty_to
2218+
}
2219+
_ => {
2220+
span_mirbug!(
2221+
self,
2222+
rvalue,
2223+
"ArrayToPointer cast to unexpected type {:?}",
21972224
ty,
21982225
);
21992226
return;
2200-
};
2201-
2202-
// Handle the direct cast from `&[T; N]` to `*const T` by unwrapping
2203-
// any array we find.
2204-
while let ty::Array(ty_elem_from, _) = ty_from.kind {
2205-
ty_from = ty_elem_from;
2206-
if let ty::Array(ty_elem_to, _) = ty_to.kind {
2207-
ty_to = ty_elem_to;
2208-
} else {
2209-
break;
2210-
}
22112227
}
2228+
};
22122229

2213-
if let hir::MutMutable = mutability {
2214-
if let Err(terr) = self.eq_types(
2215-
ty_from,
2216-
ty_to,
2217-
location.to_locations(),
2218-
ConstraintCategory::Cast,
2219-
) {
2220-
span_mirbug!(
2221-
self,
2222-
rvalue,
2223-
"equating {:?} with {:?} yields {:?}",
2224-
ty_from,
2225-
ty_to,
2226-
terr
2227-
)
2228-
}
2229-
} else {
2230-
if let Err(terr) = self.sub_types(
2231-
ty_from,
2232-
ty_to,
2233-
location.to_locations(),
2234-
ConstraintCategory::Cast,
2235-
) {
2236-
span_mirbug!(
2237-
self,
2238-
rvalue,
2239-
"relating {:?} with {:?} yields {:?}",
2240-
ty_from,
2241-
ty_to,
2242-
terr
2243-
)
2230+
if let Err(terr) = self.sub_types(
2231+
ty_elem,
2232+
ty_to,
2233+
location.to_locations(),
2234+
ConstraintCategory::Cast,
2235+
) {
2236+
span_mirbug!(
2237+
self,
2238+
rvalue,
2239+
"relating {:?} with {:?} yields {:?}",
2240+
ty_elem,
2241+
ty_to,
2242+
terr
2243+
)
2244+
}
2245+
}
2246+
2247+
CastKind::Misc => {
2248+
let ty_from = op.ty(body, tcx);
2249+
let cast_ty_from = CastTy::from_ty(ty_from);
2250+
let cast_ty_to = CastTy::from_ty(ty);
2251+
match (cast_ty_from, cast_ty_to) {
2252+
(Some(CastTy::RPtr(ref_tm)), Some(CastTy::Ptr(ptr_tm))) => {
2253+
if let hir::MutMutable = ptr_tm.mutbl {
2254+
if let Err(terr) = self.eq_types(
2255+
ref_tm.ty,
2256+
ptr_tm.ty,
2257+
location.to_locations(),
2258+
ConstraintCategory::Cast,
2259+
) {
2260+
span_mirbug!(
2261+
self,
2262+
rvalue,
2263+
"equating {:?} with {:?} yields {:?}",
2264+
ref_tm.ty,
2265+
ptr_tm.ty,
2266+
terr
2267+
)
2268+
}
2269+
} else {
2270+
if let Err(terr) = self.sub_types(
2271+
ref_tm.ty,
2272+
ptr_tm.ty,
2273+
location.to_locations(),
2274+
ConstraintCategory::Cast,
2275+
) {
2276+
span_mirbug!(
2277+
self,
2278+
rvalue,
2279+
"relating {:?} with {:?} yields {:?}",
2280+
ref_tm.ty,
2281+
ptr_tm.ty,
2282+
terr
2283+
)
2284+
}
22442285
}
2245-
}
2286+
},
2287+
(None, _)
2288+
| (_, None)
2289+
| (_, Some(CastTy::FnPtr))
2290+
| (Some(CastTy::Float), Some(CastTy::Ptr(_)))
2291+
| (Some(CastTy::Ptr(_)), Some(CastTy::Float))
2292+
| (Some(CastTy::FnPtr), Some(CastTy::Float)) => span_mirbug!(
2293+
self,
2294+
rvalue,
2295+
"Invalid cast {:?} -> {:?}",
2296+
ty_from,
2297+
ty,
2298+
),
2299+
_ => (),
22462300
}
22472301
}
22482302
}

Diff for: src/librustc_mir/hair/cx/expr.rs

+5
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,11 @@ fn make_mirror_unadjusted<'a, 'tcx>(
628628
let cast = if cx.tables().is_coercion_cast(source.hir_id) {
629629
// Convert the lexpr to a vexpr.
630630
ExprKind::Use { source: source.to_ref() }
631+
} else if cx.tables().expr_ty(source).is_region_ptr() {
632+
// Special cased so that we can type check that the element
633+
// type of the source matches the pointed to type of the
634+
// destination.
635+
ExprKind::Pointer { source: source.to_ref(), cast: PointerCast::ArrayToPointer }
631636
} else {
632637
// check whether this is casting an enum variant discriminant
633638
// to prevent cycles, we refer to the discriminant initializer

Diff for: src/librustc_mir/interpret/cast.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
2626
self.unsize_into(src, dest)?;
2727
}
2828

29-
Misc | Pointer(PointerCast::MutToConstPointer) => {
29+
Misc
30+
| Pointer(PointerCast::MutToConstPointer)
31+
| Pointer(PointerCast::ArrayToPointer) => {
3032
let src = self.read_immediate(src)?;
3133
let res = self.cast_immediate(src, dest.layout)?;
3234
self.write_immediate(res, dest)?;

0 commit comments

Comments
 (0)