2014-12-15 21:11:09 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! "Object safety" refers to the ability for a trait to be converted
|
|
|
|
//! to an object. In general, traits may only be converted to an
|
|
|
|
//! object if all of their methods meet certain criteria. In particular,
|
|
|
|
//! they must:
|
|
|
|
//!
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
//! - have a suitable receiver from which we can extract a vtable and coerce to a "thin" version
|
|
|
|
//! that doesn't contain the vtable;
|
2014-12-15 21:11:09 -05:00
|
|
|
//! - not reference the erased type `Self` except for in this receiver;
|
|
|
|
//! - not have generic type parameters
|
|
|
|
|
|
|
|
use super::elaborate_predicates;
|
|
|
|
|
2016-03-29 12:54:26 +03:00
|
|
|
use hir::def_id::DefId;
|
2018-05-30 09:06:08 -03:00
|
|
|
use lint;
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
use traits::{self, Obligation, ObligationCause};
|
|
|
|
use ty::{self, Ty, TyCtxt, TypeFoldable, Predicate, ToPredicate};
|
|
|
|
use ty::subst::{Subst, Substs};
|
2017-02-19 13:00:25 -05:00
|
|
|
use std::borrow::Cow;
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
use std::iter::{self};
|
|
|
|
use syntax::ast::{self, Name};
|
2018-05-30 09:06:08 -03:00
|
|
|
use syntax_pos::Span;
|
2014-12-15 21:11:09 -05:00
|
|
|
|
2018-05-30 09:06:08 -03:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
2016-11-10 02:06:34 +02:00
|
|
|
pub enum ObjectSafetyViolation {
|
2014-12-15 21:11:09 -05:00
|
|
|
/// Self : Sized declared on the trait
|
|
|
|
SizedSelf,
|
|
|
|
|
2015-02-17 10:57:15 -05:00
|
|
|
/// Supertrait reference references `Self` an in illegal location
|
|
|
|
/// (e.g. `trait Foo : Bar<Self>`)
|
|
|
|
SupertraitSelf,
|
|
|
|
|
2015-01-06 20:53:18 -05:00
|
|
|
/// Method has something illegal
|
2016-11-10 02:06:34 +02:00
|
|
|
Method(ast::Name, MethodViolationCode),
|
2017-04-23 22:00:09 -07:00
|
|
|
|
|
|
|
/// Associated const
|
|
|
|
AssociatedConst(ast::Name),
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
|
|
|
|
2017-02-19 13:00:25 -05:00
|
|
|
impl ObjectSafetyViolation {
|
|
|
|
pub fn error_msg(&self) -> Cow<'static, str> {
|
|
|
|
match *self {
|
|
|
|
ObjectSafetyViolation::SizedSelf =>
|
|
|
|
"the trait cannot require that `Self : Sized`".into(),
|
|
|
|
ObjectSafetyViolation::SupertraitSelf =>
|
|
|
|
"the trait cannot use `Self` as a type parameter \
|
2017-03-22 21:16:37 -05:00
|
|
|
in the supertraits or where-clauses".into(),
|
2017-02-19 13:00:25 -05:00
|
|
|
ObjectSafetyViolation::Method(name, MethodViolationCode::StaticMethod) =>
|
|
|
|
format!("method `{}` has no receiver", name).into(),
|
|
|
|
ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelf) =>
|
|
|
|
format!("method `{}` references the `Self` type \
|
|
|
|
in its arguments or return type", name).into(),
|
2018-05-30 09:06:08 -03:00
|
|
|
ObjectSafetyViolation::Method(name,
|
|
|
|
MethodViolationCode::WhereClauseReferencesSelf(_)) =>
|
|
|
|
format!("method `{}` references the `Self` type in where clauses", name).into(),
|
2017-02-19 13:00:25 -05:00
|
|
|
ObjectSafetyViolation::Method(name, MethodViolationCode::Generic) =>
|
|
|
|
format!("method `{}` has generic type parameters", name).into(),
|
2018-10-08 20:37:58 -04:00
|
|
|
ObjectSafetyViolation::Method(name, MethodViolationCode::UndispatchableReceiver) =>
|
|
|
|
format!("method `{}`'s receiver cannot be dispatched on", name).into(),
|
2017-04-23 22:00:09 -07:00
|
|
|
ObjectSafetyViolation::AssociatedConst(name) =>
|
2017-04-24 01:20:36 -07:00
|
|
|
format!("the trait cannot contain associated consts like `{}`", name).into(),
|
2017-02-19 13:00:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 21:11:09 -05:00
|
|
|
/// Reasons a method might not be object-safe.
|
2015-08-07 09:30:19 -04:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
2014-12-15 21:11:09 -05:00
|
|
|
pub enum MethodViolationCode {
|
2015-01-02 05:34:49 -05:00
|
|
|
/// e.g., `fn foo()`
|
2014-12-15 21:11:09 -05:00
|
|
|
StaticMethod,
|
|
|
|
|
2015-01-02 05:34:49 -05:00
|
|
|
/// e.g., `fn foo(&self, x: Self)` or `fn foo(&self) -> Self`
|
2014-12-15 21:11:09 -05:00
|
|
|
ReferencesSelf,
|
|
|
|
|
2018-05-30 09:06:08 -03:00
|
|
|
/// e.g. `fn foo(&self) where Self: Clone`
|
|
|
|
WhereClauseReferencesSelf(Span),
|
|
|
|
|
2015-01-02 05:34:49 -05:00
|
|
|
/// e.g., `fn foo<A>()`
|
2014-12-15 21:11:09 -05:00
|
|
|
Generic,
|
2017-11-08 05:27:39 -05:00
|
|
|
|
2018-10-08 20:37:58 -04:00
|
|
|
/// the method's receiver (`self` argument) can't be dispatched on
|
|
|
|
UndispatchableReceiver,
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
|
|
|
|
2018-07-02 18:30:59 -04:00
|
|
|
impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
|
2015-09-24 18:27:29 +03:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
/// Returns the object safety violations that affect
|
|
|
|
/// astconv - currently, Self in supertraits. This is needed
|
|
|
|
/// because `object_safety_violations` can't be used during
|
|
|
|
/// type collection.
|
|
|
|
pub fn astconv_object_safety_violations(self, trait_def_id: DefId)
|
2016-11-10 02:06:34 +02:00
|
|
|
-> Vec<ObjectSafetyViolation>
|
2016-05-11 08:48:12 +03:00
|
|
|
{
|
2018-07-25 13:20:53 +02:00
|
|
|
let violations = traits::supertrait_def_ids(self, trait_def_id)
|
|
|
|
.filter(|&def_id| self.predicates_reference_self(def_id, true))
|
|
|
|
.map(|_| ObjectSafetyViolation::SupertraitSelf)
|
|
|
|
.collect();
|
2015-09-24 18:27:29 +03:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
debug!("astconv_object_safety_violations(trait_def_id={:?}) = {:?}",
|
|
|
|
trait_def_id,
|
|
|
|
violations);
|
2015-09-24 18:27:29 +03:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
violations
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn object_safety_violations(self, trait_def_id: DefId)
|
2016-11-10 02:06:34 +02:00
|
|
|
-> Vec<ObjectSafetyViolation>
|
2016-05-11 08:48:12 +03:00
|
|
|
{
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
debug!("object_safety_violations: {:?}", trait_def_id);
|
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
traits::supertrait_def_ids(self, trait_def_id)
|
|
|
|
.flat_map(|def_id| self.object_safety_violations_for_trait(def_id))
|
|
|
|
.collect()
|
|
|
|
}
|
2014-12-15 21:11:09 -05:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
fn object_safety_violations_for_trait(self, trait_def_id: DefId)
|
2016-11-10 02:06:34 +02:00
|
|
|
-> Vec<ObjectSafetyViolation>
|
2016-05-11 08:48:12 +03:00
|
|
|
{
|
|
|
|
// Check methods for violations.
|
2016-11-10 02:06:34 +02:00
|
|
|
let mut violations: Vec<_> = self.associated_items(trait_def_id)
|
|
|
|
.filter(|item| item.kind == ty::AssociatedKind::Method)
|
2018-09-12 16:57:19 +02:00
|
|
|
.filter_map(|item|
|
2016-11-10 02:06:34 +02:00
|
|
|
self.object_safety_violation_for_method(trait_def_id, &item)
|
2018-06-10 22:24:24 +03:00
|
|
|
.map(|code| ObjectSafetyViolation::Method(item.ident.name, code))
|
2018-09-12 16:57:19 +02:00
|
|
|
).filter(|violation| {
|
2018-05-30 09:06:08 -03:00
|
|
|
if let ObjectSafetyViolation::Method(_,
|
2018-09-12 16:57:19 +02:00
|
|
|
MethodViolationCode::WhereClauseReferencesSelf(span)) = violation
|
|
|
|
{
|
|
|
|
// Using `CRATE_NODE_ID` is wrong, but it's hard to get a more precise id.
|
2018-05-30 09:06:08 -03:00
|
|
|
// It's also hard to get a use site span, so we use the method definition span.
|
|
|
|
self.lint_node_note(
|
|
|
|
lint::builtin::WHERE_CLAUSES_OBJECT_SAFETY,
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
*span,
|
|
|
|
&format!("the trait `{}` cannot be made into an object",
|
2018-09-12 16:57:19 +02:00
|
|
|
self.item_path_str(trait_def_id)),
|
2018-05-30 09:06:08 -03:00
|
|
|
&violation.error_msg());
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
2016-11-10 02:06:34 +02:00
|
|
|
}).collect();
|
2014-12-15 21:11:09 -05:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
// Check the trait itself.
|
|
|
|
if self.trait_has_sized_self(trait_def_id) {
|
|
|
|
violations.push(ObjectSafetyViolation::SizedSelf);
|
|
|
|
}
|
2016-12-31 02:41:19 +02:00
|
|
|
if self.predicates_reference_self(trait_def_id, false) {
|
2016-05-11 08:48:12 +03:00
|
|
|
violations.push(ObjectSafetyViolation::SupertraitSelf);
|
|
|
|
}
|
2014-12-15 21:11:09 -05:00
|
|
|
|
2017-04-23 22:00:09 -07:00
|
|
|
violations.extend(self.associated_items(trait_def_id)
|
|
|
|
.filter(|item| item.kind == ty::AssociatedKind::Const)
|
2018-06-10 22:24:24 +03:00
|
|
|
.map(|item| ObjectSafetyViolation::AssociatedConst(item.ident.name)));
|
2017-04-23 22:00:09 -07:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
debug!("object_safety_violations_for_trait(trait_def_id={:?}) = {:?}",
|
|
|
|
trait_def_id,
|
|
|
|
violations);
|
2014-12-15 21:11:09 -05:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
violations
|
|
|
|
}
|
2014-12-15 21:11:09 -05:00
|
|
|
|
2016-12-31 02:41:19 +02:00
|
|
|
fn predicates_reference_self(
|
|
|
|
self,
|
|
|
|
trait_def_id: DefId,
|
|
|
|
supertraits_only: bool) -> bool
|
|
|
|
{
|
2018-06-29 06:59:00 -04:00
|
|
|
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(self, trait_def_id));
|
2016-12-31 02:41:19 +02:00
|
|
|
let predicates = if supertraits_only {
|
2017-04-24 15:20:46 +03:00
|
|
|
self.super_predicates_of(trait_def_id)
|
2016-12-31 02:41:19 +02:00
|
|
|
} else {
|
2017-04-24 15:20:46 +03:00
|
|
|
self.predicates_of(trait_def_id)
|
2016-12-31 02:41:19 +02:00
|
|
|
};
|
2016-05-11 08:48:12 +03:00
|
|
|
predicates
|
|
|
|
.predicates
|
|
|
|
.into_iter()
|
2018-09-16 20:15:49 +03:00
|
|
|
.map(|(predicate, _)| predicate.subst_supertrait(self, &trait_ref))
|
2016-05-11 08:48:12 +03:00
|
|
|
.any(|predicate| {
|
|
|
|
match predicate {
|
|
|
|
ty::Predicate::Trait(ref data) => {
|
|
|
|
// In the case of a trait predicate, we can skip the "self" type.
|
2016-08-18 08:32:50 +03:00
|
|
|
data.skip_binder().input_types().skip(1).any(|t| t.has_self_ty())
|
2016-05-11 08:48:12 +03:00
|
|
|
}
|
|
|
|
ty::Predicate::Projection(..) |
|
|
|
|
ty::Predicate::WellFormed(..) |
|
|
|
|
ty::Predicate::ObjectSafe(..) |
|
|
|
|
ty::Predicate::TypeOutlives(..) |
|
|
|
|
ty::Predicate::RegionOutlives(..) |
|
|
|
|
ty::Predicate::ClosureKind(..) |
|
2017-03-09 21:47:09 -05:00
|
|
|
ty::Predicate::Subtype(..) |
|
2017-08-07 08:08:53 +03:00
|
|
|
ty::Predicate::ConstEvaluatable(..) => {
|
2016-05-11 08:48:12 +03:00
|
|
|
false
|
|
|
|
}
|
2015-02-17 10:57:15 -05:00
|
|
|
}
|
2016-05-11 08:48:12 +03:00
|
|
|
})
|
|
|
|
}
|
2015-02-17 10:57:15 -05:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
fn trait_has_sized_self(self, trait_def_id: DefId) -> bool {
|
2016-08-08 23:39:49 +03:00
|
|
|
self.generics_require_sized_self(trait_def_id)
|
2016-05-11 08:48:12 +03:00
|
|
|
}
|
2015-02-09 08:54:34 -05:00
|
|
|
|
2016-08-08 23:39:49 +03:00
|
|
|
fn generics_require_sized_self(self, def_id: DefId) -> bool {
|
2017-08-31 08:57:41 -07:00
|
|
|
let sized_def_id = match self.lang_items().sized_trait() {
|
2016-05-11 08:48:12 +03:00
|
|
|
Some(def_id) => def_id,
|
|
|
|
None => { return false; /* No Sized trait, can't require it! */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Search for a predicate like `Self : Sized` amongst the trait bounds.
|
2017-04-24 15:20:46 +03:00
|
|
|
let predicates = self.predicates_of(def_id);
|
2017-05-11 15:05:00 +03:00
|
|
|
let predicates = predicates.instantiate_identity(self).predicates;
|
2016-05-11 08:48:12 +03:00
|
|
|
elaborate_predicates(self, predicates)
|
2018-09-12 16:57:19 +02:00
|
|
|
.any(|predicate| match predicate {
|
|
|
|
ty::Predicate::Trait(ref trait_pred) if trait_pred.def_id() == sized_def_id => {
|
|
|
|
trait_pred.skip_binder().self_ty().is_self()
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
2018-09-12 16:57:19 +02:00
|
|
|
ty::Predicate::Projection(..) |
|
|
|
|
ty::Predicate::Trait(..) |
|
|
|
|
ty::Predicate::Subtype(..) |
|
|
|
|
ty::Predicate::RegionOutlives(..) |
|
|
|
|
ty::Predicate::WellFormed(..) |
|
|
|
|
ty::Predicate::ObjectSafe(..) |
|
|
|
|
ty::Predicate::ClosureKind(..) |
|
|
|
|
ty::Predicate::TypeOutlives(..) |
|
|
|
|
ty::Predicate::ConstEvaluatable(..) => {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2016-05-11 08:48:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `Some(_)` if this method makes the containing trait not object safe.
|
|
|
|
fn object_safety_violation_for_method(self,
|
|
|
|
trait_def_id: DefId,
|
2016-11-10 02:06:34 +02:00
|
|
|
method: &ty::AssociatedItem)
|
2016-05-11 08:48:12 +03:00
|
|
|
-> Option<MethodViolationCode>
|
|
|
|
{
|
|
|
|
// Any method that has a `Self : Sized` requisite is otherwise
|
|
|
|
// exempt from the regulations.
|
2016-08-08 23:39:49 +03:00
|
|
|
if self.generics_require_sized_self(method.def_id) {
|
2016-05-11 08:48:12 +03:00
|
|
|
return None;
|
|
|
|
}
|
2014-12-15 21:11:09 -05:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
self.virtual_call_violation_for_method(trait_def_id, method)
|
2015-02-09 08:54:34 -05:00
|
|
|
}
|
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
/// We say a method is *vtable safe* if it can be invoked on a trait
|
|
|
|
/// object. Note that object-safe traits can have some
|
|
|
|
/// non-vtable-safe methods, so long as they require `Self:Sized` or
|
|
|
|
/// otherwise ensure that they cannot be used when `Self=Trait`.
|
|
|
|
pub fn is_vtable_safe_method(self,
|
|
|
|
trait_def_id: DefId,
|
2016-11-10 02:06:34 +02:00
|
|
|
method: &ty::AssociatedItem)
|
2016-05-11 08:48:12 +03:00
|
|
|
-> bool
|
|
|
|
{
|
2016-06-23 03:30:01 +03:00
|
|
|
// Any method that has a `Self : Sized` requisite can't be called.
|
2016-08-08 23:39:49 +03:00
|
|
|
if self.generics_require_sized_self(method.def_id) {
|
2016-06-23 03:30:01 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-30 09:06:08 -03:00
|
|
|
match self.virtual_call_violation_for_method(trait_def_id, method) {
|
|
|
|
None | Some(MethodViolationCode::WhereClauseReferencesSelf(_)) => true,
|
|
|
|
Some(_) => false,
|
|
|
|
}
|
2016-05-11 08:48:12 +03:00
|
|
|
}
|
2015-03-18 15:26:38 -04:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
/// Returns `Some(_)` if this method cannot be called on a trait
|
|
|
|
/// object; this does not necessarily imply that the enclosing trait
|
|
|
|
/// is not object safe, because the method might have a where clause
|
|
|
|
/// `Self:Sized`.
|
|
|
|
fn virtual_call_violation_for_method(self,
|
|
|
|
trait_def_id: DefId,
|
2016-11-10 02:06:34 +02:00
|
|
|
method: &ty::AssociatedItem)
|
2016-05-11 08:48:12 +03:00
|
|
|
-> Option<MethodViolationCode>
|
|
|
|
{
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
// The method's first parameter must be named `self`
|
2016-11-10 02:06:34 +02:00
|
|
|
if !method.method_has_self_argument {
|
|
|
|
return Some(MethodViolationCode::StaticMethod);
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
|
|
|
|
2017-11-08 05:27:39 -05:00
|
|
|
let sig = self.fn_sig(method.def_id);
|
|
|
|
|
2016-11-28 19:35:38 -07:00
|
|
|
for input_ty in &sig.skip_binder().inputs()[1..] {
|
2016-05-11 08:48:12 +03:00
|
|
|
if self.contains_illegal_self_type_reference(trait_def_id, input_ty) {
|
|
|
|
return Some(MethodViolationCode::ReferencesSelf);
|
|
|
|
}
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
2016-11-28 19:35:38 -07:00
|
|
|
if self.contains_illegal_self_type_reference(trait_def_id, sig.output().skip_binder()) {
|
2016-07-31 22:33:41 +08:00
|
|
|
return Some(MethodViolationCode::ReferencesSelf);
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
2016-05-11 08:48:12 +03:00
|
|
|
|
|
|
|
// We can't monomorphize things like `fn foo<A>(...)`.
|
2018-05-11 00:30:34 +01:00
|
|
|
if self.generics_of(method.def_id).own_counts().types != 0 {
|
2016-05-11 08:48:12 +03:00
|
|
|
return Some(MethodViolationCode::Generic);
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
|
|
|
|
2018-05-22 12:09:35 -03:00
|
|
|
if self.predicates_of(method.def_id).predicates.into_iter()
|
|
|
|
// A trait object can't claim to live more than the concrete type,
|
|
|
|
// so outlives predicates will always hold.
|
2018-09-16 20:15:49 +03:00
|
|
|
.filter(|(p, _)| p.to_opt_type_outlives().is_none())
|
2018-05-22 12:09:35 -03:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
// Do a shallow visit so that `contains_illegal_self_type_reference`
|
|
|
|
// may apply it's custom visiting.
|
|
|
|
.visit_tys_shallow(|t| self.contains_illegal_self_type_reference(trait_def_id, t)) {
|
2018-05-30 09:06:08 -03:00
|
|
|
let span = self.def_span(method.def_id);
|
|
|
|
return Some(MethodViolationCode::WhereClauseReferencesSelf(span));
|
2018-05-22 12:09:35 -03:00
|
|
|
}
|
|
|
|
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
let receiver_ty = self.liberate_late_bound_regions(
|
|
|
|
method.def_id,
|
|
|
|
&sig.map_bound(|sig| sig.inputs()[0]),
|
|
|
|
);
|
|
|
|
|
2018-10-03 23:40:21 -04:00
|
|
|
// until `unsized_locals` is fully implemented, `self: Self` can't be dispatched on.
|
|
|
|
// However, this is already considered object-safe. We allow it as a special case here.
|
|
|
|
// FIXME(mikeyhew) get rid of this `if` statement once `receiver_is_dispatchable` allows
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
// `Receiver: Unsize<Receiver[Self => dyn Trait]>`
|
|
|
|
if receiver_ty != self.mk_self_type() {
|
2018-10-03 23:40:21 -04:00
|
|
|
if !self.receiver_is_dispatchable(method, receiver_ty) {
|
2018-10-08 20:37:58 -04:00
|
|
|
return Some(MethodViolationCode::UndispatchableReceiver);
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
None
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
|
|
|
|
2018-10-03 23:40:21 -04:00
|
|
|
/// checks the method's receiver (the `self` argument) can be dispatched on when `Self` is a
|
|
|
|
/// trait object. We require that `DispatchableFromDyn` be implemented for the receiver type
|
|
|
|
/// in the following way:
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
/// - let `Receiver` be the type of the `self` argument, i.e `Self`, `&Self`, `Rc<Self>`
|
|
|
|
/// - require the following bound:
|
|
|
|
/// forall(T: Trait) {
|
2018-10-03 23:40:21 -04:00
|
|
|
/// Receiver[Self => T]: DispatchFromDyn<Receiver[Self => dyn Trait]>
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
/// }
|
|
|
|
/// where `Foo[X => Y]` means "the same type as `Foo`, but with `X` replaced with `Y`"
|
|
|
|
/// (substitution notation).
|
|
|
|
///
|
|
|
|
/// some examples of receiver types and their required obligation
|
2018-10-03 23:40:21 -04:00
|
|
|
/// - `&'a mut self` requires `&'a mut T: DispatchFromDyn<&'a mut dyn Trait>`
|
|
|
|
/// - `self: Rc<Self>` requires `Rc<T>: DispatchFromDyn<Rc<dyn Trait>>`
|
|
|
|
/// - `self: Pin<Box<Self>>` requires `Pin<Box<T>>: DispatchFromDyn<Pin<Box<dyn Trait>>>`
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
///
|
2018-10-03 23:40:21 -04:00
|
|
|
/// The only case where the receiver is not dispatchable, but is still a valid receiver
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
/// type (just not object-safe), is when there is more than one level of pointer indirection.
|
|
|
|
/// e.g. `self: &&Self`, `self: &Rc<Self>`, `self: Box<Box<Self>>`. In these cases, there
|
2018-10-03 23:40:21 -04:00
|
|
|
/// is no way, or at least no inexpensive way, to coerce the receiver from the version where
|
|
|
|
/// `Self = dyn Trait` to the version where `Self = T`, where `T` is the unknown erased type
|
|
|
|
/// contained by the trait object, because the object that needs to be coerced is behind
|
|
|
|
/// a pointer.
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
///
|
|
|
|
/// In practice, there are issues with the above bound: `where` clauses that apply to `Self`
|
|
|
|
/// would have to apply to `T`, trait object types have a lot of parameters that need to
|
|
|
|
/// be filled in (lifetime and type parameters, and the lifetime of the actual object), and
|
|
|
|
/// I'm pretty sure using `dyn Trait` in the query causes another object-safety query for
|
|
|
|
/// `Trait`, resulting in cyclic queries. So in the implementation, we use the following,
|
|
|
|
/// more general bound:
|
|
|
|
///
|
|
|
|
/// forall (U: ?Sized) {
|
|
|
|
/// if (Self: Unsize<U>) {
|
2018-10-03 23:40:21 -04:00
|
|
|
/// Receiver: DispatchFromDyn<Receiver[Self => U]>
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2018-10-03 23:40:21 -04:00
|
|
|
/// for `self: &'a mut Self`, this means `&'a mut Self: DispatchFromDyn<&'a mut U>`
|
|
|
|
/// for `self: Rc<Self>`, this means `Rc<Self>: DispatchFromDyn<Rc<U>>`
|
|
|
|
/// for `self: Pin<Box<Self>>, this means `Pin<Box<Self>>: DispatchFromDyn<Pin<Box<U>>>`
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
//
|
|
|
|
// FIXME(mikeyhew) when unsized receivers are implemented as part of unsized rvalues, add this
|
|
|
|
// fallback query: `Receiver: Unsize<Receiver[Self => U]>` to support receivers like
|
|
|
|
// `self: Wrapper<Self>`.
|
|
|
|
#[allow(dead_code)]
|
2018-10-03 23:40:21 -04:00
|
|
|
fn receiver_is_dispatchable(
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
self,
|
|
|
|
method: &ty::AssociatedItem,
|
|
|
|
receiver_ty: Ty<'tcx>,
|
|
|
|
) -> bool {
|
2018-10-03 23:40:21 -04:00
|
|
|
debug!("receiver_is_dispatchable: method = {:?}, receiver_ty = {:?}", method, receiver_ty);
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
|
|
|
|
let traits = (self.lang_items().unsize_trait(),
|
2018-10-03 23:40:21 -04:00
|
|
|
self.lang_items().dispatch_from_dyn_trait());
|
|
|
|
let (unsize_did, dispatch_from_dyn_did) = if let (Some(u), Some(cu)) = traits {
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
(u, cu)
|
|
|
|
} else {
|
2018-10-03 23:40:21 -04:00
|
|
|
debug!("receiver_is_dispatchable: Missing Unsize or DispatchFromDyn traits");
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
// use a bogus type parameter to mimick a forall(U) query using u32::MAX for now.
|
|
|
|
// FIXME(mikeyhew) this is a total hack, and we should replace it when real forall queries
|
|
|
|
// are implemented
|
2018-10-03 23:40:21 -04:00
|
|
|
let unsized_self_ty: Ty<'tcx> = self.mk_ty_param(
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
::std::u32::MAX,
|
|
|
|
Name::intern("RustaceansAreAwesome").as_interned_str(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// create a modified param env, with `Self: Unsize<U>` added to the caller bounds
|
|
|
|
let param_env = {
|
|
|
|
let mut param_env = self.param_env(method.def_id);
|
|
|
|
|
|
|
|
let predicate = ty::TraitRef {
|
|
|
|
def_id: unsize_did,
|
2018-10-03 23:40:21 -04:00
|
|
|
substs: self.mk_substs_trait(self.mk_self_type(), &[unsized_self_ty.into()]),
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
}.to_predicate();
|
|
|
|
|
|
|
|
let caller_bounds: Vec<Predicate<'tcx>> = param_env.caller_bounds.iter().cloned()
|
|
|
|
.chain(iter::once(predicate))
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
param_env.caller_bounds = self.intern_predicates(&caller_bounds);
|
|
|
|
|
|
|
|
param_env
|
|
|
|
};
|
|
|
|
|
|
|
|
let receiver_substs = Substs::for_item(self, method.def_id, |param, _| {
|
|
|
|
if param.index == 0 {
|
2018-10-03 23:40:21 -04:00
|
|
|
unsized_self_ty.into()
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
} else {
|
|
|
|
self.mk_param_from_def(param)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// the type `Receiver[Self => U]` in the query
|
|
|
|
let unsized_receiver_ty = receiver_ty.subst(self, receiver_substs);
|
|
|
|
|
2018-10-03 23:40:21 -04:00
|
|
|
// Receiver: DispatchFromDyn<Receiver[Self => U]>
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
let obligation = {
|
|
|
|
let predicate = ty::TraitRef {
|
2018-10-03 23:40:21 -04:00
|
|
|
def_id: dispatch_from_dyn_did,
|
|
|
|
substs: self.mk_substs_trait(receiver_ty, &[unsized_receiver_ty.into()]),
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
}.to_predicate();
|
|
|
|
|
|
|
|
Obligation::new(
|
|
|
|
ObligationCause::dummy(),
|
|
|
|
param_env,
|
|
|
|
predicate,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
self.infer_ctxt().enter(|ref infcx| {
|
2018-10-03 23:40:21 -04:00
|
|
|
// the receiver is dispatchable iff the obligation holds
|
Implement the object-safety checks for arbitrary_self_types: part 1
For a trait method to be considered object-safe, the receiver type must
satisfy certain properties: first, we need to be able to get the vtable
to so we can look up the method, and second, we need to convert the
receiver from the version where `Self=dyn Trait`, to the version where
`Self=T`, `T` being some unknown, `Sized` type that implements `Trait`.
To check that the receiver satisfies those properties, we use the
following query:
forall (U) {
if (Self: Unsize<U>) {
Receiver[Self => U]: CoerceSized<Receiver>
}
}
where `Receiver` is the receiver type of the method (e.g. `Rc<Self>`),
and `Receiver[Self => U]` is the receiver type where `Self = U`, e.g.
`Rc<U>`.
forall queries like this aren’t implemented in the trait system yet, so
for now we are using a bit of a hack — see the code for explanation.
2018-09-20 03:12:00 -04:00
|
|
|
infcx.predicate_must_hold(&obligation)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
fn contains_illegal_self_type_reference(self,
|
|
|
|
trait_def_id: DefId,
|
|
|
|
ty: Ty<'tcx>)
|
|
|
|
-> bool
|
|
|
|
{
|
|
|
|
// This is somewhat subtle. In general, we want to forbid
|
|
|
|
// references to `Self` in the argument and return types,
|
|
|
|
// since the value of `Self` is erased. However, there is one
|
|
|
|
// exception: it is ok to reference `Self` in order to access
|
|
|
|
// an associated type of the current trait, since we retain
|
|
|
|
// the value of those associated types in the object type
|
|
|
|
// itself.
|
|
|
|
//
|
|
|
|
// ```rust
|
|
|
|
// trait SuperTrait {
|
|
|
|
// type X;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// trait Trait : SuperTrait {
|
|
|
|
// type Y;
|
|
|
|
// fn foo(&self, x: Self) // bad
|
|
|
|
// fn foo(&self) -> Self // bad
|
|
|
|
// fn foo(&self) -> Option<Self> // bad
|
|
|
|
// fn foo(&self) -> Self::Y // OK, desugars to next example
|
|
|
|
// fn foo(&self) -> <Self as Trait>::Y // OK
|
|
|
|
// fn foo(&self) -> Self::X // OK, desugars to next example
|
|
|
|
// fn foo(&self) -> <Self as SuperTrait>::X // OK
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// However, it is not as simple as allowing `Self` in a projected
|
|
|
|
// type, because there are illegal ways to use `Self` as well:
|
|
|
|
//
|
|
|
|
// ```rust
|
|
|
|
// trait Trait : SuperTrait {
|
|
|
|
// ...
|
|
|
|
// fn foo(&self) -> <Self as SomeOtherTrait>::X;
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Here we will not have the type of `X` recorded in the
|
|
|
|
// object type, and we cannot resolve `Self as SomeOtherTrait`
|
|
|
|
// without knowing what `Self` is.
|
|
|
|
|
|
|
|
let mut supertraits: Option<Vec<ty::PolyTraitRef<'tcx>>> = None;
|
|
|
|
let mut error = false;
|
|
|
|
ty.maybe_walk(|ty| {
|
|
|
|
match ty.sty {
|
2018-08-22 01:35:29 +01:00
|
|
|
ty::Param(ref param_ty) => {
|
2016-08-15 01:07:09 +03:00
|
|
|
if param_ty.is_self() {
|
2016-05-11 08:48:12 +03:00
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
false // no contained types to walk
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
|
|
|
|
2018-08-22 01:35:02 +01:00
|
|
|
ty::Projection(ref data) => {
|
2016-05-11 08:48:12 +03:00
|
|
|
// This is a projected type `<Foo as SomeTrait>::X`.
|
|
|
|
|
|
|
|
// Compute supertraits of current trait lazily.
|
|
|
|
if supertraits.is_none() {
|
2018-06-29 06:59:00 -04:00
|
|
|
let trait_ref = ty::Binder::bind(
|
|
|
|
ty::TraitRef::identity(self, trait_def_id),
|
|
|
|
);
|
2016-05-11 08:48:12 +03:00
|
|
|
supertraits = Some(traits::supertraits(self, trait_ref).collect());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine whether the trait reference `Foo as
|
|
|
|
// SomeTrait` is in fact a supertrait of the
|
|
|
|
// current trait. In that case, this type is
|
|
|
|
// legal, because the type `X` will be specified
|
|
|
|
// in the object type. Note that we can just use
|
|
|
|
// direct equality here because all of these types
|
|
|
|
// are part of the formal parameter listing, and
|
|
|
|
// hence there should be no inference variables.
|
2018-04-24 21:45:49 -05:00
|
|
|
let projection_trait_ref = ty::Binder::bind(data.trait_ref(self));
|
2016-05-11 08:48:12 +03:00
|
|
|
let is_supertrait_of_current_trait =
|
|
|
|
supertraits.as_ref().unwrap().contains(&projection_trait_ref);
|
|
|
|
|
|
|
|
if is_supertrait_of_current_trait {
|
|
|
|
false // do not walk contained types, do not report error, do collect $200
|
|
|
|
} else {
|
|
|
|
true // DO walk contained types, POSSIBLY reporting an error
|
|
|
|
}
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
_ => true, // walk contained types, if any
|
2014-12-15 21:11:09 -05:00
|
|
|
}
|
2016-05-11 08:48:12 +03:00
|
|
|
});
|
2014-12-15 21:11:09 -05:00
|
|
|
|
2016-05-11 08:48:12 +03:00
|
|
|
error
|
|
|
|
}
|
2016-03-17 00:15:31 +02:00
|
|
|
}
|
2017-05-11 16:01:19 +02:00
|
|
|
|
|
|
|
pub(super) fn is_object_safe_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2018-04-18 17:15:53 +01:00
|
|
|
trait_def_id: DefId) -> bool {
|
2017-05-11 16:01:19 +02:00
|
|
|
tcx.object_safety_violations(trait_def_id).is_empty()
|
|
|
|
}
|