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

Unexpected comment position after macro expansion #125239

Closed
thomasyonug opened this issue May 18, 2024 · 1 comment
Closed

Unexpected comment position after macro expansion #125239

thomasyonug opened this issue May 18, 2024 · 1 comment
Labels
A-pretty Area: Pretty printing. C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@thomasyonug
Copy link

I tried this code:

// macro by example
#[macro_export]
macro_rules! mdriver {
    ($f:ident) => {
        #[no_mangle]
        pub fn lookup_inc(buf: &[u8; 5], $f: Bar) -> u8 {
            buf[$f as usize + 1]
        }
    };
}

// macro call 
#[derive(Clone, Copy)]
enum Bar { A, B, C, D, E }

mdriver!(A);

mod test_module {
    use super::*;
    mdriver!(B); // Reusing in a module for diversity
}

I expected to see this happen:

#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
extern crate mbe_1080819556052783730;
use mbe_1080819556052783730::mdriver;

enum Bar { A, B, C, D, E, }
#[automatically_derived]
impl ::core::clone::Clone for Bar {
    #[inline]
    fn clone<'_>(self: &'_ Self) -> Bar { *self }
}
#[automatically_derived]
impl ::core::marker::Copy for Bar { }


#[no_mangle]
fn lookup_inc<'_>(buf: &'_ [u8; 5], A: Bar) -> u8 { buf[A as usize + 1] }
mod test_module {
    use super::*;
    // Reusing in a module for diversity
    #[no_mangle]
    fn lookup_inc<'_>(buf: &'_ [u8; 5], B: Bar) -> u8 { buf[B as usize + 1] }
}

Instead, this happened:

#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;
extern crate mbe_1080819556052783730;
use mbe_1080819556052783730::mdriver;

enum Bar { A, B, C, D, E, }
#[automatically_derived]
impl ::core::clone::Clone for Bar {
    #[inline]
    fn clone<'_>(self: &'_ Self) -> Bar { *self }
}
#[automatically_derived]
impl ::core::marker::Copy for Bar { }


// Reusing in a module for diversity
#[no_mangle]
fn lookup_inc<'_>(buf: &'_ [u8; 5], A: Bar) -> u8 { buf[A as usize + 1] }
mod test_module {
    use super::*;
    #[no_mangle]
    fn lookup_inc<'_>(buf: &'_ [u8; 5], B: Bar) -> u8 { buf[B as usize + 1] }
}

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 18, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 18, 2024
@bjorn3
Copy link
Member

bjorn3 commented May 18, 2024

Comments are not preserved in the AST. -Zunpretty=expanded and similar try to preserve comments by guessing where they should be placed. As you noticed this is always accurate. This is not a bug as comment preservation is best effort.

@bjorn3 bjorn3 added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. labels May 18, 2024
@fmease fmease changed the title Unexpected comment position after macro expension Unexpected comment position after macro expansion May 18, 2024
@fmease fmease added A-pretty Area: Pretty printing. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 18, 2024
@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pretty Area: Pretty printing. C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

4 participants