Skip to content

Commit cdbe3a7

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

File tree

20 files changed

+48
-41
lines changed

20 files changed

+48
-41
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+2-1
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

+2-2
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

+1-1
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.boxed_ty_unchecked(), 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

+1-1
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.boxed_ty_unchecked(),
193193
_ => return Ok(None),
194194
}))
195195
};

compiler/rustc_hir_typeck/src/method/prelude_edition_lints.rs

+1-2
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

+1-2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'tcx> LateLintPass<'tcx> for ShadowedIntoIter {
9595
if let ty::Ref(_, pointee_ty, _) = *ty.kind() { pointee_ty.is_array() } else { false }
9696
}
9797
fn is_boxed_slice(ty: Ty<'_>) -> bool {
98-
ty.is_box() && ty.boxed_ty().is_slice()
98+
ty.boxed_ty().is_some_and(Ty::is_slice)
9999
}
100100
fn is_ref_to_boxed_slice(ty: Ty<'_>) -> bool {
101101
if let ty::Ref(_, pointee_ty, _) = *ty.kind() {

compiler/rustc_lint/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
13051305
match *ty.kind() {
13061306
ty::Adt(def, args) => {
13071307
if def.is_box() && matches!(self.mode, CItemKind::Definition) {
1308-
if ty.boxed_ty().is_sized(tcx, self.cx.param_env) {
1308+
if ty.boxed_ty_unchecked().is_sized(tcx, self.cx.param_env) {
13091309
return FfiSafe;
13101310
} else {
13111311
return FfiUnsafe {

compiler/rustc_lint/src/unused.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
284284

285285
match *ty.kind() {
286286
ty::Adt(..) if ty.is_box() => {
287-
let boxed_ty = ty.boxed_ty();
288-
is_ty_must_use(cx, boxed_ty, expr, span)
287+
is_ty_must_use(cx, ty.boxed_ty_unchecked(), 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

+4-2
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
}

compiler/rustc_middle/src/ty/sty.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1170,14 +1170,18 @@ impl<'tcx> Ty<'tcx> {
11701170
}
11711171
}
11721172

1173-
/// Panics if called on any type other than `Box<T>`.
1174-
pub fn boxed_ty(self) -> Ty<'tcx> {
1173+
pub fn boxed_ty(self) -> Option<Ty<'tcx>> {
11751174
match self.kind() {
1176-
Adt(def, args) if def.is_box() => args.type_at(0),
1177-
_ => bug!("`boxed_ty` is called on non-box type {:?}", self),
1175+
Adt(def, args) if def.is_box() => Some(args.type_at(0)),
1176+
_ => None,
11781177
}
11791178
}
11801179

1180+
/// Panics if called on any type other than `Box<T>`.
1181+
pub fn boxed_ty_unchecked(self) -> Ty<'tcx> {
1182+
self.boxed_ty().unwrap_or_else(|| bug!("`boxed_ty` is called on non-box type {:?}", self))
1183+
}
1184+
11811185
/// A scalar type is one that denotes an atomic datum, with no sub-components.
11821186
/// (A RawPtr is scalar because it represents a non-managed pointer, so its
11831187
/// contents are abstract to rustc.)
@@ -1323,7 +1327,7 @@ impl<'tcx> Ty<'tcx> {
13231327
/// Some types -- notably unsafe ptrs -- can only be dereferenced explicitly.
13241328
pub fn builtin_deref(self, explicit: bool) -> Option<Ty<'tcx>> {
13251329
match *self.kind() {
1326-
Adt(def, _) if def.is_box() => Some(self.boxed_ty()),
1330+
Adt(def, _) if def.is_box() => Some(self.boxed_ty_unchecked()),
13271331
Ref(_, ty, _) => Some(ty),
13281332
RawPtr(ty, _) if explicit => Some(ty),
13291333
_ => None,

compiler/rustc_middle/src/ty/util.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,9 @@ impl<'tcx> ExplicitSelf<'tcx> {
16281628
_ if is_self_ty(self_arg_ty) => ByValue,
16291629
ty::Ref(region, ty, mutbl) if is_self_ty(ty) => ByReference(region, mutbl),
16301630
ty::RawPtr(ty, mutbl) if is_self_ty(ty) => ByRawPointer(mutbl),
1631-
ty::Adt(def, _) if def.is_box() && is_self_ty(self_arg_ty.boxed_ty()) => ByBox,
1631+
ty::Adt(def, _) if def.is_box() && is_self_ty(self_arg_ty.boxed_ty_unchecked()) => {
1632+
ByBox
1633+
}
16321634
_ => Other,
16331635
}
16341636
}

compiler/rustc_mir_transform/src/elaborate_box_derefs.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ impl<'tcx, 'a> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'tcx, 'a> {
6262
let base_ty = self.local_decls[place.local].ty;
6363

6464
// Derefer ensures that derefs are always the first projection
65-
if place.projection.first() == Some(&PlaceElem::Deref) && base_ty.is_box() {
65+
if let Some(PlaceElem::Deref) = place.projection.first()
66+
&& let Some(boxed_ty) = base_ty.boxed_ty()
67+
{
6668
let source_info = self.local_decls[place.local].source_info;
6769

6870
let (unique_ty, nonnull_ty, ptr_ty) =
69-
build_ptr_tys(tcx, base_ty.boxed_ty(), self.unique_did, self.nonnull_did);
71+
build_ptr_tys(tcx, boxed_ty, self.unique_did, self.nonnull_did);
7072

7173
let ptr_local = self.patch.new_temp(ptr_ty, source_info.span);
7274

@@ -120,13 +122,15 @@ impl<'tcx> MirPass<'tcx> for ElaborateBoxDerefs {
120122
for (base, elem) in place.iter_projections() {
121123
let base_ty = base.ty(&body.local_decls, tcx).ty;
122124

123-
if elem == PlaceElem::Deref && base_ty.is_box() {
125+
if let PlaceElem::Deref = elem
126+
&& let Some(boxed_ty) = base_ty.boxed_ty()
127+
{
124128
// Clone the projections before us, since now we need to mutate them.
125129
let new_projections =
126130
new_projections.get_or_insert_with(|| base.projection.to_vec());
127131

128132
let (unique_ty, nonnull_ty, ptr_ty) =
129-
build_ptr_tys(tcx, base_ty.boxed_ty(), unique_did, nonnull_did);
133+
build_ptr_tys(tcx, boxed_ty, unique_did, nonnull_did);
130134

131135
new_projections.extend_from_slice(&build_projection(
132136
unique_ty, nonnull_ty, ptr_ty,

compiler/rustc_monomorphize/src/collector.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ fn find_vtable_types_for_unsizing<'tcx>(
10421042
(&ty::Ref(_, a, _), &ty::Ref(_, b, _) | &ty::RawPtr(b, _))
10431043
| (&ty::RawPtr(a, _), &ty::RawPtr(b, _)) => ptr_vtable(a, b),
10441044
(&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
1045-
ptr_vtable(source_ty.boxed_ty(), target_ty.boxed_ty())
1045+
ptr_vtable(source_ty.boxed_ty_unchecked(), target_ty.boxed_ty_unchecked())
10461046
}
10471047

10481048
// T as dyn* Trait

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
348348
}
349349
}
350350
if let Some(ty::error::ExpectedFound { found, .. }) = exp_found
351-
&& ty.is_box()
352-
&& ty.boxed_ty() == found
351+
&& ty.boxed_ty() == Some(found)
353352
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
354353
{
355354
err.span_suggestion(

src/tools/clippy/clippy_lints/src/escape.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ declare_clippy_lint! {
5050
}
5151

5252
fn is_non_trait_box(ty: Ty<'_>) -> bool {
53-
ty.is_box() && !ty.boxed_ty().is_trait()
53+
ty.boxed_ty().is_none_or(|boxed_ty| !boxed_ty.is_trait())
5454
}
5555

5656
struct EscapeDelegate<'a, 'tcx> {
@@ -191,8 +191,8 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
191191
impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> {
192192
fn is_large_box(&self, ty: Ty<'tcx>) -> bool {
193193
// Large types need to be boxed to avoid stack overflows.
194-
if ty.is_box() {
195-
self.cx.layout_of(ty.boxed_ty()).map_or(0, |l| l.size.bytes()) > self.too_large_for_stack
194+
if let Some(boxed_ty) = ty.boxed_ty() {
195+
self.cx.layout_of(boxed_ty).map_or(0, |l| l.size.bytes()) > self.too_large_for_stack
196196
} else {
197197
false
198198
}

src/tools/clippy/clippy_lints/src/methods/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5187,8 +5187,8 @@ impl SelfKind {
51875187
fn matches_value<'a>(cx: &LateContext<'a>, parent_ty: Ty<'a>, ty: Ty<'a>) -> bool {
51885188
if ty == parent_ty {
51895189
true
5190-
} else if ty.is_box() {
5191-
ty.boxed_ty() == parent_ty
5190+
} else if let Some(boxed_ty) = ty.boxed_ty() {
5191+
boxed_ty == parent_ty
51925192
} else if is_type_diagnostic_item(cx, ty, sym::Rc) || is_type_diagnostic_item(cx, ty, sym::Arc) {
51935193
if let ty::Adt(_, args) = ty.kind() {
51945194
args.types().next().map_or(false, |t| t == parent_ty)

src/tools/clippy/clippy_lints/src/methods/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(super) fn derefs_to_slice<'tcx>(
1616
fn may_slice<'a>(cx: &LateContext<'a>, ty: Ty<'a>) -> bool {
1717
match ty.kind() {
1818
ty::Slice(_) => true,
19-
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
19+
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty_unchecked()),
2020
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym::Vec),
2121
ty::Array(_, size) => size.try_eval_target_usize(cx.tcx, cx.param_env).is_some(),
2222
ty::Ref(_, inner, _) => may_slice(cx, *inner),
@@ -33,7 +33,7 @@ pub(super) fn derefs_to_slice<'tcx>(
3333
} else {
3434
match ty.kind() {
3535
ty::Slice(_) => Some(expr),
36-
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty()) => Some(expr),
36+
ty::Adt(def, _) if def.is_box() && may_slice(cx, ty.boxed_ty_unchecked()) => Some(expr),
3737
ty::Ref(_, inner, _) => {
3838
if may_slice(cx, *inner) {
3939
Some(expr)

src/tools/clippy/clippy_lints/src/unnecessary_box_returns.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,9 @@ impl UnnecessaryBoxReturns {
7575
.instantiate_bound_regions_with_erased(cx.tcx.fn_sig(def_id).skip_binder())
7676
.output();
7777

78-
if !return_ty.is_box() {
78+
let Some(boxed_ty) = return_ty.boxed_ty() else {
7979
return;
80-
}
81-
82-
let boxed_ty = return_ty.boxed_ty();
80+
};
8381

8482
// It's sometimes useful to return Box<T> if T is unsized, so don't lint those.
8583
// Also, don't lint if we know that T is very large, in which case returning

src/tools/clippy/clippy_utils/src/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,8 @@ pub fn expr_sig<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) -> Option<ExprFnS
704704

705705
/// If the type is function like, get the signature for it.
706706
pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<ExprFnSig<'tcx>> {
707-
if ty.is_box() {
708-
return ty_sig(cx, ty.boxed_ty());
707+
if let Some(boxed_ty) = ty.boxed_ty() {
708+
return ty_sig(cx, boxed_ty);
709709
}
710710
match *ty.kind() {
711711
ty::Closure(id, subs) => {

0 commit comments

Comments
 (0)