Skip to content

Commit cd31e6f

Browse files
committed
Forbid static methods in object safe traits
Closes rust-lang#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.
1 parent fea5aa6 commit cd31e6f

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/librustc_typeck/check/vtable.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,13 @@ fn check_object_safety_inner<'tcx>(tcx: &ty::ctxt<'tcx>,
166166
}
167167
}
168168

169-
/// Returns a vec of error messages. If hte vec is empty - no errors!
169+
/// Returns a vec of error messages. If the vec is empty - no errors!
170170
///
171171
/// There are some limitations to calling functions through an object, because (a) the self
172172
/// type is not known (that's the whole point of a trait instance, after all, to obscure the
173-
/// self type) and (b) the call must go through a vtable and hence cannot be monomorphized.
173+
/// self type), (b) the call must go through a vtable and hence cannot be monomorphized and
174+
/// (c) the trait contains static methods which can't be called because we don't know the
175+
/// concrete type.
174176
fn check_object_safety_of_method<'tcx>(tcx: &ty::ctxt<'tcx>,
175177
method: &ty::Method<'tcx>)
176178
-> Vec<String> {
@@ -185,9 +187,11 @@ fn check_object_safety_inner<'tcx>(tcx: &ty::ctxt<'tcx>,
185187
}
186188

187189
ty::StaticExplicitSelfCategory => {
188-
// Static methods are always object-safe since they
189-
// can't be called through a trait object
190-
return msgs
190+
// Static methods are never object safe (reason (c)).
191+
msgs.push(format!("cannot call a static method (`{}`) \
192+
through a trait object",
193+
method_name));
194+
return msgs;
191195
}
192196
ty::ByReferenceExplicitSelfCategory(..) |
193197
ty::ByBoxExplicitSelfCategory => {}

0 commit comments

Comments
 (0)