Make #[debug_format] an attribute in newtype_index

This removes the `custom` format functionality as its only user was
trivially migrated to using a normal format.

If a new use case for a custom formatting impl pops up, you can add it
back.
This commit is contained in:
Nilstrieb 2022-12-18 21:37:38 +01:00
parent 91c3c2040c
commit d679764fb6
24 changed files with 64 additions and 92 deletions

View File

@ -2555,8 +2555,8 @@ pub enum AttrStyle {
rustc_index::newtype_index! {
#[custom_encodable]
#[debug_format = "AttrId({})]"]
pub struct AttrId {
DEBUG_FORMAT = "AttrId({})"
}
}

View File

@ -8,8 +8,8 @@ rustc_index::newtype_index! {
/// This is later turned into [`DefId`] and `HirId` for the HIR.
///
/// [`DefId`]: rustc_span::def_id::DefId
#[debug_format = "NodeId({})"]
pub struct NodeId {
DEBUG_FORMAT = "NodeId({})"
}
}

View File

@ -115,13 +115,13 @@ impl<'tcx> fmt::Debug for OutlivesConstraint<'tcx> {
}
rustc_index::newtype_index! {
#[debug_format = "OutlivesConstraintIndex({})"]
pub struct OutlivesConstraintIndex {
DEBUG_FORMAT = "OutlivesConstraintIndex({})"
}
}
rustc_index::newtype_index! {
#[debug_format = "ConstraintSccIndex({})"]
pub struct ConstraintSccIndex {
DEBUG_FORMAT = "ConstraintSccIndex({})"
}
}

View File

@ -108,8 +108,8 @@ impl_visitable! {
}
rustc_index::newtype_index! {
#[debug_format = "bw{}"]
pub struct BorrowIndex {
DEBUG_FORMAT = "bw{}"
}
}

View File

@ -20,8 +20,8 @@ pub struct LocationTable {
}
rustc_index::newtype_index! {
#[debug_format = "LocationIndex({})"]
pub struct LocationIndex {
DEBUG_FORMAT = "LocationIndex({})"
}
}

View File

@ -55,8 +55,8 @@ pub(crate) struct NllMemberConstraint<'tcx> {
}
rustc_index::newtype_index! {
#[debug_format = "MemberConstraintIndex({})"]
pub(crate) struct NllMemberConstraintIndex {
DEBUG_FORMAT = "MemberConstraintIndex({})"
}
}

View File

@ -90,12 +90,14 @@ impl RegionValueElements {
rustc_index::newtype_index! {
/// A single integer representing a `Location` in the MIR control-flow
/// graph. Constructed efficiently from `RegionValueElements`.
pub struct PointIndex { DEBUG_FORMAT = "PointIndex({})" }
#[debug_format = "PointIndex({})"]
pub struct PointIndex {}
}
rustc_index::newtype_index! {
/// A single integer representing a `ty::Placeholder`.
pub struct PlaceholderIndex { DEBUG_FORMAT = "PlaceholderIndex({})" }
#[debug_format = "PlaceholderIndex({})"]
pub struct PlaceholderIndex {}
}
/// An individual element in a region value -- the value of a

View File

@ -4,14 +4,14 @@ use rustc_index::vec::IndexVec;
use rustc_middle::ty::error::TypeError;
rustc_index::newtype_index! {
#[debug_format = "ExpectedIdx({})"]
pub(crate) struct ExpectedIdx {
DEBUG_FORMAT = "ExpectedIdx({})",
}
}
rustc_index::newtype_index! {
#[debug_format = "ProvidedIdx({})"]
pub(crate) struct ProvidedIdx {
DEBUG_FORMAT = "ProvidedIdx({})",
}
}

View File

@ -96,14 +96,14 @@ fn for_each_consumable<'tcx>(hir: Map<'tcx>, place: TrackedValue, mut f: impl Fn
}
rustc_index::newtype_index! {
#[debug_format = "id({})"]
pub struct PostOrderId {
DEBUG_FORMAT = "id({})",
}
}
rustc_index::newtype_index! {
#[debug_format = "hidx({})"]
pub struct TrackedValueIndex {
DEBUG_FORMAT = "hidx({})",
}
}

View File

@ -357,14 +357,14 @@ impl<'tcx> SccUniverse<'tcx> {
}
rustc_index::newtype_index! {
#[debug_format = "LeakCheckNode({})"]
struct LeakCheckNode {
DEBUG_FORMAT = "LeakCheckNode({})"
}
}
rustc_index::newtype_index! {
#[debug_format = "LeakCheckScc({})"]
struct LeakCheckScc {
DEBUG_FORMAT = "LeakCheckScc({})"
}
}

View File

@ -3,22 +3,6 @@ use quote::quote;
use syn::parse::*;
use syn::*;
mod kw {
syn::custom_keyword!(DEBUG_FORMAT);
syn::custom_keyword!(MAX);
syn::custom_keyword!(custom);
}
#[derive(Debug)]
enum DebugFormat {
// The user will provide a custom `Debug` impl, so we shouldn't generate
// one
Custom,
// Use the specified format string in the generated `Debug` impl
// By default, this is "{}"
Format(String),
}
// We parse the input and emit the output in a single step.
// This field stores the final macro output
struct Newtype(TokenStream);
@ -35,7 +19,7 @@ impl Parse for Newtype {
// Any additional `#[derive]` macro paths to apply
let mut derive_paths: Vec<Path> = Vec::new();
let mut debug_format: Option<DebugFormat> = None;
let mut debug_format: Option<Lit> = None;
let mut max = None;
let mut consts = Vec::new();
let mut encodable = true;
@ -65,7 +49,18 @@ impl Parse for Newtype {
};
if let Some(old) = max.replace(literal.lit) {
panic!("Specified multiple MAX: {:?}", old);
panic!("Specified multiple max: {:?}", old);
}
false
}
"debug_format" => {
let Ok(Meta::NameValue(literal) )= attr.parse_meta() else {
panic!("#[debug_format = FMT] attribute requires a format");
};
if let Some(old) = debug_format.replace(literal.lit) {
panic!("Specified multiple debug format options: {:?}", old);
}
false
@ -79,23 +74,6 @@ impl Parse for Newtype {
body.parse::<Token![..]>()?;
} else {
loop {
if body.lookahead1().peek(kw::DEBUG_FORMAT) {
body.parse::<kw::DEBUG_FORMAT>()?;
body.parse::<Token![=]>()?;
let new_debug_format = if body.lookahead1().peek(kw::custom) {
body.parse::<kw::custom>()?;
DebugFormat::Custom
} else {
let format_str: LitStr = body.parse()?;
DebugFormat::Format(format_str.value())
};
try_comma()?;
if let Some(old) = debug_format.replace(new_debug_format) {
panic!("Specified multiple debug format options: {:?}", old);
}
continue;
}
// We've parsed everything that the user provided, so we're done
if body.is_empty() {
break;
@ -112,7 +90,9 @@ impl Parse for Newtype {
}
}
let debug_format = debug_format.unwrap_or(DebugFormat::Format("{}".to_string()));
let debug_format =
debug_format.unwrap_or_else(|| Lit::Str(LitStr::new("{}", Span::call_site())));
// shave off 256 indices at the end to allow space for packing these indices into enums
let max = max.unwrap_or_else(|| Lit::Int(LitInt::new("0xFFFF_FF00", Span::call_site())));
@ -167,18 +147,14 @@ impl Parse for Newtype {
quote! {}
};
let debug_impl = match debug_format {
DebugFormat::Custom => quote! {},
DebugFormat::Format(format) => {
quote! {
impl ::std::fmt::Debug for #name {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(fmt, #format, self.as_u32())
}
}
let debug_impl = quote! {
impl ::std::fmt::Debug for #name {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(fmt, #debug_format, self.as_u32())
}
}
};
let spec_partial_eq_impl = if let Lit::Int(max) = &max {
if let Ok(max_val) = max.base10_parse::<u32>() {
quote! {

View File

@ -12,8 +12,8 @@ rustc_index::newtype_index! {
/// constant value of `0`.
#[derive(HashStable)]
#[max = 0xFFFF_FFFF]
#[debug_format = "ExpressionOperandId({})"]
pub struct ExpressionOperandId {
DEBUG_FORMAT = "ExpressionOperandId({})",
}
}
@ -34,8 +34,8 @@ impl ExpressionOperandId {
rustc_index::newtype_index! {
#[derive(HashStable)]
#[max = 0xFFFF_FFFF]
#[debug_format = "CounterValueReference({})"]
pub struct CounterValueReference {
DEBUG_FORMAT = "CounterValueReference({})",
}
}
@ -58,8 +58,8 @@ rustc_index::newtype_index! {
/// Values descend from u32::MAX.
#[derive(HashStable)]
#[max = 0xFFFF_FFFF]
#[debug_format = "InjectedExpressionId({})"]
pub struct InjectedExpressionId {
DEBUG_FORMAT = "InjectedExpressionId({})",
}
}
@ -69,8 +69,8 @@ rustc_index::newtype_index! {
/// Values ascend from 0.
#[derive(HashStable)]
#[max = 0xFFFF_FFFF]
#[debug_format = "InjectedExpressionIndex({})"]
pub struct InjectedExpressionIndex {
DEBUG_FORMAT = "InjectedExpressionIndex({})",
}
}
@ -80,8 +80,8 @@ rustc_index::newtype_index! {
/// "mapgen" process. They cannot be computed algorithmically, from the other `newtype_index`s.
#[derive(HashStable)]
#[max = 0xFFFF_FFFF]
#[debug_format = "MappedExpressionIndex({})"]
pub struct MappedExpressionIndex {
DEBUG_FORMAT = "MappedExpressionIndex({})",
}
}

View File

@ -655,8 +655,8 @@ impl SourceInfo {
rustc_index::newtype_index! {
#[derive(HashStable)]
#[debug_format = "_{}"]
pub struct Local {
DEBUG_FORMAT = "_{}",
const RETURN_PLACE = 0,
}
}
@ -1147,8 +1147,8 @@ rustc_index::newtype_index! {
/// [`CriticalCallEdges`]: ../../rustc_const_eval/transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
/// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
#[derive(HashStable)]
#[debug_format = "bb{}"]
pub struct BasicBlock {
DEBUG_FORMAT = "bb{}",
const START_BLOCK = 0,
}
}
@ -1531,8 +1531,8 @@ rustc_index::newtype_index! {
/// [CFG]: https://rustc-dev-guide.rust-lang.org/appendix/background.html#cfg
/// [mir-datatypes]: https://rustc-dev-guide.rust-lang.org/mir/index.html#mir-data-types
#[derive(HashStable)]
#[debug_format = "field[{}]"]
pub struct Field {
DEBUG_FORMAT = "field[{}]"
}
}
@ -1758,8 +1758,8 @@ impl Debug for Place<'_> {
rustc_index::newtype_index! {
#[derive(HashStable)]
#[debug_format = "scope[{}]"]
pub struct SourceScope {
DEBUG_FORMAT = "scope[{}]",
const OUTERMOST_SOURCE_SCOPE = 0,
}
}
@ -2756,8 +2756,8 @@ impl<'tcx> TypeVisitable<'tcx> for UserTypeProjection {
rustc_index::newtype_index! {
#[derive(HashStable)]
#[debug_format = "promoted[{}]"]
pub struct Promoted {
DEBUG_FORMAT = "promoted[{}]"
}
}

View File

@ -131,8 +131,8 @@ pub struct UnsafetyCheckResult {
rustc_index::newtype_index! {
#[derive(HashStable)]
#[debug_format = "_{}"]
pub struct GeneratorSavedLocal {
DEBUG_FORMAT = "_{}",
}
}

View File

@ -35,8 +35,8 @@ macro_rules! thir_with_elements {
$(
newtype_index! {
#[derive(HashStable)]
#[debug_format = $format]
pub struct $id {
DEBUG_FORMAT = $format
}
}
)*

View File

@ -99,12 +99,6 @@ impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
}
}
impl fmt::Debug for ty::RegionVid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "'_#{}r", self.index())
}
}
impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
with_no_trimmed_paths!(fmt::Display::fmt(self, f))

View File

@ -1378,8 +1378,8 @@ pub struct ConstVid<'tcx> {
rustc_index::newtype_index! {
/// A **region** (lifetime) **v**ariable **ID**.
#[derive(HashStable)]
#[debug_format = "'_#{}r"]
pub struct RegionVid {
DEBUG_FORMAT = custom,
}
}

View File

@ -609,8 +609,8 @@ impl<'a, V> LocalTableInContextMut<'a, V> {
rustc_index::newtype_index! {
#[derive(HashStable)]
#[debug_format = "UserType({})"]
pub struct UserTypeAnnotationIndex {
DEBUG_FORMAT = "UserType({})",
const START_INDEX = 0,
}
}

View File

@ -14,8 +14,8 @@ use self::abs_domain::{AbstractElem, Lift};
mod abs_domain;
rustc_index::newtype_index! {
#[debug_format = "mp{}"]
pub struct MovePathIndex {
DEBUG_FORMAT = "mp{}"
}
}
@ -26,14 +26,14 @@ impl polonius_engine::Atom for MovePathIndex {
}
rustc_index::newtype_index! {
#[debug_format = "mo{}"]
pub struct MoveOutIndex {
DEBUG_FORMAT = "mo{}"
}
}
rustc_index::newtype_index! {
#[debug_format = "in{}"]
pub struct InitIndex {
DEBUG_FORMAT = "in{}"
}
}

View File

@ -282,8 +282,8 @@ impl graph::WithPredecessors for CoverageGraph {
rustc_index::newtype_index! {
/// A node in the control-flow graph of CoverageGraph.
#[debug_format = "bcb{}"]
pub(super) struct BasicCoverageBlock {
DEBUG_FORMAT = "bcb{}",
const START_BCB = 0,
}
}

View File

@ -108,14 +108,14 @@ use std::rc::Rc;
mod rwu_table;
rustc_index::newtype_index! {
#[debug_format = "v({})"]
pub struct Variable {
DEBUG_FORMAT = "v({})",
}
}
rustc_index::newtype_index! {
#[debug_format = "ln({})"]
pub struct LiveNode {
DEBUG_FORMAT = "ln({})",
}
}

View File

@ -11,8 +11,8 @@ use std::hash::{Hash, Hasher};
rustc_index::newtype_index! {
#[custom_encodable]
#[debug_format = "crate{}"]
pub struct CrateNum {
DEBUG_FORMAT = "crate{}"
}
}
@ -195,8 +195,8 @@ rustc_index::newtype_index! {
/// particular definition. It should really be considered an interned
/// shorthand for a particular DefPath.
#[custom_encodable] // (only encodable in metadata)
#[debug_format = "DefIndex({})"]
pub struct DefIndex {
DEBUG_FORMAT = "DefIndex({})",
/// The crate root is always assigned index 0 by the AST Map code,
/// thanks to `NodeCollector::new`.
const CRATE_DEF_INDEX = 0,

View File

@ -84,8 +84,8 @@ rustc_index::newtype_index! {
/// A unique ID associated with a macro invocation and expansion.
#[custom_encodable]
#[no_ord_impl]
#[debug_format = "expn{}"]
pub struct LocalExpnId {
DEBUG_FORMAT = "expn{}"
}
}

View File

@ -301,8 +301,8 @@ rustc_index::newtype_index! {
///
/// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
#[derive(HashStable_Generic)]
#[debug_format = "DebruijnIndex({})"]
pub struct DebruijnIndex {
DEBUG_FORMAT = "DebruijnIndex({})",
const INNERMOST = 0,
}
}
@ -499,8 +499,8 @@ pub struct FloatVarValue(pub FloatTy);
rustc_index::newtype_index! {
/// A **ty**pe **v**ariable **ID**.
#[debug_format = "_#{}t"]
pub struct TyVid {
DEBUG_FORMAT = "_#{}t"
}
}
@ -788,8 +788,8 @@ rustc_index::newtype_index! {
/// type -- an idealized representative of "types in general" that we
/// use for checking generic functions.
#[derive(HashStable_Generic)]
#[debug_format = "U{}"]
pub struct UniverseIndex {
DEBUG_FORMAT = "U{}",
}
}