Confused about coercions place

RFC401 says where the coercions take place, but doesn't mention this case, the "=" assign can also occur coercion. Is this an oversight by rfc or some other issue?

fn main() {
    let a = String::from("abc");
    let mut b = &a;
    let c = String::from("def");
    b = &&&&&&c;//just "="
}
1 Like

Probably just an oversight.

The RFCs are a great learning resource in some ways, and also valuable for documenting some technical details that aren't yet properly documented elsewhere. I've read many and cite them frequently. But as a heads-up, it's not uncommon for them to be incomplete or otherwise different from reality. Sometimes stabilized implementation has drifted from the RFC, sometimes RFCs are overrode, abandoned, or only partially implemented, sometimes the language has just evolved, etc.

There's not much transitive coercion for example, and it would be bad for some patterns if there were.

let x: &mut Something = todo!();
// Extremely ambiguous if coercion were actually transitive
let raw = x as *mut _ as *const _;
2 Likes

The reference also says:

Coercion is allowed between the following types:

  • ...

  • T_1 to T_3 where T_1 coerces to T_2 and T_2 coerces to T_3 (transitive case)

    Note that this is not fully supported yet.

  • ...

So, it is not just the RFC, and if transitivity is sometimes bad then this really ought to be changed to be narrower.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.