MirPass: make name more const
This commit is contained in:
parent
94df917a74
commit
c5e6df0c78
4 changed files with 25 additions and 12 deletions
|
@ -30,6 +30,7 @@
|
||||||
#![feature(assert_matches)]
|
#![feature(assert_matches)]
|
||||||
#![feature(box_patterns)]
|
#![feature(box_patterns)]
|
||||||
#![feature(core_intrinsics)]
|
#![feature(core_intrinsics)]
|
||||||
|
#![feature(const_type_name)]
|
||||||
#![feature(discriminant_kind)]
|
#![feature(discriminant_kind)]
|
||||||
#![feature(exhaustive_patterns)]
|
#![feature(exhaustive_patterns)]
|
||||||
#![feature(coroutines)]
|
#![feature(coroutines)]
|
||||||
|
|
|
@ -140,8 +140,12 @@ fn to_profiler_name(type_name: &'static str) -> &'static str {
|
||||||
/// loop that goes over each available MIR and applies `run_pass`.
|
/// loop that goes over each available MIR and applies `run_pass`.
|
||||||
pub trait MirPass<'tcx> {
|
pub trait MirPass<'tcx> {
|
||||||
fn name(&self) -> &'static str {
|
fn name(&self) -> &'static str {
|
||||||
let name = std::any::type_name::<Self>();
|
// FIXME Simplify the implementation once more `str` methods get const-stable.
|
||||||
if let Some((_, tail)) = name.rsplit_once(':') { tail } else { name }
|
// See copypaste in `MirLint`
|
||||||
|
const {
|
||||||
|
let name = std::any::type_name::<Self>();
|
||||||
|
crate::util::common::c_name(name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn profiler_name(&self) -> &'static str {
|
fn profiler_name(&self) -> &'static str {
|
||||||
|
|
|
@ -65,3 +65,19 @@ pub fn indenter() -> Indenter {
|
||||||
debug!(">>");
|
debug!(">>");
|
||||||
Indenter { _cannot_construct_outside_of_this_module: () }
|
Indenter { _cannot_construct_outside_of_this_module: () }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// const wrapper for `if let Some((_, tail)) = name.rsplit_once(':') { tail } else { name }`
|
||||||
|
pub const fn c_name(name: &'static str) -> &'static str {
|
||||||
|
// FIXME Simplify the implementation once more `str` methods get const-stable.
|
||||||
|
// and inline into call site
|
||||||
|
let bytes = name.as_bytes();
|
||||||
|
let mut i = bytes.len();
|
||||||
|
while i > 0 && bytes[i - 1] != b':' {
|
||||||
|
i = i - 1;
|
||||||
|
}
|
||||||
|
let (_, bytes) = bytes.split_at(i);
|
||||||
|
match std::str::from_utf8(bytes) {
|
||||||
|
Ok(name) => name,
|
||||||
|
Err(_) => name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -8,18 +8,10 @@ use crate::{lint::lint_body, validate, MirPass};
|
||||||
pub trait MirLint<'tcx> {
|
pub trait MirLint<'tcx> {
|
||||||
fn name(&self) -> &'static str {
|
fn name(&self) -> &'static str {
|
||||||
// FIXME Simplify the implementation once more `str` methods get const-stable.
|
// FIXME Simplify the implementation once more `str` methods get const-stable.
|
||||||
|
// See copypaste in `MirPass`
|
||||||
const {
|
const {
|
||||||
let name = std::any::type_name::<Self>();
|
let name = std::any::type_name::<Self>();
|
||||||
let bytes = name.as_bytes();
|
rustc_middle::util::common::c_name(name)
|
||||||
let mut i = bytes.len();
|
|
||||||
while i > 0 && bytes[i - 1] != b':' {
|
|
||||||
i = i - 1;
|
|
||||||
}
|
|
||||||
let (_, bytes) = bytes.split_at(i);
|
|
||||||
match std::str::from_utf8(bytes) {
|
|
||||||
Ok(name) => name,
|
|
||||||
Err(_) => name,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue