Skip to content

Commit ba29372

Browse files
authored
Unrolled build for rust-lang#136336
Rollup merge of rust-lang#136336 - nnethercote:overhaul-rustc_middle-util, r=jieyouxu Overhaul `rustc_middle::util` It's an odd module with some odd stuff in it. r? `@Noratrieb`
2 parents aa4cfd0 + 0c47091 commit ba29372

File tree

16 files changed

+102
-135
lines changed

16 files changed

+102
-135
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3777,7 +3777,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
37773777
let tcx = self.infcx.tcx;
37783778
if let Some(Terminator { kind: TerminatorKind::Call { call_source, fn_span, .. }, .. }) =
37793779
&self.body[loan.reserve_location.block].terminator
3780-
&& let Some((method_did, method_args)) = rustc_middle::util::find_self_call(
3780+
&& let Some((method_did, method_args)) = mir::find_self_call(
37813781
tcx,
37823782
self.body,
37833783
loan.assigned_place.local,

compiler/rustc_borrowck/src/diagnostics/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::mir::tcx::PlaceTy;
1717
use rustc_middle::mir::{
1818
AggregateKind, CallSource, ConstOperand, ConstraintCategory, FakeReadCause, Local, LocalInfo,
1919
LocalKind, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement,
20-
StatementKind, Terminator, TerminatorKind,
20+
StatementKind, Terminator, TerminatorKind, find_self_call,
2121
};
2222
use rustc_middle::ty::print::Print;
2323
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -1016,12 +1016,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
10161016
kind: TerminatorKind::Call { fn_span, call_source, .. }, ..
10171017
}) = &self.body[location.block].terminator
10181018
{
1019-
let Some((method_did, method_args)) = rustc_middle::util::find_self_call(
1020-
self.infcx.tcx,
1021-
self.body,
1022-
target_temp,
1023-
location.block,
1024-
) else {
1019+
let Some((method_did, method_args)) =
1020+
find_self_call(self.infcx.tcx, self.body, target_temp, location.block)
1021+
else {
10251022
return normal_ret;
10261023
};
10271024

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub mod sync;
7676
pub mod tagged_ptr;
7777
pub mod temp_dir;
7878
pub mod thinvec;
79+
pub mod thousands;
7980
pub mod transitive_relation;
8081
pub mod unhash;
8182
pub mod unord;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! This is an extremely bare-bones alternative to the `thousands` crate on
2+
//! crates.io, for printing large numbers in a readable fashion.
3+
4+
#[cfg(test)]
5+
mod tests;
6+
7+
// Converts the number to a string, with underscores as the thousands separator.
8+
pub fn format_with_underscores(n: usize) -> String {
9+
let mut s = n.to_string();
10+
let mut i = s.len();
11+
while i > 3 {
12+
i -= 3;
13+
s.insert(i, '_');
14+
}
15+
s
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use super::*;
2+
3+
#[test]
4+
fn test_format_with_underscores() {
5+
assert_eq!("0", format_with_underscores(0));
6+
assert_eq!("1", format_with_underscores(1));
7+
assert_eq!("99", format_with_underscores(99));
8+
assert_eq!("345", format_with_underscores(345));
9+
assert_eq!("1_000", format_with_underscores(1_000));
10+
assert_eq!("12_001", format_with_underscores(12_001));
11+
assert_eq!("999_999", format_with_underscores(999_999));
12+
assert_eq!("1_000_000", format_with_underscores(1_000_000));
13+
assert_eq!("12_345_678", format_with_underscores(12_345_678));
14+
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
99
use rustc_data_structures::memmap::{Mmap, MmapMut};
1010
use rustc_data_structures::sync::{Lrc, join, par_for_each_in};
1111
use rustc_data_structures::temp_dir::MaybeTempDir;
12+
use rustc_data_structures::thousands::format_with_underscores;
1213
use rustc_feature::Features;
1314
use rustc_hir as hir;
1415
use rustc_hir::def_id::{CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE, LocalDefId, LocalDefIdSet};
@@ -22,7 +23,6 @@ use rustc_middle::traits::specialization_graph;
2223
use rustc_middle::ty::codec::TyEncoder;
2324
use rustc_middle::ty::fast_reject::{self, TreatParams};
2425
use rustc_middle::ty::{AssocItemContainer, SymbolName};
25-
use rustc_middle::util::common::to_readable_str;
2626
use rustc_middle::{bug, span_bug};
2727
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque};
2828
use rustc_session::config::{CrateType, OptLevel};
@@ -782,7 +782,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
782782
"{} {:<23}{:>10} ({:4.1}%)",
783783
prefix,
784784
label,
785-
to_readable_str(size),
785+
format_with_underscores(size),
786786
perc(size)
787787
);
788788
}
@@ -791,7 +791,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
791791
"{} {:<23}{:>10} (of which {:.1}% are zero bytes)",
792792
prefix,
793793
"Total",
794-
to_readable_str(total_bytes),
794+
format_with_underscores(total_bytes),
795795
perc(zero_bytes)
796796
);
797797
eprintln!("{prefix}");

compiler/rustc_middle/src/macros.rs

+8-23
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
///
55
/// If you have a span available, you should use [`span_bug`] instead.
66
///
7-
/// If the bug should only be emitted when compilation didn't fail, [`DiagCtxtHandle::span_delayed_bug`]
8-
/// may be useful.
7+
/// If the bug should only be emitted when compilation didn't fail,
8+
/// [`DiagCtxtHandle::span_delayed_bug`] may be useful.
99
///
1010
/// [`DiagCtxtHandle::span_delayed_bug`]: rustc_errors::DiagCtxtHandle::span_delayed_bug
1111
/// [`span_bug`]: crate::span_bug
@@ -14,14 +14,8 @@ macro_rules! bug {
1414
() => (
1515
$crate::bug!("impossible case reached")
1616
);
17-
($msg:expr) => (
18-
$crate::util::bug::bug_fmt(::std::format_args!($msg))
19-
);
20-
($msg:expr,) => (
21-
$crate::bug!($msg)
22-
);
23-
($fmt:expr, $($arg:tt)+) => (
24-
$crate::util::bug::bug_fmt(::std::format_args!($fmt, $($arg)+))
17+
($($arg:tt)+) => (
18+
$crate::util::bug::bug_fmt(::std::format_args!($($arg)+))
2519
);
2620
}
2721

@@ -30,20 +24,14 @@ macro_rules! bug {
3024
/// at the code the compiler was compiling when it ICEd. This is the preferred way to trigger
3125
/// ICEs.
3226
///
33-
/// If the bug should only be emitted when compilation didn't fail, [`DiagCtxtHandle::span_delayed_bug`]
34-
/// may be useful.
27+
/// If the bug should only be emitted when compilation didn't fail,
28+
/// [`DiagCtxtHandle::span_delayed_bug`] may be useful.
3529
///
3630
/// [`DiagCtxtHandle::span_delayed_bug`]: rustc_errors::DiagCtxtHandle::span_delayed_bug
3731
#[macro_export]
3832
macro_rules! span_bug {
39-
($span:expr, $msg:expr) => (
40-
$crate::util::bug::span_bug_fmt($span, ::std::format_args!($msg))
41-
);
42-
($span:expr, $msg:expr,) => (
43-
$crate::span_bug!($span, $msg)
44-
);
45-
($span:expr, $fmt:expr, $($arg:tt)+) => (
46-
$crate::util::bug::span_bug_fmt($span, ::std::format_args!($fmt, $($arg)+))
33+
($span:expr, $($arg:tt)+) => (
34+
$crate::util::bug::span_bug_fmt($span, ::std::format_args!($($arg)+))
4735
);
4836
}
4937

@@ -53,7 +41,6 @@ macro_rules! span_bug {
5341
// When possible, use one of these (relatively) convenient macros to write
5442
// the impls for you.
5543

56-
#[macro_export]
5744
macro_rules! TrivialLiftImpls {
5845
($($ty:ty),+ $(,)?) => {
5946
$(
@@ -69,7 +56,6 @@ macro_rules! TrivialLiftImpls {
6956

7057
/// Used for types that are `Copy` and which **do not care about arena
7158
/// allocated data** (i.e., don't need to be folded).
72-
#[macro_export]
7359
macro_rules! TrivialTypeTraversalImpls {
7460
($($ty:ty),+ $(,)?) => {
7561
$(
@@ -104,7 +90,6 @@ macro_rules! TrivialTypeTraversalImpls {
10490
};
10591
}
10692

107-
#[macro_export]
10893
macro_rules! TrivialTypeTraversalAndLiftImpls {
10994
($($t:tt)*) => {
11095
TrivialTypeTraversalImpls! { $($t)* }

compiler/rustc_middle/src/mir/mod.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisit
2727
use rustc_serialize::{Decodable, Encodable};
2828
use rustc_span::source_map::Spanned;
2929
use rustc_span::{DUMMY_SP, Span, Symbol};
30-
use tracing::trace;
30+
use tracing::{debug, trace};
3131

3232
pub use self::query::*;
3333
use self::visit::TyContext;
@@ -1796,6 +1796,47 @@ impl DefLocation {
17961796
}
17971797
}
17981798

1799+
/// Checks if the specified `local` is used as the `self` parameter of a method call
1800+
/// in the provided `BasicBlock`. If it is, then the `DefId` of the called method is
1801+
/// returned.
1802+
pub fn find_self_call<'tcx>(
1803+
tcx: TyCtxt<'tcx>,
1804+
body: &Body<'tcx>,
1805+
local: Local,
1806+
block: BasicBlock,
1807+
) -> Option<(DefId, GenericArgsRef<'tcx>)> {
1808+
debug!("find_self_call(local={:?}): terminator={:?}", local, body[block].terminator);
1809+
if let Some(Terminator { kind: TerminatorKind::Call { func, args, .. }, .. }) =
1810+
&body[block].terminator
1811+
&& let Operand::Constant(box ConstOperand { const_, .. }) = func
1812+
&& let ty::FnDef(def_id, fn_args) = *const_.ty().kind()
1813+
&& let Some(ty::AssocItem { fn_has_self_parameter: true, .. }) =
1814+
tcx.opt_associated_item(def_id)
1815+
&& let [Spanned { node: Operand::Move(self_place) | Operand::Copy(self_place), .. }, ..] =
1816+
**args
1817+
{
1818+
if self_place.as_local() == Some(local) {
1819+
return Some((def_id, fn_args));
1820+
}
1821+
1822+
// Handle the case where `self_place` gets reborrowed.
1823+
// This happens when the receiver is `&T`.
1824+
for stmt in &body[block].statements {
1825+
if let StatementKind::Assign(box (place, rvalue)) = &stmt.kind
1826+
&& let Some(reborrow_local) = place.as_local()
1827+
&& self_place.as_local() == Some(reborrow_local)
1828+
&& let Rvalue::Ref(_, _, deref_place) = rvalue
1829+
&& let PlaceRef { local: deref_local, projection: [ProjectionElem::Deref] } =
1830+
deref_place.as_ref()
1831+
&& deref_local == local
1832+
{
1833+
return Some((def_id, fn_args));
1834+
}
1835+
}
1836+
}
1837+
None
1838+
}
1839+
17991840
// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
18001841
#[cfg(target_pointer_width = "64")]
18011842
mod size_asserts {

compiler/rustc_middle/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ pub enum IsConstable {
428428
Ctor,
429429
}
430430

431-
crate::TrivialTypeTraversalAndLiftImpls! {
431+
TrivialTypeTraversalAndLiftImpls! {
432432
IsConstable,
433433
}
434434

compiler/rustc_middle/src/util/bug.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// These functions are used by macro expansion for bug! and span_bug!
1+
// These functions are used by macro expansion for `bug!` and `span_bug!`.
22

33
use std::fmt;
44
use std::panic::{Location, panic_any};
@@ -8,15 +8,15 @@ use rustc_span::Span;
88

99
use crate::ty::{TyCtxt, tls};
1010

11+
// This wrapper makes for more compact code at callsites than calling `opt_span_buf_fmt` directly.
1112
#[cold]
1213
#[inline(never)]
1314
#[track_caller]
1415
pub fn bug_fmt(args: fmt::Arguments<'_>) -> ! {
15-
// this wrapper mostly exists so I don't have to write a fully
16-
// qualified path of None::<Span> inside the bug!() macro definition
1716
opt_span_bug_fmt(None::<Span>, args, Location::caller());
1817
}
1918

19+
// This wrapper makes for more compact code at callsites than calling `opt_span_buf_fmt` directly.
2020
#[cold]
2121
#[inline(never)]
2222
#[track_caller]

compiler/rustc_middle/src/util/common.rs

-22
This file was deleted.

compiler/rustc_middle/src/util/common/tests.rs

-14
This file was deleted.

compiler/rustc_middle/src/util/find_self_call.rs

-47
This file was deleted.

compiler/rustc_middle/src/util/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
pub mod bug;
2-
pub mod common;
3-
pub mod find_self_call;
4-
5-
pub use find_self_call::find_self_call;
62

73
#[derive(Default, Copy, Clone)]
84
pub struct Providers {

compiler/rustc_mir_transform/src/check_const_item_mutation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'tcx> Visitor<'tcx> for ConstMutationChecker<'_, 'tcx> {
133133
// the `self` parameter of a method call (as the terminator of our current
134134
// BasicBlock). If so, we emit a more specific lint.
135135
let method_did = self.target_local.and_then(|target_local| {
136-
rustc_middle::util::find_self_call(self.tcx, self.body, target_local, loc.block)
136+
find_self_call(self.tcx, self.body, target_local, loc.block)
137137
});
138138
let lint_loc =
139139
if method_did.is_some() { self.body.terminator_loc(loc.block) } else { loc };

0 commit comments

Comments
 (0)