Skip to content

Commit bdb71fa

Browse files
authored
Unrolled build for rust-lang#129969
Rollup merge of rust-lang#129969 - GrigorenkoPV:boxed-ty, r=compiler-errors Make `Ty::boxed_ty` return an `Option` Looks like a good place to use Rust's type system. --- Most of https://github.com/rust-lang/rust/blob/4ac7bcbaad8d6fd7a51bdf1b696cbc3ba4c796cf/compiler/rustc_middle/src/ty/sty.rs#L971-L1963 looks like it could be moved to `TyKind` (then I guess `Ty` should be made to deref to `TyKind`).
2 parents d678b81 + f6e8a84 commit bdb71fa

File tree

21 files changed

+58
-48
lines changed

21 files changed

+58
-48
lines changed

Diff for: 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.

Diff for: 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, _) => {

Diff for: 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.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),

Diff for: 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.expect_boxed_ty(),
193193
_ => return Ok(None),
194194
}))
195195
};

Diff for: 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.

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

Diff for: compiler/rustc_lint/src/shadowed_into_iter.rs

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

Diff for: compiler/rustc_lint/src/types.rs

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

Diff for: compiler/rustc_lint/src/unused.rs

+2-3
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) => {

Diff for: 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
}

Diff for: compiler/rustc_middle/src/ty/sty.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1170,14 +1170,19 @@ 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 expect_boxed_ty(self) -> Ty<'tcx> {
1182+
self.boxed_ty()
1183+
.unwrap_or_else(|| bug!("`expect_boxed_ty` is called on non-box type {:?}", self))
1184+
}
1185+
11811186
/// A scalar type is one that denotes an atomic datum, with no sub-components.
11821187
/// (A RawPtr is scalar because it represents a non-managed pointer, so its
11831188
/// contents are abstract to rustc.)
@@ -1323,7 +1328,7 @@ impl<'tcx> Ty<'tcx> {
13231328
/// Some types -- notably unsafe ptrs -- can only be dereferenced explicitly.
13241329
pub fn builtin_deref(self, explicit: bool) -> Option<Ty<'tcx>> {
13251330
match *self.kind() {
1326-
Adt(def, _) if def.is_box() => Some(self.boxed_ty()),
1331+
_ if let Some(boxed) = self.boxed_ty() => Some(boxed),
13271332
Ref(_, ty, _) => Some(ty),
13281333
RawPtr(ty, _) if explicit => Some(ty),
13291334
_ => None,

Diff for: compiler/rustc_middle/src/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ 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+
_ if self_arg_ty.boxed_ty().is_some_and(is_self_ty) => ByBox,
16321632
_ => Other,
16331633
}
16341634
}

Diff for: 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> crate::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,

Diff for: compiler/rustc_monomorphize/src/collector.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,11 @@ fn find_vtable_types_for_unsizing<'tcx>(
10411041
match (source_ty.kind(), target_ty.kind()) {
10421042
(&ty::Ref(_, a, _), &ty::Ref(_, b, _) | &ty::RawPtr(b, _))
10431043
| (&ty::RawPtr(a, _), &ty::RawPtr(b, _)) => ptr_vtable(a, b),
1044-
(&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())
1044+
(_, _)
1045+
if let Some(source_boxed) = source_ty.boxed_ty()
1046+
&& let Some(target_boxed) = target_ty.boxed_ty() =>
1047+
{
1048+
ptr_vtable(source_boxed, target_boxed)
10461049
}
10471050

10481051
// T as dyn* Trait

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// tidy-alphabetical-start
22
#![feature(array_windows)]
3+
#![feature(if_let_guard)]
4+
#![feature(let_chains)]
35
#![warn(unreachable_pub)]
46
// tidy-alphabetical-end
57

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

Diff for: 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_some_and(|boxed| !boxed.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
}

Diff for: 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)

Diff for: 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(..) if let Some(boxed) = ty.boxed_ty() => may_slice(cx, boxed),
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+
_ if ty.boxed_ty().is_some_and(|boxed| may_slice(cx, boxed)) => Some(expr),
3737
ty::Ref(_, inner, _) => {
3838
if may_slice(cx, *inner) {
3939
Some(expr)

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

Diff for: 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)