Rust: unexpected panic with path+idents in Macro

Created on 3 May 2017  路  4Comments  路  Source: rust-lang/rust

I tried this code:
lib.rs

#![feature(associated_consts)]
#![feature(concat_idents)]
#![feature(use_extern_macros)]

#[macro_use]
mod macros;
// m.foo = regs[GlobalRegisters::r_kon] 
macro_rules! reg {
    ($n:path) => {
        {
            m.regs[GlobalRegisters::concat_idents!(r_, $n)]
        }
    }



md5-b3e8625f7d9a709b31eb4effeeb02f3a



rustc 1.19.0-nightly (777ee2079 2017-05-01)
binary: rustc
commit-hash: 777ee20796e80a31d4b7c985dd68eda2941460d6
commit-date: 2017-05-01
host: x86_64-unknown-linux-gnu
release: 1.19.0-nightly
LLVM version: 4.0



md5-50c8b86995080652bcd1ca4f942c8e51



   Compiling SPC_DSP v0.1.0 (file:///home/insi/Projects/smpd/SPC_DSP)
     Running `rustc --crate-name SPC_DSP src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=32ecbd31718c60b7 -C extra-filename=-32ecbd31718c60b7 --out-dir /home/insi/Projects/smpd/SPC_DSP/target/debug/deps -L dependency=/home/insi/Projects/smpd/SPC_DSP/target/debug/deps`
error: expected type, found `'a`
  --> src/state.rs:46:26
   |
46 |     pub fn create() -> &<'a> mut State<'a> {
   |                          ^^

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: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'Expected Def::Macro(..)', /checkout/src/librustc_resolve/build_reduced_graph.rs:513
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
             at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at /checkout/src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at /checkout/src/libstd/sys_common/backtrace.rs:60
             at /checkout/src/libstd/panicking.rs:355
   3: std::panicking::default_hook
             at /checkout/src/libstd/panicking.rs:365
   4: std::panicking::rust_panic_with_hook
             at /checkout/src/libstd/panicking.rs:549
   5: std::panicking::begin_panic
   6: rustc_resolve::build_reduced_graph::<impl rustc_resolve::Resolver<'a>>::get_macro
   7: rustc_resolve::macros::<impl syntax::ext::base::Resolver for rustc_resolve::Resolver<'a>>::resolve_invoc
   8: syntax::ext::expand::MacroExpander::expand
   9: syntax::ext::expand::MacroExpander::expand_crate
  10: rustc_driver::driver::phase_2_configure_and_expand::{{closure}}
  11: rustc_driver::driver::phase_2_configure_and_expand
  12: rustc_driver::driver::compile_input
  13: rustc_driver::run_compiler
  14: std::panicking::try::do_call
  15: __rust_maybe_catch_panic
             at /checkout/src/libpanic_unwind/lib.rs:98
  16: <F as alloc::boxed::FnBox<A>>::call_box
  17: std::sys::imp::thread::Thread::new::thread_start
             at /checkout/src/liballoc/boxed.rs:658
             at /checkout/src/libstd/sys_common/thread.rs:21
             at /checkout/src/libstd/sys/unix/thread.rs:84
  18: start_thread
  19: clone

error: Could not compile `SPC_DSP`.

Caused by:
  process didn't exit successfully: `rustc --crate-name SPC_DSP src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=32ecbd31718c60b7 -C extra-filename=-32ecbd31718c60b7 --out-dir /home/insi/Projects/smpd/SPC_DSP/target/debug/deps -L dependency=/home/insi/Projects/smpd/SPC_DSP/target/debug/deps` (exit code: 101)

Hope this helps. I tried searching for an issue but did not see one

A-macros A-resolve C-bug I-ICE

Most helpful comment

First off this use of concat_idents seems suspicious, first because it operates on idents, not paths, and also it doesn't look like your macro will work due to hygiene (e.g. even this doesn't work). In general, the process of using concat_idents is as follows:

  1. I have a problem
  2. I can use concat_idents to solve this problem!
  3. This problem is unsolvable

Anyhow, there shouldn't be a panic, but the error that's printed doesn't seem to have to do with the macro you showed. Can we see the rest of the code, especially around line 46?

All 4 comments

First off this use of concat_idents seems suspicious, first because it operates on idents, not paths, and also it doesn't look like your macro will work due to hygiene (e.g. even this doesn't work). In general, the process of using concat_idents is as follows:

  1. I have a problem
  2. I can use concat_idents to solve this problem!
  3. This problem is unsolvable

Anyhow, there shouldn't be a panic, but the error that's printed doesn't seem to have to do with the macro you showed. Can we see the rest of the code, especially around line 46?

Ah, now I see the problem, it is in fact looking for the macro GlobalRegisters::concat_idents. Minimized:

#![feature(use_extern_macros)]

fn main() {
    enum Foo {}
    let _ = Foo::bar!();
}

cc @jseyfried

(My sense remains that even after we fix the ICE, this approach to your problem is not going anywhere.)

Thanks!

Yeah I thought it was mostly my fault (I am in the process of learning), but still found it odd that the compiler should panic. In any case, hopefully I helped through my own misdirection!

The same ICE affects derive macros as well (from #43921).

#![feature(use_extern_macros)]

enum Foo {}

#[derive(Foo::bar)]
struct A {}
Was this page helpful?
0 / 5 - 0 ratings