Skip to content

A shebang is displaced in HIR & expanded outputs #134643

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
fluiderson opened this issue Dec 22, 2024 · 2 comments · Fixed by #137474
Closed

A shebang is displaced in HIR & expanded outputs #134643

fluiderson opened this issue Dec 22, 2024 · 2 comments · Fixed by #137474
Assignees
Labels
A-pretty Area: Pretty printing (including `-Z unpretty`) C-enhancement Category: An issue proposing an enhancement or a PR with one. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fluiderson
Copy link
Contributor

fluiderson commented Dec 22, 2024

I tried this code:

#!/usr/bin/env rust

fn test() {}

After running rustc -Zunpretty=hir test.rs, I expected to see this happen:

#!/usr/bin/env rust
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;

fn test() { }

Instead, this happened:

#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
#!/usr/bin/env rust

fn test() { }

A shebang should remain at the beginning of an output but instead is placed in-between. The same happens with rustc -Zunpretty=expanded test.rs.

Meta

rustc --version --verbose:

rustc 1.85.0-nightly (5f23ef7d3 2024-12-20)
binary: rustc
commit-hash: 5f23ef7d3f7a8c3e0ca5c4e1978829c0448a3686
commit-date: 2024-12-20
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6
@fluiderson fluiderson added the C-bug Category: This is a bug. label Dec 22, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 22, 2024
@fmease fmease added A-pretty Area: Pretty printing (including `-Z unpretty`) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 22, 2024
@fmease
Copy link
Member

fmease commented Dec 22, 2024

Marking this as an enhancement instead of a bug because technically speaking we don't guarantee anything about the shape of the output produced by -Zunpretty. Of course, a PR fixing this would be very welcome regardless :)

Some breadcrumbs: Both -Zunpretty={expanded,hir} / rustc_{ast,hir}_pretty collect comments (which includes shebangs) via rustc_ast_pretty::pprust::state::Comments:

pub struct Comments<'a> {
sm: &'a SourceMap,
// Stored in reverse order so we can consume them by popping.
reversed_comments: Vec<Comment>,
}

And print them again via maybe_print_comment / print_remaining_comments.

@VlaDexa
Copy link
Contributor

VlaDexa commented Feb 24, 2025

@rustbot claim

VlaDexa added a commit to VlaDexa/rust that referenced this issue May 2, 2025
…ywiser

pretty-print: Print shebang at the top of the output

Shebang should stay at the top of the file, even after pretty-printing.

Closes rust-lang#134643
@bors bors closed this as completed in 8619438 May 2, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 2, 2025
Rollup merge of rust-lang#137474 - VlaDexa:shebang-placement, r=wesleywiser

pretty-print: Print shebang at the top of the output

Shebang should stay at the top of the file, even after pretty-printing.

Closes rust-lang#134643
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-pretty Area: Pretty printing (including `-Z unpretty`) C-enhancement Category: An issue proposing an enhancement or a PR with one. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants