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

Macro causes panic #2636

Closed
ambaxter opened this issue Apr 6, 2018 · 3 comments
Closed

Macro causes panic #2636

ambaxter opened this issue Apr 6, 2018 · 3 comments
Labels
I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️ T-macros Type: Issues with macros and macro expansion

Comments

@ambaxter
Copy link

ambaxter commented Apr 6, 2018

The macro in question from https://github.com/ambaxter/expert-rs/blob/master/src/network/node_cmp.rs#L514

macro_rules! test_hash {
    ($($t:ident => $ord:expr),+ ) => {
        impl<T:Fact>Hash for TestData<T> {
            fn hash < H: Hasher > ( & self, state: & mut H) {
                use self::TestData::*;
                    match self {
                    $ ( & $ t(accessor, ref test) => Self::hash_self($ord, accessor as usize, test, state),
                    )*
                }
            }
        }
    };
}


test_hash!(I8 => 0, I16 => 1, I32 => 2, I64 => 3,
    U8 => 4, U16 => 5, U32 => 6, U64 => 7,
    ISIZE => 8, USIZE => 9,
    F32 => 10, F64 => 11,
    STR => 12
);

Backtrace output:

warning: you don't need to add `&` to all patterns
   --> src/network/node_cmp.rs:519:21
    |
519 | /                     match self {
520 | |                     $ ( & $ t(accessor, ref test) => Self::hash_self($ord, accessor as usize, test, state),
521 | |                     )*
522 | |                 }
    | |_________________^
...
529 | / test_hash!(I8 => 0, I16 => 1, I32 => 2, I64 => 3,
530 | |     U8 => 4, U16 => 5, U32 => 6, U64 => 7,
531 | |     ISIZE => 8, USIZE => 9,
532 | |     F32 => 10, F64 => 11,
533 | |     STR => 12
534 | | );
    | |__- in this macro invocation
    |
    = help: for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.192/index.html#match_ref_pats
thread 'main' panicked at 'begin <= end (49 <= 24) when slicing `                    $ ( & $ t(accessor, ref test) => Self::hash_self($ord, accessor as usize, test, state),`', libcore/str/mod.rs:2244:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
   1: std::sys_common::backtrace::print
   2: std::panicking::default_hook::{{closure}}
   3: std::panicking::default_hook
   4: core::ops::function::Fn::call
   5: std::panicking::rust_panic_with_hook
   6: std::panicking::begin_panic_fmt
   7: rust_begin_unwind
   8: core::panicking::panic_fmt
   9: core::str::slice_error_fail
  10: core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::Range<usize>>::index::{{closure}}
  11: rustc_errors::CodeSuggestion::splice_lines::push_trailing
  12: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &'a mut F>::call_once
  13: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T, I>>::from_iter
  14: <rustc_errors::emitter::EmitterWriter as rustc_errors::emitter::Emitter>::emit
  15: rustc_errors::Handler::emit_db
  16: rustc_errors::diagnostic_builder::DiagnosticBuilder::emit
  17: clippy_lints::utils::span_lint_and_then
  18: <clippy_lints::matches::MatchPass as rustc::lint::LateLintPass<'a, 'tcx>>::check_expr
  19: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  20: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_block
  21: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  22: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_body
  23: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_nested_body
  24: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_fn
  25: rustc::hir::intravisit::walk_impl_item
  26: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_impl_item
  27: rustc::hir::intravisit::walk_impl_item_ref
  28: rustc::hir::intravisit::walk_item
  29: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_item
  30: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_mod
  31: rustc::hir::intravisit::walk_item
  32: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_item
  33: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_mod
  34: rustc::hir::intravisit::walk_item
  35: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_item
  36: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_mod
  37: rustc::hir::intravisit::walk_crate
  38: rustc::lint::context::check_crate
  39: rustc::ty::context::tls::enter_context
  40: <std::thread::local::LocalKey<T>>::with
  41: rustc::ty::context::TyCtxt::create_and_enter
  42: rustc_driver::driver::compile_input
  43: rustc_driver::run_compiler_impl
  44: syntax::with_globals
  45: rustc_driver::run_compiler
  46: rustc_driver::run
  47: clippy_driver::main
  48: std::rt::lang_start::{{closure}}
  49: std::panicking::try::do_call
  50: __rust_maybe_catch_panic
  51: std::rt::lang_start_internal
  52: main
error: Could not compile `expert`.

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

@oli-obk oli-obk added the I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️ label Apr 7, 2018
@phansch phansch added the T-macros Type: Issues with macros and macro expansion label Jul 17, 2018
@phansch
Copy link
Member

phansch commented Jul 18, 2018

I extracted a minimal crashing example with the current Clippy and rustc 1.29.0-nightly (4f3c7a472 2018-07-17):

use std::hash::{Hash, Hasher};

fn main() {}

pub trait Fact: 'static + Eq + Hash { }

pub enum TestData<T: Fact> {
    F32(fn(&T) -> &f32, Vec<u16>),
    F64(fn(&T) -> &f64, Vec<u16>),
}

impl<T: Fact> TestData<T> {
    fn hash_self() {}
}

macro_rules! test_hash {
    ($($t:ident => $ord:expr),+ ) => {
        impl<T:Fact>Hash for TestData<T> {
            fn hash < H: Hasher > ( & self, _state: & mut H) {
                use self::TestData::*;
                    match self {
                    $ ( & $ t(_accessor, ref _test) => Self::hash_self(),
                    )*
                }
            }
        }
    };
}


test_hash!(F32 => 10, F64 => 11);

The backtrace with the relevant linenumbers:

thread 'main' panicked at 'begin <= end (51 <= 24) when slicing `                    $ ( & $ t(_accessor, ref _test) => Self::hash_self(),`', libcore/str/mod.rs:2098:5
stack backtrace:                                                                                                                                
   0:     0x7fd90255c47e - std::sys::unix::backtrace::tracing::imp::unwind_backtrace::h53bc450f4e05b019                                        
                               at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49                 
   1:     0x7fd902533d66 - std::sys_common::backtrace::print::h04a0ea06a78a82c5                 
                               at libstd/sys_common/backtrace.rs:71                             
                               at libstd/sys_common/backtrace.rs:59                             
   2:     0x7fd90256549d - std::panicking::default_hook::{{closure}}::h13c5ed342ec5a2b6                                                  
                               at libstd/panicking.rs:211                                       
   3:     0x7fd902565210 - std::panicking::default_hook::h72a62d53239218b1                                                               
                               at libstd/panicking.rs:227                                       
   4:     0x7fd90414a1d5 - rustc::util::common::panic_hook::h168384fcf80a795b                                                           
   5:     0x7fd902565b63 - std::panicking::rust_panic_with_hook::h60a37fb79e459606              
                               at libstd/panicking.rs:479                                                                                                  
   6:     0x7fd902565709 - std::panicking::continue_panic_fmt::hb542a58a3d52cb5c                
                               at libstd/panicking.rs:390                                                                              
   7:     0x7fd902565605 - rust_begin_unwind                                                    
                               at libstd/panicking.rs:325                                                                           
   8:     0x7fd9025d96fb - core::panicking::panic_fmt::h68c841fe431ac371             
                               at libcore/panicking.rs:77                 
   9:     0x7fd9025beb39 - core::str::slice_error_fail::h93a5792dbea52a7c              
                               at libcore/str/mod.rs:0   
  10:     0x7fd903180e00 - core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::Range<usize>>::index::{{closure}}::h18cc22f14291afb5
  11:     0x7fd903181766 - rustc_errors::CodeSuggestion::splice_lines::push_trailing::hd4c79701b80d10b3
  12:     0x7fd90317fcd5 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &'a mut F>::call_once::h78b32457629d864c
  13:     0x7fd903174cb3 - <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T, I>>::from_iter::h5c6488beec1d678b
  14:     0x7fd90315ef41 - <rustc_errors::emitter::EmitterWriter as rustc_errors::emitter::Emitter>::emit::hc32c70a132b377c8
  15:     0x7fd9031839fc - rustc_errors::Handler::emit_db::h9df80e598f0d45b8                    
  16:     0x7fd903169f67 - rustc_errors::diagnostic_builder::DiagnosticBuilder::emit::hcdea8bf6febd0a27
  17:     0x55bf7ce7aced - <clippy_lints::utils::DiagnosticWrapper<'a> as core::ops::drop::Drop>::drop::h10e91841851ebbcf          
                               at clippy_lints/src/utils/mod.rs:547       
  18:     0x55bf7cbfe680 - core::ptr::drop_in_place::hb33effe3f8e59aaa  
                               at /checkout/src/libcore/ptr.rs:59       
  19:     0x55bf7cb7c182 - clippy_lints::utils::span_lint_and_then::h2c9632d3a6e04427           
                               at clippy_lints/src/utils/mod.rs:608                   
  20:     0x55bf7cbdd275 - clippy_lints::matches::check_match_ref_pats::h3d17a24e0bbaa6bd
                               at clippy_lints/src/matches.rs:431                    
  21:     0x55bf7cbdb149 - <clippy_lints::matches::MatchPass as rustc::lint::LateLintPass<'a, 'tcx>>::check_expr::he32b70248af29f29
                               at clippy_lints/src/matches.rs:195                    

If someone wants to have a go at this crash, feel free!

phansch added a commit to phansch/rust-clippy that referenced this issue Sep 6, 2018
I'm not sure what exact change fixed the issue, but the minimal example
doesn't crash anymore. Therefore this..

Closes rust-lang#2636
@phansch phansch assigned phansch and unassigned phansch Oct 2, 2018
@matthiaskrgr
Copy link
Member

This panic is also triggered when checking quick-error-0.1.4.

@flip1995
Copy link
Member

Even more minimal example: Playground

#![allow(dead_code)]

enum Foo {
    A,
    B,
    C,
}

macro_rules! test_hash {
    ($foo:expr, $($t:ident => $ord:expr),+ ) => {
        use self::Foo::*;
        match $foo {
            $ ( & $t => $ord,
            )*
        };
    };
}

fn main() {
    let a = Foo::A;
    test_hash!(&a, A => 0, B => 1, C => 2);
}

Relevant backtrace:

   8: core::panicking::panic_fmt
             at libcore/panicking.rs:77
   9: core::str::slice_error_fail
             at libcore/str/mod.rs:0
  10: core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::Range<usize>>::index::{{closure}}
  11: rustc_errors::CodeSuggestion::splice_lines::push_trailing
  12: <core::iter::Map<I, F> as core::iter::iterator::Iterator>::fold
  13: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T, I>>::from_iter
  14: <rustc_errors::emitter::EmitterWriter as rustc_errors::emitter::Emitter>::emit
  15: rustc_errors::Handler::emit_db
  16: rustc_errors::diagnostic_builder::DiagnosticBuilder::emit
  17: <clippy_lints::utils::DiagnosticWrapper<'a> as core::ops::drop::Drop>::drop
             at clippy_lints/src/utils/mod.rs:534
  18: core::ptr::drop_in_place
             at /rustc/6f93e93af6f823948cc13d2938957757c6486d88/src/libcore/ptr.rs:194
  19: clippy_lints::utils::span_lint_and_then
             at clippy_lints/src/utils/mod.rs:595
  20: clippy_lints::matches::check_match_ref_pats
             at clippy_lints/src/matches.rs:459
  21: <clippy_lints::matches::MatchPass as rustc::lint::LateLintPass<'a, 'tcx>>::check_expr
             at clippy_lints/src/matches.rs:218

bors bot added a commit that referenced this issue Nov 19, 2018
3432: match_ref_pats: don't emit suggestions inside of a macro r=Manishearth a=flip1995

Fixes #2636 

Co-authored-by: flip1995 <hello@philkrones.com>
@bors bors bot closed this as completed in #3432 Nov 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️ T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants