Inference fails when a type using a default also implements Deref.

9ab5ced
Opened by meh. at 2024-12-21 05:10:29

The current code fails compilation:

#![feature(default_type_parameter_fallback)]
#![allow(unused_variables)]

use std::ops::Deref;

#[derive(Clone, Debug)]
pub struct Foo<T: Clone + Default = ()> {
    bar: T,
}

impl<T: Clone + Default> Foo<T> {
    #[inline(always)]
    pub fn new() -> Self {
        Foo {
            bar: Default::default(),
        }
    }

    #[inline(always)]
    pub fn with<U: Clone + Default>(self, value: U) -> Foo<U> {
        Foo {
            bar: value,
        }
    }
}

impl<T: Clone + Default> Deref for Foo<T> {
    type Target = T;

    #[inline(always)]
    fn deref(&self) -> &T {
        &self.bar
    }
}

fn main() {
    Foo::new().with(0u8);
}

With:

test.rs:37:13: 37:22 error: the type of this value must be known in this context
test.rs:37      Foo::new().with(0u8);
                           ^~~~~~~~~

/cc @bluss @jroesch

  1. I'll look into this more on Monday but it looks like there is a subtle interaction with deref, you can fix the first error by attaching a default to the impl, but it still fails on the call to with.

    http://is.gd/hk1xuf

    Jared Roesch at 2015-08-09 07:21:50

  2. Triage: this still reproduces today.

    Steve Klabnik at 2017-02-07 20:12:03

  3. Triage: still reproduces.

    Steve Klabnik at 2019-12-25 16:04:45

  4. Triage: no change

    Maayan Hanin at 2022-10-17 19:15:46