Skip to content

Commit

Permalink
Change the return type of Ty::kind from &TyKind to TyKind.
Browse files Browse the repository at this point in the history
This is valid because `TyKind` impls `Copy`, and makes `Ty` consistent
with `Predicate` and `Region`, both of which have `kind` methods that
return a non-reference.

The change removes more than one thousand `&` and `*` sigils, while
requiring the addition of just five! It's clearly moving with the grain
of the existing code.

Almost all of the removed sigils fit one of the following three
patterns.

- Remove the dereference in the match expression:

    `match *ty.kind() { ... }` ->
    `match ty.kind() { ... }`

- Remove the dereference in the match pattern:

    `match ty.kind() { &ty::Foo(foo) => { ... foo ... }` ->
    `match ty.kind() { ty::Foo(foo) => { ... foo ... }`

- Remove the derefernce in the match arm:

    `match ty.kind() { ty::Foo(foo) => { ... *foo ... }` ->
    `match ty.kind() { ty::Foo(foo) => { ... foo ... }`

A couple of other methods had their signatures changed.
- `TypeErrCtxt::cmp_fn_sig`: `&` removed from two args.
- `EncodableWithShorthand::variant`: `&` removed from return type.
  • Loading branch information
nnethercote committed Jun 6, 2024
1 parent 2b6a342 commit 25e854a
Show file tree
Hide file tree
Showing 322 changed files with 1,096 additions and 1,134 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
// borrowing the type, since `&mut F: FnMut` iff `F: FnMut` and similarly for `Fn`.
// These types seem reasonably opaque enough that they could be instantiated with their
// borrowed variants in a function body when we see a move error.
let borrow_level = match *ty.kind() {
let borrow_level = match ty.kind() {
ty::Param(_) => tcx
.explicit_predicates_of(self.mir_def_id().to_def_id())
.predicates
Expand Down Expand Up @@ -1210,7 +1210,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
} else if let ty::Param(param) = ty.kind()
&& let Some(_clone_trait_def) = self.infcx.tcx.lang_items().clone_trait()
&& let generics = self.infcx.tcx.generics_of(self.mir_def_id())
&& let generic_param = generics.type_param(*param, self.infcx.tcx)
&& let generic_param = generics.type_param(param, self.infcx.tcx)
&& let param_span = self.infcx.tcx.def_span(generic_param.def_id)
&& if let Some(UseSpans::FnSelfUse { kind, .. }) = use_spans
&& let CallKind::FnCall { fn_trait_id, self_ty } = kind
Expand Down Expand Up @@ -1358,7 +1358,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
.into_iter()
.map(|err| match err.obligation.predicate.kind().skip_binder() {
PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
match *predicate.self_ty().kind() {
match predicate.self_ty().kind() {
ty::Param(param_ty) => Ok((
generics.type_param(param_ty, tcx),
predicate.trait_ref.print_trait_sugared().to_string(),
Expand Down
57 changes: 27 additions & 30 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,40 +317,37 @@ impl<'tcx> BorrowExplanation<'tcx> {
let mut has_dyn = false;
let mut failed = false;

let elaborated_args =
std::iter::zip(*args, &generics.own_params).map(|(arg, param)| {
if let Some(ty::Dynamic(obj, _, ty::Dyn)) = arg.as_type().map(Ty::kind) {
let default = tcx.object_lifetime_default(param.def_id);

let re_static = tcx.lifetimes.re_static;

let implied_region = match default {
// This is not entirely precise.
ObjectLifetimeDefault::Empty => re_static,
ObjectLifetimeDefault::Ambiguous => {
let elaborated_args = std::iter::zip(args, &generics.own_params).map(|(arg, param)| {
if let Some(ty::Dynamic(obj, _, ty::Dyn)) = arg.as_type().map(Ty::kind) {
let default = tcx.object_lifetime_default(param.def_id);

let re_static = tcx.lifetimes.re_static;

let implied_region = match default {
// This is not entirely precise.
ObjectLifetimeDefault::Empty => re_static,
ObjectLifetimeDefault::Ambiguous => {
failed = true;
re_static
}
ObjectLifetimeDefault::Param(param_def_id) => {
let index = generics.param_def_id_to_index[&param_def_id] as usize;
args.get(index).and_then(|arg| arg.as_region()).unwrap_or_else(|| {
failed = true;
re_static
}
ObjectLifetimeDefault::Param(param_def_id) => {
let index = generics.param_def_id_to_index[&param_def_id] as usize;
args.get(index).and_then(|arg| arg.as_region()).unwrap_or_else(
|| {
failed = true;
re_static
},
)
}
ObjectLifetimeDefault::Static => re_static,
};
})
}
ObjectLifetimeDefault::Static => re_static,
};

has_dyn = true;
has_dyn = true;

Ty::new_dynamic(tcx, obj, implied_region, ty::Dyn).into()
} else {
arg
}
});
let elaborated_ty = Ty::new_adt(tcx, *def, tcx.mk_args_from_iter(elaborated_args));
Ty::new_dynamic(tcx, obj, implied_region, ty::Dyn).into()
} else {
arg
}
});
let elaborated_ty = Ty::new_adt(tcx, def, tcx.mk_args_from_iter(elaborated_args));

if has_dyn && !failed {
err.note(format!(
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
..
} = &terminator.kind
{
if let ty::FnDef(id, _) = *const_.ty().kind() {
if let ty::FnDef(id, _) = const_.ty().kind() {
debug!("add_moved_or_invoked_closure_note: id={:?}", id);
if Some(self.infcx.tcx.parent(id)) == self.infcx.tcx.lang_items().fn_once_trait() {
let closure = match args.first() {
Expand Down Expand Up @@ -356,7 +356,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
// If the type is a box, the field is described from the boxed type
self.describe_field_from_ty(ty.boxed_ty(), field, variant_index, including_tuple_field)
} else {
match *ty.kind() {
match ty.kind() {
ty::Adt(def, _) => {
let variant = if let Some(idx) = variant_index {
assert!(def.is_enum());
Expand Down Expand Up @@ -467,7 +467,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
// this by hooking into the pretty printer and telling it to label the
// lifetimes without names with the value `'0`.
if let ty::Ref(region, ..) = ty.kind() {
match **region {
match *region {
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
| ty::RePlaceholder(ty::PlaceholderRegion {
bound: ty::BoundRegion { kind: br, .. },
Expand All @@ -487,7 +487,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, Namespace::TypeNS);

let region = if let ty::Ref(region, ..) = ty.kind() {
match **region {
match *region {
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
| ty::RePlaceholder(ty::PlaceholderRegion {
bound: ty::BoundRegion { kind: br, .. },
Expand Down Expand Up @@ -763,7 +763,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
}

fn from_call(func: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<Self> {
match *func.kind() {
match func.kind() {
ty::FnDef(def_id, args) => {
let trait_id = tcx.trait_of_item(def_id)?;

Expand Down Expand Up @@ -1073,7 +1073,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
// LL | blk();
// | ----- this value implements `FnOnce`, which causes it to be moved when called
// ```
if let ty::Param(param_ty) = *self_ty.kind()
if let ty::Param(param_ty) = self_ty.kind()
&& let generics = self.infcx.tcx.generics_of(self.mir_def_id())
&& let param = generics.type_param(param_ty, self.infcx.tcx)
&& let Some(hir_generics) = self
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
if suggest {
self.construct_mut_suggestion_for_local_binding_patterns(&mut err, local);
let tcx = self.infcx.tcx;
if let ty::Closure(id, _) = *the_place_err.ty(self.body, tcx).ty.kind() {
if let ty::Closure(id, _) = the_place_err.ty(self.body, tcx).ty.kind() {
self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
}
}
Expand Down Expand Up @@ -431,7 +431,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {

let tcx = self.infcx.tcx;
if let ty::Ref(_, ty, Mutability::Mut) = the_place_err.ty(self.body, tcx).ty.kind()
&& let ty::Closure(id, _) = *ty.kind()
&& let ty::Closure(id, _) = ty.kind()
{
self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
}
Expand Down Expand Up @@ -950,7 +950,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
if let Some(ty::FnDef(def_id, _)) =
tables.node_type_opt(func.hir_id).as_ref().map(|ty| ty.kind())
{
let arg = match hir.get_if_local(*def_id) {
let arg = match hir.get_if_local(def_id) {
Some(
hir::Node::Item(hir::Item {
ident, kind: hir::ItemKind::Fn(sig, ..), ..
Expand Down
21 changes: 10 additions & 11 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
ty::VarianceDiagInfo::Invariant { ty, param_index } => {
let (desc, note) = match ty.kind() {
ty::RawPtr(ty, mutbl) => {
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
assert_eq!(mutbl, rustc_hir::Mutability::Mut);
(
format!("a mutable pointer to `{}`", ty),
"mutable pointers are invariant over their type parameter".to_string(),
)
}
ty::Ref(_, inner_ty, mutbl) => {
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
assert_eq!(mutbl, rustc_hir::Mutability::Mut);
(
format!("a mutable reference to `{inner_ty}`"),
"mutable references are invariant over their type parameter"
Expand All @@ -527,7 +527,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let generic_arg = args[param_index as usize];
let identity_args =
GenericArgs::identity_for_item(self.infcx.tcx, adt.did());
let base_ty = Ty::new_adt(self.infcx.tcx, *adt, identity_args);
let base_ty = Ty::new_adt(self.infcx.tcx, adt, identity_args);
let base_generic_arg = identity_args[param_index as usize];
let adt_desc = adt.descr();

Expand All @@ -540,8 +540,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
(desc, note)
}
ty::FnDef(def_id, _) => {
let name = self.infcx.tcx.item_name(*def_id);
let identity_args = GenericArgs::identity_for_item(self.infcx.tcx, *def_id);
let name = self.infcx.tcx.item_name(def_id);
let identity_args = GenericArgs::identity_for_item(self.infcx.tcx, def_id);
let desc = format!("a function pointer to `{name}`");
let note = format!(
"the function `{name}` is invariant over the parameter `{}`",
Expand Down Expand Up @@ -593,7 +593,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let ErrorConstraintInfo { outlived_fr, span, .. } = errci;

let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *output_ty.kind() {
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = output_ty.kind() {
output_ty = self.infcx.tcx.type_of(def_id).instantiate_identity()
};

Expand All @@ -602,7 +602,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let err = FnMutError {
span: *span,
ty_err: match output_ty.kind() {
ty::Coroutine(def, ..) if self.infcx.tcx.coroutine_is_async(*def) => {
ty::Coroutine(def, ..) if self.infcx.tcx.coroutine_is_async(def) => {
FnMutReturnTypeErr::ReturnAsyncBlock { span: *span }
}
_ if output_ty.contains_closure() => {
Expand Down Expand Up @@ -952,7 +952,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
if let Ok(Some(instance)) = ty::Instance::resolve(
tcx,
self.param_env,
*fn_did,
fn_did,
self.infcx.resolve_vars_if_possible(args),
) {
instance
Expand Down Expand Up @@ -1070,8 +1070,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
else {
return;
};
let ty::Closure(_, args) = *tcx.type_of(closure_def_id).instantiate_identity().kind()
else {
let ty::Closure(_, args) = tcx.type_of(closure_def_id).instantiate_identity().kind() else {
return;
};
let args = args.as_closure();
Expand All @@ -1092,7 +1091,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let liberated_sig = tcx.liberate_late_bound_regions(closure_def_id.to_def_id(), args.sig());
let mut peeled_ty = liberated_sig.output();
let mut count = 0;
while let ty::Ref(_, ref_ty, _) = *peeled_ty.kind() {
while let ty::Ref(_, ref_ty, _) = peeled_ty.kind() {
peeled_ty = ref_ty;
count += 1;
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
}

// Otherwise, let's descend into the referent types.
search_stack.push((*referent_ty, &referent_hir_ty.ty));
search_stack.push((referent_ty, &referent_hir_ty.ty));
}

// Match up something like `Foo<'1>`
Expand Down Expand Up @@ -548,17 +548,17 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
// The following cases don't have lifetimes, so we
// just worry about trying to match up the rustc type
// with the HIR types:
(&ty::Tuple(elem_tys), hir::TyKind::Tup(elem_hir_tys)) => {
(ty::Tuple(elem_tys), hir::TyKind::Tup(elem_hir_tys)) => {
search_stack.extend(iter::zip(elem_tys, *elem_hir_tys));
}

(ty::Slice(elem_ty), hir::TyKind::Slice(elem_hir_ty))
| (ty::Array(elem_ty, _), hir::TyKind::Array(elem_hir_ty, _)) => {
search_stack.push((*elem_ty, elem_hir_ty));
search_stack.push((elem_ty, elem_hir_ty));
}

(ty::RawPtr(mut_ty, _), hir::TyKind::Ptr(mut_hir_ty)) => {
search_stack.push((*mut_ty, &mut_hir_ty.ty));
search_stack.push((mut_ty, &mut_hir_ty.ty));
}

_ => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ fn do_mir_borrowck<'tcx>(
// The first argument is the coroutine type passed by value
if let Some(local) = body.local_decls.raw.get(1)
// Get the interior types and args which typeck computed
&& let ty::Coroutine(def_id, _) = *local.ty.kind()
&& let ty::Coroutine(def_id, _) = local.ty.kind()
&& tcx.coroutine_movability(def_id) == hir::Movability::Movable
{
true
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
use ty::TypeSuperFoldable as _;
let tcx = self.tcx;
let &ty::Alias(ty::Opaque, ty::AliasTy { args, def_id, .. }) = t.kind() else {
let ty::Alias(ty::Opaque, ty::AliasTy { args, def_id, .. }) = t.kind() else {
return t.super_fold_with(self);
};
let args = std::iter::zip(args, tcx.variances_of(def_id)).map(|(arg, v)| {
Expand Down
Loading

0 comments on commit 25e854a

Please sign in to comment.