Skip to content

Commit e1a177b

Browse files
committed
Improve hir pretty-printing of attributes.
1 parent aa7bb1c commit e1a177b

File tree

12 files changed

+49
-21
lines changed

12 files changed

+49
-21
lines changed

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl<'a> State<'a> {
110110
}
111111
self.print_attr_item(&unparsed, unparsed.span);
112112
self.word("]");
113+
self.hardbreak()
113114
}
114115
hir::Attribute::Parsed(AttributeKind::DocComment { style, kind, comment, .. }) => {
115116
self.word(rustc_ast_pretty::pprust::state::doc_comment_to_string(

tests/pretty/hir-delegation.pp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//@ pretty-mode:hir
33
//@ pp-exact:hir-delegation.pp
44

5-
#![allow(incomplete_features)]#![feature(fn_delegation)]
5+
#![allow(incomplete_features)]
6+
#![feature(fn_delegation)]
67
#[prelude_import]
78
use ::std::prelude::rust_2015::*;
89
#[macro_use]

tests/rustdoc-json/attrs/automatically_derived.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ impl Default for Manual {
99
}
1010
}
1111

12-
//@ is '$.index[?(@.inner.impl.for.resolved_path.path == "Derive" && @.inner.impl.trait.path == "Default")].attrs' '["#[automatically_derived]"]'
12+
//@ is '$.index[?(@.inner.impl.for.resolved_path.path == "Derive" && @.inner.impl.trait.path == "Default")].attrs' '["#[automatically_derived]\n"]'
1313
//@ is '$.index[?(@.inner.impl.for.resolved_path.path == "Manual" && @.inner.impl.trait.path == "Default")].attrs' '[]'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ edition: 2021
22
#![no_std]
33

4-
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]"]'
4+
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]\n"]'
55
#[export_name = "altered"]
66
pub extern "C" fn example() {}

tests/rustdoc-json/attrs/export_name_2024.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
// The representation of `#[unsafe(export_name = ..)]` in rustdoc in edition 2024
55
// is still `#[export_name = ..]` without the `unsafe` attribute wrapper.
66

7-
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]"]'
7+
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]\n"]'
88
#[unsafe(export_name = "altered")]
99
pub extern "C" fn example() {}

tests/rustdoc-json/attrs/must_use.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#![no_std]
22

3-
//@ is "$.index[?(@.name=='example')].attrs" '["#[must_use]"]'
3+
//@ is "$.index[?(@.name=='example')].attrs" '["#[must_use]\n"]'
44
#[must_use]
55
pub fn example() -> impl Iterator<Item = i64> {}
66

7-
//@ is "$.index[?(@.name=='explicit_message')].attrs" '["#[must_use = \"does nothing if you do not use it\"]"]'
7+
//@ is "$.index[?(@.name=='explicit_message')].attrs" '["#[must_use = \"does nothing if you do not use it\"]\n"]'
88
#[must_use = "does nothing if you do not use it"]
99
pub fn explicit_message() -> impl Iterator<Item = i64> {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ edition: 2021
22
#![no_std]
33

4-
//@ is "$.index[?(@.name=='example')].attrs" '["#[no_mangle]"]'
4+
//@ is "$.index[?(@.name=='example')].attrs" '["#[no_mangle]\n"]'
55
#[no_mangle]
66
pub extern "C" fn example() {}

tests/rustdoc-json/attrs/no_mangle_2024.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
// The representation of `#[unsafe(no_mangle)]` in rustdoc in edition 2024
55
// is still `#[no_mangle]` without the `unsafe` attribute wrapper.
66

7-
//@ is "$.index[?(@.name=='example')].attrs" '["#[no_mangle]"]'
7+
//@ is "$.index[?(@.name=='example')].attrs" '["#[no_mangle]\n"]'
88
#[unsafe(no_mangle)]
99
pub extern "C" fn example() {}

tests/rustdoc-json/attrs/non_exhaustive.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#![no_std]
22

3-
//@ is "$.index[?(@.name=='MyEnum')].attrs" '["#[non_exhaustive]"]'
3+
//@ is "$.index[?(@.name=='MyEnum')].attrs" '["#[non_exhaustive]\n"]'
44
#[non_exhaustive]
55
pub enum MyEnum {
66
First,
77
}
88

99
pub enum NonExhaustiveVariant {
10-
//@ is "$.index[?(@.name=='Variant')].attrs" '["#[non_exhaustive]"]'
10+
//@ is "$.index[?(@.name=='Variant')].attrs" '["#[non_exhaustive]\n"]'
1111
#[non_exhaustive]
1212
Variant(i64),
1313
}
1414

15-
//@ is "$.index[?(@.name=='MyStruct')].attrs" '["#[non_exhaustive]"]'
15+
//@ is "$.index[?(@.name=='MyStruct')].attrs" '["#[non_exhaustive]\n"]'
1616
#[non_exhaustive]
1717
pub struct MyStruct {
1818
pub x: i64,

tests/rustdoc-json/keyword_private.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
//@ !has "$.index[?(@.name=='match')]"
77
//@ has "$.index[?(@.name=='foo')]"
8-
//@ is "$.index[?(@.name=='foo')].attrs" '["#[doc(keyword = \"match\")]"]'
8+
//@ is "$.index[?(@.name=='foo')].attrs" '["#[doc(keyword = \"match\")]\n"]'
99
//@ is "$.index[?(@.name=='foo')].docs" '"this is a test!"'
1010
#[doc(keyword = "match")]
1111
/// this is a test!
1212
pub mod foo {}
1313

1414
//@ !has "$.index[?(@.name=='break')]"
1515
//@ has "$.index[?(@.name=='bar')]"
16-
//@ is "$.index[?(@.name=='bar')].attrs" '["#[doc(keyword = \"break\")]"]'
16+
//@ is "$.index[?(@.name=='bar')].attrs" '["#[doc(keyword = \"break\")]\n"]'
1717
//@ is "$.index[?(@.name=='bar')].docs" '"hello"'
1818
#[doc(keyword = "break")]
1919
/// hello

tests/ui-fulldeps/stable-mir/check_attribute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ fn test_stable_mir() -> ControlFlow<()> {
3535
fn test_tool(items: &CrateItems) {
3636
let rustfmt_fn = *get_item(&items, "do_not_format").unwrap();
3737
let rustfmt_attrs = rustfmt_fn.tool_attrs(&["rustfmt".to_string(), "skip".to_string()]);
38-
assert_eq!(rustfmt_attrs[0].as_str(), "#[rustfmt::skip]");
38+
assert_eq!(rustfmt_attrs[0].as_str(), "#[rustfmt::skip]\n");
3939

4040
let clippy_fn = *get_item(&items, "complex_fn").unwrap();
4141
let clippy_attrs = clippy_fn.tool_attrs(&["clippy".to_string(),
4242
"cyclomatic_complexity".to_string()]);
43-
assert_eq!(clippy_attrs[0].as_str(), "#[clippy::cyclomatic_complexity = \"100\"]");
43+
assert_eq!(clippy_attrs[0].as_str(), "#[clippy::cyclomatic_complexity = \"100\"]\n");
4444
}
4545

4646
fn get_item<'a>(

tests/ui/unpretty/exhaustive.hir.stdout

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,28 @@
88
// Note: the HIR revision includes a `.stderr` file because there are some
99
// errors that only occur once we get past the AST.
1010

11-
#![feature(auto_traits)]#![feature(box_patterns)]#![feature(builtin_syntax)]#![feature(concat_idents)]#![feature(const_trait_impl)]#![feature(decl_macro)]#![feature(deref_patterns)]#![feature(dyn_star)]#![feature(explicit_tail_calls)]#![feature(gen_blocks)]#![feature(more_qualified_paths)]#![feature(never_patterns)]#![feature(never_type)]#![feature(pattern_types)]#![feature(pattern_type_macro)]#![feature(prelude_import)]#![feature(specialization)]#![feature(trace_macros)]#![feature(trait_alias)]#![feature(try_blocks)]#![feature(yeet_expr)]#![allow(incomplete_features)]
11+
#![feature(auto_traits)]
12+
#![feature(box_patterns)]
13+
#![feature(builtin_syntax)]
14+
#![feature(concat_idents)]
15+
#![feature(const_trait_impl)]
16+
#![feature(decl_macro)]
17+
#![feature(deref_patterns)]
18+
#![feature(dyn_star)]
19+
#![feature(explicit_tail_calls)]
20+
#![feature(gen_blocks)]
21+
#![feature(more_qualified_paths)]
22+
#![feature(never_patterns)]
23+
#![feature(never_type)]
24+
#![feature(pattern_types)]
25+
#![feature(pattern_type_macro)]
26+
#![feature(prelude_import)]
27+
#![feature(specialization)]
28+
#![feature(trace_macros)]
29+
#![feature(trait_alias)]
30+
#![feature(try_blocks)]
31+
#![feature(yeet_expr)]
32+
#![allow(incomplete_features)]
1233
#[prelude_import]
1334
use std::prelude::rust_2024::*;
1435
#[macro_use]
@@ -33,20 +54,25 @@ mod prelude {
3354
/*!
3455
* inner multi-line doc comment
3556
*/
36-
#[doc = "inner doc attribute"]#[allow(dead_code, unused_variables)]#[no_std]
57+
#[doc = "inner doc attribute"]
58+
#[allow(dead_code, unused_variables)]
59+
#[no_std]
3760
mod attributes {//! inner single-line doc comment
3861
/*!
3962
* inner multi-line doc comment
4063
*/
41-
#![doc =
42-
"inner doc attribute"]#![allow(dead_code, unused_variables)]#![no_std]
64+
#![doc = "inner doc attribute"]
65+
#![allow(dead_code, unused_variables)]
66+
#![no_std]
4367

4468
/// outer single-line doc comment
4569
/**
4670
* outer multi-line doc comment
4771
*/
48-
#[doc =
49-
"outer doc attribute"]#[doc = "macro"]#[allow()]#[attr = Repr([ReprC])]
72+
#[doc = "outer doc attribute"]
73+
#[doc = "macro"]
74+
#[allow()]
75+
#[attr = Repr([ReprC])]
5076
struct Struct;
5177
}
5278

0 commit comments

Comments
 (0)