<p dir="auto">I don't know why I cannot cast <code class="notranslate">FooBar</code> back to <code class="notranslate">Foo</code> or <code class="notranslate">Bar</code>.</p>
<p dir="auto">I tried this code:</p>
<div class="highlight highlight-source-rust notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="trait Foo {}

trait Bar {}

trait FooBar: Foo + Bar {}

impl &lt;T: Foo + Bar&gt; FooBar for T {}

struct A;

impl Foo for A {}
impl Bar for A {}

fn test(fb: &amp;FooBar) -&gt; &amp;Foo {
    fb as &amp;Foo
}

fn main() {
    let a = A;
    test(&amp;a);
}"><pre class="notranslate"><span class="pl-k">trait</span> <span class="pl-smi">Foo</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>

<span class="pl-k">trait</span> <span class="pl-smi">Bar</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>

<span class="pl-k">trait</span> <span class="pl-smi">FooBar</span><span class="pl-kos">:</span> <span class="pl-smi">Foo</span> + <span class="pl-smi">Bar</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>

<span class="pl-k">impl</span> <span class="pl-kos">&lt;</span><span class="pl-smi">T</span><span class="pl-kos">:</span> <span class="pl-smi">Foo</span> + <span class="pl-smi">Bar</span><span class="pl-kos">&gt;</span> <span class="pl-smi">FooBar</span> <span class="pl-k">for</span> <span class="pl-smi">T</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>

<span class="pl-k">struct</span> <span class="pl-smi">A</span><span class="pl-kos">;</span>

<span class="pl-k">impl</span> <span class="pl-smi">Foo</span> <span class="pl-k">for</span> <span class="pl-smi">A</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>
<span class="pl-k">impl</span> <span class="pl-smi">Bar</span> <span class="pl-k">for</span> <span class="pl-smi">A</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>

<span class="pl-k">fn</span> <span class="pl-en">test</span><span class="pl-kos">(</span><span class="pl-s1">fb</span><span class="pl-kos">:</span> <span class="pl-c1">&amp;</span><span class="pl-smi">FooBar</span><span class="pl-kos">)</span> -&gt; <span class="pl-c1">&amp;</span><span class="pl-smi">Foo</span> <span class="pl-kos">{</span>
    fb <span class="pl-k">as</span> <span class="pl-c1">&amp;</span><span class="pl-smi">Foo</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> a = <span class="pl-v">A</span><span class="pl-kos">;</span>
    <span class="pl-en">test</span><span class="pl-kos">(</span><span class="pl-c1">&amp;</span>a<span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">I expected it to compile.</p>
<p dir="auto">Instead, this happened:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&lt;anon&gt;:15:5: 15:15 error: cast to fat pointer: `&amp;FooBar` as `&amp;Foo`
&lt;anon&gt;:15     fb as &amp;Foo
              ^~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101"><pre class="notranslate"><code class="notranslate">&lt;anon&gt;:15:5: 15:15 error: cast to fat pointer: `&amp;FooBar` as `&amp;Foo`
&lt;anon&gt;:15     fb as &amp;Foo
              ^~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101
</code></pre></div>
<h2 dir="auto">Meta</h2>
<p dir="auto">I used playpen with the stable channel.</p>
<p dir="auto">This is using "rustc 1.0.0-nightly (<a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/rust-lang/rust/commit/458a6a2f6e9dfb6ed3d76f14418ff1f2f5e97f86/hovercard" href="https://github.com/rust-lang/rust/commit/458a6a2f6e9dfb6ed3d76f14418ff1f2f5e97f86"><tt>458a6a2</tt></a> 2015-01-25 21:20:37 +0000)".</p>
<p dir="auto">Code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="#![start]
#[no_mangle]
fn main() {}"><pre class="notranslate"><code class="notranslate">#![start]
#[no_mangle]
fn main() {}
</code></pre></div>
<p dir="auto">Command line:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="rustc test.rs"><pre class="notranslate"><code class="notranslate">rustc test.rs
</code></pre></div>
<p dir="auto">Error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="test.rs:1:1: 1:10 warning: unused attribute, #[warn(unused_attributes)] on by default
test.rs:1 #![start]
          ^~~~~~~~~
rustc: /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/llvm/include/llvm/Support/Casting.h:237: typename llvm::cast_retty&lt;X, Y*&gt;::ret_type llvm::cast(Y*) [with X = llvm::Function; Y = llvm::Value; typename llvm::cast_retty&lt;X, Y*&gt;::ret_type = llvm::Function*]: Assertion `isa&lt;X&gt;(Val) &amp;&amp; &quot;cast&lt;Ty&gt;() argument of incompatible type!&quot;' failed.
Aborted"><pre class="notranslate"><code class="notranslate">test.rs:1:1: 1:10 warning: unused attribute, #[warn(unused_attributes)] on by default
test.rs:1 #![start]
          ^~~~~~~~~
rustc: /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/llvm/include/llvm/Support/Casting.h:237: typename llvm::cast_retty&lt;X, Y*&gt;::ret_type llvm::cast(Y*) [with X = llvm::Function; Y = llvm::Value; typename llvm::cast_retty&lt;X, Y*&gt;::ret_type = llvm::Function*]: Assertion `isa&lt;X&gt;(Val) &amp;&amp; "cast&lt;Ty&gt;() argument of incompatible type!"' failed.
Aborted
</code></pre></div>