Skip to content

Commit 7fe022f

Browse files
committed
Auto merge of rust-lang#99687 - RalfJung:rollup-bojacrc, r=RalfJung
Rollup of 4 pull requests Successful merges: - rust-lang#99644 (remove some provenance-related machine hooks that Miri no longer needs) - rust-lang#99657 (Docs - remove unnecessary `mut` that gives a warning) - rust-lang#99672 (Remove Clean trait implementation for more items) - rust-lang#99678 (Update doc comments that refer to config parameter) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 2a990f7 + 34d0c0a commit 7fe022f

File tree

16 files changed

+115
-146
lines changed

16 files changed

+115
-146
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,17 @@ pub(super) fn op_to_const<'tcx>(
173173
Immediate::ScalarPair(a, b) => {
174174
debug!("ScalarPair(a: {:?}, b: {:?})", a, b);
175175
// We know `offset` is relative to the allocation, so we can use `into_parts`.
176-
let (data, start) =
177-
match ecx.scalar_to_ptr(a.check_init().unwrap()).unwrap().into_parts() {
178-
(Some(alloc_id), offset) => {
179-
(ecx.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes())
180-
}
181-
(None, _offset) => (
182-
ecx.tcx.intern_const_alloc(
183-
Allocation::from_bytes_byte_aligned_immutable(b"" as &[u8]),
184-
),
185-
0,
186-
),
187-
};
176+
let (data, start) = match a.to_pointer(ecx).unwrap().into_parts() {
177+
(Some(alloc_id), offset) => {
178+
(ecx.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes())
179+
}
180+
(None, _offset) => (
181+
ecx.tcx.intern_const_alloc(Allocation::from_bytes_byte_aligned_immutable(
182+
b"" as &[u8],
183+
)),
184+
0,
185+
),
186+
};
188187
let len = b.to_machine_usize(ecx).unwrap();
189188
let start = start.try_into().unwrap();
190189
let len: usize = len.try_into().unwrap();

compiler/rustc_const_eval/src/interpret/cast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
180180
assert!(cast_ty.is_integral());
181181

182182
let scalar = src.to_scalar()?;
183-
let ptr = self.scalar_to_ptr(scalar)?;
183+
let ptr = scalar.to_pointer(self)?;
184184
match ptr.into_pointer_or_addr() {
185185
Ok(ptr) => M::expose_ptr(self, ptr)?,
186186
Err(_) => {} // Do nothing, exposing an invalid pointer (`None` provenance) is a NOP.
@@ -299,7 +299,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
299299
}
300300
(&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => {
301301
let (old_data, old_vptr) = self.read_immediate(src)?.to_scalar_pair()?;
302-
let old_vptr = self.scalar_to_ptr(old_vptr)?;
302+
let old_vptr = old_vptr.to_pointer(self)?;
303303
let (ty, old_trait) = self.get_ptr_vtable(old_vptr)?;
304304
if old_trait != data_a.principal() {
305305
throw_ub_format!("upcast on a pointer whose vtable does not match its type");

compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
629629
Ok(Some((size, align)))
630630
}
631631
ty::Dynamic(..) => {
632-
let vtable = self.scalar_to_ptr(metadata.unwrap_meta())?;
632+
let vtable = metadata.unwrap_meta().to_pointer(self)?;
633633
// Read size and align from vtable (already checks size).
634634
Ok(Some(self.get_vtable_size_and_align(vtable)?))
635635
}

compiler/rustc_const_eval/src/interpret/intern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory
245245
if let ty::Dynamic(..) =
246246
tcx.struct_tail_erasing_lifetimes(referenced_ty, self.ecx.param_env).kind()
247247
{
248-
let ptr = self.ecx.scalar_to_ptr(mplace.meta.unwrap_meta())?;
248+
let ptr = mplace.meta.unwrap_meta().to_pointer(&tcx)?;
249249
if let Some(alloc_id) = ptr.provenance {
250250
// Explicitly choose const mode here, since vtables are immutable, even
251251
// if the reference of the fat pointer is mutable.

compiler/rustc_const_eval/src/interpret/machine.rs

-23
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
135135
/// Whether to enforce integers and floats being initialized.
136136
fn enforce_number_init(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
137137

138-
/// Whether to enforce integers and floats not having provenance.
139-
fn enforce_number_no_provenance(ecx: &InterpCx<'mir, 'tcx, Self>) -> bool;
140-
141138
/// Whether function calls should be [ABI](CallAbi)-checked.
142139
fn enforce_abi(_ecx: &InterpCx<'mir, 'tcx, Self>) -> bool {
143140
true
@@ -300,13 +297,6 @@ pub trait Machine<'mir, 'tcx>: Sized {
300297
addr: u64,
301298
) -> InterpResult<'tcx, Pointer<Option<Self::Provenance>>>;
302299

303-
/// Hook for returning a pointer from a transmute-like operation on an addr.
304-
/// This is only needed to support Miri's (unsound) "allow-ptr-int-transmute" flag.
305-
fn ptr_from_addr_transmute(
306-
ecx: &InterpCx<'mir, 'tcx, Self>,
307-
addr: u64,
308-
) -> Pointer<Option<Self::Provenance>>;
309-
310300
/// Marks a pointer as exposed, allowing it's provenance
311301
/// to be recovered. "Pointer-to-int cast"
312302
fn expose_ptr(
@@ -469,11 +459,6 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
469459
true
470460
}
471461

472-
#[inline(always)]
473-
fn enforce_number_no_provenance(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
474-
true
475-
}
476-
477462
#[inline(always)]
478463
fn checked_binop_checks_overflow(_ecx: &InterpCx<$mir, $tcx, Self>) -> bool {
479464
true
@@ -518,14 +503,6 @@ pub macro compile_time_machine(<$mir: lifetime, $tcx: lifetime>) {
518503
ptr
519504
}
520505

521-
#[inline(always)]
522-
fn ptr_from_addr_transmute(
523-
_ecx: &InterpCx<$mir, $tcx, Self>,
524-
addr: u64,
525-
) -> Pointer<Option<AllocId>> {
526-
Pointer::from_addr(addr)
527-
}
528-
529506
#[inline(always)]
530507
fn ptr_from_addr_cast(
531508
_ecx: &InterpCx<$mir, $tcx, Self>,

compiler/rustc_const_eval/src/interpret/memory.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use std::assert_matches::assert_matches;
1010
use std::borrow::Cow;
1111
use std::collections::VecDeque;
12-
use std::convert::TryFrom;
1312
use std::fmt;
1413
use std::ptr;
1514

@@ -1172,34 +1171,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11721171

11731172
/// Machine pointer introspection.
11741173
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
1175-
pub fn scalar_to_ptr(
1176-
&self,
1177-
scalar: Scalar<M::Provenance>,
1178-
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
1179-
// We use `to_bits_or_ptr_internal` since we are just implementing the method people need to
1180-
// call to force getting out a pointer.
1181-
Ok(
1182-
match scalar
1183-
.to_bits_or_ptr_internal(self.pointer_size())
1184-
.map_err(|s| err_ub!(ScalarSizeMismatch(s)))?
1185-
{
1186-
Err(ptr) => ptr.into(),
1187-
Ok(bits) => {
1188-
let addr = u64::try_from(bits).unwrap();
1189-
M::ptr_from_addr_transmute(&self, addr)
1190-
}
1191-
},
1192-
)
1193-
}
1194-
11951174
/// Test if this value might be null.
11961175
/// If the machine does not support ptr-to-int casts, this is conservative.
11971176
pub fn scalar_may_be_null(&self, scalar: Scalar<M::Provenance>) -> InterpResult<'tcx, bool> {
11981177
Ok(match scalar.try_to_int() {
11991178
Ok(int) => int.is_null(),
12001179
Err(_) => {
12011180
// Can only happen during CTFE.
1202-
let ptr = self.scalar_to_ptr(scalar)?;
1181+
let ptr = scalar.to_pointer(self)?;
12031182
match self.ptr_try_get_alloc_id(ptr) {
12041183
Ok((alloc_id, offset, _)) => {
12051184
let (size, _align, _kind) = self.get_alloc_info(alloc_id);

compiler/rustc_const_eval/src/interpret/operand.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_target::abi::{VariantIdx, Variants};
1414
use super::{
1515
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, GlobalId,
1616
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Place, PlaceTy, Pointer,
17-
PointerArithmetic, Provenance, Scalar, ScalarMaybeUninit,
17+
Provenance, Scalar, ScalarMaybeUninit,
1818
};
1919

2020
/// An `Immediate` represents a single immediate self-contained Rust value.
@@ -363,17 +363,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
363363
Abi::Scalar(s) if force => Some(s.primitive()),
364364
_ => None,
365365
};
366-
let read_provenance = |s: abi::Primitive, size| {
367-
// Should be just `s.is_ptr()`, but we support a Miri flag that accepts more
368-
// questionable ptr-int transmutes.
369-
let number_may_have_provenance = !M::enforce_number_no_provenance(self);
370-
s.is_ptr() || (number_may_have_provenance && size == self.pointer_size())
371-
};
372366
if let Some(s) = scalar_layout {
373367
let size = s.size(self);
374368
assert_eq!(size, mplace.layout.size, "abi::Scalar size does not match layout size");
375-
let scalar =
376-
alloc.read_scalar(alloc_range(Size::ZERO, size), read_provenance(s, size))?;
369+
let scalar = alloc
370+
.read_scalar(alloc_range(Size::ZERO, size), /*read_provenance*/ s.is_ptr())?;
377371
return Ok(Some(ImmTy { imm: scalar.into(), layout: mplace.layout }));
378372
}
379373
let scalar_pair_layout = match mplace.layout.abi {
@@ -391,10 +385,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
391385
let (a_size, b_size) = (a.size(self), b.size(self));
392386
let b_offset = a_size.align_to(b.align(self).abi);
393387
assert!(b_offset.bytes() > 0); // in `operand_field` we use the offset to tell apart the fields
394-
let a_val =
395-
alloc.read_scalar(alloc_range(Size::ZERO, a_size), read_provenance(a, a_size))?;
396-
let b_val =
397-
alloc.read_scalar(alloc_range(b_offset, b_size), read_provenance(b, b_size))?;
388+
let a_val = alloc.read_scalar(
389+
alloc_range(Size::ZERO, a_size),
390+
/*read_provenance*/ a.is_ptr(),
391+
)?;
392+
let b_val = alloc
393+
.read_scalar(alloc_range(b_offset, b_size), /*read_provenance*/ b.is_ptr())?;
398394
return Ok(Some(ImmTy {
399395
imm: Immediate::ScalarPair(a_val, b_val),
400396
layout: mplace.layout,
@@ -459,7 +455,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
459455
&self,
460456
op: &OpTy<'tcx, M::Provenance>,
461457
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
462-
self.scalar_to_ptr(self.read_scalar(op)?.check_init()?)
458+
self.read_scalar(op)?.to_pointer(self)
463459
}
464460

465461
/// Turn the wide MPlace into a string (must already be dereferenced!)

compiler/rustc_const_eval/src/interpret/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ where
331331
Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)),
332332
};
333333

334-
let mplace = MemPlace { ptr: self.scalar_to_ptr(ptr.check_init()?)?, meta };
334+
let mplace = MemPlace { ptr: ptr.to_pointer(self)?, meta };
335335
// When deref'ing a pointer, the *static* alignment given by the type is what matters.
336336
let align = layout.align.abi;
337337
Ok(MPlaceTy { mplace, layout, align })
@@ -889,7 +889,7 @@ where
889889
&self,
890890
mplace: &MPlaceTy<'tcx, M::Provenance>,
891891
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
892-
let vtable = self.scalar_to_ptr(mplace.vtable())?; // also sanity checks the type
892+
let vtable = mplace.vtable().to_pointer(self)?; // also sanity checks the type
893893
let (ty, _) = self.get_ptr_vtable(vtable)?;
894894
let layout = self.layout_of(ty)?;
895895

compiler/rustc_const_eval/src/interpret/terminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
561561
};
562562

563563
// Get the required information from the vtable.
564-
let vptr = self.scalar_to_ptr(receiver_place.meta.unwrap_meta())?;
564+
let vptr = receiver_place.meta.unwrap_meta().to_pointer(self)?;
565565
let (dyn_ty, dyn_trait) = self.get_ptr_vtable(vptr)?;
566566
if dyn_trait != data.principal() {
567567
throw_ub_format!(

compiler/rustc_const_eval/src/interpret/validity.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
312312
let tail = self.ecx.tcx.struct_tail_erasing_lifetimes(pointee.ty, self.ecx.param_env);
313313
match tail.kind() {
314314
ty::Dynamic(..) => {
315-
let vtable = self.ecx.scalar_to_ptr(meta.unwrap_meta())?;
315+
let vtable = meta.unwrap_meta().to_pointer(self.ecx)?;
316316
// Make sure it is a genuine vtable pointer.
317317
let (_ty, _trait) = try_validation!(
318318
self.ecx.get_ptr_vtable(vtable),
@@ -517,15 +517,13 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
517517
{ "{:x}", value } expected { "initialized bytes" }
518518
);
519519
}
520-
if M::enforce_number_no_provenance(self.ecx) {
521-
// As a special exception we *do* match on a `Scalar` here, since we truly want
522-
// to know its underlying representation (and *not* cast it to an integer).
523-
let is_ptr = value.check_init().map_or(false, |v| matches!(v, Scalar::Ptr(..)));
524-
if is_ptr {
525-
throw_validation_failure!(self.path,
526-
{ "{:x}", value } expected { "plain (non-pointer) bytes" }
527-
)
528-
}
520+
// As a special exception we *do* match on a `Scalar` here, since we truly want
521+
// to know its underlying representation (and *not* cast it to an integer).
522+
let is_ptr = value.check_init().map_or(false, |v| matches!(v, Scalar::Ptr(..)));
523+
if is_ptr {
524+
throw_validation_failure!(self.path,
525+
{ "{:x}", value } expected { "plain (non-pointer) bytes" }
526+
)
529527
}
530528
Ok(true)
531529
}
@@ -568,7 +566,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
568566

569567
// If we check references recursively, also check that this points to a function.
570568
if let Some(_) = self.ref_tracking {
571-
let ptr = self.ecx.scalar_to_ptr(value)?;
569+
let ptr = value.to_pointer(self.ecx)?;
572570
let _fn = try_validation!(
573571
self.ecx.get_ptr_fn(ptr),
574572
self.path,
@@ -906,7 +904,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
906904
match alloc.check_bytes(
907905
alloc_range(Size::ZERO, size),
908906
/*allow_uninit*/ !M::enforce_number_init(self.ecx),
909-
/*allow_ptr*/ !M::enforce_number_no_provenance(self.ecx),
907+
/*allow_ptr*/ false,
910908
) {
911909
// In the happy case, we needn't check anything else.
912910
Ok(()) => {}

compiler/rustc_middle/src/mir/interpret/value.rs

+18
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,19 @@ impl<Prov> Scalar<Prov> {
331331
}
332332

333333
impl<'tcx, Prov: Provenance> Scalar<Prov> {
334+
pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>> {
335+
match self
336+
.to_bits_or_ptr_internal(cx.pointer_size())
337+
.map_err(|s| err_ub!(ScalarSizeMismatch(s)))?
338+
{
339+
Err(ptr) => Ok(ptr.into()),
340+
Ok(bits) => {
341+
let addr = u64::try_from(bits).unwrap();
342+
Ok(Pointer::from_addr(addr))
343+
}
344+
}
345+
}
346+
334347
/// Fundamental scalar-to-int (cast) operation. Many convenience wrappers exist below, that you
335348
/// likely want to use instead.
336349
///
@@ -546,6 +559,11 @@ impl<Prov> ScalarMaybeUninit<Prov> {
546559
}
547560

548561
impl<'tcx, Prov: Provenance> ScalarMaybeUninit<Prov> {
562+
#[inline(always)]
563+
pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>> {
564+
self.check_init()?.to_pointer(cx)
565+
}
566+
549567
#[inline(always)]
550568
pub fn to_bool(self) -> InterpResult<'tcx, bool> {
551569
self.check_init()?.to_bool()

compiler/rustc_parse/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ pub fn new_parser_from_file<'a>(sess: &'a ParseSess, path: &Path, sp: Option<Spa
117117
source_file_to_parser(sess, file_to_source_file(sess, path, sp))
118118
}
119119

120-
/// Given a `source_file` and config, returns a parser.
120+
/// Given a session and a `source_file`, returns a parser.
121121
fn source_file_to_parser(sess: &ParseSess, source_file: Lrc<SourceFile>) -> Parser<'_> {
122122
panictry_buffer!(&sess.span_diagnostic, maybe_source_file_to_parser(sess, source_file))
123123
}
124124

125-
/// Given a `source_file` and config, return a parser. Returns any buffered errors from lexing the
125+
/// Given a session and a `source_file`, return a parser. Returns any buffered errors from lexing the
126126
/// initial token stream.
127127
fn maybe_source_file_to_parser(
128128
sess: &ParseSess,

library/std/src/io/stdio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
220220
///
221221
/// fn main() -> io::Result<()> {
222222
/// let mut buffer = String::new();
223-
/// let mut stdin = io::stdin(); // We get `Stdin` here.
223+
/// let stdin = io::stdin(); // We get `Stdin` here.
224224
/// stdin.read_line(&mut buffer)?;
225225
/// Ok(())
226226
/// }

src/librustdoc/clean/inline.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use rustc_span::hygiene::MacroKind;
1616
use rustc_span::symbol::{kw, sym, Symbol};
1717

1818
use crate::clean::{
19-
self, clean_fn_decl_from_did_and_sig, clean_middle_ty, clean_ty, clean_ty_generics, utils,
20-
Attributes, AttributesExt, Clean, ImplKind, ItemId, Type, Visibility,
19+
self, clean_fn_decl_from_did_and_sig, clean_middle_field, clean_middle_ty, clean_ty,
20+
clean_ty_generics, utils, Attributes, AttributesExt, Clean, ImplKind, ItemId, Type, Visibility,
2121
};
2222
use crate::core::DocContext;
2323
use crate::formats::item_type::ItemType;
@@ -246,7 +246,7 @@ fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct {
246246
clean::Struct {
247247
struct_type: variant.ctor_kind,
248248
generics: clean_ty_generics(cx, cx.tcx.generics_of(did), predicates),
249-
fields: variant.fields.iter().map(|x| x.clean(cx)).collect(),
249+
fields: variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect(),
250250
}
251251
}
252252

@@ -255,7 +255,7 @@ fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
255255
let variant = cx.tcx.adt_def(did).non_enum_variant();
256256

257257
let generics = clean_ty_generics(cx, cx.tcx.generics_of(did), predicates);
258-
let fields = variant.fields.iter().map(|x| x.clean(cx)).collect();
258+
let fields = variant.fields.iter().map(|x| clean_middle_field(x, cx)).collect();
259259
clean::Union { generics, fields }
260260
}
261261

0 commit comments

Comments
 (0)