Skip to content

Rollup of 5 pull requests #105909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
Noratrieb committed Dec 18, 2022
commit d679764fb6b84a5cb72f6a9c9fefce4c6b41f16c
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2555,8 +2555,8 @@ pub enum AttrStyle {

rustc_index::newtype_index! {
#[custom_encodable]
#[debug_format = "AttrId({})]"]
pub struct AttrId {
DEBUG_FORMAT = "AttrId({})"
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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({})"
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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({})"
}
}
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ impl_visitable! {
}

rustc_index::newtype_index! {
#[debug_format = "bw{}"]
pub struct BorrowIndex {
DEBUG_FORMAT = "bw{}"
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub struct LocationTable {
}

rustc_index::newtype_index! {
#[debug_format = "LocationIndex({})"]
pub struct LocationIndex {
DEBUG_FORMAT = "LocationIndex({})"
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/member_constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub(crate) struct NllMemberConstraint<'tcx> {
}

rustc_index::newtype_index! {
#[debug_format = "MemberConstraintIndex({})"]
pub(crate) struct NllMemberConstraintIndex {
DEBUG_FORMAT = "MemberConstraintIndex({})"
}
}

Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_borrowck/src/region_infer/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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({})",
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({})",
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({})"
}
}

Expand Down
66 changes: 21 additions & 45 deletions compiler/rustc_macros/src/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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())));

Expand Down Expand Up @@ -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! {
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_middle/src/mir/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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({})",
}
}

Expand All @@ -34,8 +34,8 @@ impl ExpressionOperandId {
rustc_index::newtype_index! {
#[derive(HashStable)]
#[max = 0xFFFF_FFFF]
#[debug_format = "CounterValueReference({})"]
pub struct CounterValueReference {
DEBUG_FORMAT = "CounterValueReference({})",
}
}

Expand All @@ -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({})",
}
}

Expand All @@ -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({})",
}
}

Expand All @@ -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({})",
}
}

Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@ impl SourceInfo {

rustc_index::newtype_index! {
#[derive(HashStable)]
#[debug_format = "_{}"]
pub struct Local {
DEBUG_FORMAT = "_{}",
const RETURN_PLACE = 0,
}
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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[{}]"
}
}

Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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[{}]"
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ pub struct UnsafetyCheckResult {

rustc_index::newtype_index! {
#[derive(HashStable)]
#[debug_format = "_{}"]
pub struct GeneratorSavedLocal {
DEBUG_FORMAT = "_{}",
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ macro_rules! thir_with_elements {
$(
newtype_index! {
#[derive(HashStable)]
#[debug_format = $format]
pub struct $id {
DEBUG_FORMAT = $format
}
}
)*
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_middle/src/ty/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/typeck_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
Loading