Skip to content

[E0207] if type parameter used only in impl constraints #33374

Closed
@KalitaAlexey

Description

@KalitaAlexey
use std::borrow::BorrowMut;

pub trait Value {
    fn get(&self) -> i32;
    fn set(&mut self, value: i32);
}

pub struct UnmanagedValue {
    value: i32,
}

impl Value for UnmanagedValue {
    fn get(&self) -> i32 {
        self.value
    }

    fn set(&mut self, value: i32) {
        self.value = value;
    }
}

pub struct ManagedValue {
    value: i32,
}

impl Value for ManagedValue {
    fn get(&self) -> i32 {
        self.value
    }

    fn set(&mut self, value: i32) {
        if self.value == value {
            return;
        }
        self.value = value;
    }
}

pub struct SetValueAction {
    value: i32,
}

pub trait Action<T>: Sized {
    fn execute(self, target: &mut T);
}

impl<V: Value, T: BorrowMut<V>> Action<T> for SetValueAction {
    fn execute(self, target: &mut T) {
        target.borrow_mut().set(self.value);
    }
}

fn main() {}

fails with error:

<anon>:47:6: 47:7 error: the type parameter `V` is not constrained by the impl trait, self type, or predicates [E0207]
<anon>:47 impl<V: Value, T: BorrowMut<V>> Action<T> for SetValueAction {
               ^
<anon>:47:6: 47:7 help: see the detailed explanation for E0207
error: aborting due to previous error
playpen: application terminated with error code 101

But this function compiles

pub fn execute<V: Value, T: BorrowMut<V>>(action: SetValueAction, target: &mut T) {
    target.borrow_mut().set(action.value);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions