Skip to content

Commit 0b27dcc

Browse files
committed
modify ExplicitSelf::determine to take an is_self_type predicate closure, instead of infcx
1 parent 1d29966 commit 0b27dcc

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

src/librustc_typeck/astconv.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use hir::def::Def;
1919
use hir::def_id::DefId;
2020
use middle::resolve_lifetime as rl;
2121
use namespace::Namespace;
22-
use rustc::infer::InferCtxt;
2322
use rustc::ty::subst::{Kind, Subst, Substs};
2423
use rustc::traits;
2524
use rustc::ty::{self, Ty, TyCtxt, ToPredicate, TypeFoldable};
@@ -1415,9 +1414,10 @@ pub enum ExplicitSelf<'tcx> {
14151414
impl<'tcx> ExplicitSelf<'tcx> {
14161415
/// Categorizes an explicit self declaration like `self: SomeType`
14171416
/// into either `self`, `&self`, `&mut self`, `Box<self>`, or
1418-
/// `Other` (meaning the arbitrary_self_types feature is used).
1419-
/// We do this here via a combination of pattern matching and
1420-
/// `can_eq`. A more precise check is done in `check_method_receiver()`.
1417+
/// `Other`.
1418+
/// This is mainly used to require the arbitrary_self_types feature
1419+
/// in the case of `Other`, to improve error messages in the common cases,
1420+
/// and to make `Other` non-object-safe.
14211421
///
14221422
/// Examples:
14231423
///
@@ -1436,21 +1436,19 @@ impl<'tcx> ExplicitSelf<'tcx> {
14361436
/// }
14371437
/// ```
14381438
///
1439-
pub fn determine<'a, 'gcx>(
1440-
infcx: &InferCtxt<'a, 'gcx, 'tcx>,
1441-
param_env: ty::ParamEnv<'tcx>,
1442-
self_ty: Ty<'tcx>,
1443-
self_arg_ty: Ty<'tcx>
1439+
pub fn determine<'a, 'gcx, P>(
1440+
self_arg_ty: Ty<'tcx>,
1441+
is_self_ty: P
14441442
) -> ExplicitSelf<'tcx>
1443+
where
1444+
P: Fn(Ty<'tcx>) -> bool
14451445
{
14461446
use self::ExplicitSelf::*;
14471447

1448-
let can_eq = |expected, actual| infcx.can_eq(param_env, expected, actual).is_ok();
1449-
14501448
match self_arg_ty.sty {
1451-
_ if can_eq(self_arg_ty, self_ty) => ByValue,
1452-
ty::TyRef(region, ty::TypeAndMut { ty, mutbl}) if can_eq(ty, self_ty) => ByReference(region, mutbl),
1453-
ty::TyAdt(def, _) if def.is_box() && can_eq(self_arg_ty.boxed_ty(), self_ty) => ByBox,
1449+
_ if is_self_ty(self_arg_ty) => ByValue,
1450+
ty::TyRef(region, ty::TypeAndMut { ty, mutbl}) if is_self_ty(ty) => ByReference(region, mutbl),
1451+
ty::TyAdt(def, _) if def.is_box() && is_self_ty(self_arg_ty.boxed_ty()) => ByBox,
14541452
_ => Other
14551453
}
14561454
}

src/librustc_typeck/check/compare_method.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,8 @@ fn compare_self_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
506506
let param_env = ty::ParamEnv::empty(Reveal::All);
507507

508508
tcx.infer_ctxt().enter(|infcx| {
509-
match ExplicitSelf::determine(&infcx, param_env, untransformed_self_ty, self_arg_ty) {
509+
let can_eq_self = |ty| infcx.can_eq(param_env, untransformed_self_ty, ty).is_ok();
510+
match ExplicitSelf::determine(self_arg_ty, can_eq_self) {
510511
ExplicitSelf::ByValue => "self".to_string(),
511512
ExplicitSelf::ByReference(_, hir::MutImmutable) => "&self".to_string(),
512513
ExplicitSelf::ByReference(_, hir::MutMutable) => "&mut self".to_string(),

src/librustc_typeck/check/wfcheck.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,8 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
495495
}
496496
}
497497

498-
let self_kind = ExplicitSelf::determine(fcx, fcx.param_env, self_ty, self_arg_ty);
498+
let is_self_ty = |ty| fcx.infcx.can_eq(fcx.param_env, self_ty, ty).is_ok();
499+
let self_kind = ExplicitSelf::determine(self_arg_ty, is_self_ty);
499500

500501
if let ExplicitSelf::Other = self_kind {
501502
if !fcx.tcx.sess.features.borrow().arbitrary_self_types {

0 commit comments

Comments
 (0)