Skip to content

Commit 1517f5d

Browse files
committed
Auto merge of #99024 - matthiaskrgr:rollup-8ygpcpg, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #97917 (Implement ExitCodeExt for Windows) - #98844 (Reword comments and rename HIR visiting methods.) - #98979 (interpret: use AllocRange in UninitByteAccess) - #98986 (Fix missing word in comment) - #98994 (replace process exit with more detailed exit in src/bootstrap/*.rs) - #98995 (Add a test for #80471) - #99002 (suggest adding a derive for #[default] applied to variants) - #99004 (Add a test for #70408) - #99017 (Replace boolean argument for print_where_clause with an enum to make code more clear) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 0f573a0 + ec0c156 commit 1517f5d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+343
-236
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
427427
err_ub!(DanglingIntPointer(0, _)) =>
428428
{ "a null {kind}" },
429429
err_ub!(DanglingIntPointer(i, _)) =>
430-
{ "a dangling {kind} (address 0x{i:x} is unallocated)" },
430+
{ "a dangling {kind} (address {i:#x} is unallocated)" },
431431
err_ub!(PointerOutOfBounds { .. }) =>
432432
{ "a dangling {kind} (going beyond the bounds of its allocation)" },
433433
// This cannot happen during const-eval (because interning already detects
@@ -941,7 +941,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
941941
// element that byte belongs to so we can
942942
// provide an index.
943943
let i = usize::try_from(
944-
access.uninit_offset.bytes() / layout.size.bytes(),
944+
access.uninit.start.bytes() / layout.size.bytes(),
945945
)
946946
.unwrap();
947947
self.path.push(PathElem::ArrayElem(i));

compiler/rustc_hir/src/intravisit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! - Example: Examine each expression to look for its type and do some check or other.
2020
//! - How: Implement `intravisit::Visitor` and override the `NestedFilter` type to
2121
//! `nested_filter::OnlyBodies` (and implement `nested_visit_map`), and use
22-
//! `tcx.hir().deep_visit_all_item_likes(&mut visitor)`. Within your
22+
//! `tcx.hir().visit_all_item_likes_in_crate(&mut visitor)`. Within your
2323
//! `intravisit::Visitor` impl, implement methods like `visit_expr()` (don't forget to invoke
2424
//! `intravisit::walk_expr()` to keep walking the subparts).
2525
//! - Pro: Visitor methods for any kind of HIR node, not just item-like things.
@@ -190,7 +190,7 @@ use nested_filter::NestedFilter;
190190
/// (this is why the module is called `intravisit`, to distinguish it
191191
/// from the AST's `visit` module, which acts differently). If you
192192
/// simply want to visit all items in the crate in some order, you
193-
/// should call `Crate::visit_all_items`. Otherwise, see the comment
193+
/// should call `tcx.hir().visit_all_item_likes_in_crate`. Otherwise, see the comment
194194
/// on `visit_nested_item` for details on how to visit nested items.
195195
///
196196
/// If you want to ensure that your code handles every variant

compiler/rustc_incremental/src/assert_dep_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn assert_dep_graph(tcx: TyCtxt<'_>) {
7575
let mut visitor =
7676
IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] };
7777
visitor.process_attrs(hir::CRATE_HIR_ID);
78-
tcx.hir().deep_visit_all_item_likes(&mut visitor);
78+
tcx.hir().visit_all_item_likes_in_crate(&mut visitor);
7979
(visitor.if_this_changed, visitor.then_this_would_need)
8080
};
8181

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
419419
return;
420420
}
421421

422-
self.tcx.hir().deep_visit_all_item_likes(self);
422+
self.tcx.hir().visit_all_item_likes_in_crate(self);
423423
}
424424

425425
fn encode_def_path_table(&mut self) {

compiler/rustc_middle/src/hir/map/mod.rs

+37-29
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl<'hir> Map<'hir> {
561561
}
562562
}
563563

564-
/// Walks the contents of a crate. See also `Crate::visit_all_items`.
564+
/// Walks the contents of the local crate. See also `visit_all_item_likes_in_crate`.
565565
pub fn walk_toplevel_module(self, visitor: &mut impl Visitor<'hir>) {
566566
let (top_mod, span, hir_id) = self.get_module(CRATE_DEF_ID);
567567
visitor.visit_mod(top_mod, span, hir_id);
@@ -581,53 +581,61 @@ impl<'hir> Map<'hir> {
581581
}
582582
}
583583

584-
/// Visits all items in the crate in some deterministic (but
585-
/// unspecified) order. If you need to process every item,
586-
/// and care about nesting -- usually because your algorithm
587-
/// follows lexical scoping rules -- then this method is the best choice.
588-
/// If you don't care about nesting, you should use the `tcx.hir_crate_items()` query
589-
/// or `items()` instead.
584+
/// Visits all item-likes in the crate in some deterministic (but unspecified) order. If you
585+
/// need to process every item-like, and don't care about visiting nested items in a particular
586+
/// order then this method is the best choice. If you do care about this nesting, you should
587+
/// use the `tcx.hir().walk_toplevel_module`.
588+
///
589+
/// Note that this function will access HIR for all the item-likes in the crate. If you only
590+
/// need to access some of them, it is usually better to manually loop on the iterators
591+
/// provided by `tcx.hir_crate_items(())`.
590592
///
591593
/// Please see the notes in `intravisit.rs` for more information.
592-
pub fn deep_visit_all_item_likes<V>(self, visitor: &mut V)
594+
pub fn visit_all_item_likes_in_crate<V>(self, visitor: &mut V)
593595
where
594596
V: Visitor<'hir>,
595597
{
596-
let krate = self.krate();
597-
for owner in krate.owners.iter().filter_map(|i| i.as_owner()) {
598-
match owner.node() {
599-
OwnerNode::Item(item) => visitor.visit_item(item),
600-
OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item),
601-
OwnerNode::ImplItem(item) => visitor.visit_impl_item(item),
602-
OwnerNode::TraitItem(item) => visitor.visit_trait_item(item),
603-
OwnerNode::Crate(_) => {}
604-
}
598+
let krate = self.tcx.hir_crate_items(());
599+
600+
for id in krate.items() {
601+
visitor.visit_item(self.item(id));
602+
}
603+
604+
for id in krate.trait_items() {
605+
visitor.visit_trait_item(self.trait_item(id));
606+
}
607+
608+
for id in krate.impl_items() {
609+
visitor.visit_impl_item(self.impl_item(id));
610+
}
611+
612+
for id in krate.foreign_items() {
613+
visitor.visit_foreign_item(self.foreign_item(id));
605614
}
606615
}
607616

608-
/// If you don't care about nesting, you should use the
609-
/// `tcx.hir_module_items()` query or `module_items()` instead.
610-
/// Please see notes in `deep_visit_all_item_likes`.
611-
pub fn deep_visit_item_likes_in_module<V>(self, module: LocalDefId, visitor: &mut V)
617+
/// This method is the equivalent of `visit_all_item_likes_in_crate` but restricted to
618+
/// item-likes in a single module.
619+
pub fn visit_item_likes_in_module<V>(self, module: LocalDefId, visitor: &mut V)
612620
where
613621
V: Visitor<'hir>,
614622
{
615623
let module = self.tcx.hir_module_items(module);
616624

617-
for id in module.items.iter() {
618-
visitor.visit_item(self.item(*id));
625+
for id in module.items() {
626+
visitor.visit_item(self.item(id));
619627
}
620628

621-
for id in module.trait_items.iter() {
622-
visitor.visit_trait_item(self.trait_item(*id));
629+
for id in module.trait_items() {
630+
visitor.visit_trait_item(self.trait_item(id));
623631
}
624632

625-
for id in module.impl_items.iter() {
626-
visitor.visit_impl_item(self.impl_item(*id));
633+
for id in module.impl_items() {
634+
visitor.visit_impl_item(self.impl_item(id));
627635
}
628636

629-
for id in module.foreign_items.iter() {
630-
visitor.visit_foreign_item(self.foreign_item(*id));
637+
for id in module.foreign_items() {
638+
visitor.visit_foreign_item(self.foreign_item(id));
631639
}
632640
}
633641

compiler/rustc_middle/src/hir/nested_filter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::intravisit::nested_filter::NestedFilter;
88
/// constant arguments of types, e.g. in `let _: [(); /* HERE */];`.
99
///
1010
/// **This is the most common choice.** A very common pattern is
11-
/// to use `deep_visit_all_item_likes()` as an outer loop,
11+
/// to use `visit_all_item_likes_in_crate()` as an outer loop,
1212
/// and to have the visitor that visits the contents of each item
1313
/// using this setting.
1414
pub struct OnlyBodies(());

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ pub fn alloc_range(start: Size, size: Size) -> AllocRange {
179179
}
180180

181181
impl AllocRange {
182+
#[inline]
183+
pub fn from(r: Range<Size>) -> Self {
184+
alloc_range(r.start, r.end - r.start) // `Size` subtraction (overflow-checked)
185+
}
186+
182187
#[inline(always)]
183188
pub fn end(self) -> Size {
184189
self.start + self.size // This does overflow checking.
@@ -1095,17 +1100,17 @@ impl InitMask {
10951100
/// Returns `Ok(())` if it's initialized. Otherwise returns a range of byte
10961101
/// indexes for the first contiguous span of the uninitialized access.
10971102
#[inline]
1098-
pub fn is_range_initialized(&self, start: Size, end: Size) -> Result<(), Range<Size>> {
1103+
pub fn is_range_initialized(&self, start: Size, end: Size) -> Result<(), AllocRange> {
10991104
if end > self.len {
1100-
return Err(self.len..end);
1105+
return Err(AllocRange::from(self.len..end));
11011106
}
11021107

11031108
let uninit_start = self.find_bit(start, end, false);
11041109

11051110
match uninit_start {
11061111
Some(uninit_start) => {
11071112
let uninit_end = self.find_bit(uninit_start, end, true).unwrap_or(end);
1108-
Err(uninit_start..uninit_end)
1113+
Err(AllocRange::from(uninit_start..uninit_end))
11091114
}
11101115
None => Ok(()),
11111116
}
@@ -1176,19 +1181,17 @@ impl<Tag: Copy, Extra> Allocation<Tag, Extra> {
11761181
///
11771182
/// Returns `Ok(())` if it's initialized. Otherwise returns the range of byte
11781183
/// indexes of the first contiguous uninitialized access.
1179-
fn is_init(&self, range: AllocRange) -> Result<(), Range<Size>> {
1184+
fn is_init(&self, range: AllocRange) -> Result<(), AllocRange> {
11801185
self.init_mask.is_range_initialized(range.start, range.end()) // `Size` addition
11811186
}
11821187

11831188
/// Checks that a range of bytes is initialized. If not, returns the `InvalidUninitBytes`
11841189
/// error which will report the first range of bytes which is uninitialized.
11851190
fn check_init(&self, range: AllocRange) -> AllocResult {
1186-
self.is_init(range).map_err(|idx_range| {
1191+
self.is_init(range).map_err(|uninit_range| {
11871192
AllocError::InvalidUninitBytes(Some(UninitBytesAccess {
1188-
access_offset: range.start,
1189-
access_size: range.size,
1190-
uninit_offset: idx_range.start,
1191-
uninit_size: idx_range.end - idx_range.start, // `Size` subtraction
1193+
access: range,
1194+
uninit: uninit_range,
11921195
}))
11931196
})
11941197
}

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

+39-51
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{AllocId, ConstAlloc, Pointer, Scalar};
1+
use super::{AllocId, AllocRange, ConstAlloc, Pointer, Scalar};
22

33
use crate::mir::interpret::ConstValue;
44
use crate::ty::{layout, query::TyCtxtAt, tls, FnSig, Ty, ValTree};
@@ -162,9 +162,9 @@ impl fmt::Display for InvalidProgramInfo<'_> {
162162
AlreadyReported(ErrorGuaranteed { .. }) => {
163163
write!(f, "encountered constants with type errors, stopping evaluation")
164164
}
165-
Layout(ref err) => write!(f, "{}", err),
166-
FnAbiAdjustForForeignAbi(ref err) => write!(f, "{}", err),
167-
SizeOfUnsizedType(ty) => write!(f, "size_of called on unsized type `{}`", ty),
165+
Layout(ref err) => write!(f, "{err}"),
166+
FnAbiAdjustForForeignAbi(ref err) => write!(f, "{err}"),
167+
SizeOfUnsizedType(ty) => write!(f, "size_of called on unsized type `{ty}`"),
168168
}
169169
}
170170
}
@@ -205,14 +205,10 @@ impl fmt::Display for CheckInAllocMsg {
205205
/// Details of an access to uninitialized bytes where it is not allowed.
206206
#[derive(Debug)]
207207
pub struct UninitBytesAccess {
208-
/// Location of the original memory access.
209-
pub access_offset: Size,
210-
/// Size of the original memory access.
211-
pub access_size: Size,
212-
/// Location of the first uninitialized byte that was accessed.
213-
pub uninit_offset: Size,
214-
/// Number of consecutive uninitialized bytes that were accessed.
215-
pub uninit_size: Size,
208+
/// Range of the original memory access.
209+
pub access: AllocRange,
210+
/// Range of the uninit memory that was encountered. (Might not be maximal.)
211+
pub uninit: AllocRange,
216212
}
217213

218214
/// Information about a size mismatch.
@@ -308,30 +304,28 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
308304
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
309305
use UndefinedBehaviorInfo::*;
310306
match self {
311-
Ub(msg) => write!(f, "{}", msg),
307+
Ub(msg) => write!(f, "{msg}"),
312308
Unreachable => write!(f, "entering unreachable code"),
313309
BoundsCheckFailed { ref len, ref index } => {
314-
write!(f, "indexing out of bounds: the len is {} but the index is {}", len, index)
310+
write!(f, "indexing out of bounds: the len is {len} but the index is {index}")
315311
}
316312
DivisionByZero => write!(f, "dividing by zero"),
317313
RemainderByZero => write!(f, "calculating the remainder with a divisor of zero"),
318314
DivisionOverflow => write!(f, "overflow in signed division (dividing MIN by -1)"),
319315
RemainderOverflow => write!(f, "overflow in signed remainder (dividing MIN by -1)"),
320316
PointerArithOverflow => write!(f, "overflowing in-bounds pointer arithmetic"),
321-
InvalidMeta(msg) => write!(f, "invalid metadata in wide pointer: {}", msg),
317+
InvalidMeta(msg) => write!(f, "invalid metadata in wide pointer: {msg}"),
322318
InvalidVtableDropFn(sig) => write!(
323319
f,
324-
"invalid drop function signature: got {}, expected exactly one argument which must be a pointer type",
325-
sig
320+
"invalid drop function signature: got {sig}, expected exactly one argument which must be a pointer type",
326321
),
327322
InvalidVtableSize => {
328323
write!(f, "invalid vtable: size is bigger than largest supported object")
329324
}
330-
InvalidVtableAlignment(msg) => write!(f, "invalid vtable: alignment {}", msg),
325+
InvalidVtableAlignment(msg) => write!(f, "invalid vtable: alignment {msg}"),
331326
UnterminatedCString(p) => write!(
332327
f,
333-
"reading a null-terminated string starting at {:?} with no null found before end of allocation",
334-
p,
328+
"reading a null-terminated string starting at {p:?} with no null found before end of allocation",
335329
),
336330
PointerUseAfterFree(a) => {
337331
write!(f, "pointer to {a:?} was dereferenced after this allocation got freed")
@@ -359,41 +353,36 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
359353
}
360354
AlignmentCheckFailed { required, has } => write!(
361355
f,
362-
"accessing memory with alignment {}, but alignment {} is required",
363-
has.bytes(),
364-
required.bytes()
356+
"accessing memory with alignment {has}, but alignment {required} is required",
357+
has = has.bytes(),
358+
required = required.bytes()
365359
),
366360
WriteToReadOnly(a) => write!(f, "writing to {a:?} which is read-only"),
367361
DerefFunctionPointer(a) => write!(f, "accessing {a:?} which contains a function"),
368362
ValidationFailure { path: None, msg } => {
369-
write!(f, "constructing invalid value: {}", msg)
363+
write!(f, "constructing invalid value: {msg}")
370364
}
371365
ValidationFailure { path: Some(path), msg } => {
372-
write!(f, "constructing invalid value at {}: {}", path, msg)
366+
write!(f, "constructing invalid value at {path}: {msg}")
373367
}
374368
InvalidBool(b) => {
375-
write!(f, "interpreting an invalid 8-bit value as a bool: 0x{:02x}", b)
369+
write!(f, "interpreting an invalid 8-bit value as a bool: 0x{b:02x}")
376370
}
377371
InvalidChar(c) => {
378-
write!(f, "interpreting an invalid 32-bit value as a char: 0x{:08x}", c)
372+
write!(f, "interpreting an invalid 32-bit value as a char: 0x{c:08x}")
379373
}
380-
InvalidTag(val) => write!(f, "enum value has invalid tag: {:x}", val),
374+
InvalidTag(val) => write!(f, "enum value has invalid tag: {val:x}"),
381375
InvalidFunctionPointer(p) => {
382-
write!(f, "using {:?} as function pointer but it does not point to a function", p)
376+
write!(f, "using {p:?} as function pointer but it does not point to a function")
383377
}
384-
InvalidStr(err) => write!(f, "this string is not valid UTF-8: {}", err),
385-
InvalidUninitBytes(Some((alloc, access))) => write!(
378+
InvalidStr(err) => write!(f, "this string is not valid UTF-8: {err}"),
379+
InvalidUninitBytes(Some((alloc, info))) => write!(
386380
f,
387-
"reading {} byte{} of memory starting at {:?}, \
388-
but {} byte{} {} uninitialized starting at {:?}, \
381+
"reading memory at {alloc:?}{access:?}, \
382+
but memory is uninitialized at {uninit:?}, \
389383
and this operation requires initialized memory",
390-
access.access_size.bytes(),
391-
pluralize!(access.access_size.bytes()),
392-
Pointer::new(*alloc, access.access_offset),
393-
access.uninit_size.bytes(),
394-
pluralize!(access.uninit_size.bytes()),
395-
pluralize!("is", access.uninit_size.bytes()),
396-
Pointer::new(*alloc, access.uninit_offset),
384+
access = info.access,
385+
uninit = info.uninit,
397386
),
398387
InvalidUninitBytes(None) => write!(
399388
f,
@@ -402,8 +391,7 @@ impl fmt::Display for UndefinedBehaviorInfo<'_> {
402391
DeadLocal => write!(f, "accessing a dead local variable"),
403392
ScalarSizeMismatch(self::ScalarSizeMismatch { target_size, data_size }) => write!(
404393
f,
405-
"scalar size mismatch: expected {} bytes but got {} bytes instead",
406-
target_size, data_size
394+
"scalar size mismatch: expected {target_size} bytes but got {data_size} bytes instead",
407395
),
408396
UninhabitedEnumVariantWritten => {
409397
write!(f, "writing discriminant of an uninhabited enum")
@@ -437,13 +425,13 @@ impl fmt::Display for UnsupportedOpInfo {
437425
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
438426
use UnsupportedOpInfo::*;
439427
match self {
440-
Unsupported(ref msg) => write!(f, "{}", msg),
428+
Unsupported(ref msg) => write!(f, "{msg}"),
441429
ReadPointerAsBytes => write!(f, "unable to turn pointer into raw bytes"),
442430
PartialPointerOverwrite(ptr) => {
443-
write!(f, "unable to overwrite parts of a pointer in memory at {:?}", ptr)
431+
write!(f, "unable to overwrite parts of a pointer in memory at {ptr:?}")
444432
}
445-
ThreadLocalStatic(did) => write!(f, "cannot access thread local static ({:?})", did),
446-
ReadExternStatic(did) => write!(f, "cannot read from extern static ({:?})", did),
433+
ThreadLocalStatic(did) => write!(f, "cannot access thread local static ({did:?})"),
434+
ReadExternStatic(did) => write!(f, "cannot read from extern static ({did:?})"),
447435
}
448436
}
449437
}
@@ -526,11 +514,11 @@ impl fmt::Display for InterpError<'_> {
526514
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
527515
use InterpError::*;
528516
match *self {
529-
Unsupported(ref msg) => write!(f, "{}", msg),
530-
InvalidProgram(ref msg) => write!(f, "{}", msg),
531-
UndefinedBehavior(ref msg) => write!(f, "{}", msg),
532-
ResourceExhaustion(ref msg) => write!(f, "{}", msg),
533-
MachineStop(ref msg) => write!(f, "{}", msg),
517+
Unsupported(ref msg) => write!(f, "{msg}"),
518+
InvalidProgram(ref msg) => write!(f, "{msg}"),
519+
UndefinedBehavior(ref msg) => write!(f, "{msg}"),
520+
ResourceExhaustion(ref msg) => write!(f, "{msg}"),
521+
MachineStop(ref msg) => write!(f, "{msg}"),
534522
}
535523
}
536524
}

0 commit comments

Comments
 (0)