1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! "Object safety" refers to the ability for a trait to be converted
12 //! to an object. In general, traits may only be converted to an
13 //! object if all of their methods meet certain criteria. In particular,
16 //! - have a suitable receiver from which we can extract a vtable;
17 //! - not reference the erased type `Self` except for in this receiver;
18 //! - not have generic type parameters
20 use super::elaborate_predicates;
22 use hir::def_id::DefId;
25 use ty::{self, Ty, TyCtxt, TypeFoldable};
26 use ty::util::ExplicitSelf;
31 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
32 pub enum ObjectSafetyViolation {
33 /// Self : Sized declared on the trait
36 /// Supertrait reference references `Self` an in illegal location
37 /// (e.g. `trait Foo : Bar<Self>`)
40 /// Method has something illegal
41 Method(ast::Name, MethodViolationCode),
44 AssociatedConst(ast::Name),
47 impl ObjectSafetyViolation {
48 pub fn error_msg(&self) -> Cow<'static, str> {
50 ObjectSafetyViolation::SizedSelf =>
51 "the trait cannot require that `Self : Sized`".into(),
52 ObjectSafetyViolation::SupertraitSelf =>
53 "the trait cannot use `Self` as a type parameter \
54 in the supertraits or where-clauses".into(),
55 ObjectSafetyViolation::Method(name, MethodViolationCode::StaticMethod) =>
56 format!("method `{}` has no receiver", name).into(),
57 ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelf) =>
58 format!("method `{}` references the `Self` type \
59 in its arguments or return type", name).into(),
60 ObjectSafetyViolation::Method(name,
61 MethodViolationCode::WhereClauseReferencesSelf(_)) =>
62 format!("method `{}` references the `Self` type in where clauses", name).into(),
63 ObjectSafetyViolation::Method(name, MethodViolationCode::Generic) =>
64 format!("method `{}` has generic type parameters", name).into(),
65 ObjectSafetyViolation::Method(name, MethodViolationCode::NonStandardSelfType) =>
66 format!("method `{}` has a non-standard `self` type", name).into(),
67 ObjectSafetyViolation::AssociatedConst(name) =>
68 format!("the trait cannot contain associated consts like `{}`", name).into(),
73 /// Reasons a method might not be object-safe.
74 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
75 pub enum MethodViolationCode {
79 /// e.g., `fn foo(&self, x: Self)` or `fn foo(&self) -> Self`
82 /// e.g. `fn foo(&self) where Self: Clone`
83 WhereClauseReferencesSelf(Span),
85 /// e.g., `fn foo<A>()`
88 /// arbitrary `self` type, e.g. `self: Rc<Self>`
92 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
94 /// Returns the object safety violations that affect
95 /// astconv - currently, Self in supertraits. This is needed
96 /// because `object_safety_violations` can't be used during
98 pub fn astconv_object_safety_violations(self, trait_def_id: DefId)
99 -> Vec<ObjectSafetyViolation>
101 let mut violations = vec![];
103 for def_id in traits::supertrait_def_ids(self, trait_def_id) {
104 if self.predicates_reference_self(def_id, true) {
105 violations.push(ObjectSafetyViolation::SupertraitSelf);
109 debug!("astconv_object_safety_violations(trait_def_id={:?}) = {:?}",
116 pub fn object_safety_violations(self, trait_def_id: DefId)
117 -> Vec<ObjectSafetyViolation>
119 traits::supertrait_def_ids(self, trait_def_id)
120 .flat_map(|def_id| self.object_safety_violations_for_trait(def_id))
124 fn object_safety_violations_for_trait(self, trait_def_id: DefId)
125 -> Vec<ObjectSafetyViolation>
127 // Check methods for violations.
128 let mut violations: Vec<_> = self.associated_items(trait_def_id)
129 .filter(|item| item.kind == ty::AssociatedKind::Method)
131 self.object_safety_violation_for_method(trait_def_id, &item)
132 .map(|code| ObjectSafetyViolation::Method(item.ident.name, code))
133 }).filter(|violation| {
134 if let ObjectSafetyViolation::Method(_,
135 MethodViolationCode::WhereClauseReferencesSelf(span)) = violation {
136 // Using`CRATE_NODE_ID` is wrong, but it's hard to get a more precise id.
137 // It's also hard to get a use site span, so we use the method definition span.
139 lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY,
142 &format!("the trait `{}` cannot be made into an object",
143 self.item_path_str(trait_def_id)),
144 &violation.error_msg());
151 // Check the trait itself.
152 if self.trait_has_sized_self(trait_def_id) {
153 violations.push(ObjectSafetyViolation::SizedSelf);
155 if self.predicates_reference_self(trait_def_id, false) {
156 violations.push(ObjectSafetyViolation::SupertraitSelf);
159 violations.extend(self.associated_items(trait_def_id)
160 .filter(|item| item.kind == ty::AssociatedKind::Const)
161 .map(|item| ObjectSafetyViolation::AssociatedConst(item.ident.name)));
163 debug!("object_safety_violations_for_trait(trait_def_id={:?}) = {:?}",
170 fn predicates_reference_self(
173 supertraits_only: bool) -> bool
175 let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(self, trait_def_id));
176 let predicates = if supertraits_only {
177 self.super_predicates_of(trait_def_id)
179 self.predicates_of(trait_def_id)
184 .map(|predicate| predicate.subst_supertrait(self, &trait_ref))
187 ty::Predicate::Trait(ref data) => {
188 // In the case of a trait predicate, we can skip the "self" type.
189 data.skip_binder().input_types().skip(1).any(|t| t.has_self_ty())
191 ty::Predicate::Projection(..) |
192 ty::Predicate::WellFormed(..) |
193 ty::Predicate::ObjectSafe(..) |
194 ty::Predicate::TypeOutlives(..) |
195 ty::Predicate::RegionOutlives(..) |
196 ty::Predicate::ClosureKind(..) |
197 ty::Predicate::Subtype(..) |
198 ty::Predicate::ConstEvaluatable(..) => {
205 fn trait_has_sized_self(self, trait_def_id: DefId) -> bool {
206 self.generics_require_sized_self(trait_def_id)
209 fn generics_require_sized_self(self, def_id: DefId) -> bool {
210 let sized_def_id = match self.lang_items().sized_trait() {
211 Some(def_id) => def_id,
212 None => { return false; /* No Sized trait, can't require it! */ }
215 // Search for a predicate like `Self : Sized` amongst the trait bounds.
216 let predicates = self.predicates_of(def_id);
217 let predicates = predicates.instantiate_identity(self).predicates;
218 elaborate_predicates(self, predicates)
221 ty::Predicate::Trait(ref trait_pred) if trait_pred.def_id() == sized_def_id => {
222 trait_pred.skip_binder().self_ty().is_self()
224 ty::Predicate::Projection(..) |
225 ty::Predicate::Trait(..) |
226 ty::Predicate::Subtype(..) |
227 ty::Predicate::RegionOutlives(..) |
228 ty::Predicate::WellFormed(..) |
229 ty::Predicate::ObjectSafe(..) |
230 ty::Predicate::ClosureKind(..) |
231 ty::Predicate::TypeOutlives(..) |
232 ty::Predicate::ConstEvaluatable(..) => {
239 /// Returns `Some(_)` if this method makes the containing trait not object safe.
240 fn object_safety_violation_for_method(self,
242 method: &ty::AssociatedItem)
243 -> Option<MethodViolationCode>
245 // Any method that has a `Self : Sized` requisite is otherwise
246 // exempt from the regulations.
247 if self.generics_require_sized_self(method.def_id) {
251 self.virtual_call_violation_for_method(trait_def_id, method)
254 /// We say a method is *vtable safe* if it can be invoked on a trait
255 /// object. Note that object-safe traits can have some
256 /// non-vtable-safe methods, so long as they require `Self:Sized` or
257 /// otherwise ensure that they cannot be used when `Self=Trait`.
258 pub fn is_vtable_safe_method(self,
260 method: &ty::AssociatedItem)
263 // Any method that has a `Self : Sized` requisite can't be called.
264 if self.generics_require_sized_self(method.def_id) {
268 match self.virtual_call_violation_for_method(trait_def_id, method) {
269 None | Some(MethodViolationCode::WhereClauseReferencesSelf(_)) => true,
274 /// Returns `Some(_)` if this method cannot be called on a trait
275 /// object; this does not necessarily imply that the enclosing trait
276 /// is not object safe, because the method might have a where clause
278 fn virtual_call_violation_for_method(self,
280 method: &ty::AssociatedItem)
281 -> Option<MethodViolationCode>
283 // The method's first parameter must be something that derefs (or
284 // autorefs) to `&self`. For now, we only accept `self`, `&self`
286 if !method.method_has_self_argument {
287 return Some(MethodViolationCode::StaticMethod);
290 let sig = self.fn_sig(method.def_id);
292 let self_ty = self.mk_self_type();
293 let self_arg_ty = sig.skip_binder().inputs()[0];
294 if let ExplicitSelf::Other = ExplicitSelf::determine(self_arg_ty, |ty| ty == self_ty) {
295 return Some(MethodViolationCode::NonStandardSelfType);
298 // The `Self` type is erased, so it should not appear in list of
299 // arguments or return type apart from the receiver.
300 for input_ty in &sig.skip_binder().inputs()[1..] {
301 if self.contains_illegal_self_type_reference(trait_def_id, input_ty) {
302 return Some(MethodViolationCode::ReferencesSelf);
305 if self.contains_illegal_self_type_reference(trait_def_id, sig.output().skip_binder()) {
306 return Some(MethodViolationCode::ReferencesSelf);
309 // We can't monomorphize things like `fn foo<A>(...)`.
310 if self.generics_of(method.def_id).own_counts().types != 0 {
311 return Some(MethodViolationCode::Generic);
314 if self.predicates_of(method.def_id).predicates.into_iter()
315 // A trait object can't claim to live more than the concrete type,
316 // so outlives predicates will always hold.
317 .filter(|p| p.to_opt_type_outlives().is_none())
319 // Do a shallow visit so that `contains_illegal_self_type_reference`
320 // may apply it's custom visiting.
321 .visit_tys_shallow(|t| self.contains_illegal_self_type_reference(trait_def_id, t)) {
322 let span = self.def_span(method.def_id);
323 return Some(MethodViolationCode::WhereClauseReferencesSelf(span));
329 fn contains_illegal_self_type_reference(self,
334 // This is somewhat subtle. In general, we want to forbid
335 // references to `Self` in the argument and return types,
336 // since the value of `Self` is erased. However, there is one
337 // exception: it is ok to reference `Self` in order to access
338 // an associated type of the current trait, since we retain
339 // the value of those associated types in the object type
343 // trait SuperTrait {
347 // trait Trait : SuperTrait {
349 // fn foo(&self, x: Self) // bad
350 // fn foo(&self) -> Self // bad
351 // fn foo(&self) -> Option<Self> // bad
352 // fn foo(&self) -> Self::Y // OK, desugars to next example
353 // fn foo(&self) -> <Self as Trait>::Y // OK
354 // fn foo(&self) -> Self::X // OK, desugars to next example
355 // fn foo(&self) -> <Self as SuperTrait>::X // OK
359 // However, it is not as simple as allowing `Self` in a projected
360 // type, because there are illegal ways to use `Self` as well:
363 // trait Trait : SuperTrait {
365 // fn foo(&self) -> <Self as SomeOtherTrait>::X;
369 // Here we will not have the type of `X` recorded in the
370 // object type, and we cannot resolve `Self as SomeOtherTrait`
371 // without knowing what `Self` is.
373 let mut supertraits: Option<Vec<ty::PolyTraitRef<'tcx>>> = None;
374 let mut error = false;
377 ty::TyParam(ref param_ty) => {
378 if param_ty.is_self() {
382 false // no contained types to walk
385 ty::TyProjection(ref data) => {
386 // This is a projected type `<Foo as SomeTrait>::X`.
388 // Compute supertraits of current trait lazily.
389 if supertraits.is_none() {
390 let trait_ref = ty::Binder::bind(
391 ty::TraitRef::identity(self, trait_def_id),
393 supertraits = Some(traits::supertraits(self, trait_ref).collect());
396 // Determine whether the trait reference `Foo as
397 // SomeTrait` is in fact a supertrait of the
398 // current trait. In that case, this type is
399 // legal, because the type `X` will be specified
400 // in the object type. Note that we can just use
401 // direct equality here because all of these types
402 // are part of the formal parameter listing, and
403 // hence there should be no inference variables.
404 let projection_trait_ref = ty::Binder::bind(data.trait_ref(self));
405 let is_supertrait_of_current_trait =
406 supertraits.as_ref().unwrap().contains(&projection_trait_ref);
408 if is_supertrait_of_current_trait {
409 false // do not walk contained types, do not report error, do collect $200
411 true // DO walk contained types, POSSIBLY reporting an error
415 _ => true, // walk contained types, if any
423 pub(super) fn is_object_safe_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
424 trait_def_id: DefId) -> bool {
425 tcx.object_safety_violations(trait_def_id).is_empty()