Skip to content

Commit e0a24ad

Browse files
authored
Unrolled build for rust-lang#140606
Rollup merge of rust-lang#140606 - nnethercote:hir-pp, r=dtolnay Improve hir pretty printing It's currently pretty bad, so a few small improvements can make a big difference. r? `@dtolnay`
2 parents 2ad5f86 + 9af0842 commit e0a24ad

File tree

15 files changed

+95
-95
lines changed

15 files changed

+95
-95
lines changed

compiler/rustc_ast_pretty/src/pp.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,6 @@ struct BufEntry {
244244
// forgotten will trigger a panic in `drop`. (Closing a box more than once
245245
// isn't possible because `BoxMarker` doesn't implement `Copy` or `Clone`.)
246246
//
247-
// FIXME(nnethercote): the panic in `drop` is currently disabled because a few
248-
// places fail to close their boxes. It can be enabled once they are fixed.
249-
//
250247
// Note: it would be better to make open/close mismatching impossible and avoid
251248
// the need for this marker type altogether by having functions like
252249
// `with_ibox` that open a box, call a closure, and then close the box. That
@@ -261,8 +258,7 @@ impl !Copy for BoxMarker {}
261258

262259
impl Drop for BoxMarker {
263260
fn drop(&mut self) {
264-
// FIXME(nnethercote): enable once the bad cases are fixed
265-
//panic!("BoxMarker not ended with `Printer::end()`");
261+
panic!("BoxMarker not ended with `Printer::end()`");
266262
}
267263
}
268264

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+3-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_ast::{
1313

1414
use crate::pp::Breaks::Inconsistent;
1515
use crate::pprust::state::fixup::FixupContext;
16-
use crate::pprust::state::{AnnNode, BoxMarker, INDENT_UNIT, PrintState, State};
16+
use crate::pprust::state::{AnnNode, INDENT_UNIT, PrintState, State};
1717

1818
impl<'a> State<'a> {
1919
fn print_else(&mut self, els: Option<&ast::Expr>) {
@@ -485,12 +485,12 @@ impl<'a> State<'a> {
485485
self.print_block_with_attrs(body, attrs, cb, ib);
486486
}
487487
ast::ExprKind::Loop(blk, opt_label, _) => {
488+
let cb = self.cbox(0);
489+
let ib = self.ibox(0);
488490
if let Some(label) = opt_label {
489491
self.print_ident(label.ident);
490492
self.word_space(":");
491493
}
492-
let cb = self.cbox(0);
493-
let ib = self.ibox(0);
494494
self.word_nbsp("loop");
495495
self.print_block_with_attrs(blk, attrs, cb, ib);
496496
}
@@ -542,15 +542,6 @@ impl<'a> State<'a> {
542542
self.print_fn_params_and_ret(fn_decl, true);
543543
self.space();
544544
self.print_expr(body, FixupContext::default());
545-
// FIXME(nnethercote): Bogus. Reduce visibility of `ended` once it's fixed.
546-
let fake_ib = BoxMarker;
547-
self.end(fake_ib);
548-
549-
// A box will be closed by print_expr, but we didn't want an overall
550-
// wrapper so we closed the corresponding opening. so create an
551-
// empty box to satisfy the close.
552-
// FIXME(nnethercote): Bogus.
553-
let _ib = self.ibox(0);
554545
}
555546
ast::ExprKind::Block(blk, opt_label) => {
556547
if let Some(label) = opt_label {

compiler/rustc_hir_pretty/src/lib.rs

+12-25
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(
@@ -183,7 +184,7 @@ impl<'a> State<'a> {
183184
Node::Ty(a) => self.print_type(a),
184185
Node::AssocItemConstraint(a) => self.print_assoc_item_constraint(a),
185186
Node::TraitRef(a) => self.print_trait_ref(a),
186-
Node::OpaqueTy(o) => self.print_opaque_ty(o),
187+
Node::OpaqueTy(_) => panic!("cannot print Node::OpaqueTy"),
187188
Node::Pat(a) => self.print_pat(a),
188189
Node::TyPat(a) => self.print_ty_pat(a),
189190
Node::PatField(a) => self.print_patfield(a),
@@ -654,10 +655,11 @@ impl<'a> State<'a> {
654655
self.bclose(item.span, cb);
655656
}
656657
hir::ItemKind::GlobalAsm { asm, .. } => {
657-
// FIXME(nnethercote): `ib` is unclosed
658-
let (cb, _ib) = self.head("global_asm!");
658+
let (cb, ib) = self.head("global_asm!");
659659
self.print_inline_asm(asm);
660-
self.end(cb)
660+
self.word(";");
661+
self.end(cb);
662+
self.end(ib);
661663
}
662664
hir::ItemKind::TyAlias(ident, ty, generics) => {
663665
let (cb, ib) = self.head("type");
@@ -764,14 +766,6 @@ impl<'a> State<'a> {
764766
self.print_path(t.path, false);
765767
}
766768

767-
fn print_opaque_ty(&mut self, o: &hir::OpaqueTy<'_>) {
768-
// FIXME(nnethercote): `cb` and `ib` are unclosed
769-
let (_cb, _ib) = self.head("opaque");
770-
self.word("{");
771-
self.print_bounds("impl", o.bounds);
772-
self.word("}");
773-
}
774-
775769
fn print_formal_generic_params(&mut self, generic_params: &[hir::GenericParam<'_>]) {
776770
if !generic_params.is_empty() {
777771
self.word("for");
@@ -1509,7 +1503,7 @@ impl<'a> State<'a> {
15091503
}
15101504
hir::ExprKind::DropTemps(init) => {
15111505
// Print `{`:
1512-
let cb = self.cbox(INDENT_UNIT);
1506+
let cb = self.cbox(0);
15131507
let ib = self.ibox(0);
15141508
self.bopen(ib);
15151509

@@ -1532,16 +1526,18 @@ impl<'a> State<'a> {
15321526
self.print_if(test, blk, elseopt);
15331527
}
15341528
hir::ExprKind::Loop(blk, opt_label, _, _) => {
1529+
let cb = self.cbox(0);
1530+
let ib = self.ibox(0);
15351531
if let Some(label) = opt_label {
15361532
self.print_ident(label.ident);
15371533
self.word_space(":");
15381534
}
1539-
let (cb, ib) = self.head("loop");
1535+
self.word_nbsp("loop");
15401536
self.print_block(blk, cb, ib);
15411537
}
15421538
hir::ExprKind::Match(expr, arms, _) => {
1543-
let cb = self.cbox(INDENT_UNIT);
1544-
let ib = self.ibox(INDENT_UNIT);
1539+
let cb = self.cbox(0);
1540+
let ib = self.ibox(0);
15451541
self.word_nbsp("match");
15461542
self.print_expr_as_cond(expr);
15471543
self.space();
@@ -1572,15 +1568,6 @@ impl<'a> State<'a> {
15721568

15731569
// This is a bare expression.
15741570
self.ann.nested(self, Nested::Body(body));
1575-
// FIXME(nnethercote): this is bogus
1576-
let fake_ib = BoxMarker;
1577-
self.end(fake_ib);
1578-
1579-
// A box will be closed by `print_expr`, but we didn't want an overall
1580-
// wrapper so we closed the corresponding opening. so create an
1581-
// empty box to satisfy the close.
1582-
// FIXME(nnethercote): this is bogus, and `print_expr` is missing
1583-
let _ib = self.ibox(0);
15841571
}
15851572
hir::ExprKind::Block(blk, opt_label) => {
15861573
if let Some(label) = opt_label {

tests/pretty/hir-delegation.pp

+2-1
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

+1-1
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' '[]'
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

+1-1
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

+2-2
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> {}
+1-1
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

+1-1
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

+3-3
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

+2-2
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

+2-2
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-asm.hir.stdout

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ mod expressions {
2727
mod items {
2828
/// ItemKind::GlobalAsm
2929
mod item_global_asm {/// ItemKind::GlobalAsm
30-
global_asm! (".globl my_asm_func") }
30+
global_asm! (".globl my_asm_func");
3131
}
32+
}

0 commit comments

Comments
 (0)