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

internal compiler error: coherence failed to report ambiguit #23853

Closed
max6cn opened this issue Mar 30, 2015 · 3 comments · Fixed by #24056
Closed

internal compiler error: coherence failed to report ambiguit #23853

max6cn opened this issue Mar 30, 2015 · 3 comments · Fixed by #24056
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@max6cn
Copy link

max6cn commented Mar 30, 2015

I am trying the example from http://ironframework.io and got the following error:

/Users/max6/.cargo/registry/src/github.com-1ecc6299db9ec823/plugin-0.2.2/src/lib.rs:75:45: 75:59
error: internal compiler error: coherence failed to report ambiguity:
cannot locate the impl of the trait `core::any::Any` for the type `<P as typemap::Key>::Value`

Steps to reproduce this error:

  1. create new project
cargo new webex1  --bin
  1. edit main.rs as following

    extern crate iron;
    
    use iron::prelude::*;
    use iron::status;
    
    fn main() {
        fn hello_world(_: &mut Request) -> IronResult<Response> {
            Ok(Response::with((status::Ok, "Hello World!")))
        }
    
        Iron::new(hello_world).http("localhost:3000").unwrap();
        println!("On 3000");
    }

    add following lines to Cargo.toml

    [dependencies]
    iron = "*"
  2. RUST_BACKTRACE=1 cargo run

Output

Compiling conduit-mime-types v0.7.3
Compiling time v0.1.22
Compiling openssl-sys v0.5.3
Compiling num_cpus v0.1.0
Compiling url v0.2.28
Compiling mime v0.0.10
Compiling plugin v0.2.2
/Users/max6/.cargo/registry/src/github.com-1ecc6299db9ec823/plugin-0.2.2/src/lib.rs:75:45: 75:59 error: internal compiler error: coherence failed to report ambiguity: cannot locate the impl of the trait `core::any::Any` for the type `<P as typemap::Key>::Value`
/Users/max6/.cargo/registry/src/github.com-1ecc6299db9ec823/plugin-0.2.2/src/lib.rs:75             return Ok(self.extensions_mut().get_mut::<P>().unwrap());
^~~~~~~~~~~~~~
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: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'Box<Any>', /Users/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-mac/build/src/libsyntax/diagnostic.rs:130


Related Source file

cat /Users/max6/.cargo/registry/src/github.com-1ecc6299db9ec823/plugin-0.2.2/src/lib.rs

#![feature(core)]
#![deny(missing_docs, warnings)]

//! Lazily-Evaluated, Order-Independent Plugins for Extensible Types.

extern crate typemap;
use typemap::{TypeMap, Key};

/// Implementers of this trait can act as plugins for other types, via `OtherType::get<P>()`.
///
/// To create a plugin, implement this trait and provide an empty implementation
/// of `Key` to associate the plugin with its return type, `Key::Value`.
pub trait Plugin<E: ?Sized>: Key {
  /// The error type associated with this plugin.
  type Error;

  /// Create the plugin from an instance of the extended type.
  ///
  /// While `eval` is given a mutable reference to the extended
  /// type, it is important for implementers to remember that
  /// the result of `eval` is usually cached, so care should
  /// be taken when doing mutation on the extended type.
  fn eval(&mut E) -> Result<Self::Value, Self::Error>;
}

/// Defines an interface that extensible types must implement.
///
/// Extensible types must contain a TypeMap.
pub trait Extensible {
  /// Get a reference to the type's extension storage.
  fn extensions(&self) -> &TypeMap;

  /// Get a mutable reference to the type's extension storage.
  fn extensions_mut(&mut self) -> &mut TypeMap;
}

/// An interface for plugins that cache values between calls.
///
/// `R` is the type of the plugin's return value, which must be cloneable.
pub trait Pluggable {
  /// Return a copy of the plugin's produced value.
  ///
  /// The plugin will be created if it doesn't exist already.
  /// If plugin creation fails, an error is returned.
  ///
  /// `P` is the plugin type.
  fn get<P: Plugin<Self>>(&mut self) -> Result<P::Value, P::Error>
  where P::Value: Clone + 'static, Self: Extensible, P::Error: Clone {
    self.get_ref::<P>().map(|v| v.clone())
  }

  /// Return a reference to the plugin's produced value.
  ///
  /// The plugin will be created if it doesn't exist already.
  /// If plugin creation fails an error is returned.
  ///
  /// `P` is the plugin type.
  fn get_ref<P: Plugin<Self>>(&mut self) -> Result<&P::Value, P::Error>
  where P::Value: 'static, Self: Extensible {
    self.get_mut::<P>().map(|mutref| &*mutref)
  }

  /// Return a mutable reference to the plugin's produced value.
  ///
  /// The plugin will be created if it doesn't exist already.
  /// If plugin creation fail an error is returned.
  ///
  /// `P` is the plugin type.
  fn get_mut<P: Plugin<Self>>(&mut self) -> Result<&mut P::Value, P::Error>
  where P::Value: 'static, Self: Extensible {
    use typemap::Entry::{Occupied, Vacant};
    use std::intrinsics::unreachable;

    if self.extensions().contains::<P>() {
      return Ok(self.extensions_mut().get_mut::<P>().unwrap());
    }

    <P as Plugin<Self>>::eval(self).map(move |data| {
      match self.extensions_mut().entry::<P>() {
        Vacant(entry) => entry.insert(data),
        Occupied(..) => unsafe { unreachable() }
      }
      })
    }

    /// Create and evaluate a once-off instance of a plugin.
    fn compute<P: Plugin<Self>>(&mut self) -> Result<P::Value, P::Error> {
      <P as Plugin<Self>>::eval(self)
    }
  }

  #[cfg(test)]
  mod test {
    extern crate void;

    use test::void::{Void, VoidExtensions};

    use typemap::{TypeMap, Key};
    use super::{Extensible, Plugin, Pluggable};

    struct Extended {
      map: TypeMap
    }

    impl Extended {
      fn new() -> Extended {
        Extended { map: TypeMap::new() }
      }
    }

    impl Extensible for Extended {
      fn extensions(&self) -> &TypeMap { &self.map }
      fn extensions_mut(&mut self) -> &mut TypeMap { &mut self.map }
    }

    impl Pluggable for Extended {}

      macro_rules! generate_simple_plugin (
        ($t:ty, $v:ident, $v2:expr) => {
          #[derive(PartialEq, Debug, Clone)]
          struct $v(i32);

          impl Key for $t { type Value = $t; }

          impl Plugin<Extended> for $t {
            type Error = Void;

            fn eval(_: &mut Extended) -> Result<$t, Void> {
              Ok($v($v2))
            }
          }
        }
        );

        generate_simple_plugin!(One, One, 1);
        generate_simple_plugin!(Two, Two, 2);
        generate_simple_plugin!(Three, Three, 3);
        generate_simple_plugin!(Four, Four, 4);
        generate_simple_plugin!(Five, Five, 5);
        generate_simple_plugin!(Six, Six, 6);
        generate_simple_plugin!(Seven, Seven, 7);
        generate_simple_plugin!(Eight, Eight, 8);
        generate_simple_plugin!(Nine, Nine, 9);
        generate_simple_plugin!(Ten, Ten, 10);

        #[test] fn test_simple() {
          let mut extended = Extended::new();
          assert_eq!(extended.get::<One>(),   Ok(One(1)));
          assert_eq!(extended.get::<Two>(),   Ok(Two(2)));
          assert_eq!(extended.get::<Three>(), Ok(Three(3)));
        }

        #[test] fn test_resize() {
          let mut extended = Extended::new();
          extended.get::<One>().void_unwrap();
          extended.get::<Two>().void_unwrap();
          extended.get::<Three>().void_unwrap();
          extended.get::<Four>().void_unwrap();
          extended.get::<Five>().void_unwrap();
          extended.get::<Six>().void_unwrap();
          extended.get::<Seven>().void_unwrap();
          extended.get::<Eight>().void_unwrap();
          extended.get::<Nine>().void_unwrap();
          extended.get::<Ten>().void_unwrap();
          assert_eq!(extended.get_ref::<One>(), Ok(&One(1)))
        }

        #[test] fn test_custom_return_type() {
          let mut extended = Extended::new();

          // Define a struct.
          struct IntPlugin;

          // Map it onto an `i32` value.
          impl Key for IntPlugin { type Value = i32; }

          // Define the plugin evaluation function.
          impl Plugin<Extended> for IntPlugin {
            type Error = Void;

            fn eval(_: &mut Extended) -> Result<i32, Void> {
              Ok(0i32)
            }
          }
          assert_eq!(extended.get::<IntPlugin>().void_unwrap(), 0i32);
        }
      }

Backtrace and version information

rustc --version --verbose:

rustc --version --verbose
rustc 1.0.0-nightly (27901849e 2015-03-25) (built 2015-03-26)
binary: rustc
commit-hash: 27901849e07558639b8decc03707e0317ae8280e
commit-date: 2015-03-25
build-date: 2015-03-26
host: x86_64-apple-darwin
release: 1.0.0-nightly

Backtrace:

stack backtrace:
1:        0x11145dae4 - sys::backtrace::write::h60a4d01122ac4cc2OjD
2:        0x111488c38 - panicking::on_panic::h71bf3930cf08a95dY9I
3:        0x1113a687e - rt::unwind::begin_unwind_inner::h8510ec4880ca5ddc8RI
4:        0x110ba4c1e - rt::unwind::begin_unwind::h11428392113330414505
5:        0x110ba4bcb - diagnostic::SpanHandler::span_bug::h51d3c08c25edd395taB
6:        0x10e53c241 - middle::traits::error_reporting::report_fulfillment_errors::h5d246675425c9d4bhwM
7:        0x10dadd912 - check::vtable::select_all_fcx_obligations_or_error::h25f1bd2bc40df770k2b
8:        0x10db8e6fa - check::check_bare_fn::hef1e97f4f82ddc463nn
9:        0x10db93d2f - check::check_method_body::hbbb7de48d1cfdabeTVn
10:        0x10db8a0c4 - check::check_item::h25c21617996c325dOGn
11:        0x10dc5fed6 - check_crate::closure.36000
12:        0x10dc5ad9a - check_crate::hd293b9d434113177OmC
13:        0x10d996627 - driver::phase_3_run_analysis_passes::h4f2a12d60051e411rGa
14:        0x10d97c667 - driver::compile_input::h16042e7910f69e15Rba
15:        0x10da36173 - run_compiler::ha385e70672e841abs2b
16:        0x10da33d05 - thunk::F.Invoke<A, R>::invoke::h16437619201372565195
17:        0x10da330c7 - rt::unwind::try::try_fn::h3771335316142457703
18:        0x11150f1e8 - rust_try_inner
19:        0x11150f1d5 - rust_try
20:        0x10da33465 - thunk::F.Invoke<A, R>::invoke::h15947769794543045347
21:        0x111473bcd - sys::thread::create::thread_start::hbb06c6fdb1ad3e1eYPH
22:     0x7fff83b6b267 - _pthread_body
23:     0x7fff83b6b1e4 - _pthread_start

@steveklabnik steveklabnik added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Mar 30, 2015
@oh-its-jimjam
Copy link
Contributor

Also got this problem when building plugin v0.2.2

@lily-mara
Copy link

Same error compiling plugin v0.2.2. Slightly different backtrace, and using the latest nightly

stack backtrace:
   1:     0x7f4b0f9d2c59 - sys::backtrace::write::h433d43302839b3d0VvD
   2:     0x7f4b0f9fd56c - panicking::on_panic::h85bb7c48a7bf6d691LJ
   3:     0x7f4b0f923803 - rt::unwind::begin_unwind_inner::h30ebc8aea74a1411IrJ
   4:     0x7f4b0cd1959d - rt::unwind::begin_unwind::h6513247106139423927
   5:     0x7f4b0cd19543 - diagnostic::SpanHandler::span_bug::h4646ed0c65aeed34GcB
   6:     0x7f4b0d74e778 - middle::traits::error_reporting::report_fulfillment_errors::hc2dd1decb6d68dbfksM
   7:     0x7f4b0e619d0c - check::vtable::select_all_fcx_obligations_or_error::h8069d3ac5867ba9b81b
   8:     0x7f4b0e6d0122 - check::check_bare_fn::hd874778758fc14aenln
   9:     0x7f4b0e6d5849 - check::check_method_body::h4be5d0afb72be9efdTn
  10:     0x7f4b0e6cb8b6 - check::check_item::h80165565e9f9866b8Dn
  11:     0x7f4b0e7a9126 - check_crate::closure.36042
  12:     0x7f4b0e7a34f3 - check_crate::hc8ab794f01a8befa5iC
  13:     0x7f4b1006136d - driver::phase_3_run_analysis_passes::hc9d84862133d3ad1oGa
  14:     0x7f4b10045295 - driver::compile_input::hc4e76294d74f8411Qba
  15:     0x7f4b100fcda5 - run_compiler::h5900aba33b437c68p2b
  16:     0x7f4b100fa85a - thunk::F.Invoke<A, R>::invoke::h9689564212340425420
  17:     0x7f4b100f9aa9 - rt::unwind::try::try_fn::h16505487216291884813
  18:     0x7f4b0fa77a78 - rust_try_inner
  19:     0x7f4b0fa77a65 - rust_try
  20:     0x7f4b100f9e07 - thunk::F.Invoke<A, R>::invoke::h14412175433843103066
  21:     0x7f4b0f9e8551 - sys::thread::create::thread_start::hf9ba913050908365ulI
  22:     0x7f4b097ff0a4 - start_thread
  23:     0x7f4b0f58acfc - __clone
  24:                0x0 - <unknown>

version info:

rustc 1.0.0-nightly (d8be84eb4 2015-03-29) (built 2015-03-30)
binary: rustc
commit-hash: d8be84eb4499e21bd98a3500c8760540996df23b
commit-date: 2015-03-29
build-date: 2015-03-30
host: x86_64-unknown-linux-gnu
release: 1.0.0-nightly

@gsingh93
Copy link
Contributor

gsingh93 commented Apr 3, 2015

I get a similar error when compiling handlebars-iron:

src/middleware.rs:38:1: 40:2 error: internal compiler error: coherence failed to report ambiguity: cannot locate the impl of the trait `core::marker::MarkerTrait` for the type `handlebars::helpers::HelperDef`
src/middleware.rs:38 impl typemap::Key for HandlebarsEngine {
src/middleware.rs:39     type Value = Template;
src/middleware.rs:40 }
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: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:130
$ rustc --version
rustc 1.0.0-nightly (2e3b0c051 2015-04-01) (built 2015-04-02)

This might be a dup of #21942

nikomatsakis added a commit to nikomatsakis/rust that referenced this issue Apr 6, 2015
`Trait`, prefer the object. Also give a nice error for attempts to
manually `impl Trait for Trait`, since they will be ineffectual.

Fixes rust-lang#24015.

Fixes rust-lang#24051.
Fixes rust-lang#24037.
Fixes rust-lang#23853.
Fixes rust-lang#21942.
cc rust-lang#21756.
bors added a commit that referenced this issue Apr 6, 2015
If we find a blanket impl for `Trait` but we're matching on an object `Trait`, prefer the object (I think we could perhaps go either way, but this seems safer). Also give a nice error for attempts to manually `impl Trait for Trait`, since they will be ineffectual.

This fixes the problems around ambiguity ICEs relating to `Any` and `MarkerTrait` that were cropping up all over the place. There may still be similar ICEs reported in #21756 that this PR does not address.

Fixes #24015.

Fixes #24051.
Fixes #24037.
Fixes #23853.
Fixes #21942.
cc #21756.

cc @alexcrichton (this fixes crates.io)
r? @aturon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants