rust-lang / rust-lang/reference

Missing precedence when several coercions are possible

Open
#1,297 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-coercions
Dominant language
Rust
Stars
1.6k
Forks
607
PR merge metrics
PR metrics pending

Description

As transitive coercions are at least partially supported, it currently doesn't seem to be clear which coercion(s) will be performed when there are several possibilities.

Consider the following code:

use std::ops::Deref;

struct A;
struct B;

impl Deref for A {
    type Target = B;
    fn deref(&self) -> &B {
        &B
    }
}

trait Trt {
    fn name(&self) -> &'static str;
}

impl Trt for A {
    fn name(&self) -> &'static str {
        "A"
    }
}

impl Trt for B {
    fn name(&self) -> &'static str {
        "B"
    }
}

impl dyn Trt {
    fn foo(&self) {
        println!("dyn {}", self.name());
    }
}

fn main() {
    let a = A;
    <dyn Trt>::foo(&a); // prints `dyn A`
     // but how can we deduce from the documentation that this doesn't happen internally?
    <dyn Trt>::foo(&a as &B as &dyn Trt);
}

(Playground)

See also this thread on URLO.

Contributor guide

Open the contributing guide

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 coercion example in the issue and run the linked Rust Playground case, then read the linked URLO discussion for the unresolved precedence question. Done means the Reference clearly explains which coercion is selected when several are possible and covers the demonstrated behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.