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

Document binary operator argument passing conventions #7777

Closed
brendanzab opened this issue Jul 13, 2013 · 9 comments
Closed

Document binary operator argument passing conventions #7777

brendanzab opened this issue Jul 13, 2013 · 9 comments

Comments

@brendanzab
Copy link
Member

This works:

fn mul<T:Num>(x: &T, y: &T) -> T { *x * *y }

But this doesn't:

fn mul<T:Num>(&x: &T, &y: &T) -> T { x * y }
<anon>:7:23: 7:25 error: cannot move out of dereference of & pointer
<anon>:7          fn mul<T:Num>(&x: &T, &y: &T) -> T { x * y }
                                ^~
<anon>:7:31: 7:33 error: cannot move out of dereference of & pointer
<anon>:7          fn mul<T:Num>(&x: &T, &y: &T) -> T { x * y }
                                        ^~

The above &t: &T pattern could cut down on tons of repetitive derefs within function bodies.

@graydon
Copy link
Contributor

graydon commented Jul 13, 2013

I think this is working as intended. & patterns move out as they are evaluating into the destination if a new binding. * expressions alone (not targeting an assignment) don't. Maybe this requires clarification wrt expression evaluation rules and destination passing style?

@Kimundi
Copy link
Member

Kimundi commented Jul 13, 2013

@graydon: Wait, shouldn't moving out of a & ptr be illegal? You're just borrowing access to the value, moving out of it would mean transfer of ownership.

@huonw
Copy link
Member

huonw commented Jul 14, 2013

@Kimundi it's legal for implicitly copyable types, because the move becomes a copy.

@Kimundi
Copy link
Member

Kimundi commented Jul 14, 2013

Well sure, but then there should still be no difference between let x = *y and let &x = y.?

@Aatch
Copy link
Contributor

Aatch commented Jul 14, 2013

@Kimundi yes, but that isn't the case here. The expression interacts with autoref/deref and so complicates things.

@nikomatsakis
Copy link
Contributor

This is working as intended. Binary operators implicitly pass their arguments "by reference", so writing _x * *y is equivalent to mul(&_x, &*y), and hence there is no move in the second (working) version. Closing.

@nikomatsakis
Copy link
Contributor

Reopening and repurposing as a bug to improve documentation. I'm not sure whether this is documented or not, but I'd assume not.

@nikomatsakis nikomatsakis reopened this Jul 15, 2013
@catamorphism
Copy link
Contributor

Nominating.

@pnkfelix
Copy link
Member

pnkfelix commented Nov 7, 2013

closing in favor of #10337

@pnkfelix pnkfelix closed this as completed Nov 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants