Gilnaa / memoffset

offsetof for Rust
MIT License
224 stars 29 forks source link

0.5 main macros unusable without importing all macros from crate #15

Closed Gekkio closed 5 years ago

Gekkio commented 5 years ago

The primary macro implementations in memoffset 0.5 use other secondary macros, but don't scope them with $crate (or something like that, I don't actually know what the proper syntax is).

This causes a problem at the use site: if only the main macro is imported, the macro invocation expands to code that won't compile.

use memoffset::{offset_of, span_of};

struct Hmm {
    mmm: u32,
}

fn main() {
    println!("{}", offset_of!(Hmm, mmm));
    println!("{}", span_of!(Hmm, mmm));
}
error: cannot find macro `field_check!` in this scope
 --> src/main.rs:8:20
  |
8 |     println!("{}", offset_of!(Hmm, mmm));
  |                    ^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: cannot find macro `let_base_ptr!` in this scope
 --> src/main.rs:8:20
  |
8 |     println!("{}", offset_of!(Hmm, mmm));
  |                    ^^^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: cannot find macro `let_base_ptr!` in this scope
 --> src/main.rs:9:20
  |
9 |     println!("{}", span_of!(Hmm, mmm));
  |                    ^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: cannot find macro `field_check!` in this scope
 --> src/main.rs:9:20
  |
9 |     println!("{}", span_of!(Hmm, mmm));
  |                    ^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0425]: cannot find value `base_ptr` in this scope
 --> src/main.rs:8:20
  |
8 |     println!("{}", offset_of!(Hmm, mmm));
  |                    ^^^^^^^^^^^^^^^^^^^^ not found in this scope
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0425]: cannot find value `base_ptr` in this scope
 --> src/main.rs:8:20
  |
8 |     println!("{}", offset_of!(Hmm, mmm));
  |                    ^^^^^^^^^^^^^^^^^^^^ not found in this scope
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0425]: cannot find value `root` in this scope
 --> src/main.rs:9:20
  |
9 |     println!("{}", span_of!(Hmm, mmm));
  |                    ^^^^^^^^^^^^^^^^^^ not found in this scope
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0425]: cannot find value `root` in this scope
 --> src/main.rs:9:20
  |
9 |     println!("{}", span_of!(Hmm, mmm));
  |                    ^^^^^^^^^^^^^^^^^^ not found in this scope
  |
  = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to 8 previous errors
aloucks commented 5 years ago

You can use $crate, or #[macro_export(local_inner_macros)] for compatibility with Rust 2015 edition.

https://doc.rust-lang.org/edition-guide/rust-2018/macros/macro-changes.html#macros-using-local_inner_macros

RalfJung commented 5 years ago

I guess that's my fault, sorry. :/

$crate does not work, it fails on CI in the Rust 1.25 job.

RalfJung commented 5 years ago

Should be fixed by https://github.com/Gilnaa/memoffset/pull/16. Does anyone have a suggestion for how to test this on CI? There already is a test with Rust 1.25.