Skip to content

Commit 342c2c0

Browse files
committed
Auto merge of #126069 - nnethercote:Ty-kind-ret-ty, r=<try>
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. r? `@ghost`
2 parents fbce03b + bd3667d commit 342c2c0

File tree

314 files changed

+1077
-1112
lines changed

Some content is hidden

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

314 files changed

+1077
-1112
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
435435
&& let hir::ExprKind::Call(call, args) = parent_expr.kind
436436
&& let ty::FnDef(def_id, _) = typeck.node_type(call.hir_id).kind()
437437
{
438-
(Some(*def_id), args, 0)
438+
(Some(def_id), args, 0)
439439
} else {
440440
(None, &[][..], 0)
441441
};
@@ -498,7 +498,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
498498
.contains(&Some(predicate.def_id()))
499499
&& let ty::Param(param) = predicate.self_ty().kind()
500500
&& let generics = self.infcx.tcx.generics_of(def_id)
501-
&& let param = generics.type_param(*param, self.infcx.tcx)
501+
&& let param = generics.type_param(param, self.infcx.tcx)
502502
&& param.def_id == param_def_id
503503
{
504504
if [
@@ -870,7 +870,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
870870
// borrowing the type, since `&mut F: FnMut` iff `F: FnMut` and similarly for `Fn`.
871871
// These types seem reasonably opaque enough that they could be instantiated with their
872872
// borrowed variants in a function body when we see a move error.
873-
let borrow_level = match *ty.kind() {
873+
let borrow_level = match ty.kind() {
874874
ty::Param(_) => tcx
875875
.explicit_predicates_of(self.mir_def_id().to_def_id())
876876
.predicates
@@ -1263,7 +1263,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
12631263
} else if let ty::Param(param) = ty.kind()
12641264
&& let Some(_clone_trait_def) = self.infcx.tcx.lang_items().clone_trait()
12651265
&& let generics = self.infcx.tcx.generics_of(self.mir_def_id())
1266-
&& let generic_param = generics.type_param(*param, self.infcx.tcx)
1266+
&& let generic_param = generics.type_param(param, self.infcx.tcx)
12671267
&& let param_span = self.infcx.tcx.def_span(generic_param.def_id)
12681268
&& if let Some(UseSpans::FnSelfUse { kind, .. }) = use_spans
12691269
&& let CallKind::FnCall { fn_trait_id, self_ty } = kind
@@ -1436,7 +1436,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
14361436
.into_iter()
14371437
.map(|err| match err.obligation.predicate.kind().skip_binder() {
14381438
PredicateKind::Clause(ty::ClauseKind::Trait(predicate)) => {
1439-
match *predicate.self_ty().kind() {
1439+
match predicate.self_ty().kind() {
14401440
ty::Param(param_ty) => Ok((
14411441
generics.type_param(param_ty, tcx),
14421442
predicate.trait_ref.print_trait_sugared().to_string(),

Diff for: 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!(

Diff for: compiler/rustc_borrowck/src/diagnostics/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
112112
..
113113
} = &terminator.kind
114114
{
115-
if let ty::FnDef(id, _) = *const_.ty().kind() {
115+
if let ty::FnDef(id, _) = const_.ty().kind() {
116116
debug!("add_moved_or_invoked_closure_note: id={:?}", id);
117117
if self.infcx.tcx.is_lang_item(self.infcx.tcx.parent(id), LangItem::FnOnce) {
118118
let closure = match args.first() {
@@ -348,7 +348,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
348348
// If the type is a box, the field is described from the boxed type
349349
self.describe_field_from_ty(ty.boxed_ty(), field, variant_index, including_tuple_field)
350350
} else {
351-
match *ty.kind() {
351+
match ty.kind() {
352352
ty::Adt(def, _) => {
353353
let variant = if let Some(idx) = variant_index {
354354
assert!(def.is_enum());
@@ -459,7 +459,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
459459
// this by hooking into the pretty printer and telling it to label the
460460
// lifetimes without names with the value `'0`.
461461
if let ty::Ref(region, ..) = ty.kind() {
462-
match **region {
462+
match *region {
463463
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
464464
| ty::RePlaceholder(ty::PlaceholderRegion {
465465
bound: ty::BoundRegion { kind: br, .. },
@@ -479,7 +479,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
479479
let mut printer = ty::print::FmtPrinter::new(self.infcx.tcx, Namespace::TypeNS);
480480

481481
let region = if let ty::Ref(region, ..) = ty.kind() {
482-
match **region {
482+
match *region {
483483
ty::ReBound(_, ty::BoundRegion { kind: br, .. })
484484
| ty::RePlaceholder(ty::PlaceholderRegion {
485485
bound: ty::BoundRegion { kind: br, .. },
@@ -740,7 +740,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
740740
}
741741

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

@@ -1048,7 +1048,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
10481048
// LL | blk();
10491049
// | ----- this value implements `FnOnce`, which causes it to be moved when called
10501050
// ```
1051-
if let ty::Param(param_ty) = *self_ty.kind()
1051+
if let ty::Param(param_ty) = self_ty.kind()
10521052
&& let generics = self.infcx.tcx.generics_of(self.mir_def_id())
10531053
&& let param = generics.type_param(param_ty, self.infcx.tcx)
10541054
&& let Some(hir_generics) = self

Diff for: compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, '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<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, '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
}
@@ -953,7 +953,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
953953
if let Some(ty::FnDef(def_id, _)) =
954954
typeck_results.node_type_opt(expr.hir_id).as_ref().map(|ty| ty.kind())
955955
{
956-
Some((*def_id, expr.span, *args))
956+
Some((def_id, expr.span, *args))
957957
} else {
958958
None
959959
}

Diff for: compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -507,14 +507,14 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
507507
ty::VarianceDiagInfo::Invariant { ty, param_index } => {
508508
let (desc, note) = match ty.kind() {
509509
ty::RawPtr(ty, mutbl) => {
510-
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
510+
assert_eq!(mutbl, rustc_hir::Mutability::Mut);
511511
(
512512
format!("a mutable pointer to `{}`", ty),
513513
"mutable pointers are invariant over their type parameter".to_string(),
514514
)
515515
}
516516
ty::Ref(_, inner_ty, mutbl) => {
517-
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
517+
assert_eq!(mutbl, rustc_hir::Mutability::Mut);
518518
(
519519
format!("a mutable reference to `{inner_ty}`"),
520520
"mutable references are invariant over their type parameter"
@@ -525,7 +525,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
525525
let generic_arg = args[param_index as usize];
526526
let identity_args =
527527
GenericArgs::identity_for_item(self.infcx.tcx, adt.did());
528-
let base_ty = Ty::new_adt(self.infcx.tcx, *adt, identity_args);
528+
let base_ty = Ty::new_adt(self.infcx.tcx, adt, identity_args);
529529
let base_generic_arg = identity_args[param_index as usize];
530530
let adt_desc = adt.descr();
531531

@@ -538,8 +538,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
538538
(desc, note)
539539
}
540540
ty::FnDef(def_id, _) => {
541-
let name = self.infcx.tcx.item_name(*def_id);
542-
let identity_args = GenericArgs::identity_for_item(self.infcx.tcx, *def_id);
541+
let name = self.infcx.tcx.item_name(def_id);
542+
let identity_args = GenericArgs::identity_for_item(self.infcx.tcx, def_id);
543543
let desc = format!("a function pointer to `{name}`");
544544
let note = format!(
545545
"the function `{name}` is invariant over the parameter `{}`",
@@ -591,7 +591,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
591591
let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
592592

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

@@ -600,7 +600,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
600600
let err = FnMutError {
601601
span: *span,
602602
ty_err: match output_ty.kind() {
603-
ty::Coroutine(def, ..) if self.infcx.tcx.coroutine_is_async(*def) => {
603+
ty::Coroutine(def, ..) if self.infcx.tcx.coroutine_is_async(def) => {
604604
FnMutReturnTypeErr::ReturnAsyncBlock { span: *span }
605605
}
606606
_ if output_ty.contains_closure() => {
@@ -949,7 +949,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
949949
if let Ok(Some(instance)) = ty::Instance::try_resolve(
950950
tcx,
951951
self.param_env,
952-
*fn_did,
952+
fn_did,
953953
self.infcx.resolve_vars_if_possible(args),
954954
) {
955955
instance
@@ -1061,8 +1061,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
10611061
else {
10621062
return;
10631063
};
1064-
let ty::Closure(_, args) = *tcx.type_of(closure_def_id).instantiate_identity().kind()
1065-
else {
1064+
let ty::Closure(_, args) = tcx.type_of(closure_def_id).instantiate_identity().kind() else {
10661065
return;
10671066
};
10681067
let args = args.as_closure();
@@ -1083,7 +1082,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
10831082
let liberated_sig = tcx.liberate_late_bound_regions(closure_def_id.to_def_id(), args.sig());
10841083
let mut peeled_ty = liberated_sig.output();
10851084
let mut count = 0;
1086-
while let ty::Ref(_, ref_ty, _) = *peeled_ty.kind() {
1085+
while let ty::Ref(_, ref_ty, _) = peeled_ty.kind() {
10871086
peeled_ty = ref_ty;
10881087
count += 1;
10891088
}

Diff for: compiler/rustc_borrowck/src/diagnostics/region_name.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
523523
}
524524

525525
// Otherwise, let's descend into the referent types.
526-
search_stack.push((*referent_ty, referent_hir_ty.ty));
526+
search_stack.push((referent_ty, referent_hir_ty.ty));
527527
}
528528

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

559559
(ty::Slice(elem_ty), hir::TyKind::Slice(elem_hir_ty))
560560
| (ty::Array(elem_ty, _), hir::TyKind::Array(elem_hir_ty, _)) => {
561-
search_stack.push((*elem_ty, elem_hir_ty));
561+
search_stack.push((elem_ty, elem_hir_ty));
562562
}
563563

564564
(ty::RawPtr(mut_ty, _), hir::TyKind::Ptr(mut_hir_ty)) => {
565-
search_stack.push((*mut_ty, mut_hir_ty.ty));
565+
search_stack.push((mut_ty, mut_hir_ty.ty));
566566
}
567567

568568
_ => {

Diff for: compiler/rustc_borrowck/src/lib.rs

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

Diff for: compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
10671067
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
10681068
use ty::TypeSuperFoldable as _;
10691069
let tcx = self.tcx;
1070-
let &ty::Alias(ty::Opaque, ty::AliasTy { args, def_id, .. }) = t.kind() else {
1070+
let ty::Alias(ty::Opaque, ty::AliasTy { args, def_id, .. }) = t.kind() else {
10711071
return t.super_fold_with(self);
10721072
};
10731073
let args = std::iter::zip(args, tcx.variances_of(def_id)).map(|(arg, v)| {

0 commit comments

Comments
 (0)