Skip to content

Commit 25cdf1f

Browse files
committed
Auto merge of #140388 - GuillaumeGomez:rollup-aj9o3ch, r=GuillaumeGomez
Rollup of 7 pull requests Successful merges: - #140056 (Fix a wrong error message in 2024 edition) - #140220 (Fix detection of main function if there are expressions around it) - #140249 (Remove `weak` alias terminology) - #140316 (Introduce `BoxMarker` to improve pretty-printing correctness) - #140347 (ci: clean more disk space in codebuild) - #140349 (ci: use aws codebuild for the `dist-x86_64-linux` job) - #140379 (rustc-dev-guide subtree update) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 7d65abf + dd3ca71 commit 25cdf1f

File tree

96 files changed

+874
-743
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+874
-743
lines changed

compiler/rustc_ast_pretty/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(internal_features)]
33
#![doc(rust_logo)]
44
#![feature(box_patterns)]
5+
#![feature(negative_impls)]
56
#![feature(rustdoc_internals)]
67
// tidy-alphabetical-end
78

compiler/rustc_ast_pretty/src/pp.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,38 @@ struct BufEntry {
234234
size: isize,
235235
}
236236

237+
// Boxes opened with methods like `Printer::{cbox,ibox}` must be closed with
238+
// `Printer::end`. Failure to do so can result in bad indenting, or in extreme
239+
// cases, cause no output to be produced at all.
240+
//
241+
// Box opening and closing used to be entirely implicit, which was hard to
242+
// understand and easy to get wrong. This marker type is now returned from the
243+
// box opening methods and forgotten by `Printer::end`. Any marker that isn't
244+
// forgotten will trigger a panic in `drop`. (Closing a box more than once
245+
// isn't possible because `BoxMarker` doesn't implement `Copy` or `Clone`.)
246+
//
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+
//
250+
// Note: it would be better to make open/close mismatching impossible and avoid
251+
// the need for this marker type altogether by having functions like
252+
// `with_ibox` that open a box, call a closure, and then close the box. That
253+
// would work for simple cases, but box lifetimes sometimes interact with
254+
// complex control flow and across function boundaries in ways that are
255+
// difficult to handle with such a technique.
256+
#[must_use]
257+
pub struct BoxMarker;
258+
259+
impl !Clone for BoxMarker {}
260+
impl !Copy for BoxMarker {}
261+
262+
impl Drop for BoxMarker {
263+
fn drop(&mut self) {
264+
// FIXME(nnethercote): enable once the bad cases are fixed
265+
//panic!("BoxMarker not ended with `Printer::end()`");
266+
}
267+
}
268+
237269
impl Printer {
238270
pub fn new() -> Self {
239271
Printer {
@@ -270,23 +302,27 @@ impl Printer {
270302
}
271303
}
272304

273-
fn scan_begin(&mut self, token: BeginToken) {
305+
// This is is where `BoxMarker`s are produced.
306+
fn scan_begin(&mut self, token: BeginToken) -> BoxMarker {
274307
if self.scan_stack.is_empty() {
275308
self.left_total = 1;
276309
self.right_total = 1;
277310
self.buf.clear();
278311
}
279312
let right = self.buf.push(BufEntry { token: Token::Begin(token), size: -self.right_total });
280313
self.scan_stack.push_back(right);
314+
BoxMarker
281315
}
282316

283-
fn scan_end(&mut self) {
317+
// This is is where `BoxMarker`s are consumed.
318+
fn scan_end(&mut self, b: BoxMarker) {
284319
if self.scan_stack.is_empty() {
285320
self.print_end();
286321
} else {
287322
let right = self.buf.push(BufEntry { token: Token::End, size: -1 });
288323
self.scan_stack.push_back(right);
289324
}
325+
std::mem::forget(b)
290326
}
291327

292328
fn scan_break(&mut self, token: BreakToken) {

compiler/rustc_ast_pretty/src/pp/convenience.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
use std::borrow::Cow;
22

3-
use crate::pp::{BeginToken, BreakToken, Breaks, IndentStyle, Printer, SIZE_INFINITY, Token};
3+
use crate::pp::{
4+
BeginToken, BoxMarker, BreakToken, Breaks, IndentStyle, Printer, SIZE_INFINITY, Token,
5+
};
46

57
impl Printer {
68
/// "raw box"
7-
pub fn rbox(&mut self, indent: isize, breaks: Breaks) {
9+
pub fn rbox(&mut self, indent: isize, breaks: Breaks) -> BoxMarker {
810
self.scan_begin(BeginToken { indent: IndentStyle::Block { offset: indent }, breaks })
911
}
1012

1113
/// Inconsistent breaking box
12-
pub fn ibox(&mut self, indent: isize) {
14+
pub fn ibox(&mut self, indent: isize) -> BoxMarker {
1315
self.rbox(indent, Breaks::Inconsistent)
1416
}
1517

1618
/// Consistent breaking box
17-
pub fn cbox(&mut self, indent: isize) {
19+
pub fn cbox(&mut self, indent: isize) -> BoxMarker {
1820
self.rbox(indent, Breaks::Consistent)
1921
}
2022

21-
pub fn visual_align(&mut self) {
22-
self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent });
23+
pub fn visual_align(&mut self) -> BoxMarker {
24+
self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent })
2325
}
2426

2527
pub fn break_offset(&mut self, n: usize, off: isize) {
@@ -30,8 +32,8 @@ impl Printer {
3032
});
3133
}
3234

33-
pub fn end(&mut self) {
34-
self.scan_end()
35+
pub fn end(&mut self, b: BoxMarker) {
36+
self.scan_end(b)
3537
}
3638

3739
pub fn eof(mut self) -> String {

0 commit comments

Comments
 (0)