1 //! "Object safety" refers to the ability for a trait to be converted
2 //! to an object. In general, traits may only be converted to an
3 //! object if all of their methods meet certain criteria. In particular,
6 //! - have a suitable receiver from which we can extract a vtable and coerce to a "thin" version
7 //! that doesn't contain the vtable;
8 //! - not reference the erased type `Self` except for in this receiver;
9 //! - not have generic type parameters.
11 use super::elaborate_predicates;
14 use crate::hir::def_id::DefId;
16 use crate::traits::{self, Obligation, ObligationCause};
17 use crate::ty::subst::{InternalSubsts, Subst};
18 use crate::ty::{self, Predicate, ToPredicate, Ty, TyCtxt, TypeFoldable};
19 use rustc_span::{Span, DUMMY_SP};
21 use std::iter::{self};
22 use syntax::ast::{self};
23 use syntax::symbol::Symbol;
25 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
26 pub enum ObjectSafetyViolation {
27 /// `Self: Sized` declared on the trait.
30 /// Supertrait reference references `Self` an in illegal location
31 /// (e.g., `trait Foo : Bar<Self>`).
34 /// Method has something illegal.
35 Method(ast::Name, MethodViolationCode, Span),
38 AssocConst(ast::Name, Span),
41 impl ObjectSafetyViolation {
42 pub fn error_msg(&self) -> Cow<'static, str> {
44 ObjectSafetyViolation::SizedSelf => {
45 "the trait cannot require that `Self : Sized`".into()
47 ObjectSafetyViolation::SupertraitSelf => {
48 "the trait cannot use `Self` as a type parameter \
49 in the supertraits or where-clauses"
52 ObjectSafetyViolation::Method(name, MethodViolationCode::StaticMethod, _) => {
53 format!("associated function `{}` has no `self` parameter", name).into()
55 ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelf, _) => format!(
56 "method `{}` references the `Self` type in its parameters or return type",
60 ObjectSafetyViolation::Method(
62 MethodViolationCode::WhereClauseReferencesSelf,
64 ) => format!("method `{}` references the `Self` type in where clauses", name).into(),
65 ObjectSafetyViolation::Method(name, MethodViolationCode::Generic, _) => {
66 format!("method `{}` has generic type parameters", name).into()
68 ObjectSafetyViolation::Method(name, MethodViolationCode::UndispatchableReceiver, _) => {
69 format!("method `{}`'s `self` parameter cannot be dispatched on", name).into()
71 ObjectSafetyViolation::AssocConst(name, _) => {
72 format!("the trait cannot contain associated consts like `{}`", name).into()
77 pub fn span(&self) -> Option<Span> {
78 // When `span` comes from a separate crate, it'll be `DUMMY_SP`. Treat it as `None` so
79 // diagnostics use a `note` instead of a `span_label`.
81 ObjectSafetyViolation::AssocConst(_, span)
82 | ObjectSafetyViolation::Method(_, _, span)
83 if span != DUMMY_SP =>
92 /// Reasons a method might not be object-safe.
93 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
94 pub enum MethodViolationCode {
98 /// e.g., `fn foo(&self, x: Self)` or `fn foo(&self) -> Self`
101 /// e.g., `fn foo(&self) where Self: Clone`
102 WhereClauseReferencesSelf,
104 /// e.g., `fn foo<A>()`
107 /// the method's receiver (`self` argument) can't be dispatched on
108 UndispatchableReceiver,
111 impl<'tcx> TyCtxt<'tcx> {
112 /// Returns the object safety violations that affect
113 /// astconv -- currently, `Self` in supertraits. This is needed
114 /// because `object_safety_violations` can't be used during
116 pub fn astconv_object_safety_violations(
119 ) -> Vec<ObjectSafetyViolation> {
120 debug_assert!(self.generics_of(trait_def_id).has_self);
121 let violations = traits::supertrait_def_ids(self, trait_def_id)
122 .filter(|&def_id| self.predicates_reference_self(def_id, true))
123 .map(|_| ObjectSafetyViolation::SupertraitSelf)
127 "astconv_object_safety_violations(trait_def_id={:?}) = {:?}",
128 trait_def_id, violations
134 pub fn object_safety_violations(self, trait_def_id: DefId) -> Vec<ObjectSafetyViolation> {
135 debug_assert!(self.generics_of(trait_def_id).has_self);
136 debug!("object_safety_violations: {:?}", trait_def_id);
138 traits::supertrait_def_ids(self, trait_def_id)
139 .flat_map(|def_id| self.object_safety_violations_for_trait(def_id))
143 /// We say a method is *vtable safe* if it can be invoked on a trait
144 /// object. Note that object-safe traits can have some
145 /// non-vtable-safe methods, so long as they require `Self: Sized` or
146 /// otherwise ensure that they cannot be used when `Self = Trait`.
147 pub fn is_vtable_safe_method(self, trait_def_id: DefId, method: &ty::AssocItem) -> bool {
148 debug_assert!(self.generics_of(trait_def_id).has_self);
149 debug!("is_vtable_safe_method({:?}, {:?})", trait_def_id, method);
150 // Any method that has a `Self: Sized` bound cannot be called.
151 if self.generics_require_sized_self(method.def_id) {
155 match self.virtual_call_violation_for_method(trait_def_id, method) {
156 None | Some(MethodViolationCode::WhereClauseReferencesSelf) => true,
161 fn object_safety_violations_for_trait(self, trait_def_id: DefId) -> Vec<ObjectSafetyViolation> {
162 // Check methods for violations.
163 let mut violations: Vec<_> = self
164 .associated_items(trait_def_id)
165 .filter(|item| item.kind == ty::AssocKind::Method)
167 self.object_safety_violation_for_method(trait_def_id, &item).map(|code| {
168 ObjectSafetyViolation::Method(item.ident.name, code, item.ident.span)
171 .filter(|violation| {
172 if let ObjectSafetyViolation::Method(
174 MethodViolationCode::WhereClauseReferencesSelf,
178 // Using `CRATE_NODE_ID` is wrong, but it's hard to get a more precise id.
179 // It's also hard to get a use site span, so we use the method definition span.
181 lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY,
185 "the trait `{}` cannot be made into an object",
186 self.def_path_str(trait_def_id)
188 &violation.error_msg(),
197 // Check the trait itself.
198 if self.trait_has_sized_self(trait_def_id) {
199 violations.push(ObjectSafetyViolation::SizedSelf);
201 if self.predicates_reference_self(trait_def_id, false) {
202 violations.push(ObjectSafetyViolation::SupertraitSelf);
206 self.associated_items(trait_def_id)
207 .filter(|item| item.kind == ty::AssocKind::Const)
208 .map(|item| ObjectSafetyViolation::AssocConst(item.ident.name, item.ident.span)),
212 "object_safety_violations_for_trait(trait_def_id={:?}) = {:?}",
213 trait_def_id, violations
219 fn predicates_reference_self(self, trait_def_id: DefId, supertraits_only: bool) -> bool {
220 let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(self, trait_def_id));
221 let predicates = if supertraits_only {
222 self.super_predicates_of(trait_def_id)
224 self.predicates_of(trait_def_id)
226 let self_ty = self.types.self_param;
227 let has_self_ty = |t: Ty<'tcx>| t.walk().any(|t| t == self_ty);
231 .map(|(predicate, _)| predicate.subst_supertrait(self, &trait_ref))
234 ty::Predicate::Trait(ref data) => {
235 // In the case of a trait predicate, we can skip the "self" type.
236 data.skip_binder().input_types().skip(1).any(has_self_ty)
238 ty::Predicate::Projection(ref data) => {
239 // And similarly for projections. This should be redundant with
240 // the previous check because any projection should have a
241 // matching `Trait` predicate with the same inputs, but we do
242 // the check to be safe.
244 // Note that we *do* allow projection *outputs* to contain
245 // `self` (i.e., `trait Foo: Bar<Output=Self::Result> { type Result; }`),
246 // we just require the user to specify *both* outputs
247 // in the object type (i.e., `dyn Foo<Output=(), Result=()>`).
249 // This is ALT2 in issue #56288, see that for discussion of the
250 // possible alternatives.
258 ty::Predicate::WellFormed(..)
259 | ty::Predicate::ObjectSafe(..)
260 | ty::Predicate::TypeOutlives(..)
261 | ty::Predicate::RegionOutlives(..)
262 | ty::Predicate::ClosureKind(..)
263 | ty::Predicate::Subtype(..)
264 | ty::Predicate::ConstEvaluatable(..) => false,
269 fn trait_has_sized_self(self, trait_def_id: DefId) -> bool {
270 self.generics_require_sized_self(trait_def_id)
273 fn generics_require_sized_self(self, def_id: DefId) -> bool {
274 let sized_def_id = match self.lang_items().sized_trait() {
275 Some(def_id) => def_id,
277 return false; /* No Sized trait, can't require it! */
281 // Search for a predicate like `Self : Sized` amongst the trait bounds.
282 let predicates = self.predicates_of(def_id);
283 let predicates = predicates.instantiate_identity(self).predicates;
284 elaborate_predicates(self, predicates).any(|predicate| match predicate {
285 ty::Predicate::Trait(ref trait_pred) => {
286 trait_pred.def_id() == sized_def_id
287 && trait_pred.skip_binder().self_ty().is_param(0)
289 ty::Predicate::Projection(..)
290 | ty::Predicate::Subtype(..)
291 | ty::Predicate::RegionOutlives(..)
292 | ty::Predicate::WellFormed(..)
293 | ty::Predicate::ObjectSafe(..)
294 | ty::Predicate::ClosureKind(..)
295 | ty::Predicate::TypeOutlives(..)
296 | ty::Predicate::ConstEvaluatable(..) => false,
300 /// Returns `Some(_)` if this method makes the containing trait not object safe.
301 fn object_safety_violation_for_method(
304 method: &ty::AssocItem,
305 ) -> Option<MethodViolationCode> {
306 debug!("object_safety_violation_for_method({:?}, {:?})", trait_def_id, method);
307 // Any method that has a `Self : Sized` requisite is otherwise
308 // exempt from the regulations.
309 if self.generics_require_sized_self(method.def_id) {
313 self.virtual_call_violation_for_method(trait_def_id, method)
316 /// Returns `Some(_)` if this method cannot be called on a trait
317 /// object; this does not necessarily imply that the enclosing trait
318 /// is not object safe, because the method might have a where clause
320 fn virtual_call_violation_for_method(
323 method: &ty::AssocItem,
324 ) -> Option<MethodViolationCode> {
325 // The method's first parameter must be named `self`
326 if !method.method_has_self_argument {
327 return Some(MethodViolationCode::StaticMethod);
330 let sig = self.fn_sig(method.def_id);
332 for input_ty in &sig.skip_binder().inputs()[1..] {
333 if self.contains_illegal_self_type_reference(trait_def_id, input_ty) {
334 return Some(MethodViolationCode::ReferencesSelf);
337 if self.contains_illegal_self_type_reference(trait_def_id, sig.output().skip_binder()) {
338 return Some(MethodViolationCode::ReferencesSelf);
341 // We can't monomorphize things like `fn foo<A>(...)`.
342 let own_counts = self.generics_of(method.def_id).own_counts();
343 if own_counts.types + own_counts.consts != 0 {
344 return Some(MethodViolationCode::Generic);
348 .predicates_of(method.def_id)
351 // A trait object can't claim to live more than the concrete type,
352 // so outlives predicates will always hold.
354 .filter(|(p, _)| p.to_opt_type_outlives().is_none())
356 // Do a shallow visit so that `contains_illegal_self_type_reference`
357 // may apply it's custom visiting.
358 .visit_tys_shallow(|t| self.contains_illegal_self_type_reference(trait_def_id, t))
360 return Some(MethodViolationCode::WhereClauseReferencesSelf);
364 self.liberate_late_bound_regions(method.def_id, &sig.map_bound(|sig| sig.inputs()[0]));
366 // Until `unsized_locals` is fully implemented, `self: Self` can't be dispatched on.
367 // However, this is already considered object-safe. We allow it as a special case here.
368 // FIXME(mikeyhew) get rid of this `if` statement once `receiver_is_dispatchable` allows
369 // `Receiver: Unsize<Receiver[Self => dyn Trait]>`.
370 if receiver_ty != self.types.self_param {
371 if !self.receiver_is_dispatchable(method, receiver_ty) {
372 return Some(MethodViolationCode::UndispatchableReceiver);
374 // Do sanity check to make sure the receiver actually has the layout of a pointer.
376 use crate::ty::layout::Abi;
378 let param_env = self.param_env(method.def_id);
380 let abi_of_ty = |ty: Ty<'tcx>| -> &Abi {
381 match self.layout_of(param_env.and(ty)) {
382 Ok(layout) => &layout.abi,
384 bug!("error: {}\n while computing layout for type {:?}", err, ty)
390 let unit_receiver_ty =
391 self.receiver_for_self_ty(receiver_ty, self.mk_unit(), method.def_id);
393 match abi_of_ty(unit_receiver_ty) {
394 &Abi::Scalar(..) => (),
396 self.sess.delay_span_bug(
397 self.def_span(method.def_id),
399 "receiver when `Self = ()` should have a Scalar ABI; found {:?}",
406 let trait_object_ty =
407 self.object_ty_for_trait(trait_def_id, self.mk_region(ty::ReStatic));
409 // e.g., `Rc<dyn Trait>`
410 let trait_object_receiver =
411 self.receiver_for_self_ty(receiver_ty, trait_object_ty, method.def_id);
413 match abi_of_ty(trait_object_receiver) {
414 &Abi::ScalarPair(..) => (),
416 self.sess.delay_span_bug(
417 self.def_span(method.def_id),
419 "receiver when `Self = {}` should have a ScalarPair ABI; \
432 /// Performs a type substitution to produce the version of `receiver_ty` when `Self = self_ty`.
433 /// For example, for `receiver_ty = Rc<Self>` and `self_ty = Foo`, returns `Rc<Foo>`.
434 fn receiver_for_self_ty(
436 receiver_ty: Ty<'tcx>,
438 method_def_id: DefId,
440 debug!("receiver_for_self_ty({:?}, {:?}, {:?})", receiver_ty, self_ty, method_def_id);
441 let substs = InternalSubsts::for_item(self, method_def_id, |param, _| {
442 if param.index == 0 { self_ty.into() } else { self.mk_param_from_def(param) }
445 let result = receiver_ty.subst(self, substs);
447 "receiver_for_self_ty({:?}, {:?}, {:?}) = {:?}",
448 receiver_ty, self_ty, method_def_id, result
453 /// Creates the object type for the current trait. For example,
454 /// if the current trait is `Deref`, then this will be
455 /// `dyn Deref<Target = Self::Target> + 'static`.
456 fn object_ty_for_trait(self, trait_def_id: DefId, lifetime: ty::Region<'tcx>) -> Ty<'tcx> {
457 debug!("object_ty_for_trait: trait_def_id={:?}", trait_def_id);
459 let trait_ref = ty::TraitRef::identity(self, trait_def_id);
461 let trait_predicate = ty::ExistentialPredicate::Trait(
462 ty::ExistentialTraitRef::erase_self_ty(self, trait_ref),
465 let mut associated_types = traits::supertraits(self, ty::Binder::dummy(trait_ref))
466 .flat_map(|super_trait_ref| {
467 self.associated_items(super_trait_ref.def_id())
468 .map(move |item| (super_trait_ref, item))
470 .filter(|(_, item)| item.kind == ty::AssocKind::Type)
471 .collect::<Vec<_>>();
473 // existential predicates need to be in a specific order
474 associated_types.sort_by_cached_key(|(_, item)| self.def_path_hash(item.def_id));
476 let projection_predicates = associated_types.into_iter().map(|(super_trait_ref, item)| {
477 // We *can* get bound lifetimes here in cases like
478 // `trait MyTrait: for<'s> OtherTrait<&'s T, Output=bool>`.
480 // binder moved to (*)...
481 let super_trait_ref = super_trait_ref.skip_binder();
482 ty::ExistentialPredicate::Projection(ty::ExistentialProjection {
483 ty: self.mk_projection(item.def_id, super_trait_ref.substs),
484 item_def_id: item.def_id,
485 substs: super_trait_ref.substs,
489 let existential_predicates = self
490 .mk_existential_predicates(iter::once(trait_predicate).chain(projection_predicates));
492 let object_ty = self.mk_dynamic(
493 // (*) ... binder re-introduced here
494 ty::Binder::bind(existential_predicates),
498 debug!("object_ty_for_trait: object_ty=`{}`", object_ty);
503 /// Checks the method's receiver (the `self` argument) can be dispatched on when `Self` is a
504 /// trait object. We require that `DispatchableFromDyn` be implemented for the receiver type
505 /// in the following way:
506 /// - let `Receiver` be the type of the `self` argument, i.e `Self`, `&Self`, `Rc<Self>`,
507 /// - require the following bound:
510 /// Receiver[Self => T]: DispatchFromDyn<Receiver[Self => dyn Trait]>
513 /// where `Foo[X => Y]` means "the same type as `Foo`, but with `X` replaced with `Y`"
514 /// (substitution notation).
516 /// Some examples of receiver types and their required obligation:
517 /// - `&'a mut self` requires `&'a mut Self: DispatchFromDyn<&'a mut dyn Trait>`,
518 /// - `self: Rc<Self>` requires `Rc<Self>: DispatchFromDyn<Rc<dyn Trait>>`,
519 /// - `self: Pin<Box<Self>>` requires `Pin<Box<Self>>: DispatchFromDyn<Pin<Box<dyn Trait>>>`.
521 /// The only case where the receiver is not dispatchable, but is still a valid receiver
522 /// type (just not object-safe), is when there is more than one level of pointer indirection.
523 /// E.g., `self: &&Self`, `self: &Rc<Self>`, `self: Box<Box<Self>>`. In these cases, there
524 /// is no way, or at least no inexpensive way, to coerce the receiver from the version where
525 /// `Self = dyn Trait` to the version where `Self = T`, where `T` is the unknown erased type
526 /// contained by the trait object, because the object that needs to be coerced is behind
529 /// In practice, we cannot use `dyn Trait` explicitly in the obligation because it would result
530 /// in a new check that `Trait` is object safe, creating a cycle (until object_safe_for_dispatch
531 /// is stabilized, see tracking issue https://github.com/rust-lang/rust/issues/43561).
532 /// Instead, we fudge a little by introducing a new type parameter `U` such that
533 /// `Self: Unsize<U>` and `U: Trait + ?Sized`, and use `U` in place of `dyn Trait`.
534 /// Written as a chalk-style query:
536 /// forall (U: Trait + ?Sized) {
537 /// if (Self: Unsize<U>) {
538 /// Receiver: DispatchFromDyn<Receiver[Self => U]>
542 /// for `self: &'a mut Self`, this means `&'a mut Self: DispatchFromDyn<&'a mut U>`
543 /// for `self: Rc<Self>`, this means `Rc<Self>: DispatchFromDyn<Rc<U>>`
544 /// for `self: Pin<Box<Self>>`, this means `Pin<Box<Self>>: DispatchFromDyn<Pin<Box<U>>>`
546 // FIXME(mikeyhew) when unsized receivers are implemented as part of unsized rvalues, add this
547 // fallback query: `Receiver: Unsize<Receiver[Self => U]>` to support receivers like
548 // `self: Wrapper<Self>`.
550 fn receiver_is_dispatchable(self, method: &ty::AssocItem, receiver_ty: Ty<'tcx>) -> bool {
551 debug!("receiver_is_dispatchable: method = {:?}, receiver_ty = {:?}", method, receiver_ty);
554 (self.lang_items().unsize_trait(), self.lang_items().dispatch_from_dyn_trait());
555 let (unsize_did, dispatch_from_dyn_did) = if let (Some(u), Some(cu)) = traits {
558 debug!("receiver_is_dispatchable: Missing Unsize or DispatchFromDyn traits");
562 // the type `U` in the query
563 // use a bogus type parameter to mimick a forall(U) query using u32::MAX for now.
564 // FIXME(mikeyhew) this is a total hack. Once object_safe_for_dispatch is stabilized, we can
565 // replace this with `dyn Trait`
566 let unsized_self_ty: Ty<'tcx> =
567 self.mk_ty_param(::std::u32::MAX, Symbol::intern("RustaceansAreAwesome"));
569 // `Receiver[Self => U]`
570 let unsized_receiver_ty =
571 self.receiver_for_self_ty(receiver_ty, unsized_self_ty, method.def_id);
573 // create a modified param env, with `Self: Unsize<U>` and `U: Trait` added to caller bounds
574 // `U: ?Sized` is already implied here
576 let mut param_env = self.param_env(method.def_id);
579 let unsize_predicate = ty::TraitRef {
581 substs: self.mk_substs_trait(self.types.self_param, &[unsized_self_ty.into()]),
585 // U: Trait<Arg1, ..., ArgN>
586 let trait_predicate = {
588 InternalSubsts::for_item(self, method.container.assert_trait(), |param, _| {
589 if param.index == 0 {
590 unsized_self_ty.into()
592 self.mk_param_from_def(param)
596 ty::TraitRef { def_id: unsize_did, substs }.to_predicate()
599 let caller_bounds: Vec<Predicate<'tcx>> = param_env
603 .chain(iter::once(unsize_predicate))
604 .chain(iter::once(trait_predicate))
607 param_env.caller_bounds = self.intern_predicates(&caller_bounds);
612 // Receiver: DispatchFromDyn<Receiver[Self => U]>
614 let predicate = ty::TraitRef {
615 def_id: dispatch_from_dyn_did,
616 substs: self.mk_substs_trait(receiver_ty, &[unsized_receiver_ty.into()]),
620 Obligation::new(ObligationCause::dummy(), param_env, predicate)
623 self.infer_ctxt().enter(|ref infcx| {
624 // the receiver is dispatchable iff the obligation holds
625 infcx.predicate_must_hold_modulo_regions(&obligation)
629 fn contains_illegal_self_type_reference(self, trait_def_id: DefId, ty: Ty<'tcx>) -> bool {
630 // This is somewhat subtle. In general, we want to forbid
631 // references to `Self` in the argument and return types,
632 // since the value of `Self` is erased. However, there is one
633 // exception: it is ok to reference `Self` in order to access
634 // an associated type of the current trait, since we retain
635 // the value of those associated types in the object type
639 // trait SuperTrait {
643 // trait Trait : SuperTrait {
645 // fn foo(&self, x: Self) // bad
646 // fn foo(&self) -> Self // bad
647 // fn foo(&self) -> Option<Self> // bad
648 // fn foo(&self) -> Self::Y // OK, desugars to next example
649 // fn foo(&self) -> <Self as Trait>::Y // OK
650 // fn foo(&self) -> Self::X // OK, desugars to next example
651 // fn foo(&self) -> <Self as SuperTrait>::X // OK
655 // However, it is not as simple as allowing `Self` in a projected
656 // type, because there are illegal ways to use `Self` as well:
659 // trait Trait : SuperTrait {
661 // fn foo(&self) -> <Self as SomeOtherTrait>::X;
665 // Here we will not have the type of `X` recorded in the
666 // object type, and we cannot resolve `Self as SomeOtherTrait`
667 // without knowing what `Self` is.
669 let mut supertraits: Option<Vec<ty::PolyTraitRef<'tcx>>> = None;
670 let mut error = false;
671 let self_ty = self.types.self_param;
679 false // no contained types to walk
682 ty::Projection(ref data) => {
683 // This is a projected type `<Foo as SomeTrait>::X`.
685 // Compute supertraits of current trait lazily.
686 if supertraits.is_none() {
688 ty::Binder::bind(ty::TraitRef::identity(self, trait_def_id));
689 supertraits = Some(traits::supertraits(self, trait_ref).collect());
692 // Determine whether the trait reference `Foo as
693 // SomeTrait` is in fact a supertrait of the
694 // current trait. In that case, this type is
695 // legal, because the type `X` will be specified
696 // in the object type. Note that we can just use
697 // direct equality here because all of these types
698 // are part of the formal parameter listing, and
699 // hence there should be no inference variables.
700 let projection_trait_ref = ty::Binder::bind(data.trait_ref(self));
701 let is_supertrait_of_current_trait =
702 supertraits.as_ref().unwrap().contains(&projection_trait_ref);
704 if is_supertrait_of_current_trait {
705 false // do not walk contained types, do not report error, do collect $200
707 true // DO walk contained types, POSSIBLY reporting an error
711 _ => true, // walk contained types, if any
719 pub(super) fn is_object_safe_provider(tcx: TyCtxt<'_>, trait_def_id: DefId) -> bool {
720 tcx.object_safety_violations(trait_def_id).is_empty()