Skip to content

Commit 361bbf2

Browse files
authored
Unrolled build for rust-lang#139041
Rollup merge of rust-lang#139041 - nnethercote:rm-rustc_middle-ty-util-ExplicitSelf, r=BoxyUwU Remove `rustc_middle::ty::util::ExplicitSelf`. It's an old (2017 or earlier) type that describes a `self` receiver. It's only used in `rustc_hir_analysis` for two error messages, and much of the complexity isn't used. I suspect it used to be used for more things. This commit removes it, and moves a greatly simplified version of the `determine` method into `rustc_hir_analysis`, renamed as `get_self_string`. The big comment on the method is removed because it no longer seems relevant. r? `@BoxyUwU`
2 parents 17ffbc8 + 7876836 commit 361bbf2

File tree

2 files changed

+22
-62
lines changed

2 files changed

+22
-62
lines changed

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+22-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc_hir::{self as hir, AmbigArg, GenericParamKind, ImplItemKind, intravisi
1212
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
1313
use rustc_infer::traits::util;
1414
use rustc_middle::ty::error::{ExpectedFound, TypeError};
15-
use rustc_middle::ty::util::ExplicitSelf;
1615
use rustc_middle::ty::{
1716
self, BottomUpFolder, GenericArgs, GenericParamDefKind, Ty, TyCtxt, TypeFoldable, TypeFolder,
1817
TypeSuperFoldable, TypeVisitableExt, TypingMode, Upcast,
@@ -995,6 +994,26 @@ impl<'tcx> ty::FallibleTypeFolder<TyCtxt<'tcx>> for RemapHiddenTyRegions<'tcx> {
995994
}
996995
}
997996

997+
/// Gets the string for an explicit self declaration, e.g. "self", "&self",
998+
/// etc.
999+
fn get_self_string<'tcx, P>(self_arg_ty: Ty<'tcx>, is_self_ty: P) -> String
1000+
where
1001+
P: Fn(Ty<'tcx>) -> bool,
1002+
{
1003+
if is_self_ty(self_arg_ty) {
1004+
"self".to_owned()
1005+
} else if let ty::Ref(_, ty, mutbl) = self_arg_ty.kind()
1006+
&& is_self_ty(*ty)
1007+
{
1008+
match mutbl {
1009+
hir::Mutability::Not => "&self".to_owned(),
1010+
hir::Mutability::Mut => "&mut self".to_owned(),
1011+
}
1012+
} else {
1013+
format!("self: {self_arg_ty}")
1014+
}
1015+
}
1016+
9981017
fn report_trait_method_mismatch<'tcx>(
9991018
infcx: &InferCtxt<'tcx>,
10001019
mut cause: ObligationCause<'tcx>,
@@ -1020,12 +1039,7 @@ fn report_trait_method_mismatch<'tcx>(
10201039
if trait_m.fn_has_self_parameter =>
10211040
{
10221041
let ty = trait_sig.inputs()[0];
1023-
let sugg = match ExplicitSelf::determine(ty, |ty| ty == impl_trait_ref.self_ty()) {
1024-
ExplicitSelf::ByValue => "self".to_owned(),
1025-
ExplicitSelf::ByReference(_, hir::Mutability::Not) => "&self".to_owned(),
1026-
ExplicitSelf::ByReference(_, hir::Mutability::Mut) => "&mut self".to_owned(),
1027-
_ => format!("self: {ty}"),
1028-
};
1042+
let sugg = get_self_string(ty, |ty| ty == impl_trait_ref.self_ty());
10291043

10301044
// When the `impl` receiver is an arbitrary self type, like `self: Box<Self>`, the
10311045
// span points only at the type `Box<Self`>, but we want to cover the whole
@@ -1238,12 +1252,7 @@ fn compare_self_type<'tcx>(
12381252
.build_with_typing_env(ty::TypingEnv::non_body_analysis(tcx, method.def_id));
12391253
let self_arg_ty = tcx.liberate_late_bound_regions(method.def_id, self_arg_ty);
12401254
let can_eq_self = |ty| infcx.can_eq(param_env, untransformed_self_ty, ty);
1241-
match ExplicitSelf::determine(self_arg_ty, can_eq_self) {
1242-
ExplicitSelf::ByValue => "self".to_owned(),
1243-
ExplicitSelf::ByReference(_, hir::Mutability::Not) => "&self".to_owned(),
1244-
ExplicitSelf::ByReference(_, hir::Mutability::Mut) => "&mut self".to_owned(),
1245-
_ => format!("self: {self_arg_ty}"),
1246-
}
1255+
get_self_string(self_arg_ty, can_eq_self)
12471256
};
12481257

12491258
match (trait_m.fn_has_self_parameter, impl_m.fn_has_self_parameter) {

Diff for: compiler/rustc_middle/src/ty/util.rs

-49
Original file line numberDiff line numberDiff line change
@@ -1561,55 +1561,6 @@ impl<'tcx> Ty<'tcx> {
15611561
}
15621562
}
15631563

1564-
pub enum ExplicitSelf<'tcx> {
1565-
ByValue,
1566-
ByReference(ty::Region<'tcx>, hir::Mutability),
1567-
ByRawPointer(hir::Mutability),
1568-
ByBox,
1569-
Other,
1570-
}
1571-
1572-
impl<'tcx> ExplicitSelf<'tcx> {
1573-
/// Categorizes an explicit self declaration like `self: SomeType`
1574-
/// into either `self`, `&self`, `&mut self`, `Box<Self>`, or
1575-
/// `Other`.
1576-
/// This is mainly used to require the arbitrary_self_types feature
1577-
/// in the case of `Other`, to improve error messages in the common cases,
1578-
/// and to make `Other` dyn-incompatible.
1579-
///
1580-
/// Examples:
1581-
///
1582-
/// ```ignore (illustrative)
1583-
/// impl<'a> Foo for &'a T {
1584-
/// // Legal declarations:
1585-
/// fn method1(self: &&'a T); // ExplicitSelf::ByReference
1586-
/// fn method2(self: &'a T); // ExplicitSelf::ByValue
1587-
/// fn method3(self: Box<&'a T>); // ExplicitSelf::ByBox
1588-
/// fn method4(self: Rc<&'a T>); // ExplicitSelf::Other
1589-
///
1590-
/// // Invalid cases will be caught by `check_method_receiver`:
1591-
/// fn method_err1(self: &'a mut T); // ExplicitSelf::Other
1592-
/// fn method_err2(self: &'static T) // ExplicitSelf::ByValue
1593-
/// fn method_err3(self: &&T) // ExplicitSelf::ByReference
1594-
/// }
1595-
/// ```
1596-
///
1597-
pub fn determine<P>(self_arg_ty: Ty<'tcx>, is_self_ty: P) -> ExplicitSelf<'tcx>
1598-
where
1599-
P: Fn(Ty<'tcx>) -> bool,
1600-
{
1601-
use self::ExplicitSelf::*;
1602-
1603-
match *self_arg_ty.kind() {
1604-
_ if is_self_ty(self_arg_ty) => ByValue,
1605-
ty::Ref(region, ty, mutbl) if is_self_ty(ty) => ByReference(region, mutbl),
1606-
ty::RawPtr(ty, mutbl) if is_self_ty(ty) => ByRawPointer(mutbl),
1607-
_ if self_arg_ty.boxed_ty().is_some_and(is_self_ty) => ByBox,
1608-
_ => Other,
1609-
}
1610-
}
1611-
}
1612-
16131564
/// Returns a list of types such that the given type needs drop if and only if
16141565
/// *any* of the returned types need drop. Returns `Err(AlwaysRequiresDrop)` if
16151566
/// this type always needs drop.

0 commit comments

Comments
 (0)