Preserve braces around self in use tree pretty printing#154084
Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom Mar 20, 2026
Merged
Preserve braces around self in use tree pretty printing#154084rust-bors[bot] merged 1 commit intorust-lang:mainfrom
self in use tree pretty printing#154084rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Conversation
Collaborator
|
r? @davidtwco rustbot has assigned @davidtwco. Use Why was this reviewer chosen?The reviewer was selected based on:
|
The AST pretty printer strips braces from single-item `use` sub-groups,
simplifying `use foo::{Bar}` to `use foo::Bar`. However, when the single
item is `self`, this produces `use foo::self` which is not valid Rust
(E0429) — the grammar requires `use foo::{self}`.
This affects both `stringify!` and `rustc -Zunpretty=expanded`, causing
`cargo expand` output to be unparseable when a crate uses `use
path::{self}` imports (a common pattern in the ecosystem).
The fix checks whether the single nested item's path starts with `self`
before stripping braces. If so, the braces are preserved.
Signed-off-by: Andrew V. Teylu <andrew.teylu@vector.com>
56769b0 to
082cdf7
Compare
Member
|
r? me @bors r+ rollup |
Contributor
JonathanBrouwer
added a commit
to JonathanBrouwer/rust
that referenced
this pull request
Mar 19, 2026
Preserve braces around `self` in use tree pretty printing
The AST pretty printer strips braces from single-item `use` sub-groups, simplifying `use foo::{Bar}` to `use foo::Bar`. However, when the single item is `self`, this produces `use foo::self` which is not valid Rust (E0429) — the grammar requires `use foo::{self}`.
This affects both `stringify!` and `rustc -Zunpretty=expanded`, causing `cargo expand` output to be unparseable when a crate uses `use path::{self}` imports (a common pattern in the ecosystem).
The fix checks whether the single nested item's path starts with `self` before stripping braces. If so, the braces are preserved.
## Example
**before** (`rustc 1.96.0-nightly (3b1b0ef 2026-03-11)`):
```rust
#![feature(prelude_import)]
//@ pp-exact
//@ edition:2021
#![allow(unused_imports)]
extern crate std;
#[prelude_import]
use std::prelude::rust_2021::*;
// Braces around `self` must be preserved, because `use foo::self` is not valid Rust.
use std::io::self;
use std::fmt::{self, Debug};
fn main() {}
```
**after** (this branch):
```rust
#![feature(prelude_import)]
//@ pp-exact
//@ edition:2021
#![allow(unused_imports)]
extern crate std;
#[prelude_import]
use std::prelude::rust_2021::*;
// Braces around `self` must be preserved, because `use foo::self` is not valid Rust.
use std::io::{self};
use std::fmt::{self, Debug};
fn main() {}
```
Notice the `use std::io::self` (invalid, E0429) in the before becomes `use std::io::{self}` in the after.
rust-bors bot
pushed a commit
that referenced
this pull request
Mar 19, 2026
…uwer Rollup of 9 pull requests Successful merges: - #153556 (`impl` restriction lowering) - #153992 (bootstrap: Optionally print a backtrace if a command fails) - #154019 (two smaller feature cleanups) - #154059 (tests: Activate `must_not_suspend` test for `MutexGuard` dropped before `await`) - #154075 (Rewrite `query_ensure_result`.) - #154082 (Updates derive_where and removes workaround) - #154084 (Preserve braces around `self` in use tree pretty printing) - #154086 (Insert space after float literal ending with `.` in pretty printer) - #154087 (Fix whitespace after fragment specifiers in macro pretty printing)
rust-bors bot
pushed a commit
that referenced
this pull request
Mar 19, 2026
…uwer Rollup of 9 pull requests Successful merges: - #153556 (`impl` restriction lowering) - #153992 (bootstrap: Optionally print a backtrace if a command fails) - #154019 (two smaller feature cleanups) - #154059 (tests: Activate `must_not_suspend` test for `MutexGuard` dropped before `await`) - #154075 (Rewrite `query_ensure_result`.) - #154082 (Updates derive_where and removes workaround) - #154084 (Preserve braces around `self` in use tree pretty printing) - #154086 (Insert space after float literal ending with `.` in pretty printer) - #154087 (Fix whitespace after fragment specifiers in macro pretty printing)
JonathanBrouwer
added a commit
to JonathanBrouwer/rust
that referenced
this pull request
Mar 19, 2026
Preserve braces around `self` in use tree pretty printing
The AST pretty printer strips braces from single-item `use` sub-groups, simplifying `use foo::{Bar}` to `use foo::Bar`. However, when the single item is `self`, this produces `use foo::self` which is not valid Rust (E0429) — the grammar requires `use foo::{self}`.
This affects both `stringify!` and `rustc -Zunpretty=expanded`, causing `cargo expand` output to be unparseable when a crate uses `use path::{self}` imports (a common pattern in the ecosystem).
The fix checks whether the single nested item's path starts with `self` before stripping braces. If so, the braces are preserved.
## Example
**before** (`rustc 1.96.0-nightly (3b1b0ef 2026-03-11)`):
```rust
#![feature(prelude_import)]
//@ pp-exact
//@ edition:2021
#![allow(unused_imports)]
extern crate std;
#[prelude_import]
use std::prelude::rust_2021::*;
// Braces around `self` must be preserved, because `use foo::self` is not valid Rust.
use std::io::self;
use std::fmt::{self, Debug};
fn main() {}
```
**after** (this branch):
```rust
#![feature(prelude_import)]
//@ pp-exact
//@ edition:2021
#![allow(unused_imports)]
extern crate std;
#[prelude_import]
use std::prelude::rust_2021::*;
// Braces around `self` must be preserved, because `use foo::self` is not valid Rust.
use std::io::{self};
use std::fmt::{self, Debug};
fn main() {}
```
Notice the `use std::io::self` (invalid, E0429) in the before becomes `use std::io::{self}` in the after.
JonathanBrouwer
added a commit
to JonathanBrouwer/rust
that referenced
this pull request
Mar 19, 2026
Preserve braces around `self` in use tree pretty printing
The AST pretty printer strips braces from single-item `use` sub-groups, simplifying `use foo::{Bar}` to `use foo::Bar`. However, when the single item is `self`, this produces `use foo::self` which is not valid Rust (E0429) — the grammar requires `use foo::{self}`.
This affects both `stringify!` and `rustc -Zunpretty=expanded`, causing `cargo expand` output to be unparseable when a crate uses `use path::{self}` imports (a common pattern in the ecosystem).
The fix checks whether the single nested item's path starts with `self` before stripping braces. If so, the braces are preserved.
## Example
**before** (`rustc 1.96.0-nightly (3b1b0ef 2026-03-11)`):
```rust
#![feature(prelude_import)]
//@ pp-exact
//@ edition:2021
#![allow(unused_imports)]
extern crate std;
#[prelude_import]
use std::prelude::rust_2021::*;
// Braces around `self` must be preserved, because `use foo::self` is not valid Rust.
use std::io::self;
use std::fmt::{self, Debug};
fn main() {}
```
**after** (this branch):
```rust
#![feature(prelude_import)]
//@ pp-exact
//@ edition:2021
#![allow(unused_imports)]
extern crate std;
#[prelude_import]
use std::prelude::rust_2021::*;
// Braces around `self` must be preserved, because `use foo::self` is not valid Rust.
use std::io::{self};
use std::fmt::{self, Debug};
fn main() {}
```
Notice the `use std::io::self` (invalid, E0429) in the before becomes `use std::io::{self}` in the after.
rust-bors bot
pushed a commit
that referenced
this pull request
Mar 20, 2026
…uwer Rollup of 12 pull requests Successful merges: - #152909 (sess: `-Zbranch-protection` is a target modifier) - #153556 (`impl` restriction lowering) - #154048 (Don't emit rustdoc `missing_doc_code_examples` lint on impl items) - #153992 (bootstrap: Optionally print a backtrace if a command fails) - #154019 (two smaller feature cleanups) - #154059 (tests: Activate `must_not_suspend` test for `MutexGuard` dropped before `await`) - #154075 (Rewrite `query_ensure_result`.) - #154082 (Updates derive_where and removes workaround) - #154084 (Preserve braces around `self` in use tree pretty printing) - #154086 (Insert space after float literal ending with `.` in pretty printer) - #154087 (Fix whitespace after fragment specifiers in macro pretty printing) - #154109 (tests: Add regression test for async closures involving HRTBs)
rust-bors bot
pushed a commit
that referenced
this pull request
Mar 20, 2026
Rollup of 15 pull requests Successful merges: - #152909 (sess: `-Zbranch-protection` is a target modifier) - #153556 (`impl` restriction lowering) - #154048 (Don't emit rustdoc `missing_doc_code_examples` lint on impl items) - #150935 (Introduce #[diagnostic::on_move(message)]) - #152973 (remove -Csoft-float) - #153862 (Rename `cycle_check` to `find_cycle`) - #153992 (bootstrap: Optionally print a backtrace if a command fails) - #154019 (two smaller feature cleanups) - #154059 (tests: Activate `must_not_suspend` test for `MutexGuard` dropped before `await`) - #154075 (Rewrite `query_ensure_result`.) - #154082 (Updates derive_where and removes workaround) - #154084 (Preserve braces around `self` in use tree pretty printing) - #154086 (Insert space after float literal ending with `.` in pretty printer) - #154087 (Fix whitespace after fragment specifiers in macro pretty printing) - #154109 (tests: Add regression test for async closures involving HRTBs)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The AST pretty printer strips braces from single-item
usesub-groups, simplifyinguse foo::{Bar}touse foo::Bar. However, when the single item isself, this producesuse foo::selfwhich is not valid Rust (E0429) — the grammar requiresuse foo::{self}.This affects both
stringify!andrustc -Zunpretty=expanded, causingcargo expandoutput to be unparseable when a crate usesuse path::{self}imports (a common pattern in the ecosystem).The fix checks whether the single nested item's path starts with
selfbefore stripping braces. If so, the braces are preserved.Example
before (
rustc 1.96.0-nightly (3b1b0ef4d 2026-03-11)):after (this branch):
Notice the
use std::io::self(invalid, E0429) in the before becomesuse std::io::{self}in the after.