Skip to content

The extra newline in unpretty=hir #125140

Open
@thomasyonug

Description

@thomasyonug

I tried this code when dump the hir by -Z unpretty=hir:

// file: procedural macro
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Expr};

#[proc_macro]
pub fn mdriver(input: TokenStream) -> TokenStream {
    let body = parse_macro_input!(input as Expr);

    let expanded = quote! {
        fn main() {
            #body
        }
    };

    TokenStream::from(expanded)
}

// file: macro call
mdriver!({
    println!("Hello, world!");
});

mdriver!({
    println!("Hello, world!");
});

mdriver!({
    println!("Hello, world!");
});

mdriver!({
    println!("Hello, world!");
});

mdriver!({
    println!("Hello, world!");
});

I expected to see this happen:

fn main() {
        {
                {
                        ::std::io::_print(format_arguments::new_const(&["Hello, world!\n"]));
                    };
            }
    }
fn main() {
        {
                {
                        ::std::io::_print(format_arguments::new_const(&["Hello, world!\n"]));
                    };
            }
    }
....................

Instead, this happened:

fn main() {
        {
// unexpected newline here








                {
                        ::std::io::_print(format_arguments::new_const(&["Hello, world!\n"]));
                    };
            }
    }
fn main() {
        {
                {
                        ::std::io::_print(format_arguments::new_const(&["Hello, world!\n"]));
                    };
            }
    }
...........

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (ef0027897 2024-05-12)
binary: rustc
commit-hash: ef0027897d2e9014766fb47dce9ddbb925d2f540
commit-date: 2024-05-12
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4
Backtrace

<backtrace>

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-prettyArea: Pretty printing (including `-Z unpretty`)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions