Skip to content

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

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

Open
meh opened this issue Aug 8, 2015 · 5 comments
Open

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

meh opened this issue Aug 8, 2015 · 5 comments
Labels
A-inference Area: Type inference A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@meh
Copy link
Contributor

meh commented Aug 8, 2015

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

@jroesch
Copy link
Member

jroesch commented Aug 9, 2015

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

@steveklabnik steveklabnik added the A-type-system Area: Type system label Aug 13, 2015
@steveklabnik
Copy link
Member

Triage: this still reproduces today.

@Mark-Simulacrum Mark-Simulacrum added the A-inference Area: Type inference label May 22, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@steveklabnik
Copy link
Member

Triage: still reproduces.

@Spoonbender
Copy link

Triage: no change

@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
@fmease fmease added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Dec 21, 2024
@adwinwhite
Copy link
Contributor

The feature default_type_parameter_fallback has been removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference A-type-system Area: Type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

8 participants