Skip to content

Compiler crash when dealing with Any and Ref #60622

Closed
@whoiscc

Description

@whoiscc
use std::cell::{RefCell, Ref};
use std::any::Any;

trait Foo: Any + AsAny {
    //
}

trait AsAny {
    fn as_any(&self) -> &dyn Any;
}

impl<T: Foo> AsAny for T {
    fn as_any(&self) -> &dyn Any {
        self
    }
}

fn demo<'a, T: 'static>(cell: &'a RefCell<dyn Foo>) -> Option<&T> {
    // this causes compile error correctly
    // let x: Ref<'a, _> = cell.borrow();
    let x = cell.borrow::<'a, _>();
    x.as_any().downcast_ref::<T>()
}

fn main() {
    //
}

Compiler crashes with

warning: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
   --> src/main.rs:58:27
    |
58  |     let x = cell.borrow::<'a, _>();
    |                           ^^
    |
    = note: #[warn(late_bound_lifetime_arguments)] on by default
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #42868 <https://github.com/rust-lang/rust/issues/42868>

error: internal compiler error: src/librustc_typeck/check/mod.rs:2461: no type for node 121: type _ (id=121) in fcx 0x70000a15f458

thread 'rustc' panicked at 'Box<Any>', src/librustc_errors/lib.rs:620:9
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: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
   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_typeck::check::FnCtxt::node_ty
  15: <rustc_typeck::check::writeback::WritebackCx<'cx, 'gcx, 'tcx> as rustc::hir::intravisit::Visitor<'gcx>>::visit_ty
  16: rustc::hir::intravisit::walk_expr
  17: <rustc_typeck::check::writeback::WritebackCx<'cx, 'gcx, 'tcx> as rustc::hir::intravisit::Visitor<'gcx>>::visit_expr
  18: <rustc_typeck::check::writeback::WritebackCx<'cx, 'gcx, 'tcx> as rustc::hir::intravisit::Visitor<'gcx>>::visit_local
  19: rustc::hir::intravisit::walk_expr
  20: <rustc_typeck::check::writeback::WritebackCx<'cx, 'gcx, 'tcx> as rustc::hir::intravisit::Visitor<'gcx>>::visit_expr
  21: rustc_typeck::check::writeback::<impl rustc_typeck::check::FnCtxt<'a, 'gcx, 'tcx>>::resolve_type_vars_in_body
  22: rustc::ty::context::GlobalCtxt::enter_local
  23: rustc_typeck::check::typeck_tables_of
  24: rustc::ty::query::__query_compute::typeck_tables_of
  25: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::typeck_tables_of<'tcx>>::compute
  26: rustc::dep_graph::graph::DepGraph::with_task_impl
  27: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  28: rustc::ty::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::par_body_owners
  29: rustc_typeck::check::typeck_item_bodies
  30: rustc::ty::query::__query_compute::typeck_item_bodies
  31: rustc::ty::query::<impl rustc::ty::query::config::QueryAccessors<'tcx> for rustc::ty::query::queries::typeck_item_bodies<'tcx>>::compute
  32: rustc::dep_graph::graph::DepGraph::with_task_impl
  33: rustc::ty::query::plumbing::<impl rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx>>::get_query
  34: rustc::util::common::time
  35: rustc_typeck::check_crate
  36: <std::thread::local::LocalKey<T>>::with
  37: rustc::ty::context::TyCtxt::create_and_enter
  38: rustc_driver::driver::compile_input
  39: rustc_driver::run_compiler_with_pool
  40: <scoped_tls::ScopedKey<T>>::set
  41: rustc_driver::run_compiler
  42: syntax::with_globals
  43: __rust_maybe_catch_panic
  44: <F as alloc::boxed::FnBox<A>>::call_box
  45: std::sys::unix::thread::Thread::new::thread_start
  46: _pthread_body
  47: _pthread_start
query stack during panic:
#0 [typeck_tables_of] processing `demo`
#1 [typeck_item_bodies] type-checking all item bodies
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.34.1 (fc50f328b 2019-04-24) running on x86_64-apple-darwin

note: compiler flags: -C debuginfo=2 -C incremental --crate-type bin

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

Also, I wonder what is the right way (or is there any way) to write a demo with expected behavior. Thanks.

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions