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

Compiler panic - $crate in proc macro #49706

Closed
laurmaedje opened this issue Apr 5, 2018 · 7 comments
Closed

Compiler panic - $crate in proc macro #49706

laurmaedje opened this issue Apr 5, 2018 · 7 comments
Assignees
Labels
A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@laurmaedje
Copy link
Contributor

The following code uses the idea from proc-macro-hack to custom derive on a struct, return a macro from that derive and then invoke the macro.
I was trying what $crate would do in this context and expected that my code would just break.

But instead the compiler broke:

thread 'rustc' panicked at 'no entry found for key', libcore/option.rs:917:5
note: the compiler unexpectedly panicked. this is a bug.

This script reproduces the issue:

#!/bin/bash

cargo new --bin bug
cd bug

# proc macro
cargo new bug-macro
cat >> bug-macro/Cargo.toml <<-'EOF'
    quote = "0.4"

    [lib]
    proc-macro = true
EOF

cat > bug-macro/src/lib.rs <<-'EOF'
    #[macro_use]
    extern crate quote;
    extern crate proc_macro;
    use proc_macro::TokenStream;

    #[proc_macro_derive(Test)]
    pub fn test_derive(_: TokenStream) -> TokenStream {
        (quote! {
            macro_rules! result {
                () => ($crate::Data)         // <-- this is the problem
            }
        }).into()
    }
EOF

# binary crate
cat >> Cargo.toml <<-'EOF'
    bug-macro = { path = "bug-macro" }
EOF

cat > src/main.rs <<-'EOF'
    #[macro_use]
    extern crate bug_macro;

    struct Data;

    fn main() {
        #[derive(Test)]
        struct Test;
        result!()
    }
EOF

cargo build

Meta

rustc 1.25.0-nightly (b1f8e6f 2018-02-22)
binary: rustc
commit-hash: b1f8e6f
commit-date: 2018-02-22
host: x86_64-unknown-linux-gnu
release: 1.25.0-nightly
LLVM version: 6.0

@dtolnay
Copy link
Member

dtolnay commented Apr 6, 2018

Backtrace on rustc 1.27.0-nightly (48fa6f9 2018-04-05):

   9: core::option::expect_failed
             at libcore/option.rs:916
  10: rustc_resolve::build_reduced_graph::<impl rustc_resolve::Resolver<'a>>::macro_def_scope
  11: rustc_resolve::Resolver::resolve_crate_root
  12: rustc_resolve::Resolver::resolve_path
  13: rustc_resolve::Resolver::smart_resolve_path_fragment
  14: rustc_resolve::Resolver::resolve_expr
  15: <rustc_resolve::Resolver<'a> as syntax::visit::Visitor<'tcx>>::visit_block
  16: <rustc_resolve::Resolver<'a> as syntax::visit::Visitor<'tcx>>::visit_fn
  17: syntax::visit::walk_item
  18: rustc_resolve::Resolver::resolve_item
  19: rustc_resolve::Resolver::resolve_crate
  20: rustc::util::common::time
  21: rustc_driver::driver::phase_2_configure_and_expand
  22: rustc_driver::driver::compile_input
  23: rustc_driver::run_compiler_impl
  24: syntax::with_globals
  25: rustc_driver::run
  26: rustc_driver::main

@pietroalbini pietroalbini added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 12, 2018
@nikomatsakis
Copy link
Contributor

Is this truly a regression? It seems like it's "just an ICE"? If it is a regression, can we bisect?

@pnkfelix
Copy link
Member

self-assigning to make sure we get answers to @nikomatsakis questions above.

@pnkfelix pnkfelix self-assigned this Apr 12, 2018
@pnkfelix
Copy link
Member

triage: P-high

the priority reflects my interest in getting answers to those questins. but if its "just an ICE" and not much esle (and not a true regression) then we can readily downgrade to P-medium.

@dtolnay
Copy link
Member

dtolnay commented Apr 12, 2018

Yes I believe this is a regression. If you change the result!() in the repro script to let _ = result!(); to make it type check, then this compiles with 1.23.0 but ICEs since 1.24.0.

But this is a macro_rules emitted by a custom derive which seems like a rehash of #46478, in which we decided we did not mean to support this anyway.

@nikomatsakis nikomatsakis added the P-high High priority label Apr 26, 2018
@pietroalbini pietroalbini added this to the 1.27 milestone Apr 30, 2018
@pietroalbini pietroalbini added regression-from-stable-to-beta Performance or correctness regression from stable to beta. and removed regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels May 9, 2018
@alexcrichton
Copy link
Member

Removing this from the 1.27 milestone and the 1.27 regressions as this looks to be a stable -> stable regression according to @dtolnay's comment

@alexcrichton alexcrichton removed this from the 1.27 milestone May 10, 2018
@alexcrichton alexcrichton added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels May 10, 2018
@nikomatsakis
Copy link
Contributor

Discussing in @rust-lang/compiler meeting. If indeed this is a rehash of #46478, then it seems we should close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-high High priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants