rust-lang / rust-lang/rfcs

Allow the trait checker to attempt Deref before giving up

Open
#3,155 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Fairly self explanatory but it could be a partial solution to #2393 before it is implemented in the rust compiler

Suppose you have some code

use std::ops::Deref;

trait Foo { fn foo(&self); }

struct Bar { }
impl Foo for Bar {
  fn foo(&self) { }
}

fn baz<T: Foo> (_: &T) { }

struct WrapBar { wbar: Bar }

impl Deref for WrapBar {
  type Target = Bar;
  fn deref(&self) -> &Self::Target { &self.wbar }
}

fn main() {
  let b = WrapBar { wbar: Bar {} };
  // explicit call via &Bar
  &b.wbar.foo();
  // implicit Deref to &Bar before call, hooray https://doc.rust-lang.org/reference/expressions/method-call-expr.html
  &b.foo();
  // succeeds because WrapBar can be Deref'd to Bar because we are aware of baz::<Bar> having the signature &Bar -> ()
  baz::<Bar>(&b);
  // fails because WrapBar does not implement Foo because baz has signature &(T: Foo) -> ()
  // baz(&b);
}

Maybe we can amend rust-lang/rfcs#241 so that the compiler tries Deref coercion for function arguments with unsolved traits as well.

If this is a dumb idea then feel free to just close this.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Rust example in this issue, then read RFC 241 and the referenced issue #2393 to understand the proposed trait-checking and Deref behavior. Done would mean a clear, accepted direction for whether function arguments with unsolved traits should attempt Deref coercion; no implementation files or tests are identified here.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.