Skip to content

Commit 9ef7cd9

Browse files
committed
pretty print hir attributes
1 parent e6e5c8f commit 9ef7cd9

File tree

11 files changed

+433
-31
lines changed

11 files changed

+433
-31
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,6 +3400,7 @@ version = "0.0.0"
34003400
dependencies = [
34013401
"rustc_abi",
34023402
"rustc_ast",
3403+
"rustc_ast_pretty",
34033404
"rustc_data_structures",
34043405
"rustc_macros",
34053406
"rustc_serialize",

compiler/rustc_attr_data_structures/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
# tidy-alphabetical-start
88
rustc_abi = {path = "../rustc_abi"}
99
rustc_ast = {path = "../rustc_ast"}
10+
rustc_ast_pretty = {path = "../rustc_ast_pretty"}
1011
rustc_data_structures = {path = "../rustc_data_structures"}
1112
rustc_macros = {path = "../rustc_macros"}
1213
rustc_serialize = {path = "../rustc_serialize"}

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use rustc_abi::Align;
22
use rustc_ast::token::CommentKind;
33
use rustc_ast::{self as ast, AttrStyle};
4-
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
4+
use rustc_macros::{Decodable, Encodable, HashStable_Generic, PrintAttribute};
55
use rustc_span::hygiene::Transparency;
66
use rustc_span::{Span, Symbol};
77
use thin_vec::ThinVec;
88

9-
use crate::{DefaultBodyStability, PartialConstStability, RustcVersion, Stability};
9+
use crate::{DefaultBodyStability, PartialConstStability, PrintAttribute, RustcVersion, Stability};
1010

1111
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
1212
pub enum InlineAttr {
@@ -45,15 +45,15 @@ pub enum OptimizeAttr {
4545
Size,
4646
}
4747

48-
#[derive(Clone, Debug, Encodable, Decodable, HashStable_Generic)]
48+
#[derive(Clone, Debug, Encodable, Decodable, HashStable_Generic, PrintAttribute)]
4949
pub enum DiagnosticAttribute {
5050
// tidy-alphabetical-start
5151
DoNotRecommend,
5252
OnUnimplemented,
5353
// tidy-alphabetical-end
5454
}
5555

56-
#[derive(PartialEq, Debug, Encodable, Decodable, Copy, Clone, HashStable_Generic)]
56+
#[derive(PartialEq, Debug, Encodable, Decodable, Copy, Clone, HashStable_Generic, PrintAttribute)]
5757
pub enum ReprAttr {
5858
ReprInt(IntType),
5959
ReprRust,
@@ -73,13 +73,13 @@ pub enum TransparencyError {
7373
}
7474

7575
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
76-
#[derive(Encodable, Decodable, HashStable_Generic)]
76+
#[derive(Encodable, Decodable, HashStable_Generic, PrintAttribute)]
7777
pub enum IntType {
7878
SignedInt(ast::IntTy),
7979
UnsignedInt(ast::UintTy),
8080
}
8181

82-
#[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic)]
82+
#[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic, PrintAttribute)]
8383
pub struct Deprecation {
8484
pub since: DeprecatedSince,
8585
/// The note to issue a reason.
@@ -91,7 +91,7 @@ pub struct Deprecation {
9191
}
9292

9393
/// Release in which an API is deprecated.
94-
#[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic)]
94+
#[derive(Copy, Debug, Encodable, Decodable, Clone, HashStable_Generic, PrintAttribute)]
9595
pub enum DeprecatedSince {
9696
RustcVersion(RustcVersion),
9797
/// Deprecated in the future ("to be determined").
@@ -142,7 +142,7 @@ impl Deprecation {
142142
/// happen.
143143
///
144144
/// For more docs, look in [`rustc_attr`](https://doc.rust-lang.org/stable/nightly-rustc/rustc_attr/index.html)
145-
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable)]
145+
#[derive(Clone, Debug, HashStable_Generic, Encodable, Decodable, PrintAttribute)]
146146
pub enum AttributeKind {
147147
// tidy-alphabetical-start
148148
AllowConstFnUnstable(ThinVec<Symbol>),

compiler/rustc_attr_data_structures/src/lib.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,136 @@ mod attributes;
1010
mod stability;
1111
mod version;
1212

13+
use std::num::NonZero;
14+
1315
pub use attributes::*;
16+
use rustc_abi::Align;
17+
use rustc_ast::token::CommentKind;
18+
use rustc_ast::{AttrStyle, IntTy, UintTy};
19+
use rustc_ast_pretty::pp::Printer;
20+
use rustc_span::hygiene::Transparency;
21+
use rustc_span::{Span, Symbol};
1422
pub use stability::*;
23+
use thin_vec::ThinVec;
1524
pub use version::*;
1625

1726
/// Requirements for a `StableHashingContext` to be used in this crate.
1827
/// This is a hack to allow using the `HashStable_Generic` derive macro
1928
/// instead of implementing everything in `rustc_middle`.
2029
pub trait HashStableContext: rustc_ast::HashStableContext + rustc_abi::HashStableContext {}
30+
31+
pub trait PrintAttribute {
32+
fn print_something(&self) -> bool;
33+
fn print_attribute(&self, p: &mut Printer);
34+
}
35+
36+
impl<T: PrintAttribute> PrintAttribute for &T {
37+
fn print_something(&self) -> bool {
38+
T::print_something(self)
39+
}
40+
41+
fn print_attribute(&self, p: &mut Printer) {
42+
T::print_attribute(self, p)
43+
}
44+
}
45+
impl<T: PrintAttribute> PrintAttribute for Option<T> {
46+
fn print_something(&self) -> bool {
47+
self.as_ref().is_some_and(|x| x.print_something())
48+
}
49+
fn print_attribute(&self, p: &mut Printer) {
50+
if let Some(i) = self {
51+
T::print_attribute(i, p)
52+
}
53+
}
54+
}
55+
impl<T: PrintAttribute> PrintAttribute for ThinVec<T> {
56+
fn print_something(&self) -> bool {
57+
self.is_empty() || self[0].print_something()
58+
}
59+
fn print_attribute(&self, p: &mut Printer) {
60+
let mut last_printed = false;
61+
p.word("[");
62+
for i in self {
63+
if last_printed {
64+
p.word_space(",");
65+
}
66+
i.print_attribute(p);
67+
last_printed = i.print_something();
68+
}
69+
p.word("]");
70+
}
71+
}
72+
macro_rules! print_skip {
73+
($($t: ty),* $(,)?) => {$(
74+
impl PrintAttribute for $t {
75+
fn print_something(&self) -> bool { false }
76+
fn print_attribute(&self, _: &mut Printer) { }
77+
})*
78+
};
79+
}
80+
81+
macro_rules! print_disp {
82+
($($t: ty),* $(,)?) => {$(
83+
impl PrintAttribute for $t {
84+
fn print_something(&self) -> bool { true }
85+
fn print_attribute(&self, p: &mut Printer) {
86+
p.word(format!("{}", self));
87+
}
88+
}
89+
)*};
90+
}
91+
macro_rules! print_debug {
92+
($($t: ty),* $(,)?) => {$(
93+
impl PrintAttribute for $t {
94+
fn print_something(&self) -> bool { true }
95+
fn print_attribute(&self, p: &mut Printer) {
96+
p.word(format!("{:?}", self));
97+
}
98+
}
99+
)*};
100+
}
101+
102+
macro_rules! print_tup {
103+
(num_print_something $($ts: ident)*) => { 0 $(+ $ts.print_something() as usize)* };
104+
() => {};
105+
($t: ident $($ts: ident)*) => {
106+
#[allow(non_snake_case, unused)]
107+
impl<$t: PrintAttribute, $($ts: PrintAttribute),*> PrintAttribute for ($t, $($ts),*) {
108+
fn print_something(&self) -> bool {
109+
let ($t, $($ts),*) = self;
110+
print_tup!(num_print_something $t $($ts)*) != 0
111+
}
112+
113+
fn print_attribute(&self, p: &mut Printer) {
114+
let ($t, $($ts),*) = self;
115+
let parens = print_tup!(num_print_something $t $($ts)*) > 1;
116+
if parens {
117+
p.word("(");
118+
}
119+
120+
let mut printed_anything = $t.print_something();
121+
122+
$t.print_attribute(p);
123+
124+
$(
125+
if printed_anything && $ts.print_something() {
126+
p.word_space(",");
127+
printed_anything = true;
128+
}
129+
$ts.print_attribute(p);
130+
)*
131+
132+
if parens {
133+
p.word(")");
134+
}
135+
}
136+
}
137+
138+
print_tup!($($ts)*);
139+
};
140+
}
141+
142+
print_tup!(A B C D E F G H);
143+
print_skip!(Span, ());
144+
print_disp!(Symbol, u16, bool, NonZero<u32>);
145+
print_debug!(UintTy, IntTy, Align, AttrStyle, CommentKind, Transparency);

0 commit comments

Comments
 (0)