Skip to content

Commit f6e8a84

Browse files
committed
Make Ty::boxed_ty return an Option
1 parent 842d6fc commit f6e8a84

File tree

21 files changed

+58
-48
lines changed

21 files changed

+58
-48
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,10 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
662662
// `&dyn Trait`
663663
ty::Ref(_, ty, _) if ty.is_trait() => true,
664664
// `Box<dyn Trait>`
665-
_ if ty.is_box() && ty.boxed_ty().is_trait() => {
665+
_ if ty.boxed_ty().is_some_and(Ty::is_trait) => {
666666
true
667667
}
668+
668669
// `dyn Trait`
669670
_ if ty.is_trait() => true,
670671
// Anything else.

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, '_, 'infcx, 'tcx> {
345345
variant_index: Option<VariantIdx>,
346346
including_tuple_field: IncludingTupleField,
347347
) -> Option<String> {
348-
if ty.is_box() {
348+
if let Some(boxed_ty) = ty.boxed_ty() {
349349
// If the type is a box, the field is described from the boxed type
350-
self.describe_field_from_ty(ty.boxed_ty(), field, variant_index, including_tuple_field)
350+
self.describe_field_from_ty(boxed_ty, field, variant_index, including_tuple_field)
351351
} else {
352352
match *ty.kind() {
353353
ty::Adt(def, _) => {

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ pub(crate) fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) ->
456456
if def.is_box()
457457
&& args.get(1).map_or(true, |arg| cx.layout_of(arg.expect_ty()).is_1zst()) =>
458458
{
459-
build_pointer_or_reference_di_node(cx, t, t.boxed_ty(), unique_type_id)
459+
build_pointer_or_reference_di_node(cx, t, t.expect_boxed_ty(), unique_type_id)
460460
}
461461
ty::FnDef(..) | ty::FnPtr(..) => build_subroutine_type_di_node(cx, unique_type_id),
462462
ty::Closure(..) => build_closure_env_di_node(cx, unique_type_id),

compiler/rustc_const_eval/src/interpret/call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
189189
ty::Ref(_, ty, _) => *ty,
190190
ty::RawPtr(ty, _) => *ty,
191191
// We only accept `Box` with the default allocator.
192-
_ if ty.is_box_global(*self.tcx) => ty.boxed_ty(),
192+
_ if ty.is_box_global(*self.tcx) => ty.expect_boxed_ty(),
193193
_ => return Ok(None),
194194
}))
195195
};

compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6363
// Instead, the problem is that the array-into_iter hack will no longer
6464
// apply in Rust 2021.
6565
(ARRAY_INTO_ITER, "2021")
66-
} else if self_ty.is_box()
67-
&& self_ty.boxed_ty().is_slice()
66+
} else if self_ty.boxed_ty().is_some_and(Ty::is_slice)
6867
&& !span.at_least_rust_2024()
6968
{
7069
// In this case, it wasn't really a prelude addition that was the problem.

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,8 +1485,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14851485

14861486
// Some trait methods are excluded for boxed slices before 2024.
14871487
// (`boxed_slice.into_iter()` wants a slice iterator for compatibility.)
1488-
if self_ty.is_box()
1489-
&& self_ty.boxed_ty().is_slice()
1488+
if self_ty.boxed_ty().is_some_and(Ty::is_slice)
14901489
&& !method_name.span.at_least_rust_2024()
14911490
{
14921491
let trait_def = self.tcx.trait_def(poly_trait_ref.def_id());

compiler/rustc_lint/src/shadowed_into_iter.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,9 @@ impl<'tcx> LateLintPass<'tcx> for ShadowedIntoIter {
9494
fn is_ref_to_array(ty: Ty<'_>) -> bool {
9595
if let ty::Ref(_, pointee_ty, _) = *ty.kind() { pointee_ty.is_array() } else { false }
9696
}
97-
fn is_boxed_slice(ty: Ty<'_>) -> bool {
98-
ty.is_box() && ty.boxed_ty().is_slice()
99-
}
10097
fn is_ref_to_boxed_slice(ty: Ty<'_>) -> bool {
10198
if let ty::Ref(_, pointee_ty, _) = *ty.kind() {
102-
is_boxed_slice(pointee_ty)
99+
pointee_ty.boxed_ty().is_some_and(Ty::is_slice)
103100
} else {
104101
false
105102
}
@@ -119,7 +116,7 @@ impl<'tcx> LateLintPass<'tcx> for ShadowedIntoIter {
119116
.iter()
120117
.copied()
121118
.take_while(|ty| !is_ref_to_boxed_slice(*ty))
122-
.position(|ty| is_boxed_slice(ty))
119+
.position(|ty| ty.boxed_ty().is_some_and(Ty::is_slice))
123120
{
124121
(BOXED_SLICE_INTO_ITER, "Box<[T]>", "2024", idx == 0)
125122
} else {

compiler/rustc_lint/src/types.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,8 +1304,10 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13041304

13051305
match *ty.kind() {
13061306
ty::Adt(def, args) => {
1307-
if def.is_box() && matches!(self.mode, CItemKind::Definition) {
1308-
if ty.boxed_ty().is_sized(tcx, self.cx.param_env) {
1307+
if let Some(boxed) = ty.boxed_ty()
1308+
&& matches!(self.mode, CItemKind::Definition)
1309+
{
1310+
if boxed.is_sized(tcx, self.cx.param_env) {
13091311
return FfiSafe;
13101312
} else {
13111313
return FfiUnsafe {

compiler/rustc_lint/src/unused.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,8 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
283283
}
284284

285285
match *ty.kind() {
286-
ty::Adt(..) if ty.is_box() => {
287-
let boxed_ty = ty.boxed_ty();
288-
is_ty_must_use(cx, boxed_ty, expr, span)
286+
ty::Adt(..) if let Some(boxed) = ty.boxed_ty() => {
287+
is_ty_must_use(cx, boxed, expr, span)
289288
.map(|inner| MustUsePath::Boxed(Box::new(inner)))
290289
}
291290
ty::Adt(def, args) if cx.tcx.is_lang_item(def.did(), LangItem::Pin) => {

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,11 +1075,13 @@ where
10751075
// the raw pointer, so size and align are set to the boxed type, but `pointee.safe`
10761076
// will still be `None`.
10771077
if let Some(ref mut pointee) = result {
1078-
if offset.bytes() == 0 && this.ty.is_box() {
1078+
if offset.bytes() == 0
1079+
&& let Some(boxed_ty) = this.ty.boxed_ty()
1080+
{
10791081
debug_assert!(pointee.safe.is_none());
10801082
let optimize = tcx.sess.opts.optimize != OptLevel::No;
10811083
pointee.safe = Some(PointerKind::Box {
1082-
unpin: optimize && this.ty.boxed_ty().is_unpin(tcx, cx.param_env()),
1084+
unpin: optimize && boxed_ty.is_unpin(tcx, cx.param_env()),
10831085
global: this.ty.is_box_global(tcx),
10841086
});
10851087
}

0 commit comments

Comments
 (0)