Skip to content

Commit a6c1fa0

Browse files
Merge pull request #20100 from ShoyuVanilla/ignore-sized-hierarchy
Backport new sized-hierarchy trait bounds in old ways
2 parents 317c76f + 953e9d1 commit a6c1fa0

File tree

7 files changed

+142
-65
lines changed

7 files changed

+142
-65
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-assists/src/handlers/generate_enum_is_method.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::slice;
2+
13
use ide_db::assists::GroupLabel;
24
use stdx::to_lower_snake_case;
35
use syntax::ast::HasVisibility;
@@ -52,7 +54,7 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext<'_>
5254
let fn_name = format!("is_{}", &to_lower_snake_case(&variant_name.text()));
5355

5456
// Return early if we've found an existing new fn
55-
let impl_def = find_struct_impl(ctx, &parent_enum, &[fn_name.clone()])?;
57+
let impl_def = find_struct_impl(ctx, &parent_enum, slice::from_ref(&fn_name))?;
5658

5759
let target = variant.syntax().text_range();
5860
acc.add_group(

crates/ide-assists/src/handlers/generate_enum_projection_method.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::slice;
2+
13
use ide_db::assists::GroupLabel;
24
use itertools::Itertools;
35
use stdx::to_lower_snake_case;
@@ -148,7 +150,7 @@ fn generate_enum_projection_method(
148150
let fn_name = format!("{fn_name_prefix}_{}", &to_lower_snake_case(&variant_name.text()));
149151

150152
// Return early if we've found an existing new fn
151-
let impl_def = find_struct_impl(ctx, &parent_enum, &[fn_name.clone()])?;
153+
let impl_def = find_struct_impl(ctx, &parent_enum, slice::from_ref(&fn_name))?;
152154

153155
let target = variant.syntax().text_range();
154156
acc.add_group(

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,

0 commit comments

Comments
 (0)