Skip to content

Commit 538265a

Browse files
committed
pretty print hir::Attribute correctly
1 parent 17fd8aa commit 538265a

File tree

2 files changed

+101
-3
lines changed

2 files changed

+101
-3
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ fn binop_to_string(op: BinOpToken) -> &'static str {
358358
}
359359
}
360360

361-
fn doc_comment_to_string(
361+
pub fn doc_comment_to_string(
362362
comment_kind: CommentKind,
363363
attr_style: ast::AttrStyle,
364364
data: Symbol,

compiler/rustc_hir_pretty/src/lib.rs

+100-2
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,109 @@ impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
7171
pub struct State<'a> {
7272
pub s: pp::Printer,
7373
comments: Option<Comments<'a>>,
74-
attrs: &'a dyn Fn(HirId) -> &'a [ast::Attribute],
74+
attrs: &'a dyn Fn(HirId) -> &'a [hir::Attribute],
7575
ann: &'a (dyn PpAnn + 'a),
7676
}
7777

7878
impl<'a> State<'a> {
79-
fn attrs(&self, id: HirId) -> &'a [ast::Attribute] {
79+
fn attrs(&self, id: HirId) -> &'a [hir::Attribute] {
8080
(self.attrs)(id)
8181
}
8282

83+
fn print_inner_attributes(&mut self, attrs: &[hir::Attribute]) -> bool {
84+
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
85+
}
86+
87+
fn print_outer_attributes(&mut self, attrs: &[hir::Attribute]) -> bool {
88+
self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
89+
}
90+
91+
fn print_either_attributes(
92+
&mut self,
93+
attrs: &[hir::Attribute],
94+
kind: ast::AttrStyle,
95+
is_inline: bool,
96+
trailing_hardbreak: bool,
97+
) -> bool {
98+
let mut printed = false;
99+
for attr in attrs {
100+
if attr.style == kind {
101+
self.print_attribute_inline(attr, is_inline);
102+
if is_inline {
103+
self.nbsp();
104+
}
105+
printed = true;
106+
}
107+
}
108+
if printed && trailing_hardbreak && !is_inline {
109+
self.hardbreak_if_not_bol();
110+
}
111+
printed
112+
}
113+
114+
fn print_attribute_inline(&mut self, attr: &hir::Attribute, is_inline: bool) {
115+
if !is_inline {
116+
self.hardbreak_if_not_bol();
117+
}
118+
self.maybe_print_comment(attr.span.lo());
119+
match &attr.kind {
120+
hir::AttrKind::Normal(normal) => {
121+
match attr.style {
122+
ast::AttrStyle::Inner => self.word("#!["),
123+
ast::AttrStyle::Outer => self.word("#["),
124+
}
125+
self.print_attr_item(&normal, attr.span);
126+
self.word("]");
127+
}
128+
hir::AttrKind::DocComment(comment_kind, data) => {
129+
self.word(rustc_ast_pretty::pprust::state::doc_comment_to_string(
130+
*comment_kind,
131+
attr.style,
132+
*data,
133+
));
134+
self.hardbreak()
135+
}
136+
}
137+
}
138+
139+
fn print_attr_item(&mut self, item: &hir::AttrItem, span: Span) {
140+
self.ibox(0);
141+
let path = Path {
142+
span,
143+
segments: item
144+
.path
145+
.segments
146+
.iter()
147+
.map(|i| PathSegment { ident: *i, id: DUMMY_NODE_ID, args: None })
148+
.collect(),
149+
tokens: None,
150+
};
151+
152+
match &item.args {
153+
hir::AttrArgs::Delimited(DelimArgs { dspan: _, delim, tokens }) => self
154+
.print_mac_common(
155+
Some(MacHeader::Path(&path)),
156+
false,
157+
None,
158+
*delim,
159+
tokens,
160+
true,
161+
span,
162+
),
163+
hir::AttrArgs::Empty => {
164+
PrintState::print_path(self, &path, false, 0);
165+
}
166+
hir::AttrArgs::Eq { eq_span: _, value } => {
167+
PrintState::print_path(self, &path, false, 0);
168+
self.space();
169+
self.word_space("=");
170+
let token_str = self.meta_item_lit_to_string(value);
171+
self.word(token_str);
172+
}
173+
}
174+
self.end();
175+
}
176+
83177
fn print_node(&mut self, node: Node<'_>) {
84178
match node {
85179
Node::Param(a) => self.print_param(a),
@@ -199,6 +293,10 @@ where
199293
printer.s.eof()
200294
}
201295

296+
pub fn attribute_to_string(ann: &dyn PpAnn, attr: &hir::Attribute) -> String {
297+
to_string(ann, |s| s.print_attribute_inline(attr, false))
298+
}
299+
202300
pub fn ty_to_string(ann: &dyn PpAnn, ty: &hir::Ty<'_>) -> String {
203301
to_string(ann, |s| s.print_type(ty))
204302
}

0 commit comments

Comments
 (0)