Skip to content

internal compiler error: unexpected panic using attributes #41244

Description

@qorost

The compiler failed when using a self defined attribute (compiler plugin), the code is as below:

The source code to use it

#![feature(plugin)]
#![feature(custom_attribute)]
#![plugin(myplugin)]

#[check]
fn main() {
    let x = hello_AA!();
    println!("x.x = {}",x.x);
}

The source code of the plugin code (built successfully)

#![crate_type = "dylib"]
#![feature(plugin_registrar, quote, rustc_private,debug)]
#![feature(box_syntax, rustc_private)]

#![allow(dead_code)]
#![allow(unused_imports)]
extern crate syntax;
#[macro_use]
extern crate rustc;
extern crate rustc_plugin;
extern crate syntax_pos;

use syntax::ast::{self, StructField, Unsafety};
use syntax::codemap::DUMMY_SP;
use syntax::ext::base::*;
use syntax::ext::quote::rt::ToTokens;
use syntax::parse::{self, token};
use syntax::ptr::P;
use syntax::symbol::Symbol;
use syntax::tokenstream::TokenTree;
use syntax_pos::Span;
use rustc_plugin::Registry;


//for fold
use syntax::codemap::{BytePos, Spanned};
use std::fmt::{self, Display, Formatter};
use syntax::fold::{self, Folder};
use syntax::ext::build::AstBuilder;
use syntax::ast::{BinOpKind, Block, Expr, ExprKind, Item, ItemKind, Lit,
                  LitKind, Mac, MetaItem, MetaItemKind, NestedMetaItemKind,
                  Path, PathSegment, Stmt, StmtKind, UnOp,FnDecl};
use syntax::util::small_vector::SmallVector;

struct Refchecker<'a, 'cx: 'a> {
    cx: &'a mut ExtCtxt<'cx>,
    left: bool, //tell if the expr is in the left of the statment
}

impl<'a,'cx> Folder for Refchecker<'a, 'cx> {
}

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
    #[cfg(feature = "debugmyplugin")]
    println!("DEBUG: IN the plugin_registrar expand_checkderef");
    reg.register_syntax_extension(
        Symbol::intern("check"),
        // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
        //MultiModifier(Box::new(expand_newcheckderef)));
        MultiModifier(Box::new(|cx: &mut ExtCtxt, _span: Span, mi: &MetaItem, a: Annotatable| {
           let mut checker = &mut Refchecker {
                cx: cx,
                left: false,
            };
            match a {
                Annotatable::Item(i) => Annotatable::Item (
                    checker.fold_item(i).expect_one("expected exactly one item")),
                Annotatable::TraitItem(i) => Annotatable::TraitItem (
                    i.map(|i| checker.fold_trait_item(i).expect_one("expected exactly one item"))),
                Annotatable::ImplItem(i) => Annotatable::ImplItem(
                    i.map(|i| checker.fold_impl_item(i).expect_one("expected exactly one item"))),
            }
        })));
}

The error mesage:

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

thread 'rustc' panicked at 'fold_mac disabled by default', /checkout/src/libsyntax/fold.rs:180
note: Run with `RUST_BACKTRACE=1` for a backtrace.

backtrace

thread 'rustc' panicked at 'fold_mac disabled by default', /checkout/src/libsyntax/fold.rs:180
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: syntax::fold::Folder::fold_mac
             at ./<panic macros>:3
   1: syntax::fold::noop_fold_expr
             at /checkout/src/libsyntax/fold.rs:1256
   2: syntax::fold::Folder::fold_expr::{{closure}}
             at /checkout/src/libsyntax/fold.rs:112
   3: <syntax::ptr::P<T>>::map
             at /checkout/src/libsyntax/ptr.rs:88
   4: syntax::fold::Folder::fold_expr
             at /checkout/src/libsyntax/fold.rs:112
   5: syntax::fold::noop_fold_local::{{closure}}::{{closure}}
             at /checkout/src/libsyntax/fold.rs:482
   6: <core::option::Option<T>>::map
             at /checkout/src/libcore/option.rs:392
   7: syntax::fold::noop_fold_local::{{closure}}
             at /checkout/src/libsyntax/fold.rs:482
   8: <syntax::ptr::P<T>>::map
             at /checkout/src/libsyntax/ptr.rs:88
   9: syntax::fold::noop_fold_local
             at /checkout/src/libsyntax/fold.rs:478
  10: syntax::fold::Folder::fold_local
             at /checkout/src/libsyntax/fold.rs:176
  11: syntax::fold::noop_fold_stmt_kind
             at /checkout/src/libsyntax/fold.rs:1299
  12: syntax::fold::noop_fold_stmt
             at /checkout/src/libsyntax/fold.rs:1292
  13: syntax::fold::Folder::fold_stmt
             at /checkout/src/libsyntax/fold.rs:100
  14: syntax::fold::noop_fold_block::{{closure}}::{{closure}}
             at /checkout/src/libsyntax/fold.rs:844
  15: <collections::vec::Vec<T> as syntax::util::move_map::MoveMap<T>>::move_flat_map
             at /checkout/src/libsyntax/util/move_map.rs:40
  16: syntax::fold::noop_fold_block::{{closure}}
             at /checkout/src/libsyntax/fold.rs:844
  17: <syntax::ptr::P<T>>::map
             at /checkout/src/libsyntax/ptr.rs:88
  18: syntax::fold::noop_fold_block
             at /checkout/src/libsyntax/fold.rs:842
  19: syntax::fold::Folder::fold_block
             at /checkout/src/libsyntax/fold.rs:96
  20: syntax::fold::noop_fold_item_kind
             at /checkout/src/libsyntax/fold.rs:865
  21: syntax::fold::Folder::fold_item_kind
             at /checkout/src/libsyntax/fold.rs:80
  22: syntax::fold::noop_fold_item_simple
             at /checkout/src/libsyntax/fold.rs:1013
  23: syntax::fold::Folder::fold_item_simple
             at /checkout/src/libsyntax/fold.rs:72
  24: syntax::fold::noop_fold_item::{{closure}}
             at /checkout/src/libsyntax/fold.rs:1002
  25: <syntax::ptr::P<T>>::map
             at /checkout/src/libsyntax/ptr.rs:88
  26: syntax::fold::noop_fold_item
             at /checkout/src/libsyntax/fold.rs:1002
  27: syntax::fold::Folder::fold_item
             at /checkout/src/libsyntax/fold.rs:68
  28: myplugin::plugin_registrar::{{closure}}
             at ./myplugin/src/lib.rs:83
  29: <F as syntax::ext::base::MultiItemModifier>::expand
             at /checkout/src/libsyntax/ext/base.rs:143
  30: syntax::ext::expand::MacroExpander::expand_invoc
  31: syntax::ext::expand::MacroExpander::expand
  32: syntax::ext::expand::MacroExpander::expand_crate
  33: rustc_driver::driver::phase_2_configure_and_expand::{{closure}}
  34: rustc_driver::driver::phase_2_configure_and_expand
  35: rustc_driver::driver::compile_input
  36: rustc_driver::run_compiler
  37: std::panicking::try::do_call
  38: __rust_maybe_catch_panic
             at /checkout/src/libpanic_unwind/lib.rs:98
  39: <F as alloc::boxed::FnBox<A>>::call_box
  40: std::sys::imp::thread::Thread::new::thread_start
             at /checkout/src/liballoc/boxed.rs:650
             at /checkout/src/libstd/sys_common/thread.rs:21
             at /checkout/src/libstd/sys/unix/thread.rs:84
  41: start_thread
  42: clone

error: Could not compile `macro_compiler_conflicts`.

Running on:
rustc 1.18.0-nightly (c58c928 2017-04-11)

System

No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 16.04.2 LTS
Release:	16.04
Codename:	xenial

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions