Skip to content

trait_sel: add builtin impl for PointeeSized #142663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ impl<'tcx> Ty<'tcx> {
}
}

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

ty::Str | ty::Slice(_) | ty::Dynamic(_, _, ty::Dyn) => match sizedness {
SizedTraitKind::Sized => false,
SizedTraitKind::MetaSized => true,
SizedTraitKind::MetaSized | SizedTraitKind::PointeeSized => true,
},

ty::Foreign(..) => match sizedness {
SizedTraitKind::Sized | SizedTraitKind::MetaSized => false,
SizedTraitKind::PointeeSized => true,
},

ty::Tuple(tys) => tys.last().is_none_or(|ty| ty.has_trivial_sizedness(tcx, sizedness)),
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,11 @@ where
G::consider_builtin_sizedness_candidates(self, goal, SizedTraitKind::MetaSized)
}
Some(TraitSolverLangItem::PointeeSized) => {
unreachable!("`PointeeSized` is removed during lowering");
G::consider_builtin_sizedness_candidates(
self,
goal,
SizedTraitKind::PointeeSized,
)
}
Some(TraitSolverLangItem::Copy | TraitSolverLangItem::Clone) => {
G::consider_builtin_copy_clone_candidate(self, goal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ where
I: Interner,
{
match ty.kind() {
// impl {Meta,}Sized for u*, i*, bool, f*, FnDef, FnPtr, *(const/mut) T, char
// impl {Meta,}Sized for &mut? T, [T; N], dyn* Trait, !, Coroutine, CoroutineWitness
// impl {Meta,}Sized for Closure, CoroutineClosure
// impl {Meta,Pointee,}Sized for u*, i*, bool, f*, FnDef, FnPtr, *(const/mut) T, char
// impl {Meta,Pointee,}Sized for &mut? T, [T; N], dyn* Trait, !, Coroutine, CoroutineWitness
// impl {Meta,Pointee,}Sized for Closure, CoroutineClosure
ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
| ty::Uint(_)
| ty::Int(_)
Expand All @@ -138,14 +138,19 @@ where
| ty::Dynamic(_, _, ty::DynStar)
| ty::Error(_) => Ok(ty::Binder::dummy(vec![])),

// impl {Meta,}Sized for str, [T], dyn Trait
// impl {Meta,Pointee,}Sized for str, [T], dyn Trait
ty::Str | ty::Slice(_) | ty::Dynamic(..) => match sizedness {
SizedTraitKind::Sized => Err(NoSolution),
SizedTraitKind::MetaSized => Ok(ty::Binder::dummy(vec![])),
SizedTraitKind::MetaSized | SizedTraitKind::PointeeSized => {
Ok(ty::Binder::dummy(vec![]))
}
},

// impl {} for extern type
ty::Foreign(..) => Err(NoSolution),
// impl PointeeSized for extern type
ty::Foreign(..) => match sizedness {
SizedTraitKind::Sized | SizedTraitKind::MetaSized => Err(NoSolution),
SizedTraitKind::PointeeSized => Ok(ty::Binder::dummy(vec![])),
},

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

Expand All @@ -156,17 +161,17 @@ where

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

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

// impl {Meta,}Sized for Adt<Args...>
// impl {Meta,Pointee,}Sized for Adt<Args...>
// where {meta,pointee,}sized_constraint(Adt)<Args...>: {Meta,}Sized
//
// `{meta,pointee,}sized_constraint(Adt)` is the deepest struct trail that can be
// determined by the definition of `Adt`, independent of the generic args.
//
// impl {Meta,}Sized for Adt<Args...>
// impl {Meta,Pointee,}Sized for Adt<Args...>
// if {meta,pointee,}sized_constraint(Adt) == None
//
// As a performance optimization, `{meta,pointee,}sized_constraint(Adt)` can return `None`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// begin with in those cases.
if matches!(
self.tcx.as_lang_item(trait_pred.def_id()),
Some(LangItem::Sized | LangItem::MetaSized)
Some(LangItem::Sized | LangItem::MetaSized | LangItem::PointeeSized)
) {
match self.tainted_by_errors() {
None => {
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_trait_selection/src/solve/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
{
return Some(Certainty::Yes);
}
Some(LangItem::PointeeSized)
if self
.resolve_vars_if_possible(trait_pred.self_ty().skip_binder())
.has_trivial_sizedness(self.0.tcx, SizedTraitKind::PointeeSized) =>
{
return Some(Certainty::Yes);
}
Some(LangItem::Copy | LangItem::Clone) => {
let self_ty =
self.resolve_vars_if_possible(trait_pred.self_ty().skip_binder());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
);
}
Some(LangItem::PointeeSized) => {
bug!("`PointeeSized` is removed during lowering");
self.assemble_builtin_sized_candidate(
obligation,
&mut candidates,
SizedTraitKind::PointeeSized,
);
}
Some(LangItem::Unsize) => {
self.assemble_candidates_for_unsizing(obligation, &mut candidates);
Expand Down Expand Up @@ -1113,7 +1117,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
}
}

/// Assembles the `Sized` and `MetaSized` traits which are built-in to the language itself.
/// Assembles the `Sized`, `MetaSized` and `PointeeSized` traits which are built-in to the
/// language itself.
#[instrument(level = "debug", skip(self, candidates))]
fn assemble_builtin_sized_candidate(
&mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
self.sizedness_conditions(obligation, SizedTraitKind::MetaSized)
}
Some(LangItem::PointeeSized) => {
bug!("`PointeeSized` is removing during lowering");
self.sizedness_conditions(obligation, SizedTraitKind::PointeeSized)
}
Some(LangItem::Copy | LangItem::Clone) => self.copy_clone_conditions(obligation),
Some(LangItem::FusedIterator) => self.fused_iterator_conditions(obligation),
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2129,10 +2129,15 @@ impl<'tcx> SelectionContext<'_, 'tcx> {

ty::Str | ty::Slice(_) | ty::Dynamic(..) => match sizedness {
SizedTraitKind::Sized => None,
SizedTraitKind::MetaSized => Where(ty::Binder::dummy(Vec::new())),
SizedTraitKind::MetaSized | SizedTraitKind::PointeeSized => {
Where(ty::Binder::dummy(Vec::new()))
}
},

ty::Foreign(..) => None,
ty::Foreign(..) => match sizedness {
SizedTraitKind::Sized | SizedTraitKind::MetaSized => None,
SizedTraitKind::PointeeSized => Where(ty::Binder::dummy(Vec::new())),
},

ty::Tuple(tys) => Where(
obligation.predicate.rebind(tys.last().map_or_else(Vec::new, |&last| vec![last])),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_trait_selection/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ pub fn sizedness_fast_path<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tc
let sizedness = match tcx.as_lang_item(trait_ref.def_id()) {
Some(LangItem::Sized) => SizedTraitKind::Sized,
Some(LangItem::MetaSized) => SizedTraitKind::MetaSized,
Some(LangItem::PointeeSized) => SizedTraitKind::PointeeSized,
_ => return false,
};

Expand Down
15 changes: 9 additions & 6 deletions compiler/rustc_ty_utils/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn sizedness_constraint_for_ty<'tcx>(
ty: Ty<'tcx>,
) -> Option<Ty<'tcx>> {
match ty.kind() {
// Always `Sized` or `MetaSized`
// Always `{Meta,Pointee,}Sized`
ty::Bool
| ty::Char
| ty::Int(..)
Expand All @@ -44,11 +44,11 @@ fn sizedness_constraint_for_ty<'tcx>(
ty::Str | ty::Slice(..) | ty::Dynamic(_, _, ty::Dyn) => match sizedness {
// Never `Sized`
SizedTraitKind::Sized => Some(ty),
// Always `MetaSized`
SizedTraitKind::MetaSized => None,
// Always `{Meta,Pointee}Sized`
SizedTraitKind::MetaSized | SizedTraitKind::PointeeSized => None,
},

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

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

// Never `MetaSized` or `Sized`
ty::Foreign(..) => Some(ty),
// Never `{Meta,}Sized`
ty::Foreign(..) => match sizedness {
SizedTraitKind::Sized | SizedTraitKind::MetaSized => Some(ty),
SizedTraitKind::PointeeSized => None,
},

// Recursive cases
ty::Pat(ty, _) => sizedness_constraint_for_ty(tcx, sizedness, *ty),
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_type_ir/src/solve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ pub enum SizedTraitKind {
Sized,
/// `MetaSized` trait
MetaSized,
/// `PointeeSized` trait
PointeeSized,
}

impl SizedTraitKind {
Expand All @@ -385,6 +387,7 @@ impl SizedTraitKind {
cx.require_lang_item(match self {
SizedTraitKind::Sized => TraitSolverLangItem::Sized,
SizedTraitKind::MetaSized => TraitSolverLangItem::MetaSized,
SizedTraitKind::PointeeSized => TraitSolverLangItem::PointeeSized,
})
}
}
12 changes: 12 additions & 0 deletions tests/ui/sized-hierarchy/dyn-pointeesized-issue-142652-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ check-pass
#![feature(sized_hierarchy)]

use std::marker::PointeeSized;

type Foo = dyn PointeeSized;

fn foo(f: &Foo) {}

fn main() {
foo(&());
}
9 changes: 9 additions & 0 deletions tests/ui/sized-hierarchy/dyn-pointeesized-issue-142652.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![feature(sized_hierarchy)]

use std::marker::PointeeSized;

fn main() {
let x = main;
let y: Box<dyn PointeeSized> = x;
//~^ ERROR mismatched types
}
19 changes: 19 additions & 0 deletions tests/ui/sized-hierarchy/dyn-pointeesized-issue-142652.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0308]: mismatched types
--> $DIR/dyn-pointeesized-issue-142652.rs:7:38
|
LL | let y: Box<dyn PointeeSized> = x;
| --------------------- ^ expected `Box<dyn PointeeSized>`, found fn item
| |
| expected due to this
|
= note: expected struct `Box<dyn PointeeSized>`
found fn item `fn() {main}`
= 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
help: store this in the heap by calling `Box::new`
|
LL | let y: Box<dyn PointeeSized> = Box::new(x);
| +++++++++ +

error: aborting due to 1 previous error

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