Forbid static methods in object safe traits

Closes #19949 and rust-lang/rfcs#428

[breaking change]

If you have traits used with objects with static methods, you'll need to move
the static methods to a different trait.
This commit is contained in:
Nick Cameron 2014-12-30 18:30:53 +13:00
parent fea5aa656f
commit cd31e6ff39

View File

@ -166,11 +166,13 @@ fn check_object_safety_inner<'tcx>(tcx: &ty::ctxt<'tcx>,
}
}
/// Returns a vec of error messages. If hte vec is empty - no errors!
/// Returns a vec of error messages. If the vec is empty - no errors!
///
/// There are some limitations to calling functions through an object, because (a) the self
/// type is not known (that's the whole point of a trait instance, after all, to obscure the
/// self type) and (b) the call must go through a vtable and hence cannot be monomorphized.
/// self type), (b) the call must go through a vtable and hence cannot be monomorphized and
/// (c) the trait contains static methods which can't be called because we don't know the
/// concrete type.
fn check_object_safety_of_method<'tcx>(tcx: &ty::ctxt<'tcx>,
method: &ty::Method<'tcx>)
-> Vec<String> {
@ -185,9 +187,11 @@ fn check_object_safety_inner<'tcx>(tcx: &ty::ctxt<'tcx>,
}
ty::StaticExplicitSelfCategory => {
// Static methods are always object-safe since they
// can't be called through a trait object
return msgs
// Static methods are never object safe (reason (c)).
msgs.push(format!("cannot call a static method (`{}`) \
through a trait object",
method_name));
return msgs;
}
ty::ByReferenceExplicitSelfCategory(..) |
ty::ByBoxExplicitSelfCategory => {}