rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.46k stars 1.55k forks source link

ICE when running on rustc sync-send-iterators-in-libcollections.rs #3144

Closed matthiaskrgr closed 5 years ago

matthiaskrgr commented 6 years ago

File https://github.com/rust-lang/rust/blob/master/src/test/run-pass/sync-send-iterators-in-libcollections.rs

reduced:

use std::collections::HashSet;

fn is_sync<T>(_: T) where T: Sync {}
fn is_send<T>(_: T) where T: Send {}

macro_rules! is_sync_send {
    ($ctor:expr, $iter:ident($($param:expr),+)) => ({
        let  x = $ctor;
        is_sync(x.$iter($( $param ),+));
        let  y = $ctor;
        is_send(y.$iter($( $param ),+));
    })
}

fn main() {
    is_sync_send!(HashSet::<usize>::new(), union(&HashSet::<usize>::new()));
}
RUST_BACKTRACE=Full LD_LIBRARY_PATH=~/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/ CLIPPY_TESTS=true  CARGO_MANIFEST_DIR="." ~/vcs/github/rust-clippy/target/debug/clippy-driver sync-send-iterators-in-libcollections.rs

backtrace:

error: internal compiler error: librustc/infer/canonical/canonicalizer.rs:351: failed to lift `QueryResult { var_values: CanonicalVarValues { var_values: [] }, region_constraints: [Binder(OutlivesPredicate('?0, ReErased)), Binder(OutlivesPredicate(ReErased, '?0))], certainty: Proven, value: NormalizationResult { normalized_ty: std::collections::hash_map::RandomState } }`, canonicalized from `QueryResult { var_values: CanonicalVarValues { var_values: [] }, region_constraints: [Binder(OutlivesPredicate('_#0r, ReErased)), Binder(OutlivesPredicate(ReErased, '_#0r))], certainty: Proven, value: NormalizationResult { normalized_ty: std::collections::hash_map::RandomState } }`
thread 'main' panicked at 'Box<Any>', librustc_errors/lib.rs:587:9
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::print
             at libstd/sys_common/backtrace.rs:71
             at libstd/sys_common/backtrace.rs:59
   2: std::panicking::default_hook::{{closure}}
             at libstd/panicking.rs:211
   3: std::panicking::default_hook
             at libstd/panicking.rs:227
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
             at libstd/panicking.rs:481
   6: std::panicking::begin_panic
   7: rustc_errors::Handler::bug
   8: rustc::util::bug::opt_span_bug_fmt::{{closure}}
   9: rustc::ty::context::tls::with_opt::{{closure}}
  10: rustc::ty::context::tls::with_context_opt
  11: rustc::ty::context::tls::with_opt
  12: rustc::util::bug::opt_span_bug_fmt
  13: rustc::util::bug::bug_fmt
  14: rustc::infer::canonical::canonicalizer::Canonicalizer::canonicalize
  15: rustc::infer::canonical::query_result::<impl rustc::infer::InferCtxt<'cx, 'gcx, 'tcx>>::make_canonicalized_query_result
  16: rustc::ty::context::tls::with_related_context
  17: rustc::infer::canonical::query_result::<impl rustc::infer::InferCtxtBuilder<'cx, 'gcx, 'tcx>>::enter_canonical_trait_query
  18: rustc_traits::normalize_projection_ty::normalize_projection_ty
  19: rustc::ty::query::__query_compute::normalize_projection_ty
  20: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::normalize_projection_ty<'tcx>>::compute
  21: rustc::dep_graph::graph::DepGraph::with_task_impl
  22: rustc::ty::context::tls::with_related_context
  23: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  24: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  25: <rustc::traits::query::normalize::QueryNormalizer<'cx, 'gcx, 'tcx> as rustc::ty::fold::TypeFolder<'gcx, 'tcx>>::fold_ty
  26: rustc::traits::query::normalize::<impl rustc::infer::at::At<'cx, 'gcx, 'tcx>>::normalize
  27: rustc::ty::context::tls::with_related_context
  28: rustc::infer::InferCtxtBuilder::enter
  29: rustc_traits::normalize_erasing_regions::normalize_ty_after_erasing_regions
  30: rustc::ty::query::__query_compute::normalize_ty_after_erasing_regions
  31: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::normalize_ty_after_erasing_regions<'tcx>>::compute
  32: rustc::dep_graph::graph::DepGraph::with_task_impl
  33: rustc::ty::context::tls::with_related_context
  34: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::force_query_with_job
  35: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  36: <rustc::traits::query::normalize_erasing_regions::NormalizeAfterErasingRegionsFolder<'cx, 'tcx> as rustc::ty::fold::TypeFolder<'tcx, 'tcx>>::fold_ty
  37: <smallvec::SmallVec<A> as core::iter::traits::FromIterator<<A as smallvec::Array>::Item>>::from_iter
  38: rustc::ty::fold::TypeFoldable::fold_with
  39: rustc::traits::query::normalize_erasing_regions::<impl rustc::ty::context::TyCtxt<'cx, 'tcx, 'tcx>>::normalize_erasing_late_bound_regions
  40: rustc_codegen_llvm::mir::block::<impl rustc_codegen_llvm::mir::FunctionCx<'a, 'll, 'tcx>>::codegen_terminator
  41: rustc_codegen_llvm::mir::codegen_mir
  42: rustc_codegen_llvm::base::codegen_instance
  43: rustc_codegen_llvm::mono_item::MonoItemExt::define
  44: rustc_codegen_llvm::base::compile_codegen_unit::module_codegen
  45: rustc::dep_graph::graph::DepGraph::with_task
  46: rustc_codegen_llvm::base::codegen_crate
  47: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_utils::codegen_backend::CodegenBackend>::codegen_crate
  48: rustc::util::common::time
  49: rustc_driver::driver::phase_4_codegen
  50: rustc_driver::driver::compile_input::{{closure}}
  51: rustc::ty::context::tls::enter_context
  52: <std::thread::local::LocalKey<T>>::with
  53: rustc::ty::context::TyCtxt::create_and_enter
  54: rustc_driver::driver::compile_input
  55: rustc_driver::run_compiler_with_pool
  56: <scoped_tls::ScopedKey<T>>::set
  57: rustc_driver::run_compiler
  58: clippy_driver::main::{{closure}}
             at src/driver.rs:135
  59: rustc_driver::run::{{closure}}::{{closure}}
             at /checkout/src/librustc_driver/lib.rs:190
  60: <scoped_tls::ScopedKey<T>>::set
             at /cargo/registry/src/github.com-1ecc6299db9ec823/scoped-tls-0.1.2/src/lib.rs:155
  61: syntax::with_globals::{{closure}}
             at /checkout/src/libsyntax/lib.rs:108
  62: <scoped_tls::ScopedKey<T>>::set
             at /cargo/registry/src/github.com-1ecc6299db9ec823/scoped-tls-0.1.2/src/lib.rs:155
  63: syntax::with_globals
             at /checkout/src/libsyntax/lib.rs:107
  64: rustc_driver::run::{{closure}}
             at /checkout/src/librustc_driver/lib.rs:189
  65: rustc_driver::monitor::{{closure}}
             at /checkout/src/librustc_driver/lib.rs:1660
  66: <std::panic::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once
             at /checkout/src/libstd/panic.rs:313
  67: std::panicking::try::do_call
             at /checkout/src/libstd/panicking.rs:310
  68: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:103
  69: std::panicking::try
             at /checkout/src/libstd/panicking.rs:289
  70: std::panic::catch_unwind
             at /checkout/src/libstd/panic.rs:392
  71: rustc_driver::in_named_rustc_thread
             at /checkout/src/librustc_driver/lib.rs:1574
  72: rustc_driver::in_rustc_thread
             at /checkout/src/librustc_driver/lib.rs:1585
  73: rustc_driver::monitor
             at /checkout/src/librustc_driver/lib.rs:1659
  74: rustc_driver::run
             at /checkout/src/librustc_driver/lib.rs:188
  75: clippy_driver::main
             at src/driver.rs:18
  76: std::rt::lang_start::{{closure}}
             at /checkout/src/libstd/rt.rs:74
  77: std::panicking::try::do_call
             at libstd/rt.rs:59
             at libstd/panicking.rs:310
  78: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:103
  79: std::rt::lang_start_internal
             at libstd/panicking.rs:289
             at libstd/panic.rs:392
             at libstd/rt.rs:58
  80: std::rt::lang_start
             at /checkout/src/libstd/rt.rs:74
  81: main
  82: __libc_start_main
  83: _start
query stack during panic:
#0 [normalize_projection_ty] normalizing `Canonical { variables: [], value: ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All }, value: ProjectionTy { substs: [[closure@DefId(1/1:572 ~ std[9de6]::collections[0]::hash[0]::map[0]::{{impl}}[55]::new[0]::{{closure}}[0])], (&std::cell::Cell<(u64, u64)>,)], item_def_id: DefId(2/0:963 ~ core[7daf]::ops[0]::function[0]::FnOnce[0]::Output[0]) } } }`
#1 [normalize_ty_after_erasing_regions] normalizing `ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: All }, value: <[closure@DefId(1/1:572 ~ std[9de6]::collections[0]::hash[0]::map[0]::{{impl}}[55]::new[0]::{{closure}}[0])] as std::ops::FnOnce<(&std::cell::Cell<(u64, u64)>,)>>::Output }`
end of query stack
error: aborting due to previous error
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.30.0-nightly (a8c11d216 2018-09-06) running on x86_64-unknown-linux-gnu

clippy 63a46b1e1a9a4cc3d78e1cf7a628421051c21167 Found via https://github.com/rust-lang-nursery/rust-clippy/issues/3142

flip1995 commented 6 years ago

I think this is a rustc problem, since clippy appears in the backtrace only by calling rustc_driver::run_compiler() everything else is rustc related.

matthiaskrgr commented 6 years ago

There was a similar issue that was moved from rustc to clippy https://github.com/rust-lang/rust/issues/49114#issuecomment-376600564

flip1995 commented 6 years ago

Yes it seems to be the same error again as in #2580.

matthiaskrgr commented 6 years ago

Hmm, maybe this is the same as #2831 ? // cc @phansch who said would look at 2831

phansch commented 6 years ago

Woops, #2831 fell under my radar. Both seem type inference related.

phansch commented 6 years ago

This is a different issue than #2831, but it seems to work on the playground now: https://play.rust-lang.org/?gist=c285598e88463061408310dddd738b41&version=nightly&mode=debug&edition=2018

phansch commented 6 years ago

So, this doesn't crash anymore it seems. In order to close the issue, it would be good to add a regression test to our run-pass suite.

If someone want's to pick this up as their first issue, I'm happy to write some more instructions if needed.

matthiaskrgr commented 6 years ago

@phansch it's still crashing for me :/

phansch commented 6 years ago

@matthiaskrgr huh, weird. In the playground it's not crashing for me:

https://play.rust-lang.org/?gist=c285598e88463061408310dddd738b41&version=nightly&mode=debug&edition=2018

I will try it out locally later today

matthiaskrgr commented 6 years ago

My guess is it crashes on a lint that is not enabled by default and thus seems to work on playground.

phansch commented 6 years ago

Now I can make it crash locally, just not in the playground. It also crashes if I have allowed all Clippy lints, so I think the cause is somewhere in the glue between Clippy and Rust. Maybe an incorrect configuration somewhere?

I managed to reduce the code a bit further:

use std::collections::HashSet;

fn is_sync<T>(_: T) where T: Sync {}

fn main() {
    is_sync(HashSet::<usize>::new());
}

It's probably also worth investigating why it doesn't crash on play.rust-lang.org. I will start with that tomorrow.

(This was at 3971c424)

phansch commented 6 years ago

One more thing I want to check: I've put the code into a new cargo project, ran cargo clippy (b1d0343 2018-10-19) and it passed.


cargo new i3144
tee i3144/src/main.rs << END
use std::collections::HashSet;

fn is_sync<T>(_: T) where T: Sync {}

fn main() {
    is_sync(HashSet::<usize>::new());
}
END
cd i3144
cargo clippy
matthiaskrgr commented 6 years ago

Mh yeah, I am getting that too. It crashes when directly running clippy-driver on it, but does not when running cargo clippy on it ... :confused:

phansch commented 6 years ago

So the playground is probably using cargo clippy and therefore it doesn't crash.

I experimented with src/driver.rs and found that commenting out the register_late_pass line doesn't crash it anymore:

https://github.com/phansch/rust-clippy/blob/5815a75e542b5ea8d2b7e0d337d9434b1aae1ff9/src/driver.rs#L141-L143

phansch commented 5 years ago

I was now able to to isolate the cause by commenting out the various late lint passes. Although it doesn't show up in the backtrace, it seems to be caused somewhere in the needless_pass_by_value lint.

phansch commented 5 years ago

Some more progress. The ICE is triggered from this invocation of implements_trait:

https://github.com/rust-lang/rust-clippy/blob/4259377ea61e4c830a49deda5be60c05a76a0a90/clippy_lints/src/needless_pass_by_value.rs#L188-L196

The full and exact error message is:

failed to lift:

QueryResponse {
    var_values: CanonicalVarValues { var_values: [] },
    region_constraints: [
        Binder(OutlivesPredicate(ReLateBound(DebruijnIndex(1), BrAnon(0)), ReErased)),
        Binder(OutlivesPredicate(ReLateBound(DebruijnIndex(1), BrAnon(1)), ReErased)),
        Binder(OutlivesPredicate(ReErased, ReLateBound(DebruijnIndex(1), BrAnon(0)))),
        Binder(OutlivesPredicate(ReErased, ReLateBound(DebruijnIndex(1), BrAnon(1))))
    ],
    certainty: Proven,
    value: NormalizationResult { normalized_ty: std::collections::hash_map::RandomState }
}

canonicalized from:

QueryResponse {
    var_values: CanonicalVarValues { var_values: [] },
    region_constraints: [
        Binder(OutlivesPredicate('_#0r, ReErased)),
        Binder(OutlivesPredicate('_#1r, ReErased)),
        Binder(OutlivesPredicate(ReErased, '_#0r)),
        Binder(OutlivesPredicate(ReErased, '_#1r))
    ],
    certainty: Proven,
    value: NormalizationResult { normalized_ty: std::collections::hash_map::RandomState }
}

which is emitted here: https://github.com/rust-lang/rust/blob/710ddc151baf9750e074229b8bdc794d63246eb8/src/librustc/infer/canonical/canonicalizer.rs#L481-L486