Skip to content

ICE with proc-macro expanding to illegal type syntax #66467

Closed
@jfrimmel

Description

@jfrimmel

I've written a proc macro, that consistently produces an ICE. You'll find the code and the backtrace below. Note, that the ICE occurs with any type, that originates in an external crate, that is not included in the Cargo.toml (tested with regex::Regex and spin::Once).

`proc-macro-ice` Source code

Cargo.toml

[package]
name = "proc-macro-ice"
version = "0.1.0"
edition = "2018"

[lib]
proc-macro = true

[dependencies]
quote = "1.0"

src/lib.rs

extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_attribute]
pub fn y(_: TokenStream, _: TokenStream) -> TokenStream {
    let expanded = quote! {
        struct A(regex::Regex);
        static X: A(regex::Regex) = A(regex::Regex::new("a"));
    };
    expanded.into()
}

tests/ice.rs

#[proc_macro_ice::y]
struct S;

Backtrace on cargo test:

error[E0433]: failed to resolve: use of undeclared type or module `regex`
 --> tests/ice.rs:1:1
  |
1 | #[proc_macro_ice::y]
  | ^^^^^^^^^^^^^^^^^^^^ use of undeclared type or module `regex`

thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', /rustc/4f03f4a989d1c8346c19dfb417a77c09b34408b8/src/libcore/macros.rs:41:40
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:77
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:61
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1030
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1412
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:65
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:50
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:188
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:205
  10: rustc_driver::report_ice
  11: <alloc::boxed::Box<F> as core::ops::function::Fn<A>>::call
             at /rustc/4f03f4a989d1c8346c19dfb417a77c09b34408b8/src/liballoc/boxed.rs:956
  12: proc_macro::bridge::client::<impl proc_macro::bridge::Bridge>::enter::{{closure}}::{{closure}}
             at /rustc/4f03f4a989d1c8346c19dfb417a77c09b34408b8/src/libproc_macro/bridge/client.rs:305
  13: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:468
  14: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:373
  15: rust_begin_unwind
             at src/libstd/panicking.rs:302
  16: core::panicking::panic_fmt
             at src/libcore/panicking.rs:141
  17: core::panicking::panic
             at src/libcore/panicking.rs:72
  18: rustc::hir::lowering::LoweringContext::lower_path_segment
  19: <core::iter::adapters::Map<I,F> as core::iter::traits::iterator::Iterator>::fold
  20: <rustc::hir::ptr::P<[T]> as core::iter::traits::collect::FromIterator<T>>::from_iter
  21: rustc::hir::lowering::LoweringContext::lower_qpath
  22: rustc::hir::lowering::LoweringContext::lower_path_ty
  23: rustc::hir::lowering::LoweringContext::lower_ty_direct
  24: rustc::hir::lowering::item::<impl rustc::hir::lowering::LoweringContext>::lower_item_kind
  25: rustc::hir::lowering::item::<impl rustc::hir::lowering::LoweringContext>::lower_item
  26: rustc::hir::lowering::LoweringContext::with_hir_id_owner
  27: <rustc::hir::lowering::item::ItemLowerer as syntax::visit::Visitor>::visit_mod
  28: rustc::hir::lowering::lower_crate
  29: rustc_interface::passes::BoxedResolver::access::{{closure}}
  30: rustc_interface::passes::configure_and_expand::{{closure}}
  31: rustc_data_structures::box_region::PinnedGenerator<I,A,R>::access
  32: rustc_interface::queries::Query<T>::compute
  33: rustc_interface::queries::Query<T>::compute
  34: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::global_ctxt
  35: rustc_interface::interface::run_compiler_in_existing_thread_pool
  36: std::thread::local::LocalKey<T>::with
  37: scoped_tls::ScopedKey<T>::set
  38: syntax::with_globals
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

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.40.0-nightly (4f03f4a98 2019-11-12) running on x86_64-unknown-linux-gnu

note: compiler flags: -C debuginfo=2 -C incremental

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
error: aborting due to previous error

For more information about this error, try `rustc --explain E0433`.
error: could not compile `proc-macro-ice`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions