Stream: t-compiler

Topic: try expression alternatives have incompatible types


Jake Goulding (Sep 18 2018 at 23:21, on Zulip):

I'm thinking the answer here is "because chalk isn't up and running yet", but why does this fail?

use std::io::Result;

pub struct File;

pub trait Reader<T> {
    fn read(&mut self) -> Result<T>;
}

impl Reader<u32> for File {
    fn read(&mut self) -> Result<u32> {
        Ok(10)
    }
}

impl Reader<bool> for File {
    fn read(&mut self) -> Result<bool> {
        Ok(false)
    }
}

impl<T> Reader<Vec<T>> for File
where
    File: Reader<u32> + Reader<T>,
{
    fn read(&mut self) -> Result<Vec<T>> {
        let count: u32 = self.read()?;
        panic!();
    }
}

fn main() {}

playground

error[E0308]: try expression alternatives have incompatible types
  --> src/main.rs:26:26
   |
26 |         let count: u32 = self.read()?;
   |                          ^^^^^^^^^^^^
   |                          |
   |                          expected u32, found type parameter
   |                          help: try wrapping with a success variant: `Ok(self.read()?)`
   |
   = note: expected type `u32`
              found type `T`

Original SO question

Jake Goulding (Sep 18 2018 at 23:22, on Zulip):

It's also interesting because the implementation for bool is required to trigger it, although it's seemingly unused.

nikomatsakis (Sep 19 2018 at 17:45, on Zulip):

hmm

nikomatsakis (Sep 19 2018 at 17:45, on Zulip):

seems sort of like a bug

nikomatsakis (Sep 19 2018 at 17:46, on Zulip):

one of those cases where we overcommit

Last update: Nov 20 2019 at 01:10UTC