Skip to content

Commit cc9699d

Browse files
committed
trait_sel: add builtin impl for PointeeSized
During review of rust-lang#137944, we thought we could remove the builtin impl of `PointeeSized` as it was removed during lowering, but users can still write `dyn PointeeSized` and so we need the builtin impl.
1 parent 27733d4 commit cc9699d

File tree

14 files changed

+100
-26
lines changed

14 files changed

+100
-26
lines changed

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,7 @@ impl<'tcx> Ty<'tcx> {
17791779
}
17801780
}
17811781

1782-
/// Fast path helper for testing if a type is `Sized` or `MetaSized`.
1782+
/// Fast path helper for testing if a type is `Sized`, `MetaSized` or `PointeeSized`.
17831783
///
17841784
/// Returning true means the type is known to implement the sizedness trait. Returning `false`
17851785
/// means nothing -- could be sized, might not be.
@@ -1814,11 +1814,12 @@ impl<'tcx> Ty<'tcx> {
18141814

18151815
ty::Str | ty::Slice(_) | ty::Dynamic(_, _, ty::Dyn) => match sizedness {
18161816
SizedTraitKind::Sized => false,
1817-
SizedTraitKind::MetaSized => true,
1817+
SizedTraitKind::MetaSized | SizedTraitKind::PointeeSized => true,
18181818
},
18191819

18201820
ty::Foreign(..) => match sizedness {
18211821
SizedTraitKind::Sized | SizedTraitKind::MetaSized => false,
1822+
SizedTraitKind::PointeeSized => true,
18221823
},
18231824

18241825
ty::Tuple(tys) => tys.last().is_none_or(|ty| ty.has_trivial_sizedness(tcx, sizedness)),

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,11 @@ where
476476
G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
477477
}
478478
Some(TraitSolverLangItem::PointeeSized) => {
479-
unreachable!("`PointeeSized` is removed during lowering");
479+
G::consider_builtin_sizedness_candidates(
480+
self,
481+
goal,
482+
SizedTraitKind::PointeeSized,
483+
)
480484
}
481485
Some(TraitSolverLangItem::Copy | TraitSolverLangItem::Clone) => {
482486
G::consider_builtin_copy_clone_candidate(self, goal)

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ where
115115
I: Interner,
116116
{
117117
match ty.kind() {
118-
// impl {Meta,}Sized for u*, i*, bool, f*, FnDef, FnPtr, *(const/mut) T, char
119-
// impl {Meta,}Sized for &mut? T, [T; N], dyn* Trait, !, Coroutine, CoroutineWitness
120-
// impl {Meta,}Sized for Closure, CoroutineClosure
118+
// impl {Meta,Pointee,}Sized for u*, i*, bool, f*, FnDef, FnPtr, *(const/mut) T, char
119+
// impl {Meta,Pointee,}Sized for &mut? T, [T; N], dyn* Trait, !, Coroutine, CoroutineWitness
120+
// impl {Meta,Pointee,}Sized for Closure, CoroutineClosure
121121
ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
122122
| ty::Uint(_)
123123
| ty::Int(_)
@@ -138,14 +138,19 @@ where
138138
| ty::Dynamic(_, _, ty::DynStar)
139139
| ty::Error(_) => Ok(ty::Binder::dummy(vec![])),
140140

141-
// impl {Meta,}Sized for str, [T], dyn Trait
141+
// impl {Meta,Pointee,}Sized for str, [T], dyn Trait
142142
ty::Str | ty::Slice(_) | ty::Dynamic(..) => match sizedness {
143143
SizedTraitKind::Sized => Err(NoSolution),
144-
SizedTraitKind::MetaSized => Ok(ty::Binder::dummy(vec![])),
144+
SizedTraitKind::MetaSized | SizedTraitKind::PointeeSized => {
145+
Ok(ty::Binder::dummy(vec![]))
146+
}
145147
},
146148

147-
// impl {} for extern type
148-
ty::Foreign(..) => Err(NoSolution),
149+
// impl PointeeSized for extern type
150+
ty::Foreign(..) => match sizedness {
151+
SizedTraitKind::Sized | SizedTraitKind::MetaSized => Err(NoSolution),
152+
SizedTraitKind::PointeeSized => Ok(ty::Binder::dummy(vec![])),
153+
},
149154

150155
ty::Alias(..) | ty::Param(_) | ty::Placeholder(..) => Err(NoSolution),
151156

@@ -156,17 +161,17 @@ where
156161

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

159-
// impl {Meta,}Sized for ()
160-
// impl {Meta,}Sized for (T1, T2, .., Tn) where Tn: {Meta,}Sized if n >= 1
164+
// impl {Meta,Pointee,}Sized for ()
165+
// impl {Meta,Pointee,}Sized for (T1, T2, .., Tn) where Tn: {Meta,}Sized if n >= 1
161166
ty::Tuple(tys) => Ok(ty::Binder::dummy(tys.last().map_or_else(Vec::new, |ty| vec![ty]))),
162167

163-
// impl {Meta,}Sized for Adt<Args...>
168+
// impl {Meta,Pointee,}Sized for Adt<Args...>
164169
// where {meta,pointee,}sized_constraint(Adt)<Args...>: {Meta,}Sized
165170
//
166171
// `{meta,pointee,}sized_constraint(Adt)` is the deepest struct trail that can be
167172
// determined by the definition of `Adt`, independent of the generic args.
168173
//
169-
// impl {Meta,}Sized for Adt<Args...>
174+
// impl {Meta,Pointee,}Sized for Adt<Args...>
170175
// if {meta,pointee,}sized_constraint(Adt) == None
171176
//
172177
// As a performance optimization, `{meta,pointee,}sized_constraint(Adt)` can return `None`

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
201201
// begin with in those cases.
202202
if matches!(
203203
self.tcx.as_lang_item(trait_pred.def_id()),
204-
Some(LangItem::Sized | LangItem::MetaSized)
204+
Some(LangItem::Sized | LangItem::MetaSized | LangItem::PointeeSized)
205205
) {
206206
match self.tainted_by_errors() {
207207
None => {

compiler/rustc_trait_selection/src/solve/delegate.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
9090
{
9191
return Some(Certainty::Yes);
9292
}
93+
Some(LangItem::PointeeSized)
94+
if self
95+
.resolve_vars_if_possible(trait_pred.self_ty().skip_binder())
96+
.has_trivial_sizedness(self.0.tcx, SizedTraitKind::PointeeSized) =>
97+
{
98+
return Some(Certainty::Yes);
99+
}
93100
Some(LangItem::Copy | LangItem::Clone) => {
94101
let self_ty =
95102
self.resolve_vars_if_possible(trait_pred.self_ty().skip_binder());

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
101101
);
102102
}
103103
Some(LangItem::PointeeSized) => {
104-
bug!("`PointeeSized` is removed during lowering");
104+
self.assemble_builtin_sized_candidate(
105+
obligation,
106+
&mut candidates,
107+
SizedTraitKind::PointeeSized,
108+
);
105109
}
106110
Some(LangItem::Unsize) => {
107111
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
@@ -1113,7 +1117,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
11131117
}
11141118
}
11151119

1116-
/// Assembles the `Sized` and `MetaSized` traits which are built-in to the language itself.
1120+
/// Assembles the `Sized`, `MetaSized` and `PointeeSized` traits which are built-in to the
1121+
/// language itself.
11171122
#[instrument(level = "debug", skip(self, candidates))]
11181123
fn assemble_builtin_sized_candidate(
11191124
&mut self,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
270270
self.sizedness_conditions(obligation, SizedTraitKind::MetaSized)
271271
}
272272
Some(LangItem::PointeeSized) => {
273-
bug!("`PointeeSized` is removing during lowering");
273+
self.sizedness_conditions(obligation, SizedTraitKind::PointeeSized)
274274
}
275275
Some(LangItem::Copy | LangItem::Clone) => self.copy_clone_conditions(obligation),
276276
Some(LangItem::FusedIterator) => self.fused_iterator_conditions(obligation),

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,10 +2129,15 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
21292129

21302130
ty::Str | ty::Slice(_) | ty::Dynamic(..) => match sizedness {
21312131
SizedTraitKind::Sized => None,
2132-
SizedTraitKind::MetaSized => Where(ty::Binder::dummy(Vec::new())),
2132+
SizedTraitKind::MetaSized | SizedTraitKind::PointeeSized => {
2133+
Where(ty::Binder::dummy(Vec::new()))
2134+
}
21332135
},
21342136

2135-
ty::Foreign(..) => None,
2137+
ty::Foreign(..) => match sizedness {
2138+
SizedTraitKind::Sized | SizedTraitKind::MetaSized => None,
2139+
SizedTraitKind::PointeeSized => Where(ty::Binder::dummy(Vec::new())),
2140+
},
21362141

21372142
ty::Tuple(tys) => Where(
21382143
obligation.predicate.rebind(tys.last().map_or_else(Vec::new, |&last| vec![last])),

compiler/rustc_trait_selection/src/traits/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ pub fn sizedness_fast_path<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
374374
let sizedness = match tcx.as_lang_item(trait_ref.def_id()) {
375375
Some(LangItem::Sized) => SizedTraitKind::Sized,
376376
Some(LangItem::MetaSized) => SizedTraitKind::MetaSized,
377+
Some(LangItem::PointeeSized) => SizedTraitKind::PointeeSized,
377378
_ => return false,
378379
};
379380

compiler/rustc_ty_utils/src/ty.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn sizedness_constraint_for_ty<'tcx>(
2323
ty: Ty<'tcx>,
2424
) -> Option<Ty<'tcx>> {
2525
match ty.kind() {
26-
// Always `Sized` or `MetaSized`
26+
// Always `{Meta,Pointee,}Sized`
2727
ty::Bool
2828
| ty::Char
2929
| ty::Int(..)
@@ -44,11 +44,11 @@ fn sizedness_constraint_for_ty<'tcx>(
4444
ty::Str | ty::Slice(..) | ty::Dynamic(_, _, ty::Dyn) => match sizedness {
4545
// Never `Sized`
4646
SizedTraitKind::Sized => Some(ty),
47-
// Always `MetaSized`
48-
SizedTraitKind::MetaSized => None,
47+
// Always `{Meta,Pointee}Sized`
48+
SizedTraitKind::MetaSized | SizedTraitKind::PointeeSized => None,
4949
},
5050

51-
// Maybe `Sized` or `MetaSized`
51+
// Maybe `{Meta,Pointee,}Sized`
5252
ty::Param(..) | ty::Alias(..) | ty::Error(_) => Some(ty),
5353

5454
// We cannot instantiate the binder, so just return the *original* type back,
@@ -58,8 +58,11 @@ fn sizedness_constraint_for_ty<'tcx>(
5858
sizedness_constraint_for_ty(tcx, sizedness, inner_ty.skip_binder()).map(|_| ty)
5959
}
6060

61-
// Never `MetaSized` or `Sized`
62-
ty::Foreign(..) => Some(ty),
61+
// Never `{Meta,}Sized`
62+
ty::Foreign(..) => match sizedness {
63+
SizedTraitKind::Sized | SizedTraitKind::MetaSized => Some(ty),
64+
SizedTraitKind::PointeeSized => None,
65+
},
6366

6467
// Recursive cases
6568
ty::Pat(ty, _) => sizedness_constraint_for_ty(tcx, sizedness, *ty),

compiler/rustc_type_ir/src/solve/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ pub enum SizedTraitKind {
377377
Sized,
378378
/// `MetaSized` trait
379379
MetaSized,
380+
/// `PointeeSized` trait
381+
PointeeSized,
380382
}
381383

382384
impl SizedTraitKind {
@@ -385,6 +387,7 @@ impl SizedTraitKind {
385387
cx.require_lang_item(match self {
386388
SizedTraitKind::Sized => TraitSolverLangItem::Sized,
387389
SizedTraitKind::MetaSized => TraitSolverLangItem::MetaSized,
390+
SizedTraitKind::PointeeSized => TraitSolverLangItem::PointeeSized,
388391
})
389392
}
390393
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ check-pass
2+
#![feature(sized_hierarchy)]
3+
4+
use std::marker::PointeeSized;
5+
6+
type Foo = dyn PointeeSized;
7+
8+
fn foo(f: &Foo) {}
9+
10+
fn main() {
11+
foo(&());
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(sized_hierarchy)]
2+
3+
use std::marker::PointeeSized;
4+
5+
fn main() {
6+
let x = main;
7+
let y: Box<dyn PointeeSized> = x;
8+
//~^ ERROR mismatched types
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/dyn-pointeesized-issue-142652.rs:7:38
3+
|
4+
LL | let y: Box<dyn PointeeSized> = x;
5+
| --------------------- ^ expected `Box<dyn PointeeSized>`, found fn item
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected struct `Box<dyn PointeeSized>`
10+
found fn item `fn() {main}`
11+
= note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
12+
help: store this in the heap by calling `Box::new`
13+
|
14+
LL | let y: Box<dyn PointeeSized> = Box::new(x);
15+
| +++++++++ +
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)