-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Rc/Arc: get rid of "value" terminology #64484
Comments
In my mental model I understand that these types contain a raw pointer which does not actually point to a |
Agreed. I was just looking for a term for what this pointer points to. It doesn't have to be a type, but I think "reference-counted object" is a reasonable choice. Would you agree? |
This might be true in some domains, but not Rust.
Rust doesn't have "objects", though we do in the sense you probably intend. I can appreciate trying to add clarity, but I think the current wording is a good balance between precision and understandability. |
I agree that the current wording is confusing by using the term "value" in two different senses in the same sentence. I would suggest:
I think we can afford a bit more elaboration than the current docs provide. |
It's unclear to me whether "reference-counted object" refers to the If it refers to the fn main() { unsafe {
// create two smart pointers to a T
let mut rc0 = Rc::new(Box::new(0_i32));
let ptr: *mut Box<i32> = Rc::get_mut(&mut rc0).unwrap();
let rc1 = rc0.clone();
// drop the T
std::ptr::drop_in_place(ptr);
// those two smart pointers still exist
dbg!(Rc::strong_count(&rc0)); // prints 2
// they also compare equal
assert!(Rc::ptr_eq(&rc0, &rc1)); // ALWAYS OK
// put a new T behind the Rc
ptr.write(Box::new(42));
}} A problem with the term "object" is that we have not defined what that means in Rust anywhere. I'm not sure if "place" would be the right word, but maybe we can just say that " |
I am pretty sure it is. Rust has "values" and "places", and that use of the term "value" aligns with what I said. This is also how we are using the term "value" in the UCG. Both of these uses of "value" also do not align with what So why are you saying this does not apply to Rust?
Fair. We do use the term "allocated object" in some places though, e.g. for
I still disagree, I think the current wording is not just imprecise but plain wrong.
Yeah, something like that -- though I would avoid "boxed". Also, this change affects many
I'd say unless
Yeah, "object" might be not great, which is why I opened an issue and not a PR. ;) |
That sounds reasonable to me.
Indeed. The |
I don't see any of those things that suggest any sort of demands around identity; Rust clearly has values, it's the "does not have any identity beyond mathematical interpretation" bit. I tend to see this language around OOP languages that are adding "value types" to the currently existing "object types", but Rust doesn't have those. It's all just values. |
I see, so this is about the identity on values, not what they refer to? But, if "5" is a value in Rust (in Also, we usually speak of a value being "stored" somewhere, e.g. the UCG glossary says a value is whet gets stored inside a place, and the I also think that there is no good reason to make value have an extra identity. We clearly need a term for the thing that does not have an extra identity, to be able to talk about "a value stored somewhere", to define the "value representation", and so on -- all those uses of the term "value" refer to an abstract mathematical notion of "value" that is independent of how or where such a value is concretely represented as a sequence of bytes. If we don't call that abstract thing a "value", what do we call it? We had a long bikeshed about this in the UCG and "value" was the best term we found. From what I can see, the Rc/Arc docs are the only place where we use "value" in a sense that they have extra identity beyond "mathematical value". You claim "this [not having extra identity] is not true in Rust", but do you have any evidence beyond the docs we are discussing here (which I think are just a local mistake we can and should fix)? Also note that
I have no idea what this has to do with "5" being not the same value as "5"? Indeed Rust is all just values, but the values are what gets stored inside some place in memory, and a "5" stored in two different variables is still the same "value" stored multiple times in different "places". A different form of identity comes in when talking about a pointer to 5, or more precisely a pointer to some memory location that stores 5. Clearly, two distinct pointers can both point to 5. But this does not contradict "every variable stores a value"; the variable of pointer/reference type just stores a value that is "this address in memory [and this provenance]", i.e., the mathematical value is something like This is all getting a bit long-winding because I cannot entirely figure out where our conceptual mismatch is arising from here. |
I think this is the core of it; I would say that there are two different values, both of which have a bit pattern that corresponds to
I cannot either; I suspect that it's coming from two different terminology traditions. That's usually what happens in these kinds of situations, anyway. Re-reading the original language again, I think that I have a tolerance for some level of informalism, and so that's why I was fine with the original language, but analyzing more closely, "points to the same value" is kind of a no-op, a re-statement of the same thing, because "points to the same place" is the same as "the same value." It only works because of the contrast with "not just values that compare as equal."
My main beef here is that "object" and "instance" will convey the wrong message to the folks coming from OOP languages. What do you think about:
This keeps the "place expression" terminology around memory locations, does not use the "value" term we disagree about, and doesn't bring up terms with OOP baggage. |
Some discussion in https://rust-lang.zulipchat.com/#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/rc-terminology. I think we should go with:
|
But then what is the name for the thing represented by the bit pattern? The thing that is the same?
I actually think the message conveyed is basically right ("good enough"). E.g., comparing two
Technically not just the allocations have to be the same but also the offsets... but |
I've opened rust-lang/unsafe-code-guidelines#213 to track providing a precise definition for "allocation" at some point, but in the meantime I don't think that resolving that issue should block trying to land this since I find the term "allocation" in the context of Rc hard to misunderstand, but we can always try to explain it a bit more if users start having questions. |
@steveklabnik Do you have any objections to my proposed wording? |
I had actually thought I had 👍 'd it; it looks good to me. |
Cool; let's see... who is going to implement this? :D @RalfJung maybe wants to? |
This doesn't seem like too much work; I added it to my list.
|
Submitted a PR: #65505 |
Rc: value -> allocation See rust-lang#64484. This does not yet edit `Arc` as I first wanted to be sure we agree on the terminology the way it actually ends up. "value" as a term appears a lot in this file, and sometimes it refers to the value stored inside the `RcBox` while sometimes it refers to the `RcBox` itself. I tried to properly tease these apart but may have made some mistakes. The former should now always be called "inner value" and the latter "allocation". One area where I was very unsure of which terminology is dropping: the `value` field of the `RcBox` will get dropped *earlier* than the `RcBox` itself if there are weak references. I decided that "dropping the value stored in the allocation" refers to dropping the value field, while "destroying the allocation" refers to actually freeing its backing memory. r? @Centril
Rc: value -> allocation See rust-lang#64484. This does not yet edit `Arc` as I first wanted to be sure we agree on the terminology the way it actually ends up. "value" as a term appears a lot in this file, and sometimes it refers to the value stored inside the `RcBox` while sometimes it refers to the `RcBox` itself. I tried to properly tease these apart but may have made some mistakes. The former should now always be called "inner value" and the latter "allocation". One area where I was very unsure of which terminology is dropping: the `value` field of the `RcBox` will get dropped *earlier* than the `RcBox` itself if there are weak references. I decided that "dropping the value stored in the allocation" refers to dropping the value field, while "destroying the allocation" refers to actually freeing its backing memory. r? @Centril
Completed in #65505. |
The Rc docs use the term "value" to refer to an
RcBox
instance (and similar forArc
). That's IMO a bad use of terminology: A "value" is something like "5" or "true" that does not have an identity beyond its mathematical interpretation; anRcBox
has a location so that even two distinctRcBox
that contain the same value (say, both contain "5") are "not the same".This is particularly bad in the docs for
ptr_eq
:"5" and "5" are the same value, and yet
Rc::ptr_eq(&Rc::new(5), &Rc::new(5))
returnsfalse
. So IMO the docs are just wrong -- or rather, they are using the term "value" in the wrong way.I suggest that we replace all/most uses of "value" in these docs by "reference-counted object" or maybe something involving "instance". I think that better conveys what is happening.
Opinions? Cc @Centril @SimonSapin @gnzlbg
The text was updated successfully, but these errors were encountered: