Skip to content

Generic Mul Bound Error #82046

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
pacmanmati opened this issue Feb 12, 2021 · 1 comment
Open

Generic Mul Bound Error #82046

pacmanmati opened this issue Feb 12, 2021 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-bisection Status: a bisection has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@pacmanmati
Copy link

I tried this code:

use cgmath::{Vector3, Point3};
use std::ops::Mul;

#[derive(Debug)]
pub struct Ray<S> {
    pub origin: Point3<S>,
    pub direction: Vector3<S>,
}

impl <S> Ray<S> {
    pub fn new(origin: Point3<S>, direction: Vector3<S>) -> Self {
	Self { origin, direction }
    }

    pub fn at(&self, t: S) -> Self {
	let aaa = self.direction * t;
	Self { origin: self.origin, direction: aaa }
    }
}

I expected to see this happen: Compiler suggests the following bound

where
    Vector3<S>: Mul<S, Output = Vector3<S>>

Instead, this happened: Compiler suggests the following bound

where
    S: Mul<Output = Vector3<S>>

the above bound doesn't constrain the generic correctly and the compiler will keep requesting you add it despite it already being present.

Meta

The bug also occurs when I use nightly.

rustc --version --verbose:

rustc 1.50.0 (cb75ad5db 2021-02-10)
binary: rustc
commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
commit-date: 2021-02-10
host: aarch64-apple-darwin
release: 1.50.0
@pacmanmati pacmanmati added the C-bug Category: This is a bug. label Feb 12, 2021
@RalfJung RalfJung added the A-diagnostics Area: Messages for errors, warnings, and lints label Feb 21, 2021
@estebank estebank added A-trait-system Area: Trait system D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 22, 2021
@fmease fmease added the E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example label Feb 11, 2025
@moxian
Copy link
Contributor

moxian commented Mar 25, 2025

This has been fixed in #94034

minimization

mod cgmath {
    pub trait BaseNum {}
    pub struct Vector3<S>(S);

    impl<S: BaseNum> std::ops::Mul<S> for Vector3<S>
    {
        type Output = Self;
        fn mul(self, other: S) -> Self {
            todo!()
        }
    }
}

use cgmath::Vector3;

pub struct Ray<S> {
    pub direction: Vector3<S>,
}

impl<S> Ray<S>
where
    S: std::ops::Mul<Output = S>,
{
    pub fn at(&self, t: S) -> Self {
        let aaa = self.direction * t;
        Self { direction: aaa }
    }
}

nightly-2022-04-26 output:

error[E0369]: cannot multiply `Vector3<S>` by `S`
  --> src\lib.rs:25:34
   |
25 |         let aaa = self.direction * t;
   |                   -------------- ^ - S
   |                   |
   |                   Vector3<S>
   |
help: consider further restricting this bound
   |
22 |     S: std::ops::Mul<Output = S> + std::ops::Mul<Output = S>,
   |                                  +++++++++++++++++++++++++++

For more information about this error, try `rustc --explain E0369`.

nightly-2022-04-27 output:

error[E0369]: cannot multiply `Vector3<S>` by `S`
  --> src\lib.rs:25:34
   |
25 |         let aaa = self.direction * t;
   |                   -------------- ^ - S
   |                   |
   |                   Vector3<S>
   |
help: consider further restricting this bound
   |
22 |     S: std::ops::Mul<Output = S> + cgmath::BaseNum,
   |                                  +++++++++++++++++

For more information about this error, try `rustc --explain E0369`.

@rustbot label: -E-needs-mcve +S-has-bisection

@rustbot rustbot added S-has-bisection Status: a bisection has been found for this issue and removed E-needs-mcve Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example labels Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. S-has-bisection Status: a bisection has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants