Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idiomatic Rc::clone is unusable with dyn Trait parameters #58716

Closed
neyo8826 opened this issue Feb 25, 2019 · 3 comments
Closed

Idiomatic Rc::clone is unusable with dyn Trait parameters #58716

neyo8826 opened this issue Feb 25, 2019 · 3 comments
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-traits Area: Trait system T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@neyo8826
Copy link

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=0771a25efd95be83580de70b3d08db75

AFAIK Rc::clone() should be used, instead of .clone().
It seems free functions are getting their type parameters from the outer scope, not from their own arguments.
Can we coerce it somehow

@Centril Centril added A-traits Area: Trait system T-lang Relevant to the language team, which will review and decide on the PR/issue. A-coercions Area: implicit and explicit `expr as Type` coercions labels Feb 25, 2019
@jonas-schievink
Copy link
Contributor

Isn't this just caused by the fact that .-syntax performs coercions on the receiver?

@neyo8826
Copy link
Author

fn clone(&self) -> Self;
fn clone(&self) -> Rc<T>;

So, the UFCS version gets it actual type from the caller site (Rc<T>).
The coercion shoud occour after the function call, since the type of self is fix, not the coerced result.
We could restrict this deduction inversion for cases invovin subtyping only (T as dyn Trait)

@kennytm
Copy link
Member

kennytm commented Feb 25, 2019

This is rust-lang/rust-clippy#2048 which is also one of the reasons why the clippy::clone_on_ref_ptr lint has been demoted to the "restriction" group. That is, we no longer consider in general Rc::clone(&st) is more idiomatic than st.clone().

The workaround is to write Rc::<St>::clone(&st), BTW.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coercions Area: implicit and explicit `expr as Type` coercions A-traits Area: Trait system T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants