Skip to content

Commit fb4095d

Browse files
authored
Update comments re type parameter hack in object safety
To check if a method's receiver type is object safe, we create a new receiver type by substituting in a bogus type parameter (let's call it `U`) for `Self`, and checking that the unmodified receiver type implements `DispatchFromDyn<receiver type with Self = U>`. It would be better to use `dyn Trait` directly, and the only reason we don't is because it triggers another check that `Trait` is object safe, resulting in a query cycle. Once the feature `object_safe_for_dispatch` (tracking issue rust-lang#43561) is stabilized, this will no longer be the case, and we'll be able to use `dyn Trait` as the unsized `Self` type. I've updated the comments in object_safety.rs accordingly.
1 parent 46e6c53 commit fb4095d

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/librustc/traits/object_safety.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,11 @@ impl<'tcx> TyCtxt<'tcx> {
520520
/// a pointer.
521521
///
522522
/// In practice, we cannot use `dyn Trait` explicitly in the obligation because it would result
523-
/// in a new check that `Trait` is object safe, creating a cycle. So instead, we fudge a little
524-
/// by introducing a new type parameter `U` such that `Self: Unsize<U>` and `U: Trait + ?Sized`,
525-
/// and use `U` in place of `dyn Trait`. Written as a chalk-style query:
523+
/// in a new check that `Trait` is object safe, creating a cycle (until object_safe_for_dispatch
524+
/// is stabilized, see tracking issue https://github.com/rust-lang/rust/issues/43561).
525+
/// Instead, we fudge a little by introducing a new type parameter `U` such that
526+
/// `Self: Unsize<U>` and `U: Trait + ?Sized`, and use `U` in place of `dyn Trait`.
527+
/// Written as a chalk-style query:
526528
///
527529
/// forall (U: Trait + ?Sized) {
528530
/// if (Self: Unsize<U>) {
@@ -556,8 +558,8 @@ impl<'tcx> TyCtxt<'tcx> {
556558

557559
// the type `U` in the query
558560
// use a bogus type parameter to mimick a forall(U) query using u32::MAX for now.
559-
// FIXME(mikeyhew) this is a total hack, and we should replace it when real forall queries
560-
// are implemented
561+
// FIXME(mikeyhew) this is a total hack. Once object_safe_for_dispatch is stabilized, we can
562+
// replace this with `dyn Trait`
561563
let unsized_self_ty: Ty<'tcx> = self.mk_ty_param(
562564
::std::u32::MAX,
563565
Symbol::intern("RustaceansAreAwesome"),

0 commit comments

Comments
 (0)