Skip to content

Commit 50a3cde

Browse files
committed
Change the return type of Ty::kind from TyKind to &TyKind.
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.
1 parent 2b6a342 commit 50a3cde

File tree

322 files changed

+1092
-1130
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

322 files changed

+1092
-1130
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
721721
// borrowing the type, since `&mut F: FnMut` iff `F: FnMut` and similarly for `Fn`.
722722
// These types seem reasonably opaque enough that they could be instantiated with their
723723
// borrowed variants in a function body when we see a move error.
724-
let borrow_level = match *ty.kind() {
724+
let borrow_level = match ty.kind() {
725725
ty::Param(_) => tcx
726726
.explicit_predicates_of(self.mir_def_id().to_def_id())
727727
.predicates
@@ -1210,7 +1210,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12101210
} else if let ty::Param(param) = ty.kind()
12111211
&& let Some(_clone_trait_def) = self.infcx.tcx.lang_items().clone_trait()
12121212
&& let generics = self.infcx.tcx.generics_of(self.mir_def_id())
1213-
&& let generic_param = generics.type_param(*param, self.infcx.tcx)
1213+
&& let generic_param = generics.type_param(param, self.infcx.tcx)
12141214
&& let param_span = self.infcx.tcx.def_span(generic_param.def_id)
12151215
&& if let Some(UseSpans::FnSelfUse { kind, .. }) = use_spans
12161216
&& let CallKind::FnCall { fn_trait_id, self_ty } = kind
@@ -1358,7 +1358,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13581358
.into_iter()
13591359
.map(|err| match err.obligation.predicate.kind().skip_binder() {
13601360
PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
1361-
match *predicate.self_ty().kind() {
1361+
match predicate.self_ty().kind() {
13621362
ty::Param(param_ty) => Ok((
13631363
generics.type_param(param_ty, tcx),
13641364
predicate.trait_ref.print_trait_sugared().to_string(),

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+27-30
Original file line numberDiff line numberDiff line change
@@ -317,40 +317,37 @@ impl<'tcx> BorrowExplanation<'tcx> {
317317
let mut has_dyn = false;
318318
let mut failed = false;
319319

320-
let elaborated_args =
321-
std::iter::zip(*args, &generics.own_params).map(|(arg, param)| {
322-
if let Some(ty::Dynamic(obj, _, ty::Dyn)) = arg.as_type().map(Ty::kind) {
323-
let default = tcx.object_lifetime_default(param.def_id);
324-
325-
let re_static = tcx.lifetimes.re_static;
326-
327-
let implied_region = match default {
328-
// This is not entirely precise.
329-
ObjectLifetimeDefault::Empty => re_static,
330-
ObjectLifetimeDefault::Ambiguous => {
320+
let elaborated_args = std::iter::zip(args, &generics.own_params).map(|(arg, param)| {
321+
if let Some(ty::Dynamic(obj, _, ty::Dyn)) = arg.as_type().map(Ty::kind) {
322+
let default = tcx.object_lifetime_default(param.def_id);
323+
324+
let re_static = tcx.lifetimes.re_static;
325+
326+
let implied_region = match default {
327+
// This is not entirely precise.
328+
ObjectLifetimeDefault::Empty => re_static,
329+
ObjectLifetimeDefault::Ambiguous => {
330+
failed = true;
331+
re_static
332+
}
333+
ObjectLifetimeDefault::Param(param_def_id) => {
334+
let index = generics.param_def_id_to_index[&param_def_id] as usize;
335+
args.get(index).and_then(|arg| arg.as_region()).unwrap_or_else(|| {
331336
failed = true;
332337
re_static
333-
}
334-
ObjectLifetimeDefault::Param(param_def_id) => {
335-
let index = generics.param_def_id_to_index[&param_def_id] as usize;
336-
args.get(index).and_then(|arg| arg.as_region()).unwrap_or_else(
337-
|| {
338-
failed = true;
339-
re_static
340-
},
341-
)
342-
}
343-
ObjectLifetimeDefault::Static => re_static,
344-
};
338+
})
339+
}
340+
ObjectLifetimeDefault::Static => re_static,
341+
};
345342

346-
has_dyn = true;
343+
has_dyn = true;
347344

348-
Ty::new_dynamic(tcx, obj, implied_region, ty::Dyn).into()
349-
} else {
350-
arg
351-
}
352-
});
353-
let elaborated_ty = Ty::new_adt(tcx, *def, tcx.mk_args_from_iter(elaborated_args));
345+
Ty::new_dynamic(tcx, obj, implied_region, ty::Dyn).into()
346+
} else {
347+
arg
348+
}
349+
});
350+
let elaborated_ty = Ty::new_adt(tcx, def, tcx.mk_args_from_iter(elaborated_args));
354351

355352
if has_dyn && !failed {
356353
err.note(format!(

compiler/rustc_borrowck/src/diagnostics/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
114114
..
115115
} = &terminator.kind
116116
{
117-
if let ty::FnDef(id, _) = *const_.ty().kind() {
117+
if let ty::FnDef(id, _) = const_.ty().kind() {
118118
debug!("add_moved_or_invoked_closure_note: id={:?}", id);
119119
if Some(self.infcx.tcx.parent(id)) == self.infcx.tcx.lang_items().fn_once_trait() {
120120
let closure = match args.first() {
@@ -356,7 +356,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
356356
// If the type is a box, the field is described from the boxed type
357357
self.describe_field_from_ty(ty.boxed_ty(), field, variant_index, including_tuple_field)
358358
} else {
359-
match *ty.kind() {
359+
match ty.kind() {
360360
ty::Adt(def, _) => {
361361
let variant = if let Some(idx) = variant_index {
362362
assert!(def.is_enum());
@@ -467,7 +467,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
467467
// this by hooking into the pretty printer and telling it to label the
468468
// lifetimes without names with the value `'0`.
469469
if let ty::Ref(region, ..) = ty.kind() {
470-
match **region {
470+
match *region {
471471
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
472472
| ty::RePlaceholder(ty::PlaceholderRegion {
473473
bound: ty::BoundRegion { kind: br, .. },
@@ -487,7 +487,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
487487
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, Namespace::TypeNS);
488488

489489
let region = if let ty::Ref(region, ..) = ty.kind() {
490-
match **region {
490+
match *region {
491491
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
492492
| ty::RePlaceholder(ty::PlaceholderRegion {
493493
bound: ty::BoundRegion { kind: br, .. },
@@ -763,7 +763,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
763763
}
764764

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

@@ -1073,7 +1073,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10731073
// LL | blk();
10741074
// | ----- this value implements `FnOnce`, which causes it to be moved when called
10751075
// ```
1076-
if let ty::Param(param_ty) = *self_ty.kind()
1076+
if let ty::Param(param_ty) = self_ty.kind()
10771077
&& let generics = self.infcx.tcx.generics_of(self.mir_def_id())
10781078
&& let param = generics.type_param(param_ty, self.infcx.tcx)
10791079
&& let Some(hir_generics) = self

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
377377
if suggest {
378378
self.construct_mut_suggestion_for_local_binding_patterns(&mut err, local);
379379
let tcx = self.infcx.tcx;
380-
if let ty::Closure(id, _) = *the_place_err.ty(self.body, tcx).ty.kind() {
380+
if let ty::Closure(id, _) = the_place_err.ty(self.body, tcx).ty.kind() {
381381
self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
382382
}
383383
}
@@ -431,7 +431,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
431431

432432
let tcx = self.infcx.tcx;
433433
if let ty::Ref(_, ty, Mutability::Mut) = the_place_err.ty(self.body, tcx).ty.kind()
434-
&& let ty::Closure(id, _) = *ty.kind()
434+
&& let ty::Closure(id, _) = ty.kind()
435435
{
436436
self.show_mutating_upvar(tcx, id.expect_local(), the_place_err, &mut err);
437437
}
@@ -950,7 +950,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
950950
if let Some(ty::FnDef(def_id, _)) =
951951
tables.node_type_opt(func.hir_id).as_ref().map(|ty| ty.kind())
952952
{
953-
let arg = match hir.get_if_local(*def_id) {
953+
let arg = match hir.get_if_local(def_id) {
954954
Some(
955955
hir::Node::Item(hir::Item {
956956
ident, kind: hir::ItemKind::Fn(sig, ..), ..

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
509509
ty::VarianceDiagInfo::Invariant { ty, param_index } => {
510510
let (desc, note) = match ty.kind() {
511511
ty::RawPtr(ty, mutbl) => {
512-
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
512+
assert_eq!(mutbl, rustc_hir::Mutability::Mut);
513513
(
514514
format!("a mutable pointer to `{}`", ty),
515515
"mutable pointers are invariant over their type parameter".to_string(),
516516
)
517517
}
518518
ty::Ref(_, inner_ty, mutbl) => {
519-
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
519+
assert_eq!(mutbl, rustc_hir::Mutability::Mut);
520520
(
521521
format!("a mutable reference to `{inner_ty}`"),
522522
"mutable references are invariant over their type parameter"
@@ -527,7 +527,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
527527
let generic_arg = args[param_index as usize];
528528
let identity_args =
529529
GenericArgs::identity_for_item(self.infcx.tcx, adt.did());
530-
let base_ty = Ty::new_adt(self.infcx.tcx, *adt, identity_args);
530+
let base_ty = Ty::new_adt(self.infcx.tcx, adt, identity_args);
531531
let base_generic_arg = identity_args[param_index as usize];
532532
let adt_desc = adt.descr();
533533

@@ -540,8 +540,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
540540
(desc, note)
541541
}
542542
ty::FnDef(def_id, _) => {
543-
let name = self.infcx.tcx.item_name(*def_id);
544-
let identity_args = GenericArgs::identity_for_item(self.infcx.tcx, *def_id);
543+
let name = self.infcx.tcx.item_name(def_id);
544+
let identity_args = GenericArgs::identity_for_item(self.infcx.tcx, def_id);
545545
let desc = format!("a function pointer to `{name}`");
546546
let note = format!(
547547
"the function `{name}` is invariant over the parameter `{}`",
@@ -593,7 +593,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
593593
let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
594594

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

@@ -602,7 +602,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
602602
let err = FnMutError {
603603
span: *span,
604604
ty_err: match output_ty.kind() {
605-
ty::Coroutine(def, ..) if self.infcx.tcx.coroutine_is_async(*def) => {
605+
ty::Coroutine(def, ..) if self.infcx.tcx.coroutine_is_async(def) => {
606606
FnMutReturnTypeErr::ReturnAsyncBlock { span: *span }
607607
}
608608
_ if output_ty.contains_closure() => {
@@ -952,7 +952,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
952952
if let Ok(Some(instance)) = ty::Instance::resolve(
953953
tcx,
954954
self.param_env,
955-
*fn_did,
955+
fn_did,
956956
self.infcx.resolve_vars_if_possible(args),
957957
) {
958958
instance
@@ -1070,8 +1070,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10701070
else {
10711071
return;
10721072
};
1073-
let ty::Closure(_, args) = *tcx.type_of(closure_def_id).instantiate_identity().kind()
1074-
else {
1073+
let ty::Closure(_, args) = tcx.type_of(closure_def_id).instantiate_identity().kind() else {
10751074
return;
10761075
};
10771076
let args = args.as_closure();
@@ -1092,7 +1091,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10921091
let liberated_sig = tcx.liberate_late_bound_regions(closure_def_id.to_def_id(), args.sig());
10931092
let mut peeled_ty = liberated_sig.output();
10941093
let mut count = 0;
1095-
while let ty::Ref(_, ref_ty, _) = *peeled_ty.kind() {
1094+
while let ty::Ref(_, ref_ty, _) = peeled_ty.kind() {
10961095
peeled_ty = ref_ty;
10971096
count += 1;
10981097
}

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
519519
}
520520

521521
// Otherwise, let's descend into the referent types.
522-
search_stack.push((*referent_ty, &referent_hir_ty.ty));
522+
search_stack.push((referent_ty, &referent_hir_ty.ty));
523523
}
524524

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

555555
(ty::Slice(elem_ty), hir::TyKind::Slice(elem_hir_ty))
556556
| (ty::Array(elem_ty, _), hir::TyKind::Array(elem_hir_ty, _)) => {
557-
search_stack.push((*elem_ty, elem_hir_ty));
557+
search_stack.push((elem_ty, elem_hir_ty));
558558
}
559559

560560
(ty::RawPtr(mut_ty, _), hir::TyKind::Ptr(mut_hir_ty)) => {
561-
search_stack.push((*mut_ty, &mut_hir_ty.ty));
561+
search_stack.push((mut_ty, &mut_hir_ty.ty));
562562
}
563563

564564
_ => {

compiler/rustc_borrowck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn do_mir_borrowck<'tcx>(
268268
// The first argument is the coroutine type passed by value
269269
if let Some(local) = body.local_decls.raw.get(1)
270270
// Get the interior types and args which typeck computed
271-
&& let ty::Coroutine(def_id, _) = *local.ty.kind()
271+
&& let ty::Coroutine(def_id, _) = local.ty.kind()
272272
&& tcx.coroutine_movability(def_id) == hir::Movability::Movable
273273
{
274274
true

compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
11271127
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
11281128
use ty::TypeSuperFoldable as _;
11291129
let tcx = self.tcx;
1130-
let &ty::Alias(ty::Opaque, ty::AliasTy { args, def_id, .. }) = t.kind() else {
1130+
let ty::Alias(ty::Opaque, ty::AliasTy { args, def_id, .. }) = t.kind() else {
11311131
return t.super_fold_with(self);
11321132
};
11331133
let args = std::iter::zip(args, tcx.variances_of(def_id)).map(|(arg, v)| {

0 commit comments

Comments
 (0)