Skip to content

Commit 1f7f4e1

Browse files
authored
Rollup merge of #120733 - klensy:trait-const-fn, r=oli-obk
MirPass: make name more const Continues #120161, this time applied to `MirPass` instead of `MirLint`, locally shaves few (very few) instructions off. r? ``@cjgillot``
2 parents dacdd1a + c5e6df0 commit 1f7f4e1

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

Diff for: compiler/rustc_middle/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#![feature(assert_matches)]
3131
#![feature(box_patterns)]
3232
#![feature(core_intrinsics)]
33+
#![feature(const_type_name)]
3334
#![feature(discriminant_kind)]
3435
#![feature(exhaustive_patterns)]
3536
#![feature(coroutines)]

Diff for: compiler/rustc_middle/src/mir/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ fn to_profiler_name(type_name: &'static str) -> &'static str {
140140
/// loop that goes over each available MIR and applies `run_pass`.
141141
pub trait MirPass<'tcx> {
142142
fn name(&self) -> &'static str {
143-
let name = std::any::type_name::<Self>();
144-
if let Some((_, tail)) = name.rsplit_once(':') { tail } else { name }
143+
// FIXME Simplify the implementation once more `str` methods get const-stable.
144+
// See copypaste in `MirLint`
145+
const {
146+
let name = std::any::type_name::<Self>();
147+
crate::util::common::c_name(name)
148+
}
145149
}
146150

147151
fn profiler_name(&self) -> &'static str {

Diff for: compiler/rustc_middle/src/util/common.rs

+16
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,19 @@ pub fn indenter() -> Indenter {
6565
debug!(">>");
6666
Indenter { _cannot_construct_outside_of_this_module: () }
6767
}
68+
69+
// const wrapper for `if let Some((_, tail)) = name.rsplit_once(':') { tail } else { name }`
70+
pub const fn c_name(name: &'static str) -> &'static str {
71+
// FIXME Simplify the implementation once more `str` methods get const-stable.
72+
// and inline into call site
73+
let bytes = name.as_bytes();
74+
let mut i = bytes.len();
75+
while i > 0 && bytes[i - 1] != b':' {
76+
i = i - 1;
77+
}
78+
let (_, bytes) = bytes.split_at(i);
79+
match std::str::from_utf8(bytes) {
80+
Ok(name) => name,
81+
Err(_) => name,
82+
}
83+
}

Diff for: compiler/rustc_mir_transform/src/pass_manager.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,10 @@ use crate::{lint::lint_body, validate, MirPass};
88
pub trait MirLint<'tcx> {
99
fn name(&self) -> &'static str {
1010
// FIXME Simplify the implementation once more `str` methods get const-stable.
11+
// See copypaste in `MirPass`
1112
const {
1213
let name = std::any::type_name::<Self>();
13-
let bytes = name.as_bytes();
14-
let mut i = bytes.len();
15-
while i > 0 && bytes[i - 1] != b':' {
16-
i = i - 1;
17-
}
18-
let (_, bytes) = bytes.split_at(i);
19-
match std::str::from_utf8(bytes) {
20-
Ok(name) => name,
21-
Err(_) => name,
22-
}
14+
rustc_middle::util::common::c_name(name)
2315
}
2416
}
2517

0 commit comments

Comments
 (0)