Skip to content

Commit 80c0b7e

Browse files
committed
Use non-exhaustive matches for TyKind
Also no longer export noop async_drop_in_place_raw
1 parent 24a24ec commit 80c0b7e

File tree

13 files changed

+150
-182
lines changed

13 files changed

+150
-182
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ fn exported_symbols_provider_local(
364364
));
365365
}
366366
MonoItem::Fn(Instance {
367-
def: InstanceDef::AsyncDropGlueCtorShim(def_id, ty),
367+
def: InstanceDef::AsyncDropGlueCtorShim(def_id, Some(ty)),
368368
args,
369369
}) => {
370370
// A little sanity-check

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn dump_path<'tcx>(
187187
}));
188188
s
189189
}
190-
ty::InstanceDef::AsyncDropGlueCtorShim(_, ty) => {
190+
ty::InstanceDef::AsyncDropGlueCtorShim(_, Some(ty)) => {
191191
// Unfortunately, pretty-printed typed are not very filename-friendly.
192192
// We dome some filtering.
193193
let mut s = ".".to_owned();

compiler/rustc_middle/src/mir/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,14 @@ macro_rules! make_mir_visitor {
350350
receiver_by_ref: _,
351351
} |
352352
ty::InstanceDef::CoroutineKindShim { coroutine_def_id: _def_id } |
353+
ty::InstanceDef::AsyncDropGlueCtorShim(_def_id, None) |
353354
ty::InstanceDef::DropGlue(_def_id, None) => {}
354355

355356
ty::InstanceDef::FnPtrShim(_def_id, ty) |
356357
ty::InstanceDef::DropGlue(_def_id, Some(ty)) |
357358
ty::InstanceDef::CloneShim(_def_id, ty) |
358359
ty::InstanceDef::FnPtrAddrShim(_def_id, ty) |
359-
ty::InstanceDef::AsyncDropGlueCtorShim(_def_id, ty) => {
360+
ty::InstanceDef::AsyncDropGlueCtorShim(_def_id, Some(ty)) => {
360361
// FIXME(eddyb) use a better `TyContext` here.
361362
self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));
362363
}

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub enum InstanceDef<'tcx> {
173173
///
174174
/// The `DefId` is for `core::future::async_drop::async_drop_in_place`, the `Ty`
175175
/// is the type `T`.
176-
AsyncDropGlueCtorShim(DefId, Ty<'tcx>),
176+
AsyncDropGlueCtorShim(DefId, Option<Ty<'tcx>>),
177177
}
178178

179179
impl<'tcx> Instance<'tcx> {
@@ -406,7 +406,8 @@ fn fmt_instance(
406406
InstanceDef::DropGlue(_, Some(ty)) => write!(f, " - shim(Some({ty}))"),
407407
InstanceDef::CloneShim(_, ty) => write!(f, " - shim({ty})"),
408408
InstanceDef::FnPtrAddrShim(_, ty) => write!(f, " - shim({ty})"),
409-
InstanceDef::AsyncDropGlueCtorShim(_, ty) => write!(f, " - shim({ty})"),
409+
InstanceDef::AsyncDropGlueCtorShim(_, None) => write!(f, " - shim(None)"),
410+
InstanceDef::AsyncDropGlueCtorShim(_, Some(ty)) => write!(f, " - shim(Some({ty}))"),
410411
}
411412
}
412413

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,6 +2319,10 @@ impl<'tcx> Ty<'tcx> {
23192319

23202320
/// Returns the type of the async destructor of this type.
23212321
pub fn async_destructor_ty(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Ty<'tcx> {
2322+
if self.is_async_destructor_noop(tcx, param_env) || matches!(self.kind(), ty::Error(_)) {
2323+
return Ty::async_destructor_combinator(tcx, LangItem::AsyncDropNoop)
2324+
.instantiate_identity();
2325+
}
23222326
match *self.kind() {
23232327
ty::Param(_) | ty::Alias(..) | ty::Infer(ty::TyVar(_)) => {
23242328
let assoc_items = tcx
@@ -2333,9 +2337,6 @@ impl<'tcx> Ty<'tcx> {
23332337
.instantiate(tcx, &[dtor.into()])
23342338
}
23352339

2336-
ty::Adt(adt_def, _) if adt_def.is_manually_drop() => {
2337-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropNoop).instantiate_identity()
2338-
}
23392340
ty::Adt(adt_def, args) if adt_def.is_enum() || adt_def.is_struct() => self
23402341
.adt_async_destructor_ty(
23412342
tcx,
@@ -2357,34 +2358,10 @@ impl<'tcx> Ty<'tcx> {
23572358
ty::Adt(adt_def, _) => {
23582359
assert!(adt_def.is_union());
23592360

2360-
match self.surface_async_dropper_ty(tcx, param_env) {
2361-
None => Ty::async_destructor_combinator(tcx, LangItem::AsyncDropNoop)
2362-
.instantiate_identity(),
2363-
Some(surface_drop) => {
2364-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
2365-
.instantiate(tcx, &[surface_drop.into()])
2366-
}
2367-
}
2368-
}
2369-
2370-
ty::Never
2371-
| ty::Bool
2372-
| ty::Char
2373-
| ty::Int(_)
2374-
| ty::Uint(_)
2375-
| ty::Float(_)
2376-
| ty::Str
2377-
| ty::RawPtr(_, _)
2378-
| ty::Ref(..)
2379-
| ty::FnDef(..)
2380-
| ty::FnPtr(..)
2381-
| ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
2382-
| ty::Error(_) => {
2383-
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropNoop).instantiate_identity()
2384-
}
2361+
let surface_drop = self.surface_async_dropper_ty(tcx, param_env).unwrap();
23852362

2386-
ty::Dynamic(..) | ty::CoroutineWitness(..) | ty::Coroutine(..) | ty::Pat(..) => {
2387-
bug!("`async_destructor_ty` is not yet implemented for type: {self:?}")
2363+
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropFuse)
2364+
.instantiate(tcx, &[surface_drop.into()])
23882365
}
23892366

23902367
ty::Bound(..)
@@ -2393,6 +2370,8 @@ impl<'tcx> Ty<'tcx> {
23932370
| ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
23942371
bug!("`async_destructor_ty` applied to unexpected type: {self:?}")
23952372
}
2373+
2374+
_ => bug!("`async_destructor_ty` is not yet implemented for type: {self:?}"),
23962375
}
23972376
}
23982377

@@ -2406,6 +2385,8 @@ impl<'tcx> Ty<'tcx> {
24062385
I: Iterator + ExactSizeIterator,
24072386
I::Item: IntoIterator<Item = Ty<'tcx>>,
24082387
{
2388+
debug_assert!(!self.is_async_destructor_noop(tcx, param_env));
2389+
24092390
let defer = Ty::async_destructor_combinator(tcx, LangItem::AsyncDropDefer);
24102391
let chain = Ty::async_destructor_combinator(tcx, LangItem::AsyncDropChain);
24112392

@@ -2425,7 +2406,7 @@ impl<'tcx> Ty<'tcx> {
24252406
.reduce(|other, matched| {
24262407
either.instantiate(tcx, &[other.into(), matched.into(), self.into()])
24272408
})
2428-
.unwrap_or(noop);
2409+
.unwrap();
24292410

24302411
let dtor = if let Some(dropper_ty) = self.surface_async_dropper_ty(tcx, param_env) {
24312412
Ty::async_destructor_combinator(tcx, LangItem::AsyncDropChain)

compiler/rustc_middle/src/ty/util.rs

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,8 +1306,7 @@ impl<'tcx> Ty<'tcx> {
13061306
/// Checks whether values of this type `T` implements the `AsyncDrop`
13071307
/// trait.
13081308
pub fn has_surface_async_drop(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
1309-
self.trivially_has_surface_async_drop()
1310-
&& tcx.has_surface_async_drop_raw(param_env.and(self))
1309+
self.could_have_surface_async_drop() && tcx.has_surface_async_drop_raw(param_env.and(self))
13111310
}
13121311

13131312
/// Fast path helper for testing if a type has `AsyncDrop`
@@ -1316,52 +1315,68 @@ impl<'tcx> Ty<'tcx> {
13161315
/// Returning `false` means the type is known to not have `AsyncDrop`
13171316
/// implementation. Returning `true` means nothing -- could be
13181317
/// `AsyncDrop`, might not be.
1319-
fn trivially_has_surface_async_drop(self) -> bool {
1320-
match self.kind() {
1321-
ty::Int(_)
1322-
| ty::Uint(_)
1323-
| ty::Float(_)
1324-
| ty::Bool
1325-
| ty::Char
1326-
| ty::Str
1327-
| ty::Never
1328-
| ty::Ref(..)
1329-
| ty::RawPtr(_, _)
1330-
| ty::FnDef(..)
1331-
| ty::FnPtr(_)
1332-
| ty::Error(_)
1333-
| ty::Tuple(_)
1334-
| ty::Slice(_)
1335-
| ty::Array(_, _)
1336-
| ty::Closure(..)
1337-
| ty::CoroutineClosure(..)
1338-
| ty::Coroutine(..)
1339-
| ty::CoroutineWitness(..)
1340-
| ty::Pat(..) => false,
1341-
ty::Adt(..)
1342-
| ty::Bound(..)
1343-
| ty::Dynamic(..)
1344-
| ty::Foreign(_)
1345-
| ty::Infer(_)
1346-
| ty::Alias(..)
1347-
| ty::Param(_)
1348-
| ty::Placeholder(_) => true,
1349-
}
1318+
fn could_have_surface_async_drop(self) -> bool {
1319+
!self.is_async_destructor_trivially_noop()
1320+
&& !matches!(
1321+
self.kind(),
1322+
ty::Tuple(_)
1323+
| ty::Slice(_)
1324+
| ty::Array(_, _)
1325+
| ty::Closure(..)
1326+
| ty::CoroutineClosure(..)
1327+
| ty::Coroutine(..)
1328+
)
13501329
}
13511330

13521331
/// Checks whether values of this type `T` implements the `AsyncDrop`
13531332
/// trait.
13541333
pub fn has_surface_drop(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
1355-
self.trivially_has_surface_drop() && tcx.has_surface_drop_raw(param_env.and(self))
1334+
self.could_have_surface_drop() && tcx.has_surface_drop_raw(param_env.and(self))
13561335
}
13571336

1358-
/// Fast path helper for testing if a type has `AsyncDrop`
1359-
/// implementation.
1337+
/// Fast path helper for testing if a type has `Drop` implementation.
13601338
///
1361-
/// Returning `false` means the type is known to not have `AsyncDrop`
1339+
/// Returning `false` means the type is known to not have `Drop`
13621340
/// implementation. Returning `true` means nothing -- could be
1363-
/// `AsyncDrop`, might not be.
1364-
fn trivially_has_surface_drop(self) -> bool {
1341+
/// `Drop`, might not be.
1342+
fn could_have_surface_drop(self) -> bool {
1343+
self.is_async_destructor_trivially_noop()
1344+
&& !matches!(
1345+
self.kind(),
1346+
ty::Tuple(_)
1347+
| ty::Slice(_)
1348+
| ty::Array(_, _)
1349+
| ty::Closure(..)
1350+
| ty::CoroutineClosure(..)
1351+
| ty::Coroutine(..)
1352+
)
1353+
}
1354+
1355+
/// Checks whether values of this type `T` implement has noop async destructor.
1356+
//
1357+
// FIXME: implement optimization to make ADTs, which do not need drop,
1358+
// to skip fields or to have noop async destructor.
1359+
pub fn is_async_destructor_noop(
1360+
self,
1361+
tcx: TyCtxt<'tcx>,
1362+
param_env: ty::ParamEnv<'tcx>,
1363+
) -> bool {
1364+
self.is_async_destructor_trivially_noop()
1365+
|| if let ty::Adt(adt_def, _) = self.kind() {
1366+
(adt_def.is_union() || adt_def.is_payloadfree())
1367+
&& !self.has_surface_async_drop(tcx, param_env)
1368+
&& !self.has_surface_drop(tcx, param_env)
1369+
} else {
1370+
false
1371+
}
1372+
}
1373+
1374+
/// Fast path helper for testing if a type has noop async destructor.
1375+
///
1376+
/// Returning `true` means the type is known to have noop async destructor
1377+
/// implementation. Returning `true` means nothing -- could be
1378+
/// `Drop`, might not be.
1379+
fn is_async_destructor_trivially_noop(self) -> bool {
13651380
match self.kind() {
13661381
ty::Int(_)
13671382
| ty::Uint(_)
@@ -1371,26 +1386,12 @@ impl<'tcx> Ty<'tcx> {
13711386
| ty::Str
13721387
| ty::Never
13731388
| ty::Ref(..)
1374-
| ty::RawPtr(_, _)
1389+
| ty::RawPtr(..)
13751390
| ty::FnDef(..)
1376-
| ty::FnPtr(_)
1377-
| ty::Error(_)
1378-
| ty::Tuple(_)
1379-
| ty::Slice(_)
1380-
| ty::Array(_, _)
1381-
| ty::Closure(..)
1382-
| ty::CoroutineClosure(..)
1383-
| ty::Coroutine(..)
1384-
| ty::CoroutineWitness(..)
1385-
| ty::Pat(..) => false,
1386-
ty::Adt(..)
1387-
| ty::Bound(..)
1388-
| ty::Dynamic(..)
1389-
| ty::Foreign(_)
1390-
| ty::Infer(_)
1391-
| ty::Alias(..)
1392-
| ty::Param(_)
1393-
| ty::Placeholder(_) => true,
1391+
| ty::FnPtr(_) => true,
1392+
ty::Tuple(tys) => tys.is_empty(),
1393+
ty::Adt(adt_def, _) => adt_def.is_manually_drop(),
1394+
_ => false,
13941395
}
13951396
}
13961397

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,8 @@ fn try_instance_mir<'tcx>(
10721072
tcx: TyCtxt<'tcx>,
10731073
instance: InstanceDef<'tcx>,
10741074
) -> Result<&'tcx Body<'tcx>, &'static str> {
1075-
if let ty::InstanceDef::DropGlue(_, Some(ty)) | ty::InstanceDef::AsyncDropGlueCtorShim(_, ty) =
1076-
instance
1075+
if let ty::InstanceDef::DropGlue(_, Some(ty))
1076+
| ty::InstanceDef::AsyncDropGlueCtorShim(_, Some(ty)) = instance
10771077
&& let ty::Adt(def, args) = ty.kind()
10781078
{
10791079
let fields = def.all_fields();

0 commit comments

Comments
 (0)