Skip to content

Typechecker doesn't honor Deref as a trait bound on references when using * operator #75208

@djrenren

Description

@djrenren

I tried this code:

// Ought to work fine but doesn't
fn borrow_and_deref<T>(p: T) -> i32 where for<'a> &'a T: Deref<Target=i32> {
    *&p
}

I expected to see this happen: Successful typechecking

Instead, this happened:
image

If we invoke deref manually, rather than implicitly through syntax sugar everything works fine:

// Works, so the issue is specifically at the intersection of syntax
// sugar and the trait bound
fn borrow_and_deref<T>(p: T) -> i32 where for<'a> &'a T: Deref<Target=i32> {
    *(&p).deref()
}

So this issue is specifically at the intersection of this kind of constraint and the syntax sugar.

Playground link

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-langRelevant to the language team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions