You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
error[E0308]: mismatched types
--> src/main.rs:11:4
|
8 | fn dewrap<T>(d:&Wrapper<T>) -> Wrapper<T>
| ---------- expected `Wrapper<T>` because of returntype
...
11 | d.clone()
| ^^^^^^^^^^^ expected struct `Wrapper`, found &Wrapper<T>
|
= note:expected type `Wrapper<T>`
found type `&Wrapper<T>`
which is super weird, because the signature of .clone() is clone(&T)->T.
I'm not fully certain what exactly is happening here (I guess, d is getting auto-referenced, and the function body is equivalent to (&d).clone()), but regardless the error message is super confusing.
I would appreciate if some kind of did you mean to add trait bounds T: Clone? diagnostic was added.
Relatedly (but may be more suitable for a separate bugreport?), the struct declaration has #[derive(Clone)] but the derive fails silently, since T does not have : Clone bounds. It still may be what the author intended (the derive will work when the struct is instantiated with a cloneable type), but I'm afraid it usually isn't.
Also relatedly: clippy does catch the error:
error: using `clone` on a double-reference; this will copy the reference instead of cloning the inner type
--> src/main.rs:11:4
|
11 | (d.clone())
| ^^^^^^^^^^^
|
= note:#[deny(clone_double_ref)] on by default
= help:for further information visit https://rust-lang-nursery.github.io/rust-clippy/v0.0.211/index.html#clone_double_ref
help: try dereferencing it
|
11 | &(*d).clone()
|
help: or try being explicit about what type to clone
|
11 | &Wrapper<T>::clone(d)
Unfortunately this is displayed only after the error is pseudo-fixed by declaring the function return type to be &Wrapper<T>, and not displayed at all when fixed for good by adding the : Clone trait bound.
It might be useful to uplift the diagnostics to rustc here (but I have zero understanding of the process or feasibility of this).
The text was updated successfully, but these errors were encountered:
When trying to compile this piece of code (playground link):
I get the following error message:
which is super weird, because the signature of
.clone()
isclone(&T)->T
.I'm not fully certain what exactly is happening here (I guess,
d
is getting auto-referenced, and the function body is equivalent to(&d).clone()
), but regardless the error message is super confusing.I would appreciate if some kind of
did you mean to add trait bounds T: Clone?
diagnostic was added.Relatedly (but may be more suitable for a separate bugreport?), the struct declaration has
#[derive(Clone)]
but the derive fails silently, sinceT
does not have: Clone
bounds. It still may be what the author intended (the derive will work when the struct is instantiated with a cloneable type), but I'm afraid it usually isn't.Also relatedly: clippy does catch the error:
Unfortunately this is displayed only after the error is pseudo-fixed by declaring the function return type to be
&Wrapper<T>
, and not displayed at all when fixed for good by adding the: Clone
trait bound.It might be useful to uplift the diagnostics to rustc here (but I have zero understanding of the process or feasibility of this).
The text was updated successfully, but these errors were encountered: