Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAllow implicit coercions for cross-borrows. #226
Conversation
This comment has been minimized.
This comment has been minimized.
|
I've been a big fan of this idea in the past. I'm not as much of a fan today, primarily because it makes it slightly harder to figure out whether a given expression is a move/copy or a cross-borrow, but I think I still fall in the I'm also in favor of implementing For what it's worth, we already have some inconsistency in moves vs borrows with macros. I can say let s: String = foo();
println!("{}", s); // note: bare `s`
consume_string(s);But I can't say let s: String = foo();
io::println(s); // bare `s` again, this time it's moved
consume_string(s);This RFC coupled with a |
This comment has been minimized.
This comment has been minimized.
|
This also seems related to rust-lang/rust#13910, which needs an RFC (extending support for |
This comment has been minimized.
This comment has been minimized.
|
FWIW, UFCS provides an argument in favor of more drastic auto-borrowing, because if any type That said, I think it's still appropriate to stick with just cross-borrowing. Full auto-borrowing just seems potentially too problematic. |
huonw
reviewed
Sep 4, 2014
| &e | &e | ||
| &*e | e | ||
| &**e | *e | ||
| &&**e | &*e |
This comment has been minimized.
This comment has been minimized.
huonw
Sep 4, 2014
Member
I don't understand how this can work?
It seems let x = &&**e; is equivalent to the following today:
let tmp = e.deref().deref();
let x = &tmp;while the latter seems like it should just be e.deref() (as &*e is now)? Or is there some deep coercions going on here, if so, that doesn't seem like it's backwards compatible, particularly, since &*e could mean two different things.
This comment has been minimized.
This comment has been minimized.
nrc
Sep 4, 2014
Author
Member
It might be wrong actually. I was imagining *e would happen first, then we would implicitly coerce to &**e and finally the explicit & would give &&**e. If we allow coercions at &e but not at *e, then I think there is no ambiguity. This distinction does seem a bit ad-hoc though (it follows where we have an expected type in the type checker at the moment). I have another RFC which goes in to depth on sites for coercions, for this RFC I'm just kind of assuming that is nailed down elsewhere.
huonw
reviewed
Sep 4, 2014
|
|
||
| # Alternatives | ||
|
|
||
| Allow cross-borrowing only in the immutable case. That is, only coerce if there |
This comment has been minimized.
This comment has been minimized.
huonw
Sep 4, 2014
Member
I'm in favour of this. For methods, it is normally fairly clear that the receiver is being mutated, e.g. vec.push, but this is not necessarily so clear for general arguments.
I know that some C++ style guides disallow non-const references; although we are at a slight advantage, since the effects of 'incorrect' mutation are not nearly as catastrophic.
This comment has been minimized.
This comment has been minimized.
|
Sounds good, my only reservation is that being able to write Maybe deep cross-borrowing wouldn't be so bad? |
alexcrichton
assigned
nrc
Sep 4, 2014
alexcrichton
force-pushed the
rust-lang:master
branch
from
6357402
to
e0acdf4
Sep 11, 2014
This comment has been minimized.
This comment has been minimized.
|
I've posted an alternative design that achieves similar goals but is somewhat more conservative: it only allows coercions on already-borrowed data, and thereby does not introduce new borrows implicitly. The RFC gives detail about the downsided of implicit borrowing. |
aturon
force-pushed the
rust-lang:master
branch
from
4c0bebf
to
b1d1bfd
Sep 16, 2014
nrc
added a commit
to nrc/rfcs
that referenced
this pull request
Sep 19, 2014
This comment has been minimized.
This comment has been minimized.
|
Some data and a little discussion: https://gist.github.com/nick29581/809614adb2bbb38232b7 |
This comment has been minimized.
This comment has been minimized.
|
Closed in favour of #241 or something like that (that idea is superior because it better respects the borrowing/ownership principles of Rust and substitutability). |
nrc commentedSep 3, 2014
Wherever a type implements
Deref<T>, allow an implicit coercion to&T. Removes the need for most occurrences of&*.