Skip to content

Commit 5482d17

Browse files
committed
Auto merge of rust-lang#138093 - oli-obk:sized-tuples, r=<try>
Make tuples implicitly sized Since rust-lang#137728 there is no sound way to create unsized tuples anymore. Let's see if we can get away with making tuples unsized at least as far as crater is concerned. With that information we can then discuss whether to actually do this or what path forward we have. r? `@ghost`
2 parents 30f168e + c42f697 commit 5482d17

File tree

59 files changed

+268
-412
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+268
-412
lines changed

compiler/rustc_middle/src/ty/sty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1918,12 +1918,11 @@ impl<'tcx> Ty<'tcx> {
19181918
| ty::CoroutineClosure(..)
19191919
| ty::Never
19201920
| ty::Error(_)
1921+
| ty::Tuple(_)
19211922
| ty::Dynamic(_, _, ty::DynStar) => true,
19221923

19231924
ty::Str | ty::Slice(_) | ty::Dynamic(_, _, ty::Dyn) | ty::Foreign(..) => false,
19241925

1925-
ty::Tuple(tys) => tys.last().is_none_or(|ty| ty.is_trivially_sized(tcx)),
1926-
19271926
ty::Adt(def, args) => def
19281927
.sized_constraint(tcx)
19291928
.is_none_or(|ty| ty.instantiate(tcx, args).is_trivially_sized(tcx)),

compiler/rustc_middle/src/ty/util.rs

-5
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,6 @@ impl<'tcx> TyCtxt<'tcx> {
269269
}
270270
}
271271

272-
ty::Tuple(tys) if let Some((&last_ty, _)) = tys.split_last() => {
273-
f();
274-
ty = last_ty;
275-
}
276-
277272
ty::Tuple(_) => break,
278273

279274
ty::Pat(inner, _) => {

compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ where
108108
match ty.kind() {
109109
// impl Sized for u*, i*, bool, f*, FnDef, FnPtr, *(const/mut) T, char, &mut? T, [T; N], dyn* Trait, !
110110
// impl Sized for Coroutine, CoroutineWitness, Closure, CoroutineClosure
111+
// impl Sized for tuple
111112
ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
112113
| ty::Uint(_)
113114
| ty::Int(_)
@@ -126,6 +127,7 @@ where
126127
| ty::CoroutineClosure(..)
127128
| ty::Never
128129
| ty::Dynamic(_, _, ty::DynStar)
130+
| ty::Tuple(_)
129131
| ty::Error(_) => Ok(ty::Binder::dummy(vec![])),
130132

131133
ty::Str
@@ -143,10 +145,6 @@ where
143145

144146
ty::UnsafeBinder(bound_ty) => Ok(bound_ty.map_bound(|ty| vec![ty])),
145147

146-
// impl Sized for ()
147-
// impl Sized for (T1, T2, .., Tn) where Tn: Sized if n >= 1
148-
ty::Tuple(tys) => Ok(ty::Binder::dummy(tys.last().map_or_else(Vec::new, |ty| vec![ty]))),
149-
150148
// impl Sized for Adt<Args...> where sized_constraint(Adt)<Args...>: Sized
151149
// `sized_constraint(Adt)` is the deepest struct trail that can be determined
152150
// by the definition of `Adt`, independent of the generic args.

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2712,9 +2712,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
27122712
ObligationCauseCode::ArrayLen(array_ty) => {
27132713
err.note(format!("the length of array `{array_ty}` must be type `usize`"));
27142714
}
2715-
ObligationCauseCode::TupleElem => {
2716-
err.note("only the last element of a tuple may have a dynamically sized type");
2717-
}
2715+
ObligationCauseCode::TupleElem => {}
27182716
ObligationCauseCode::WhereClause(item_def_id, span)
27192717
| ObligationCauseCode::WhereClauseInExpr(item_def_id, span, ..)
27202718
| ObligationCauseCode::HostEffectInExpr(item_def_id, span, ..)

compiler/rustc_trait_selection/src/traits/select/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2090,17 +2090,14 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
20902090
| ty::CoroutineClosure(..)
20912091
| ty::Never
20922092
| ty::Dynamic(_, _, ty::DynStar)
2093+
| ty::Tuple(_)
20932094
| ty::Error(_) => {
20942095
// safe for everything
20952096
Where(ty::Binder::dummy(Vec::new()))
20962097
}
20972098

20982099
ty::Str | ty::Slice(_) | ty::Dynamic(..) | ty::Foreign(..) => None,
20992100

2100-
ty::Tuple(tys) => Where(
2101-
obligation.predicate.rebind(tys.last().map_or_else(Vec::new, |&last| vec![last])),
2102-
),
2103-
21042101
ty::Pat(ty, _) => Where(obligation.predicate.rebind(vec![*ty])),
21052102

21062103
ty::Adt(def, args) => {

compiler/rustc_trait_selection/src/traits/wf.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -749,10 +749,8 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
749749
}
750750

751751
ty::Tuple(tys) => {
752-
if let Some((_last, rest)) = tys.split_last() {
753-
for &elem in rest {
754-
self.require_sized(elem, ObligationCauseCode::TupleElem);
755-
}
752+
for elem in tys {
753+
self.require_sized(elem, ObligationCauseCode::TupleElem);
756754
}
757755
}
758756

library/core/src/fmt/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -2851,7 +2851,7 @@ macro_rules! tuple {
28512851
maybe_tuple_doc! {
28522852
$($name)+ @
28532853
#[stable(feature = "rust1", since = "1.0.0")]
2854-
impl<$($name:Debug),+> Debug for ($($name,)+) where last_type!($($name,)+): ?Sized {
2854+
impl<$($name:Debug),+> Debug for ($($name,)+) {
28552855
#[allow(non_snake_case, unused_assignments)]
28562856
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
28572857
let mut builder = f.debug_tuple("");
@@ -2882,11 +2882,6 @@ macro_rules! maybe_tuple_doc {
28822882
};
28832883
}
28842884

2885-
macro_rules! last_type {
2886-
($a:ident,) => { $a };
2887-
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
2888-
}
2889-
28902885
tuple! { E, D, C, B, A, Z, Y, X, W, V, U, T, }
28912886

28922887
#[stable(feature = "rust1", since = "1.0.0")]

library/core/src/hash/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ mod impls {
886886
maybe_tuple_doc! {
887887
$($name)+ @
888888
#[stable(feature = "rust1", since = "1.0.0")]
889-
impl<$($name: Hash),+> Hash for ($($name,)+) where last_type!($($name,)+): ?Sized {
889+
impl<$($name: Hash),+> Hash for ($($name,)+) {
890890
#[allow(non_snake_case)]
891891
#[inline]
892892
fn hash<S: Hasher>(&self, state: &mut S) {
@@ -912,11 +912,6 @@ mod impls {
912912
};
913913
}
914914

915-
macro_rules! last_type {
916-
($a:ident,) => { $a };
917-
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
918-
}
919-
920915
impl_hash_tuple! {}
921916
impl_hash_tuple! { T }
922917
impl_hash_tuple! { T B }

library/core/src/ops/range.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ pub enum OneSidedRangeBound {
12111211
/// Types that implement `OneSidedRange<T>` must return `Bound::Unbounded`
12121212
/// from one of `RangeBounds::start_bound` or `RangeBounds::end_bound`.
12131213
#[unstable(feature = "one_sided_range", issue = "69780")]
1214-
pub trait OneSidedRange<T: ?Sized>: RangeBounds<T> {
1214+
pub trait OneSidedRange<T>: RangeBounds<T> {
12151215
/// An internal-only helper function for `split_off` and
12161216
/// `split_off_mut` that returns the bound of the one-sided range.
12171217
fn bound(self) -> (OneSidedRangeBound, T);

library/core/src/tuple.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ macro_rules! tuple_impls {
2222
maybe_tuple_doc! {
2323
$($T)+ @
2424
#[stable(feature = "rust1", since = "1.0.0")]
25-
impl<$($T: PartialEq),+> PartialEq for ($($T,)+)
26-
where
27-
last_type!($($T,)+): ?Sized
28-
{
25+
impl<$($T: PartialEq),+> PartialEq for ($($T,)+) {
2926
#[inline]
3027
fn eq(&self, other: &($($T,)+)) -> bool {
3128
$( ${ignore($T)} self.${index()} == other.${index()} )&&+
@@ -41,8 +38,6 @@ macro_rules! tuple_impls {
4138
$($T)+ @
4239
#[stable(feature = "rust1", since = "1.0.0")]
4340
impl<$($T: Eq),+> Eq for ($($T,)+)
44-
where
45-
last_type!($($T,)+): ?Sized
4641
{}
4742
}
4843

@@ -71,8 +66,6 @@ macro_rules! tuple_impls {
7166
$($T)+ @
7267
#[stable(feature = "rust1", since = "1.0.0")]
7368
impl<$($T: PartialOrd),+> PartialOrd for ($($T,)+)
74-
where
75-
last_type!($($T,)+): ?Sized
7669
{
7770
#[inline]
7871
fn partial_cmp(&self, other: &($($T,)+)) -> Option<Ordering> {
@@ -101,8 +94,6 @@ macro_rules! tuple_impls {
10194
$($T)+ @
10295
#[stable(feature = "rust1", since = "1.0.0")]
10396
impl<$($T: Ord),+> Ord for ($($T,)+)
104-
where
105-
last_type!($($T,)+): ?Sized
10697
{
10798
#[inline]
10899
fn cmp(&self, other: &($($T,)+)) -> Ordering {
@@ -205,9 +196,4 @@ macro_rules! lexical_cmp {
205196
($a:expr, $b:expr) => { ($a).cmp(&$b) };
206197
}
207198

208-
macro_rules! last_type {
209-
($a:ident,) => { $a };
210-
($a:ident, $($rest_a:ident,)+) => { last_type!($($rest_a,)+) };
211-
}
212-
213199
tuple_impls!(E D C B A Z Y X W V U T);

tests/ui/abi/compatibility.rs

-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ mod unsized_ {
292292
use super::*;
293293
test_transparent_unsized!(str_, str);
294294
test_transparent_unsized!(slice, [u8]);
295-
test_transparent_unsized!(slice_with_prefix, (usize, [u8]));
296295
test_transparent_unsized!(dyn_trait, dyn Any);
297296
}
298297

tests/ui/abi/debug.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,6 @@ LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
904904
| ^^^^^^^^^^ doesn't have a size known at compile-time
905905
|
906906
= help: the trait `Sized` is not implemented for `str`
907-
= note: only the last element of a tuple may have a dynamically sized type
908907

909908
error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
910909
--> $DIR/debug.rs:29:5

tests/ui/associated-types/associated-types-coherence-failure.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::marker::PhantomData;
55
use std::ops::Deref;
66

7-
pub struct Cow<'a, B: ?Sized>(PhantomData<(&'a (),B)>);
7+
pub struct Cow<'a, B: ?Sized>(PhantomData<&'a ()>, PhantomData<B>);
88

99
/// Trait for moving into a `Cow`
1010
pub trait IntoCow<'a, B: ?Sized> {
@@ -14,7 +14,7 @@ pub trait IntoCow<'a, B: ?Sized> {
1414

1515
impl<'a, B: ?Sized> IntoCow<'a, B> for <B as ToOwned>::Owned where B: ToOwned {
1616
fn into_cow(self) -> Cow<'a, B> {
17-
Cow(PhantomData)
17+
Cow(PhantomData, PhantomData)
1818
}
1919
}
2020

@@ -28,7 +28,7 @@ impl<'a, B: ?Sized> IntoCow<'a, B> for Cow<'a, B> where B: ToOwned {
2828
impl<'a, B: ?Sized> IntoCow<'a, B> for &'a B where B: ToOwned {
2929
//~^ ERROR E0119
3030
fn into_cow(self) -> Cow<'a, B> {
31-
Cow(PhantomData)
31+
Cow(PhantomData, PhantomData)
3232
}
3333
}
3434

tests/ui/async-await/awaiting-unsized-param.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::future::Future;
77

88
async fn bug<T>(mut f: dyn Future<Output = T> + Unpin) -> T {
99
//~^ ERROR the size for values of type `(dyn Future<Output = T> + Unpin + 'static)` cannot be known at compilation time
10+
//~| ERROR the size for values of type `dyn Future<Output = T> + Unpin` cannot be known at compilation time
1011
(&mut f).await
1112
}
1213

tests/ui/async-await/awaiting-unsized-param.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ LL | async fn bug<T>(mut f: dyn Future<Output = T> + Unpin) -> T {
1616
= help: the trait `Sized` is not implemented for `(dyn Future<Output = T> + Unpin + 'static)`
1717
= note: all values captured by value by a closure must have a statically known size
1818

19-
error: aborting due to 1 previous error; 1 warning emitted
19+
error[E0277]: the size for values of type `dyn Future<Output = T> + Unpin` cannot be known at compilation time
20+
--> $DIR/awaiting-unsized-param.rs:8:1
21+
|
22+
LL | async fn bug<T>(mut f: dyn Future<Output = T> + Unpin) -> T {
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
24+
|
25+
= help: the trait `Sized` is not implemented for `dyn Future<Output = T> + Unpin`
26+
27+
error: aborting due to 2 previous errors; 1 warning emitted
2028

2129
For more information about this error, try `rustc --explain E0277`.

tests/ui/box/into-boxed-slice-fail.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ LL | let _ = Box::into_boxed_slice(boxed_slice);
99
= help: the trait `Sized` is not implemented for `[u8]`
1010
note: required by a bound in `Box::<T, A>::into_boxed_slice`
1111
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
12+
help: use a unary tuple instead
13+
|
14+
LL | let _ = Box::into_boxed_slice((boxed_slice,));
15+
| + ++
1216

1317
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
1418
--> $DIR/into-boxed-slice-fail.rs:7:13
@@ -30,6 +34,10 @@ LL | let _ = Box::into_boxed_slice(boxed_trait);
3034
= help: the trait `Sized` is not implemented for `dyn Debug`
3135
note: required by a bound in `Box::<T, A>::into_boxed_slice`
3236
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
37+
help: use a unary tuple instead
38+
|
39+
LL | let _ = Box::into_boxed_slice((boxed_trait,));
40+
| + ++
3341

3442
error[E0277]: the size for values of type `dyn Debug` cannot be known at compilation time
3543
--> $DIR/into-boxed-slice-fail.rs:11:13

tests/ui/closures/issue-111932.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ LL | println!("{:?}", foo);
2020
note: required by an implicit `Sized` bound in `core::fmt::rt::Argument::<'_>::new_debug`
2121
--> $SRC_DIR/core/src/fmt/rt.rs:LL:COL
2222
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
help: use a unary tuple instead
24+
|
25+
LL | println!("{:?}", (foo,));
26+
| + ++
2327

2428
error: aborting due to 2 previous errors
2529

tests/ui/const-generics/occurs-check/unused-substs-2.rs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ struct Foo<const N: usize>;
99

1010
trait Bind<T> {
1111
fn bind() -> (T, Self);
12+
//~^ ERROR: the size for values of type `Self` cannot be known at compilation time
13+
//~| NOTE: doesn't have a size known at compile-time
1214
}
1315

1416
// `N` has to be `ConstKind::Unevaluated`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
error[E0277]: the size for values of type `Self` cannot be known at compilation time
2+
--> $DIR/unused-substs-2.rs:11:18
3+
|
4+
LL | fn bind() -> (T, Self);
5+
| ^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
help: consider further restricting `Self`
8+
|
9+
LL | fn bind() -> (T, Self) where Self: Sized;
10+
| +++++++++++++++++
11+
112
error[E0308]: mismatched types
2-
--> $DIR/unused-substs-2.rs:22:24
13+
--> $DIR/unused-substs-2.rs:24:24
314
|
415
LL | let (mut t, foo) = Foo::bind();
516
| ^^^^^^^^^^^ cyclic type of infinite size
617

7-
error: aborting due to 1 previous error
18+
error: aborting due to 2 previous errors
819

9-
For more information about this error, try `rustc --explain E0308`.
20+
Some errors have detailed explanations: E0277, E0308.
21+
For more information about an error, try `rustc --explain E0277`.

tests/ui/dst/dst-bad-coerce2.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Attempt to change the mutability as well as unsizing.
22

33
struct Fat<T: ?Sized> {
4-
ptr: T
4+
ptr: T,
55
}
66

77
struct Foo;
@@ -18,14 +18,4 @@ pub fn main() {
1818
let f1 = Fat { ptr: Foo };
1919
let f2: &Fat<Foo> = &f1;
2020
let f3: &mut Fat<dyn Bar> = f2; //~ ERROR mismatched types
21-
22-
// Tuple with a vec of ints.
23-
let f1 = ([1, 2, 3],);
24-
let f2: &([isize; 3],) = &f1;
25-
let f3: &mut ([isize],) = f2; //~ ERROR mismatched types
26-
27-
// Tuple with a trait.
28-
let f1 = (Foo,);
29-
let f2: &(Foo,) = &f1;
30-
let f3: &mut (dyn Bar,) = f2; //~ ERROR mismatched types
3121
}

tests/ui/dst/dst-bad-coerce2.stderr

+1-23
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,6 @@ LL | let f3: &mut Fat<dyn Bar> = f2;
2020
= note: expected mutable reference `&mut Fat<dyn Bar>`
2121
found reference `&Fat<Foo>`
2222

23-
error[E0308]: mismatched types
24-
--> $DIR/dst-bad-coerce2.rs:25:31
25-
|
26-
LL | let f3: &mut ([isize],) = f2;
27-
| --------------- ^^ types differ in mutability
28-
| |
29-
| expected due to this
30-
|
31-
= note: expected mutable reference `&mut ([isize],)`
32-
found reference `&([isize; 3],)`
33-
34-
error[E0308]: mismatched types
35-
--> $DIR/dst-bad-coerce2.rs:30:31
36-
|
37-
LL | let f3: &mut (dyn Bar,) = f2;
38-
| --------------- ^^ types differ in mutability
39-
| |
40-
| expected due to this
41-
|
42-
= note: expected mutable reference `&mut (dyn Bar,)`
43-
found reference `&(Foo,)`
44-
45-
error: aborting due to 4 previous errors
23+
error: aborting due to 2 previous errors
4624

4725
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)