# Object Safety is a terrible term

**URL:** <https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025>\
**Category:** documentation\
**Created:** [June 13, 2024, 12:07pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025 "2024-06-13T12:07:55Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [June 16, 2024, 4:52pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/22 "2024-06-16T16:52:53Z")

</div>

> [@newpavlov](#):
>
> "Object" is a very loaded word. Arguably, we should avoid it to prevent potential confusion related to previous OOP knowledge.

To users of high-level OOP interfaces, perhaps, but for anyone who has implemented or worked with or implemented low-level OOP interfaces (think `GObject` or manual C vtables), the parallels are pretty clear. even without full inheritance, it's still a vtable-based object with dynamic dispatch.

> [@newpavlov](#):
>
> In the vein of "`dyn` compatible", we could rename "trait objects" to "`dyn` types", which would naturally apply to both `Box<dyn Trait>` and `&dyn Trait` types.

I don't think "dyn types" is very clear. it sounds like it sounds too similar to "dynamic typing", perhaps leading people to believe rust has some sort of gradual typing system (which it does not). the closest thing to dynamic typing that rust has is `dyn Any`, which is only a subset of trait objects.

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/newpavlov/32/3290_2.png) [@newpavlov](https://internals.rust-lang.org/u/newpavlov)\
**Post date:** [June 16, 2024, 5:19pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/23 "2024-06-16T17:19:37Z")

</div>

> [@binarycat](#):
>
> I don't think "dyn types" is very clear.

The point is to have unified terminology for the trait property ("`dyn`-compatible traits") and types which rely on it ("`dyn` types"). "Object" is too vague for this purpose. Yes, because of its vagueness in some areas it's used with the meaning close to Rust's "trait object", but it does not cancel the original vagueness.

For new Rust users this terminology will be introduced together with the `dyn` keyword, while experienced Rust users already know the keyword. Plus, we can explain it as a shortened version of "`dyn`(amic dispatch) types". This proposal intentionally introduces Rust-specific terminology without relying on parallels with other languages, so I don't think there is much room for confusion here.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [June 16, 2024, 5:43pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/24 "2024-06-16T17:43:12Z")

</div>

I know overloading the term "safe" further isn't great, but informally I've been using "dyn-safe" to refer to the class of object-safe traits. It's not a _complete_ bastardization of the safety concept, though, as any attempt to make a non-dyn-safe trait into a `dyn Trait` "object" requires some amount of `unsafe`. That may still be trivially encapsulated behind just some `Box` and more `dyn`amic dispatch that doesn't require _you_ to write `unsafe`, but the fact that someone needed to do some `unsafe` in the bridge is how I justify calling it dyn-safety despite being part of the UCG and attempting to narrowly define safety as a useful term.

I'm not against finding a better term to use in docs that try to remain formally correct, and am favorably included towards "dyn-compatible," but I fully expect that at least "dyn-safety" will stick around in contexts less concerned with being fully _correct_ in their term usage.

As for what to call dyn trait types other than "objects," well, I just call them as "dyn trait" and let sentence structure dictate whether more words are even necessary to be clear about what's being discussed.

* * *

Opsem does discuss some things in terms of an "allocated object," which refers to the entire chunk of memory created by allocation, whether dynamic or stack. I don't recall that term getting put into any documentation yet, but it's certainly unfortunate how "object" is overloaded due to being a good word for "a thing" without implying further context.

(Member of T-opsem but not speaking on behalf of the team)

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [June 16, 2024, 5:48pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/25 "2024-06-16T17:48:58Z")

</div>

> [@newpavlov](#):
>
> "Object" is too vague for this purpose.

I agree, that's why I recommended the option of always using the more specific "trait object"

---

<div class="post-metadata">

**Author:** ![quinedot](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/quinedot/32/7294_2.png) [@quinedot](https://internals.rust-lang.org/u/quinedot)\
**Post date:** [June 16, 2024, 6:23pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/26 "2024-06-16T18:23:27Z")

</div>

> [@newpavlov](#):
>
> Also, "trait object" usually used for types like `Box<dyn Trait>` and not for reference types (e.g. `&dyn Trait`).

That hasn't been my experience.\[1\]

> [@kornel](#):
>
> There's a very common error text, included in hundreds of ui tests. Should it stay like that?

One never makes a trait into a `dyn Trait`, so that error is poorly worded independent of any "object" language.

> [@CAD97](#):
>
> I know overloading the term "safe" further isn't great, but informally I've been using "dyn-safe" to refer to the class of object-safe traits.

I'm guilty of this too.

Perhaps "`dyn`-capable"?

> the trait `$Trait` is not `dyn`-capable

The reference also uses "dispatchable" and for methods (and "explicitly non-dispatchable" for the `Self: Sized` opt-out on methods). And doesn't yet talk about associated types/GATs, which can now also have the opt-out. "dispatchable" wouldn't work for associated types, but "`dyn`-capable" could work for both.

* * *

1. Unless it went straight over my head somehow.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [June 16, 2024, 6:31pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/27 "2024-06-16T18:31:41Z")

</div>

> [@kornel](#):
>
> I think replacing "object safe" with "trait-object safe" is an easy improvement to make, but in the particular case of "object-safe trait", the "trait-object-safe trait" sounds repetitive. Should I leave it as "object-safe" when it's directly referring to a trait? Same goes for repetitiveness of "`dyn Trait`-compatible trait".

I think "`dyn`-compatible trait" is just as clear and not repetitive.

I personally think "`dyn`-compatible" is the clearer term to use instead of "trait-object-safe", both because it avoids overloading the meaning of "safe" and because it directly makes it clear that the issue is being _compatible_ with `dyn`.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [June 17, 2024, 2:43pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/28 "2024-06-17T14:43:04Z")

</div>

I like "dynamic type", both because it avoids "object" and because it seems pretty apt to me: the point of `dyn Trait` is to make the actual type of the underlying value a dynamic property rather than a static one. So this fits pretty well with the static vs dynamic terminology in PL: it statically encodes the information "the type of this value is only known dynamically, but we know it implements this trait".

A more theoretically founded term would be "existential type", since that's the fundamental PL concept this corresponds to, but that is too obscure so I don't think it worst as a Rust term.

One could also try to draw an analogy with erasure (the actual type is erased, the only point that is retained is that this trait is implemented). But I think that's also worse than "dynamic type".

---

<div class="post-metadata">

**Author:** ![flirora](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/flirora/32/12267_2.png) [@flirora](https://internals.rust-lang.org/u/flirora)\
**Post date:** [June 17, 2024, 7:05pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/29 "2024-06-17T19:05:02Z")

</div>

> [@RalfJung](#):
>
> A more theoretically founded term would be "existential type", since that's the fundamental PL concept this corresponds to, but that is too obscure so I don't think it worst as a Rust term.

Not to mention that “existential type” could also refer to `impl Trait` types, so that’s another reason against using it to describe dyn-types.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [June 17, 2024, 7:20pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/30 "2024-06-17T19:20:18Z")

</div>

`impl Trait` are static existential types, `dyn Trait` are dynamic existential types. Normal type theory only really has the latter.

---

<div class="post-metadata">

**Author:** ![simonbuchan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/simonbuchan/32/9390_2.png) [@simonbuchan](https://internals.rust-lang.org/u/simonbuchan)\
**Post date:** [June 18, 2024, 6:47am UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/31 "2024-06-18T06:47:43Z")

</div>

Out of curiosity, I just grabbed [the C specification from open-std.org](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf), and searching for "object" finds 728 references (in "only" 550 pages!) - "object" being defined as:

> region of data storage in the execution environment, the contents of which can represent values

I guess it's not really an issue there because nobody reads the specification 😋

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [June 18, 2024, 9:02am UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/32 "2024-06-18T09:02:10Z")

</div>

Yeah, C has a typed memory model where memory stores "objects". But in Rust memory just stores bytes (somewhat [strange bytes](https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#abstract-byte) but bytes nevertheless), and anything higher-level and typed, like the idea of a "value", only arises transiently during the execution of a single statement.

---

<div class="post-metadata">

**Author:** ![simonbuchan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/simonbuchan/32/9390_2.png) [@simonbuchan](https://internals.rust-lang.org/u/simonbuchan)\
**Post date:** [June 18, 2024, 9:05am UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/33 "2024-06-18T09:05:33Z")

</div>

But I'm pretty sure I've never heard of C programmers being tripped up by the term, thinking it refers to OOP objects - there's lots of possible reasons for that, not just my silly joke answer, but at least it's some evidence that the "object" part of the term "object safety" is only somewhat bad.

---

<div class="post-metadata">

**Author:** ![robertbastian](https://avatars.discourse-cdn.com/v4/letter/r/cdc98d/32.png) [@robertbastian](https://internals.rust-lang.org/u/robertbastian)\
**Post date:** [June 18, 2024, 10:06am UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/34 "2024-06-18T10:06:27Z")

</div>

> [@quinedot](#):
>
> Perhaps "`dyn`-capable"?
> 
> > the trait `$Trait` is not `dyn`-capable

Perhaps "`dyn`-able"?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [June 18, 2024, 11:20am UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/35 "2024-06-18T11:20:52Z")

</div>

I think there are couple of important differences: C has nothing OOP-like, so there's nothing to confuse it with, and the spec term is used mostly by language lawyers. In other contexts in C "object" be loosely used to refer to what a C pointer or struct is representing (like a network socket or an audio channel), rather than the span of memory itself.

OTOH Rust has associated functions that look very much like methods, and a CamelCase naming convention for types that resembles boxed types in Java/C#/Swift.

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tczajka/32/8923_2.png) [@tczajka](https://internals.rust-lang.org/u/tczajka)\
**Post date:** [June 18, 2024, 11:57am UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/36 "2024-06-18T11:57:30Z")

</div>

I think there is no ambiguity with OOP: Rust objects, C objects and OOP objects are the same sort of thing. Object-oriented languages aren't the only ones that have objects, just like functional languages aren't the only ones that have functions. Objects in OOP are just at the center of attention and thus get more features packed into them.

Let me make my proposal a bit more nuanced:

1. `dyn T` is a **"dynamic type"**.
2. Objects of type `dyn T` (trait objects) are **"dynamically typed objects"**.
3. Traits `T` that can be used in `dyn T` (object-safe traits) are **"dynamically typeable traits"**.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [June 18, 2024, 1:01pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/37 "2024-06-18T13:01:36Z")

</div>

> [@tczajka](#):
>
> Rust objects, C objects and OOP objects are the same sort of thing.

That would mean that in `let x = 1` the `x` is an object (as it is in C), and by that definition "object safe" is even more ambiguous.

---

<div class="post-metadata">

**Author:** ![tczajka](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tczajka/32/8923_2.png) [@tczajka](https://internals.rust-lang.org/u/tczajka)\
**Post date:** [June 18, 2024, 1:40pm UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/38 "2024-06-18T13:40:07Z")

</div>

> [@kornel](#):
>
> That would mean that in `let x = 1` the `x` is an object (as it is in C)

Yes. The reference uses the word "object" in many places, [for example](https://doc.rust-lang.org/reference/types/boolean.html):

> An object with the boolean type has a [size and alignment](https://doc.rust-lang.org/reference/type-layout.html#size-and-alignment) of 1 each. The value false has the bit pattern `0x00` and the value true has the bit pattern `0x01` . It is [undefined behavior](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) for an object with the boolean type to have any other bit pattern.

---

<div class="post-metadata">

**Author:** ![riking](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/riking/32/2818_2.png) [@riking](https://internals.rust-lang.org/u/riking)\
**Post date:** [June 20, 2024, 12:36am UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/39 "2024-06-20T00:36:03Z")

</div>

Allow me to counterpropose wording of "Trait object **eligible**"

Edit: This, basically...

> [@elidupree](#):
>
> I would shorten it further: "dyn-compatible". Since that's the only thing the keyword `dyn` means, and then there's less words to stumble over in the phrase "this trait isn't dyn-trait compatible".

---

<div class="post-metadata">

**Author:** ![notriddle](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/notriddle/32/14082_2.png) [@notriddle](https://internals.rust-lang.org/u/notriddle)\
**Post date:** [June 20, 2024, 12:56am UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/40 "2024-06-20T00:56:19Z")

</div>

So I guess [the note in rustdoc](https://doc.rust-lang.org/1.79.0/std/clone/trait.Clone.html#object-safety) would be rephrased?

> ### ~~Object~~ Dyn Safety
> 
> This trait is **not** [~~object~~ dyn safe](https://doc.rust-lang.org/1.79.0/reference/items/traits.html#object-safety).

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [June 20, 2024, 10:51am UTC](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025/41 "2024-06-20T10:51:38Z")

</div>

> <https://github.com/rust-lang/rust/pull/126554>
>
> "Object Safety" is a difficult to understand jargon. I'm proposing a wording tha…t makes it clearer it's about trait objects, and mentions \`dyn Trait\` which is the syntax that users are going to be more familiar with.
> 
> Related PR: https://github.com/rust-lang/reference/pull/1512
> 
> Longer explanation: https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025

[Previous page](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025.md?page=1)

[Next page](https://internals.rust-lang.org/t/object-safety-is-a-terrible-term/21025.md?page=3)
