Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

An unexpected expanded result from procedural macro #125138

Open
thomasyonug opened this issue May 15, 2024 · 0 comments
Open

An unexpected expanded result from procedural macro #125138

thomasyonug opened this issue May 15, 2024 · 0 comments
Labels
-Zunpretty Unstable option: unpretty debug print A-proc-macros Area: Procedural macros C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@thomasyonug
Copy link

I tried this code to check the expanded HIR by flag -Z unpretty=hir:

// file proc_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 expr = parse_macro_input!(input as Expr);

    let expanded = quote! {
        fn foo() {
            #expr?;
        }
    };

    TokenStream::from(expanded)
}

//file call the macro
mdriver!(0..10);

I expected to see this happen:

fn foo() {
        match #[lang = "branch"](#[lang = "Range"]{ start: 0,  end: 10,}) {
                #[lang = "Break"] {  0: residual } =>
                    #[allow(unreachable_code)]
                    return #[lang = "from_residual"](residual),
                #[lang = "Continue"] {  0: val } =>
                    #[allow(unreachable_code)]
                    val,
            };
    }

Instead, this happened:

fn foo() {
        #[lang = "Range"]{
            start: 0,

            end:
                match #[lang = "branch"](10) {
                        #[lang = "Break"] {  0: residual } =>
                            #[allow(unreachable_code)]
                            return #[lang = "from_residual"](residual),
                        #[lang = "Continue"] {  0: val } =>
                            #[allow(unreachable_code)]
                            val,
                    },};
    }

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>

@thomasyonug thomasyonug added the C-bug Category: This is a bug. label May 15, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 15, 2024
@jieyouxu jieyouxu added A-proc-macros Area: Procedural macros T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. -Zunpretty Unstable option: unpretty debug print labels May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-Zunpretty Unstable option: unpretty debug print A-proc-macros Area: Procedural macros C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants