Skip to content

Commit 953e9d1

Browse files
committed
Backport new sized-hierarchy trait bounds in old ways
1 parent d3e6dcd commit 953e9d1

File tree

5 files changed

+78
-24
lines changed

5 files changed

+78
-24
lines changed

crates/hir-def/src/lang_item.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ impl LangItem {
308308
language_item_table! {
309309
// Variant name, Name, Getter method name, Target Generic requirements;
310310
Sized, sym::sized, sized_trait, Target::Trait, GenericRequirement::Exact(0);
311+
MetaSized, sym::meta_sized, sized_trait, Target::Trait, GenericRequirement::Exact(0);
312+
PointeeSized, sym::pointee_sized, sized_trait, Target::Trait, GenericRequirement::Exact(0);
311313
Unsize, sym::unsize, unsize_trait, Target::Trait, GenericRequirement::Minimum(1);
312314
/// Trait injected by `#[derive(PartialEq)]`, (i.e. "Partial EQ").
313315
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait, GenericRequirement::None;

crates/hir-ty/src/lower.rs

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,28 @@ impl<'a> TyLoweringContext<'a> {
581581
match bound {
582582
&TypeBound::Path(path, TraitBoundModifier::None) | &TypeBound::ForLifetime(_, path) => {
583583
// FIXME Don't silently drop the hrtb lifetimes here
584-
if let Some((trait_ref, ctx)) = self.lower_trait_ref_from_path(path, self_ty) {
585-
if !ignore_bindings {
586-
assoc_bounds = ctx.assoc_type_bindings_from_type_bound(trait_ref.clone());
584+
if let Some((trait_ref, mut ctx)) =
585+
self.lower_trait_ref_from_path(path, self_ty.clone())
586+
{
587+
// FIXME(sized-hierarchy): Remove this bound modifications once we have implemented
588+
// sized-hierarchy correctly.
589+
let meta_sized = LangItem::MetaSized
590+
.resolve_trait(ctx.ty_ctx().db, ctx.ty_ctx().resolver.krate());
591+
let pointee_sized = LangItem::PointeeSized
592+
.resolve_trait(ctx.ty_ctx().db, ctx.ty_ctx().resolver.krate());
593+
if meta_sized.is_some_and(|it| it == trait_ref.hir_trait_id()) {
594+
// Ignore this bound
595+
} else if pointee_sized.is_some_and(|it| it == trait_ref.hir_trait_id()) {
596+
// Regard this as `?Sized` bound
597+
ctx.ty_ctx().unsized_types.insert(self_ty);
598+
} else {
599+
if !ignore_bindings {
600+
assoc_bounds =
601+
ctx.assoc_type_bindings_from_type_bound(trait_ref.clone());
602+
}
603+
clause =
604+
Some(crate::wrap_empty_binders(WhereClause::Implemented(trait_ref)));
587605
}
588-
clause = Some(crate::wrap_empty_binders(WhereClause::Implemented(trait_ref)));
589606
}
590607
}
591608
&TypeBound::Path(path, TraitBoundModifier::Maybe) => {
@@ -945,8 +962,32 @@ pub(crate) fn generic_predicates_for_param_query(
945962
| WherePredicate::TypeBound { target, bound, .. } => {
946963
let invalid_target = { ctx.lower_ty_only_param(*target) != Some(param_id) };
947964
if invalid_target {
948-
// If this is filtered out without lowering, `?Sized` is not gathered into `ctx.unsized_types`
949-
if let TypeBound::Path(_, TraitBoundModifier::Maybe) = bound {
965+
// FIXME(sized-hierarchy): Revisit and adjust this properly once we have implemented
966+
// sized-hierarchy correctly.
967+
// If this is filtered out without lowering, `?Sized` or `PointeeSized` is not gathered into
968+
// `ctx.unsized_types`
969+
let lower = || -> bool {
970+
match bound {
971+
TypeBound::Path(_, TraitBoundModifier::Maybe) => true,
972+
TypeBound::Path(path, _) | TypeBound::ForLifetime(_, path) => {
973+
let TypeRef::Path(path) = &ctx.store[path.type_ref()] else {
974+
return false;
975+
};
976+
let Some(pointee_sized) =
977+
LangItem::PointeeSized.resolve_trait(ctx.db, ctx.resolver.krate())
978+
else {
979+
return false;
980+
};
981+
// Lower the path directly with `Resolver` instead of PathLoweringContext`
982+
// to prevent diagnostics duplications.
983+
ctx.resolver.resolve_path_in_type_ns_fully(ctx.db, path).is_some_and(
984+
|it| matches!(it, TypeNs::TraitId(tr) if tr == pointee_sized),
985+
)
986+
}
987+
_ => false,
988+
}
989+
}();
990+
if lower {
950991
ctx.lower_where_predicate(pred, true).for_each(drop);
951992
}
952993
return false;

crates/ide/src/inlay_hints/bounds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fn foo<T>() {}
143143
file_id: FileId(
144144
1,
145145
),
146-
range: 135..140,
146+
range: 446..451,
147147
},
148148
),
149149
),

crates/intern/src/symbol/symbols.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ define_symbols! {
438438
shr,
439439
simd,
440440
sized,
441+
meta_sized,
442+
pointee_sized,
441443
skip,
442444
slice_len_fn,
443445
Some,

crates/test-utils/src/minicore.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! deref: sized
2727
//! derive:
2828
//! discriminant:
29-
//! drop:
29+
//! drop: sized
3030
//! env: option
3131
//! eq: sized
3232
//! error: fmt
@@ -37,7 +37,7 @@
3737
//! future: pin
3838
//! coroutine: pin
3939
//! dispatch_from_dyn: unsize, pin
40-
//! hash:
40+
//! hash: sized
4141
//! include:
4242
//! index: sized
4343
//! infallible:
@@ -80,24 +80,18 @@ pub mod marker {
8080
#[lang = "pointee_sized"]
8181
#[fundamental]
8282
#[rustc_specialization_trait]
83-
#[rustc_deny_explicit_impl]
84-
#[rustc_do_not_implement_via_object]
8583
#[rustc_coinductive]
8684
pub trait PointeeSized {}
8785

8886
#[lang = "meta_sized"]
8987
#[fundamental]
9088
#[rustc_specialization_trait]
91-
#[rustc_deny_explicit_impl]
92-
#[rustc_do_not_implement_via_object]
9389
#[rustc_coinductive]
9490
pub trait MetaSized: PointeeSized {}
9591

9692
#[lang = "sized"]
9793
#[fundamental]
9894
#[rustc_specialization_trait]
99-
#[rustc_deny_explicit_impl]
100-
#[rustc_do_not_implement_via_object]
10195
#[rustc_coinductive]
10296
pub trait Sized: MetaSized {}
10397
// endregion:sized
@@ -139,7 +133,7 @@ pub mod marker {
139133
// endregion:derive
140134

141135
mod copy_impls {
142-
use super::Copy;
136+
use super::{Copy, PointeeSized};
143137

144138
macro_rules! impl_copy {
145139
($($t:ty)*) => {
@@ -225,6 +219,8 @@ pub mod default {
225219

226220
// region:hash
227221
pub mod hash {
222+
use crate::marker::PointeeSized;
223+
228224
pub trait Hasher {}
229225

230226
pub trait Hash: PointeeSized {
@@ -240,6 +236,7 @@ pub mod hash {
240236

241237
// region:cell
242238
pub mod cell {
239+
use crate::marker::PointeeSized;
243240
use crate::mem;
244241

245242
#[lang = "unsafe_cell"]
@@ -376,7 +373,7 @@ pub mod convert {
376373
// endregion:from
377374

378375
// region:as_ref
379-
pub trait AsRef<T: PointeeSized>: PointeeSized {
376+
pub trait AsRef<T: crate::marker::PointeeSized>: crate::marker::PointeeSized {
380377
fn as_ref(&self) -> &T;
381378
}
382379
// endregion:as_ref
@@ -387,6 +384,8 @@ pub mod convert {
387384

388385
pub mod mem {
389386
// region:manually_drop
387+
use crate::marker::PointeeSized;
388+
390389
#[lang = "manually_drop"]
391390
#[repr(transparent)]
392391
pub struct ManuallyDrop<T: PointeeSized> {
@@ -447,7 +446,7 @@ pub mod mem {
447446
pub mod ptr {
448447
// region:drop
449448
#[lang = "drop_in_place"]
450-
pub unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T) {
449+
pub unsafe fn drop_in_place<T: crate::marker::PointeeSized>(to_drop: *mut T) {
451450
unsafe { drop_in_place(to_drop) }
452451
}
453452
pub const unsafe fn read<T>(src: *const T) -> T {
@@ -463,20 +462,22 @@ pub mod ptr {
463462
// region:pointee
464463
#[lang = "pointee_trait"]
465464
#[rustc_deny_explicit_impl(implement_via_object = false)]
466-
pub trait Pointee: PointeeSized {
465+
pub trait Pointee: crate::marker::PointeeSized {
467466
#[lang = "metadata_type"]
468467
type Metadata: Copy + Send + Sync + Ord + Hash + Unpin;
469468
}
470469
// endregion:pointee
471470
// region:non_null
472471
#[rustc_layout_scalar_valid_range_start(1)]
473472
#[rustc_nonnull_optimization_guaranteed]
474-
pub struct NonNull<T: PointeeSized> {
473+
pub struct NonNull<T: crate::marker::PointeeSized> {
475474
pointer: *const T,
476475
}
477476
// region:coerce_unsized
478-
impl<T: PointeeSized, U: PointeeSized> crate::ops::CoerceUnsized<NonNull<U>> for NonNull<T> where
479-
T: crate::marker::Unsize<U>
477+
impl<T: crate::marker::PointeeSized, U: crate::marker::PointeeSized>
478+
crate::ops::CoerceUnsized<NonNull<U>> for NonNull<T>
479+
where
480+
T: crate::marker::Unsize<U>,
480481
{
481482
}
482483
// endregion:coerce_unsized
@@ -497,7 +498,7 @@ pub mod ptr {
497498
pub mod ops {
498499
// region:coerce_unsized
499500
mod unsize {
500-
use crate::marker::Unsize;
501+
use crate::marker::{PointeeSized, Unsize};
501502

502503
#[lang = "coerce_unsized"]
503504
pub trait CoerceUnsized<T> {}
@@ -519,6 +520,8 @@ pub mod ops {
519520

520521
// region:deref
521522
mod deref {
523+
use crate::marker::PointeeSized;
524+
522525
#[lang = "deref"]
523526
pub trait Deref: PointeeSized {
524527
#[lang = "deref_target"]
@@ -1025,7 +1028,7 @@ pub mod ops {
10251028

10261029
// region:dispatch_from_dyn
10271030
mod dispatch_from_dyn {
1028-
use crate::marker::Unsize;
1031+
use crate::marker::{PointeeSized, Unsize};
10291032

10301033
#[lang = "dispatch_from_dyn"]
10311034
pub trait DispatchFromDyn<T> {}
@@ -1044,6 +1047,8 @@ pub mod ops {
10441047

10451048
// region:eq
10461049
pub mod cmp {
1050+
use crate::marker::PointeeSized;
1051+
10471052
#[lang = "eq"]
10481053
pub trait PartialEq<Rhs: PointeeSized = Self>: PointeeSized {
10491054
fn eq(&self, other: &Rhs) -> bool;
@@ -1090,6 +1095,8 @@ pub mod cmp {
10901095

10911096
// region:fmt
10921097
pub mod fmt {
1098+
use crate::marker::PointeeSized;
1099+
10931100
pub struct Error;
10941101
pub type Result = crate::result::Result<(), Error>;
10951102
pub struct Formatter<'a>;
@@ -1531,6 +1538,8 @@ pub mod iter {
15311538

15321539
mod traits {
15331540
mod iterator {
1541+
use crate::marker::PointeeSized;
1542+
15341543
#[doc(notable_trait)]
15351544
#[lang = "iterator"]
15361545
pub trait Iterator {

0 commit comments

Comments
 (0)