Skip to content

Parameterized traits can't be implemented for arbitrary types even if their parameters are "local" #20749

Closed
@netvl

Description

@netvl

This program fails to compile:

use std::ops::Add;

struct BigFloat;

impl Add<BigFloat> for f64 {
    type Output = BigFloat;

    fn add(self, rhs: BigFloat) -> BigFloat {
        BigFloat
    }
}

fn main() {}

The error:

test7.rs:5:1: 11:2 error: the type `f64` does not reference any types defined in this crate; only traits defined in the current crate can be implemented for arbitrary types [E0117]
test7.rs:5 impl Add<BigFloat> for f64 {
test7.rs:6     type Output = BigFloat;
test7.rs:7 
test7.rs:8     fn add(self, rhs: BigFloat) -> BigFloat {
test7.rs:9         BigFloat
test7.rs:10     }
            ...
error: aborting due to previous error

I would expect that this should work because Add's parameter is "local" type. Associated types and multidispatch RFC agrees with me.

reddit discussion

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions