Skip to content

Commit 994e2e4

Browse files
authored
Rollup merge of #113824 - lcnr:exhaustive-match, r=wesleywiser
a small `fn needs_drop` refactor I am generally a fan of exhaustively matching on `TyKind` once we care about more than 1 variant
2 parents 1b07da1 + d1b4b45 commit 994e2e4

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

compiler/rustc_middle/src/ty/util.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_index::bit_set::GrowableBitSet;
1919
use rustc_macros::HashStable;
2020
use rustc_session::Limit;
2121
use rustc_span::sym;
22-
use rustc_target::abi::{Integer, IntegerType, Size, TargetDataLayout};
22+
use rustc_target::abi::{Integer, IntegerType, Size};
2323
use rustc_target::spec::abi::Abi;
2424
use smallvec::SmallVec;
2525
use std::{fmt, iter};
@@ -1085,7 +1085,7 @@ impl<'tcx> Ty<'tcx> {
10851085
#[inline]
10861086
pub fn needs_drop(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
10871087
// Avoid querying in simple cases.
1088-
match needs_drop_components(self, &tcx.data_layout) {
1088+
match needs_drop_components(tcx, self) {
10891089
Err(AlwaysRequiresDrop) => true,
10901090
Ok(components) => {
10911091
let query_ty = match *components {
@@ -1118,7 +1118,7 @@ impl<'tcx> Ty<'tcx> {
11181118
#[inline]
11191119
pub fn has_significant_drop(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
11201120
// Avoid querying in simple cases.
1121-
match needs_drop_components(self, &tcx.data_layout) {
1121+
match needs_drop_components(tcx, self) {
11221122
Err(AlwaysRequiresDrop) => true,
11231123
Ok(components) => {
11241124
let query_ty = match *components {
@@ -1278,10 +1278,10 @@ impl<'tcx> ExplicitSelf<'tcx> {
12781278
/// *any* of the returned types need drop. Returns `Err(AlwaysRequiresDrop)` if
12791279
/// this type always needs drop.
12801280
pub fn needs_drop_components<'tcx>(
1281+
tcx: TyCtxt<'tcx>,
12811282
ty: Ty<'tcx>,
1282-
target_layout: &TargetDataLayout,
12831283
) -> Result<SmallVec<[Ty<'tcx>; 2]>, AlwaysRequiresDrop> {
1284-
match ty.kind() {
1284+
match *ty.kind() {
12851285
ty::Infer(ty::FreshIntTy(_))
12861286
| ty::Infer(ty::FreshFloatTy(_))
12871287
| ty::Bool
@@ -1303,11 +1303,11 @@ pub fn needs_drop_components<'tcx>(
13031303

13041304
ty::Dynamic(..) | ty::Error(_) => Err(AlwaysRequiresDrop),
13051305

1306-
ty::Slice(ty) => needs_drop_components(*ty, target_layout),
1306+
ty::Slice(ty) => needs_drop_components(tcx, ty),
13071307
ty::Array(elem_ty, size) => {
1308-
match needs_drop_components(*elem_ty, target_layout) {
1308+
match needs_drop_components(tcx, elem_ty) {
13091309
Ok(v) if v.is_empty() => Ok(v),
1310-
res => match size.try_to_bits(target_layout.pointer_size) {
1310+
res => match size.try_to_target_usize(tcx) {
13111311
// Arrays of size zero don't need drop, even if their element
13121312
// type does.
13131313
Some(0) => Ok(SmallVec::new()),
@@ -1321,7 +1321,7 @@ pub fn needs_drop_components<'tcx>(
13211321
}
13221322
// If any field needs drop, then the whole tuple does.
13231323
ty::Tuple(fields) => fields.iter().try_fold(SmallVec::new(), move |mut acc, elem| {
1324-
acc.extend(needs_drop_components(elem, target_layout)?);
1324+
acc.extend(needs_drop_components(tcx, elem)?);
13251325
Ok(acc)
13261326
}),
13271327

compiler/rustc_ty_utils/src/needs_drop.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ where
9696
return Some(Err(AlwaysRequiresDrop));
9797
}
9898

99-
let components = match needs_drop_components(ty, &tcx.data_layout) {
99+
let components = match needs_drop_components(tcx, ty) {
100100
Err(e) => return Some(Err(e)),
101101
Ok(components) => components,
102102
};
@@ -160,7 +160,7 @@ where
160160
queue_type(self, required);
161161
}
162162
}
163-
ty::Array(..) | ty::Alias(..) | ty::Param(_) => {
163+
ty::Alias(..) | ty::Array(..) | ty::Placeholder(_) | ty::Param(_) => {
164164
if ty == component {
165165
// Return the type to the caller: they may be able
166166
// to normalize further than we can.
@@ -172,7 +172,31 @@ where
172172
queue_type(self, component);
173173
}
174174
}
175-
_ => return Some(Err(AlwaysRequiresDrop)),
175+
176+
ty::Foreign(_) | ty::Dynamic(..) => {
177+
return Some(Err(AlwaysRequiresDrop));
178+
}
179+
180+
ty::Bool
181+
| ty::Char
182+
| ty::Int(_)
183+
| ty::Uint(_)
184+
| ty::Float(_)
185+
| ty::Str
186+
| ty::Slice(_)
187+
| ty::Ref(..)
188+
| ty::RawPtr(..)
189+
| ty::FnDef(..)
190+
| ty::FnPtr(..)
191+
| ty::Tuple(_)
192+
| ty::Bound(..)
193+
| ty::GeneratorWitness(..)
194+
| ty::GeneratorWitnessMIR(..)
195+
| ty::Never
196+
| ty::Infer(_)
197+
| ty::Error(_) => {
198+
bug!("unexpected type returned by `needs_drop_components`: {component}")
199+
}
176200
}
177201
}
178202
}

0 commit comments

Comments
 (0)