Skip to content

cannot infer type of the type parameter 'a declared on the trait Baz #109905

Closed
@justinlovinger

Description

@justinlovinger

Code

use std::{borrow::Borrow, marker::PhantomData};

pub struct Foo<Borrowed, B> {
    borrowed: PhantomData<Borrowed>,
    y: B,
}

pub struct Bar<A, Borrowed, B, C> {
    x: PhantomData<A>,
    borrowed: PhantomData<Borrowed>,
    y: PhantomData<B>,
    z: C,
}

pub trait Baz<'a, D, B> {
    fn baz(&'a self) -> &'a B;
}

impl<'a, A, Borrowed, B, C> Baz<'a, Bar<A, Borrowed, B, C>, Borrowed> for C
where
    B: Borrow<Borrowed> + 'a,
    C: Borrow<Foo<Borrowed, B>>,
{
    fn baz(&'a self) -> &'a Borrowed {
        self.borrow().y.borrow()
    }
}

// fn foobar<Borrowed, B>(foo: &Foo<Borrowed, B>)
// where
//     B: Borrow<Borrowed>,
// {
//     foo.baz();
// }

fn foobar<'a, Borrowed, B>(foo: &'a Foo<Borrowed, B>)
where
    B: Borrow<Borrowed>,
{
    <Foo<Borrowed, B> as Baz<'a, _, _>>::baz(foo);
}

Meta

rustc --version --verbose:

rustc 1.66.1 (90743e729 2023-01-10)
binary: rustc
commit-hash: 90743e7298aca107ddaa0c202a4d3604e29bfeb6
commit-date: 2023-01-10
host: x86_64-unknown-linux-gnu
release: 1.66.1
LLVM version: 15.0.2

This was also tried on with 1.70.0-nightly (2023-04-02 3a8a131e9509c478ece1) on Rust Playground, with the same error.

Error output

error[E0282]: type annotations needed
  --> src/lib.rs:40:5
   |
40 |     <Foo<Borrowed, B> as Baz<'a, _, _>>::baz(foo);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `'a` declared on the trait `Baz`
Backtrace

error[E0282]: type annotations needed
  --> src/lib.rs:40:5
   |
40 |     <Foo<Borrowed, B> as Baz<'a, _, _>>::baz(foo);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `'a` declared on the trait `Baz`

Note

Without the fully qualified path, see commented code, the error is error[E0282]: type annotations needed.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions