Improve hir pretty-printing of attributes.

This commit is contained in:
Nicholas Nethercote 2025-04-30 10:08:01 +10:00
parent aa7bb1c2f5
commit e1a177bbba
12 changed files with 49 additions and 21 deletions

View file

@ -110,6 +110,7 @@ impl<'a> State<'a> {
}
self.print_attr_item(&unparsed, unparsed.span);
self.word("]");
self.hardbreak()
}
hir::Attribute::Parsed(AttributeKind::DocComment { style, kind, comment, .. }) => {
self.word(rustc_ast_pretty::pprust::state::doc_comment_to_string(

View file

@ -2,7 +2,8 @@
//@ pretty-mode:hir
//@ pp-exact:hir-delegation.pp
#![allow(incomplete_features)]#![feature(fn_delegation)]
#![allow(incomplete_features)]
#![feature(fn_delegation)]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]

View file

@ -9,5 +9,5 @@ impl Default for Manual {
}
}
//@ is '$.index[?(@.inner.impl.for.resolved_path.path == "Derive" && @.inner.impl.trait.path == "Default")].attrs' '["#[automatically_derived]"]'
//@ is '$.index[?(@.inner.impl.for.resolved_path.path == "Derive" && @.inner.impl.trait.path == "Default")].attrs' '["#[automatically_derived]\n"]'
//@ is '$.index[?(@.inner.impl.for.resolved_path.path == "Manual" && @.inner.impl.trait.path == "Default")].attrs' '[]'

View file

@ -1,6 +1,6 @@
//@ edition: 2021
#![no_std]
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]"]'
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]\n"]'
#[export_name = "altered"]
pub extern "C" fn example() {}

View file

@ -4,6 +4,6 @@
// The representation of `#[unsafe(export_name = ..)]` in rustdoc in edition 2024
// is still `#[export_name = ..]` without the `unsafe` attribute wrapper.
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]"]'
//@ is "$.index[?(@.name=='example')].attrs" '["#[export_name = \"altered\"]\n"]'
#[unsafe(export_name = "altered")]
pub extern "C" fn example() {}

View file

@ -1,9 +1,9 @@
#![no_std]
//@ is "$.index[?(@.name=='example')].attrs" '["#[must_use]"]'
//@ is "$.index[?(@.name=='example')].attrs" '["#[must_use]\n"]'
#[must_use]
pub fn example() -> impl Iterator<Item = i64> {}
//@ is "$.index[?(@.name=='explicit_message')].attrs" '["#[must_use = \"does nothing if you do not use it\"]"]'
//@ is "$.index[?(@.name=='explicit_message')].attrs" '["#[must_use = \"does nothing if you do not use it\"]\n"]'
#[must_use = "does nothing if you do not use it"]
pub fn explicit_message() -> impl Iterator<Item = i64> {}

View file

@ -1,6 +1,6 @@
//@ edition: 2021
#![no_std]
//@ is "$.index[?(@.name=='example')].attrs" '["#[no_mangle]"]'
//@ is "$.index[?(@.name=='example')].attrs" '["#[no_mangle]\n"]'
#[no_mangle]
pub extern "C" fn example() {}

View file

@ -4,6 +4,6 @@
// The representation of `#[unsafe(no_mangle)]` in rustdoc in edition 2024
// is still `#[no_mangle]` without the `unsafe` attribute wrapper.
//@ is "$.index[?(@.name=='example')].attrs" '["#[no_mangle]"]'
//@ is "$.index[?(@.name=='example')].attrs" '["#[no_mangle]\n"]'
#[unsafe(no_mangle)]
pub extern "C" fn example() {}

View file

@ -1,18 +1,18 @@
#![no_std]
//@ is "$.index[?(@.name=='MyEnum')].attrs" '["#[non_exhaustive]"]'
//@ is "$.index[?(@.name=='MyEnum')].attrs" '["#[non_exhaustive]\n"]'
#[non_exhaustive]
pub enum MyEnum {
First,
}
pub enum NonExhaustiveVariant {
//@ is "$.index[?(@.name=='Variant')].attrs" '["#[non_exhaustive]"]'
//@ is "$.index[?(@.name=='Variant')].attrs" '["#[non_exhaustive]\n"]'
#[non_exhaustive]
Variant(i64),
}
//@ is "$.index[?(@.name=='MyStruct')].attrs" '["#[non_exhaustive]"]'
//@ is "$.index[?(@.name=='MyStruct')].attrs" '["#[non_exhaustive]\n"]'
#[non_exhaustive]
pub struct MyStruct {
pub x: i64,

View file

@ -5,7 +5,7 @@
//@ !has "$.index[?(@.name=='match')]"
//@ has "$.index[?(@.name=='foo')]"
//@ is "$.index[?(@.name=='foo')].attrs" '["#[doc(keyword = \"match\")]"]'
//@ is "$.index[?(@.name=='foo')].attrs" '["#[doc(keyword = \"match\")]\n"]'
//@ is "$.index[?(@.name=='foo')].docs" '"this is a test!"'
#[doc(keyword = "match")]
/// this is a test!
@ -13,7 +13,7 @@ pub mod foo {}
//@ !has "$.index[?(@.name=='break')]"
//@ has "$.index[?(@.name=='bar')]"
//@ is "$.index[?(@.name=='bar')].attrs" '["#[doc(keyword = \"break\")]"]'
//@ is "$.index[?(@.name=='bar')].attrs" '["#[doc(keyword = \"break\")]\n"]'
//@ is "$.index[?(@.name=='bar')].docs" '"hello"'
#[doc(keyword = "break")]
/// hello

View file

@ -35,12 +35,12 @@ fn test_stable_mir() -> ControlFlow<()> {
fn test_tool(items: &CrateItems) {
let rustfmt_fn = *get_item(&items, "do_not_format").unwrap();
let rustfmt_attrs = rustfmt_fn.tool_attrs(&["rustfmt".to_string(), "skip".to_string()]);
assert_eq!(rustfmt_attrs[0].as_str(), "#[rustfmt::skip]");
assert_eq!(rustfmt_attrs[0].as_str(), "#[rustfmt::skip]\n");
let clippy_fn = *get_item(&items, "complex_fn").unwrap();
let clippy_attrs = clippy_fn.tool_attrs(&["clippy".to_string(),
"cyclomatic_complexity".to_string()]);
assert_eq!(clippy_attrs[0].as_str(), "#[clippy::cyclomatic_complexity = \"100\"]");
assert_eq!(clippy_attrs[0].as_str(), "#[clippy::cyclomatic_complexity = \"100\"]\n");
}
fn get_item<'a>(

View file

@ -8,7 +8,28 @@
// Note: the HIR revision includes a `.stderr` file because there are some
// errors that only occur once we get past the AST.
#![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)]
#![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)]
#[prelude_import]
use std::prelude::rust_2024::*;
#[macro_use]
@ -33,20 +54,25 @@ mod prelude {
/*!
* inner multi-line doc comment
*/
#[doc = "inner doc attribute"]#[allow(dead_code, unused_variables)]#[no_std]
#[doc = "inner doc attribute"]
#[allow(dead_code, unused_variables)]
#[no_std]
mod attributes {//! inner single-line doc comment
/*!
* inner multi-line doc comment
*/
#![doc =
"inner doc attribute"]#![allow(dead_code, unused_variables)]#![no_std]
#![doc = "inner doc attribute"]
#![allow(dead_code, unused_variables)]
#![no_std]
/// outer single-line doc comment
/**
* outer multi-line doc comment
*/
#[doc =
"outer doc attribute"]#[doc = "macro"]#[allow()]#[attr = Repr([ReprC])]
#[doc = "outer doc attribute"]
#[doc = "macro"]
#[allow()]
#[attr = Repr([ReprC])]
struct Struct;
}