Skip to content

Compiler crash when dealing with Any and Ref #60622

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.

Activity

  1. Centril commented on May 8, 2019

    @Centril
    Contributor

    Reduced:

    trait Any {}
    
    trait AsAny {
        fn as_any(self);
    }
    
    impl<T> AsAny for T where T: Any + AsAny {
        fn as_any(self) {}
    }
    
    impl dyn AsAny {
        fn borrow<'a>(&'a self) -> &'a dyn AsAny {
            self
        }
    }
    
    fn demo<'a>(cell: &'a (dyn AsAny + 'static)) {
        cell.borrow::<'a, _>().as_any();
    }
  2. added
    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
    T-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.
    and removed on May 8, 2019
  3. Centril commented on May 8, 2019

    @Centril
    Contributor

    Regression first occurred in 1.30 according to godbolt.

  4. hellow554 commented on May 8, 2019

    @hellow554
    Contributor

    One can leave out the Any trait in your MCVE, still crashes.

    Bug was introduced somewhere between 33b923f...63d6649
    Sadly that's a very big range, but there are no nightlys available in between and also no bors artifacts because they are older than 167

  5. nikomatsakis commented on May 9, 2019

    @nikomatsakis
    Contributor

    triage: P-high

  6. jonas-schievink commented on May 14, 2019

    @jonas-schievink
    Contributor

    Smaller example from #60842:

    fn run_wild<T, DL: Borked<T>>(dl: &DL) {
        dl.a::<'_, T>();
    }
    
    pub trait Borked<T> {
        fn a(&self);
    }
    
    fn main() {}
  7. AlterionX commented on May 14, 2019

    @AlterionX

    I was trying to remove as many type parameters as possible, and ended up with this:

    struct Borked {}
    impl Borked {
        fn a(&self) {}
    }
    
    fn run_wild<T>(b: &Borked) {
        b.a::<'_, T>();
    }

    Removing <T> after run_wild still cuases an ICE. Adding a <T> after a stops causing the ICE. Removing the '_ also stops causing the ICE.

    Not sure if it's any more or less helpful, but just putting this here.

  8. davidtwco commented on May 16, 2019

    @davidtwco
    Member

    @rustbot claim

    Looks like that didn't work, either way, I'm taking a look into this issue.

  9. rustbot commented on May 16, 2019

    @rustbot
    Collaborator

    Error: The feature assign is not enabled in this repository.
    To enable it add its section in the triagebot.toml in the root of the repository.

    Please let @rust-lang/release know if you're having trouble with this bot.

  10. rustbot commented on May 16, 2019

    @rustbot
    Collaborator

    Error: The feature assign is not enabled in this repository.
    To enable it add its section in the triagebot.toml in the root of the repository.

    Please let @rust-lang/release know if you're having trouble with this bot.

  11. added a commit that references this issue on May 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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