<p dir="auto">Hey all,</p>
<p dir="auto">I'm trying to see if two &amp;Trait refer to the same object. It sounds like the correct way to do this is by casting to <code class="notranslate">*const Trait</code> and comparing those pointers for equality, however this produces an ICE. Here's a simple <a href="http://is.gd/c4UIFd" rel="nofollow">repro on playpen</a> and pasted below:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="trait MyTrait {}

struct Foo;
impl MyTrait for Foo {}

fn main() {
    let x = Foo;
    let a = &amp;x;
    let y = Foo;
    let z = &amp;y as &amp;MyTrait;

    if a as *const MyTrait == z as *const MyTrait {
        println!(&quot;They match&quot;);
    }
}"><pre class="notranslate"><span class="pl-k">trait</span> <span class="pl-smi">MyTrait</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>

<span class="pl-k">struct</span> <span class="pl-smi">Foo</span><span class="pl-kos">;</span>
<span class="pl-k">impl</span> <span class="pl-smi">MyTrait</span> <span class="pl-k">for</span> <span class="pl-smi">Foo</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>

<span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">let</span> x = <span class="pl-v">Foo</span><span class="pl-kos">;</span>
    <span class="pl-k">let</span> a = <span class="pl-c1">&amp;</span>x<span class="pl-kos">;</span>
    <span class="pl-k">let</span> y = <span class="pl-v">Foo</span><span class="pl-kos">;</span>
    <span class="pl-k">let</span> z = <span class="pl-c1">&amp;</span>y <span class="pl-k">as</span> <span class="pl-c1">&amp;</span><span class="pl-smi">MyTrait</span><span class="pl-kos">;</span>

    <span class="pl-k">if</span> a <span class="pl-k">as</span> <span class="pl-c1">*</span><span class="pl-k">const</span> <span class="pl-smi">MyTrait</span> == z <span class="pl-k">as</span> <span class="pl-c1">*</span><span class="pl-k">const</span> <span class="pl-smi">MyTrait</span> <span class="pl-kos">{</span>
        <span class="pl-en">println</span><span class="pl-en">!</span><span class="pl-kos">(</span><span class="pl-s">"They match"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Below is the output of compiling the reproduction with <code class="notranslate">RUST_BACKTRACE=1</code> on <code class="notranslate">rustc 1.3.0-nightly (4dfe7a16c 2015-07-30)</code></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="error: internal compiler error: unexpected panic
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
thread 'rustc' panicked at 'assertion failed: self.appropriate_rvalue_mode(bcx.ccx()) == ByValue', ../src/librustc_trans/trans/datum.rs:767

stack backtrace:
   1:     0x7fe6e5afa28e - sys::backtrace::write::h40e4abee08c0ae4cCys
   2:     0x7fe6e5b027f4 - panicking::on_panic::h2d2b781277adb4bfLnx
   3:     0x7fe6e5ac3ace - rt::unwind::begin_unwind_inner::ha8a0cd4a16a72098o3w
   4:     0x7fe6e48969a1 - rt::unwind::begin_unwind::h10748794639547756261
   5:     0x7fe6e49171c4 - trans::datum::Datum&lt;'tcx, K&gt;::to_llscalarish::h18191786700678863130
   6:     0x7fe6e499223b - trans::expr::trans_binary::h0bd5cb5f100e03c3SvD
   7:     0x7fe6e4987ffb - trans::expr::trans_unadjusted::hd19df31c310773b8oXB
   8:     0x7fe6e48d2c49 - trans::expr::trans::hc28cc636bef2e50fYpB
   9:     0x7fe6e4989bcf - trans::expr::trans_rvalue_dps_unadjusted::he43b91a5a089a8ed4tC
  10:     0x7fe6e49627de - trans::expr::trans_into::h8c4ef2d012c8a2c0IjB
  11:     0x7fe6e48f28b3 - trans::controlflow::trans_block::h05d9494ae3fe12b5BWv
  12:     0x7fe6e48f1666 - trans::base::trans_closure::h3121f3bcc11648c5sDi
  13:     0x7fe6e48f2ea8 - trans::base::trans_fn::ha3af5a02c0e336d7fNi
  14:     0x7fe6e48f5fa8 - trans::base::trans_item::h55d2aa0c004afbf9qcj
  15:     0x7fe6e4902a68 - trans::base::trans_crate::ha1fc42a69e474159uZj
  16:     0x7fe6e60708d4 - driver::phase_4_translate_to_llvm::hc4ec4f8818648bb75Oa
  17:     0x7fe6e606ad68 - driver::phase_3_run_analysis_passes::closure.16499
  18:     0x7fe6e6064fe1 - middle::ty::ctxt&lt;'tcx&gt;::create_and_enter::h11776421653616432545
  19:     0x7fe6e606000f - driver::phase_3_run_analysis_passes::h8749574581471310871
  20:     0x7fe6e6044751 - driver::compile_input::h2888e18c0246b44eTba
  21:     0x7fe6e6129360 - run_compiler::h216b759eb7845cb3A7b
  22:     0x7fe6e6126f2e - boxed::F.FnBox&lt;A&gt;::call_box::h6091736725415782619
  23:     0x7fe6e6126859 - rt::unwind::try::try_fn::h7532542645103148343
  24:     0x7fe6e5b022bf - __rust_try_inner
  25:     0x7fe6e5b022fa - __rust_try
  26:     0x7fe6e5aed7a7 - rt::unwind::try::inner_try::hec4722a4abfa91b1hZw
  27:     0x7fe6e6126a78 - boxed::F.FnBox&lt;A&gt;::call_box::h7421094806661540209
  28:     0x7fe6e5b01411 - sys::thread::Thread::new::thread_start::h0036aa54802cb8d7X8v
  29:     0x7fe6df71d0da - start_thread
  30:     0x7fe6e575390c - __clone
  31:                0x0 - &lt;unknown&gt;"><pre class="notranslate"><code class="notranslate">error: internal compiler error: unexpected panic
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
thread 'rustc' panicked at 'assertion failed: self.appropriate_rvalue_mode(bcx.ccx()) == ByValue', ../src/librustc_trans/trans/datum.rs:767

stack backtrace:
   1:     0x7fe6e5afa28e - sys::backtrace::write::h40e4abee08c0ae4cCys
   2:     0x7fe6e5b027f4 - panicking::on_panic::h2d2b781277adb4bfLnx
   3:     0x7fe6e5ac3ace - rt::unwind::begin_unwind_inner::ha8a0cd4a16a72098o3w
   4:     0x7fe6e48969a1 - rt::unwind::begin_unwind::h10748794639547756261
   5:     0x7fe6e49171c4 - trans::datum::Datum&lt;'tcx, K&gt;::to_llscalarish::h18191786700678863130
   6:     0x7fe6e499223b - trans::expr::trans_binary::h0bd5cb5f100e03c3SvD
   7:     0x7fe6e4987ffb - trans::expr::trans_unadjusted::hd19df31c310773b8oXB
   8:     0x7fe6e48d2c49 - trans::expr::trans::hc28cc636bef2e50fYpB
   9:     0x7fe6e4989bcf - trans::expr::trans_rvalue_dps_unadjusted::he43b91a5a089a8ed4tC
  10:     0x7fe6e49627de - trans::expr::trans_into::h8c4ef2d012c8a2c0IjB
  11:     0x7fe6e48f28b3 - trans::controlflow::trans_block::h05d9494ae3fe12b5BWv
  12:     0x7fe6e48f1666 - trans::base::trans_closure::h3121f3bcc11648c5sDi
  13:     0x7fe6e48f2ea8 - trans::base::trans_fn::ha3af5a02c0e336d7fNi
  14:     0x7fe6e48f5fa8 - trans::base::trans_item::h55d2aa0c004afbf9qcj
  15:     0x7fe6e4902a68 - trans::base::trans_crate::ha1fc42a69e474159uZj
  16:     0x7fe6e60708d4 - driver::phase_4_translate_to_llvm::hc4ec4f8818648bb75Oa
  17:     0x7fe6e606ad68 - driver::phase_3_run_analysis_passes::closure.16499
  18:     0x7fe6e6064fe1 - middle::ty::ctxt&lt;'tcx&gt;::create_and_enter::h11776421653616432545
  19:     0x7fe6e606000f - driver::phase_3_run_analysis_passes::h8749574581471310871
  20:     0x7fe6e6044751 - driver::compile_input::h2888e18c0246b44eTba
  21:     0x7fe6e6129360 - run_compiler::h216b759eb7845cb3A7b
  22:     0x7fe6e6126f2e - boxed::F.FnBox&lt;A&gt;::call_box::h6091736725415782619
  23:     0x7fe6e6126859 - rt::unwind::try::try_fn::h7532542645103148343
  24:     0x7fe6e5b022bf - __rust_try_inner
  25:     0x7fe6e5b022fa - __rust_try
  26:     0x7fe6e5aed7a7 - rt::unwind::try::inner_try::hec4722a4abfa91b1hZw
  27:     0x7fe6e6126a78 - boxed::F.FnBox&lt;A&gt;::call_box::h7421094806661540209
  28:     0x7fe6e5b01411 - sys::thread::Thread::new::thread_start::h0036aa54802cb8d7X8v
  29:     0x7fe6df71d0da - start_thread
  30:     0x7fe6e575390c - __clone
  31:                0x0 - &lt;unknown&gt;
</code></pre></div>
<p dir="auto">Attempting to cast <code class="notranslate">&amp;str</code> or a slice to a raw pointer results in a proper error message. Attempting to compare two such invalid casts, however, generates an ICE.<br>
(And I realize now that <code class="notranslate">.as_ptr()</code> is the right way to do what I was trying to do.)</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="fn main() {
    let a: &amp;[i32] = &amp;[];
    (a as *const _) == (a as *const _);
    (&quot;&quot; as *const _) == (&quot;&quot; as *const _);
}"><pre class="notranslate"><span class="pl-k">fn</span> <span class="pl-en">main</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">let</span> a<span class="pl-kos">:</span> <span class="pl-c1">&amp;</span><span class="pl-kos">[</span><span class="pl-smi">i32</span><span class="pl-kos">]</span> = <span class="pl-c1">&amp;</span><span class="pl-kos">[</span><span class="pl-kos">]</span><span class="pl-kos">;</span>
    <span class="pl-kos">(</span>a <span class="pl-k">as</span> <span class="pl-c1">*</span><span class="pl-k">const</span> <span class="pl-smi">_</span><span class="pl-kos">)</span> == <span class="pl-kos">(</span>a <span class="pl-k">as</span> <span class="pl-c1">*</span><span class="pl-k">const</span> <span class="pl-smi">_</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">(</span><span class="pl-s">""</span> <span class="pl-k">as</span> <span class="pl-c1">*</span><span class="pl-k">const</span> <span class="pl-smi">_</span><span class="pl-kos">)</span> == <span class="pl-kos">(</span><span class="pl-s">""</span> <span class="pl-k">as</span> <span class="pl-c1">*</span><span class="pl-k">const</span> <span class="pl-smi">_</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Results in:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' panicked at 'assertion failed: self.appropriate_rvalue_mode(bcx.ccx()) == ByValue', /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/librustc/middle/trans/datum.rs:644

stack backtrace:
   1:     0x7f2610acd880 - rt::backtrace::imp::write::h7a90f4ef1aa5a81fsbq
   2:     0x7f2610ad0900 - failure::on_fail::he9ef851a069b43543wq
   3:     0x7f2615036d40 - unwind::begin_unwind_inner::ha2afab22d4dc49ed8Rd
   4:     0x7f261567bb30 - unwind::begin_unwind::h15560861862153460913
   5:     0x7f2615a1eb20 - middle::trans::datum::Datum&lt;K&gt;::to_llscalarish::h5300527991557390520
   6:     0x7f2615a3f650 - middle::trans::expr::trans_binary::h6c193beb06d7e93dcG7
   7:     0x7f2615a34ab0 - middle::trans::expr::trans_unadjusted::h02c4b8f1c6b87a22SZ5
   8:     0x7f26159fbac0 - middle::trans::expr::trans_into::h4971d03ccde714e0Ue5
   9:     0x7f26159faf20 - middle::trans::controlflow::trans_stmt_semi::h8fa1003152ba3849Gu1
  10:     0x7f26159fa4d0 - middle::trans::controlflow::trans_stmt::h98ee011c11c8d6d8tq1
  11:     0x7f26159fbee0 - middle::trans::controlflow::trans_block::h9afd935ec02d5d88zv1
  12:     0x7f2615a9e510 - middle::trans::base::trans_closure::h3e4483ae2d8e884bAGh
  13:     0x7f26159ef140 - middle::trans::base::trans_fn::h7f9774a3926fbf34nSh
  14:     0x7f26159ec9a0 - middle::trans::base::trans_item::h07f36bd67b211f81Bbi
  15:     0x7f2615aa8ce0 - middle::trans::base::trans_crate::hda78095df6dfa8bfC9i
  16:     0x7f2615efa840 - driver::driver::phase_4_translate_to_llvm::h3f7adb4e900158acqyC
  17:     0x7f2615ef16e0 - driver::driver::compile_input::h1c18718eb06a01d2k5B
  18:     0x7f2615f744e0 - driver::run_compiler::hcc1eba405fd576deBVF
  19:     0x7f2615f743d0 - driver::run::closure.146438
  20:     0x7f26156b0c20 - task::TaskBuilder&lt;S&gt;::try_future::closure.104848
  21:     0x7f26156b0a10 - task::TaskBuilder&lt;S&gt;::spawn_internal::closure.104819
  22:     0x7f261537d540 - task::NativeSpawner.Spawner::spawn::closure.8448
  23:     0x7f261508bc50 - rust_try_inner
  24:     0x7f261508bc40 - rust_try
  25:     0x7f26150346c0 - unwind::try::h2eeb8f95c58da3ccQGd
  26:     0x7f2615034550 - task::Task::run::hcb23459dab25134dGMc
  27:     0x7f261537d280 - task::NativeSpawner.Spawner::spawn::closure.8386
  28:     0x7f2615035d60 - thread::thread_start::h033f0420ecdbeed0V7c
  29:     0x7f260fe7d0c0 - start_thread
  30:     0x7f2614cfff89 - __clone
  31:                0x0 - &lt;unknown&gt;"><pre class="notranslate"><code class="notranslate">error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' panicked at 'assertion failed: self.appropriate_rvalue_mode(bcx.ccx()) == ByValue', /home/rustbuild/src/rust-buildbot/slave/nightly-linux/build/src/librustc/middle/trans/datum.rs:644

stack backtrace:
   1:     0x7f2610acd880 - rt::backtrace::imp::write::h7a90f4ef1aa5a81fsbq
   2:     0x7f2610ad0900 - failure::on_fail::he9ef851a069b43543wq
   3:     0x7f2615036d40 - unwind::begin_unwind_inner::ha2afab22d4dc49ed8Rd
   4:     0x7f261567bb30 - unwind::begin_unwind::h15560861862153460913
   5:     0x7f2615a1eb20 - middle::trans::datum::Datum&lt;K&gt;::to_llscalarish::h5300527991557390520
   6:     0x7f2615a3f650 - middle::trans::expr::trans_binary::h6c193beb06d7e93dcG7
   7:     0x7f2615a34ab0 - middle::trans::expr::trans_unadjusted::h02c4b8f1c6b87a22SZ5
   8:     0x7f26159fbac0 - middle::trans::expr::trans_into::h4971d03ccde714e0Ue5
   9:     0x7f26159faf20 - middle::trans::controlflow::trans_stmt_semi::h8fa1003152ba3849Gu1
  10:     0x7f26159fa4d0 - middle::trans::controlflow::trans_stmt::h98ee011c11c8d6d8tq1
  11:     0x7f26159fbee0 - middle::trans::controlflow::trans_block::h9afd935ec02d5d88zv1
  12:     0x7f2615a9e510 - middle::trans::base::trans_closure::h3e4483ae2d8e884bAGh
  13:     0x7f26159ef140 - middle::trans::base::trans_fn::h7f9774a3926fbf34nSh
  14:     0x7f26159ec9a0 - middle::trans::base::trans_item::h07f36bd67b211f81Bbi
  15:     0x7f2615aa8ce0 - middle::trans::base::trans_crate::hda78095df6dfa8bfC9i
  16:     0x7f2615efa840 - driver::driver::phase_4_translate_to_llvm::h3f7adb4e900158acqyC
  17:     0x7f2615ef16e0 - driver::driver::compile_input::h1c18718eb06a01d2k5B
  18:     0x7f2615f744e0 - driver::run_compiler::hcc1eba405fd576deBVF
  19:     0x7f2615f743d0 - driver::run::closure.146438
  20:     0x7f26156b0c20 - task::TaskBuilder&lt;S&gt;::try_future::closure.104848
  21:     0x7f26156b0a10 - task::TaskBuilder&lt;S&gt;::spawn_internal::closure.104819
  22:     0x7f261537d540 - task::NativeSpawner.Spawner::spawn::closure.8448
  23:     0x7f261508bc50 - rust_try_inner
  24:     0x7f261508bc40 - rust_try
  25:     0x7f26150346c0 - unwind::try::h2eeb8f95c58da3ccQGd
  26:     0x7f2615034550 - task::Task::run::hcb23459dab25134dGMc
  27:     0x7f261537d280 - task::NativeSpawner.Spawner::spawn::closure.8386
  28:     0x7f2615035d60 - thread::thread_start::h033f0420ecdbeed0V7c
  29:     0x7f260fe7d0c0 - start_thread
  30:     0x7f2614cfff89 - __clone
  31:                0x0 - &lt;unknown&gt;
</code></pre></div>