Closed
Description
So I was working on a macro and ran into an rather weird bug regarding imports. It's not really high priority, and I myself found a work around for my specific use case, but here's some info in case it's helpful.
Basically, if you have a macro from another crate that imports something using a macro variable and that thing is already in scope, then rustc will panic.
ie, if you create a crate called "bug_test" with this in src/lib.rs
#[macro_export]
macro_rules! import {
($name:path) => {use $name;};
}
and this in src/main.rs
:
#[macro_use]
extern crate bug_test;
pub struct Foo;
mod bar {
use super::Foo;
import!(Foo);
}
fn main() {}
then running with cargo using RUST_BACKTRACE=full
and --verbose
will crash with the following output:
Compiling bug_test v0.1.0 (/home/anvil/workspace/rust/bug_test)
Running `rustc --edition=2018 --crate-name bug_test src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=fa25b10d441c080d -C extra-filename=-fa25b10d441c080d --out-dir /home/anvil/workspace/rust/bug_test/target/debug/deps -C incremental=/home/anvil/workspace/rust/bug_test/target/debug/incremental -L dependency=/home/anvil/workspace/rust/bug_test/target/debug/deps --extern bug_test=/home/anvil/workspace/rust/bug_test/target/debug/deps/libbug_test-d3b3ef5ed21d5637.rlib`
thread 'rustc' panicked at 'byte index 4286968243 is out of bounds of `$ name`', src/libcore/str/mod.rs:2085:9
stack backtrace:
0: 0x7f7a7826a5c3 - std::sys::unix::backtrace::tracing::imp::unwind_backtrace::h3f9fcc1e0a64f629
at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
1: 0x7f7a78262c28 - std::sys_common::backtrace::_print::he5404c8f6597b2cb
at src/libstd/sys_common/backtrace.rs:70
2: 0x7f7a78266752 - std::panicking::default_hook::{{closure}}::hed3ca28e79a98cf5
at src/libstd/sys_common/backtrace.rs:58
at src/libstd/panicking.rs:200
3: 0x7f7a782664c4 - std::panicking::default_hook::hc7d5b7a9f8bee129
at src/libstd/panicking.rs:215
4: 0x7f7a7450b15f - rustc::util::common::panic_hook::hea1362f8353fa39b
5: 0x7f7a78266f39 - std::panicking::rust_panic_with_hook::h4fcc1c9e88a2f59e
at src/libstd/panicking.rs:482
6: 0x7f7a782669e1 - std::panicking::continue_panic_fmt::h574b633a6765bec0
at src/libstd/panicking.rs:385
7: 0x7f7a782668c5 - rust_begin_unwind
at src/libstd/panicking.rs:312
8: 0x7f7a78290bfc - core::panicking::panic_fmt::h67b961e583e2baf5
at src/libcore/panicking.rs:85
9: 0x7f7a7829316d - core::str::slice_error_fail::hd643105be0239058
at src/libcore/str/mod.rs:0
10: 0x7f7a77374268 - core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::RangeTo<usize>>::index::{{closure}}::h609d9562139157ca
11: 0x7f7a773afd82 - rustc_resolve::Resolver::report_conflict::hd415e900bec39b7a
12: 0x7f7a7743aca3 - rustc_resolve::resolve_imports::ImportResolver::resolve_imports::hd8850b22213dc988
13: 0x7f7a77376fbd - rustc_resolve::macros::<impl syntax::ext::base::Resolver for rustc_resolve::Resolver<'a>>::resolve_imports::h2d11e4e31ea47e62
14: 0x7f7a7365c4a8 - syntax::ext::expand::MacroExpander::expand_fragment::h8f366f0be003c3ad
15: 0x7f7a7365a083 - syntax::ext::expand::MacroExpander::expand_crate::hb28fa852bcb14830
16: 0x7f7a785e66f4 - rustc_driver::driver::phase_2_configure_and_expand_inner::{{closure}}::hd92c6e0a2bfeeb04
17: 0x7f7a785dfe1c - rustc::util::common::time::h8ed05c6fbfb0cf6a
18: 0x7f7a786445dd - rustc_driver::driver::phase_2_configure_and_expand::h9030f89e2fe30a9a
19: 0x7f7a7863fed1 - rustc_driver::driver::compile_input::hcd14a9896da62839
20: 0x7f7a785bd560 - rustc_driver::run_compiler_with_pool::h7023af6e331d7529
21: 0x7f7a7858e9a5 - <scoped_tls::ScopedKey<T>>::set::h9be799eb2aff87c4
22: 0x7f7a785bc3aa - rustc_driver::run_compiler::h7b424221cc9d2064
23: 0x7f7a7858eb5a - <scoped_tls::ScopedKey<T>>::set::hba74b8ced87ab1cf
24: 0x7f7a7861b0f2 - std::sys_common::backtrace::__rust_begin_short_backtrace::h8f9a3d09f5c7481b
25: 0x7f7a782784e9 - __rust_maybe_catch_panic
at src/libpanic_unwind/lib.rs:92
26: 0x7f7a78632720 - <F as alloc::boxed::FnBox<A>>::call_box::hcfdf6bd7964eaa60
27: 0x7f7a782772ad - std::sys::unix::thread::Thread::new::thread_start::h8540a9e9e9bc02d2
at /rustc/c76f3c374f80e20b612601a3c375666a58ac3971/src/liballoc/boxed.rs:744
at src/libstd/sys_common/thread.rs:14
at src/libstd/sys/unix/thread.rs:81
28: 0x7f7a781a7a9c - start_thread
29: 0x7f7a780c8b22 - clone
30: 0x0 - <unknown>
query stack during panic:
end of query stack
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: rustc 1.33.0-nightly (c76f3c374 2019-01-18) running on x86_64-unknown-linux-gnu
note: compiler flags: -C debuginfo=2 -C incremental --crate-type bin
note: some of the compiler flags provided by cargo are hidden
error: Could not compile `bug_test`.
Caused by:
process didn't exit successfully: `rustc --edition=2018 --crate-name bug_test src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=fa25b10d441c080d -C extra-filename=-fa25b10d441c080d --out-dir /home/anvil/workspace/rust/bug_test/target/debug/deps -C incremental=/home/anvil/workspace/rust/bug_test/target/debug/incremental -L dependency=/home/anvil/workspace/rust/bug_test/target/debug/deps --extern bug_test=/home/anvil/workspace/rust/bug_test/target/debug/deps/libbug_test-d3b3ef5ed21d5637.rlib` (exit code: 101)
Some additional information:
- The bug goes away perfectly fine when the macro is in the same crate as the call, and it errors about duplicate names as you would expect
- You also do need it to be importing the same thing twice, otherwise it compiles just fine
- The fact that the fragment specifier is
:path
doesn't matter. It crashes even when you use:ident