Skip to content

Adjust minicore for Sized Hierarchy changes #20091

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions crates/hir-def/src/lang_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ impl LangItem {
language_item_table! {
// Variant name, Name, Getter method name, Target Generic requirements;
Sized, sym::sized, sized_trait, Target::Trait, GenericRequirement::Exact(0);
MetaSized, sym::meta_sized, sized_trait, Target::Trait, GenericRequirement::Exact(0);
PointeeSized, sym::pointee_sized, sized_trait, Target::Trait, GenericRequirement::Exact(0);
Unsize, sym::unsize, unsize_trait, Target::Trait, GenericRequirement::Minimum(1);
/// Trait injected by `#[derive(PartialEq)]`, (i.e. "Partial EQ").
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait, GenericRequirement::None;
Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/src/chalk_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ pub(crate) fn associated_ty_data_query(
Arc::new(datum)
}

// FIXME(sized-hierarchy)
pub(crate) fn trait_datum_query(
db: &dyn HirDatabase,
krate: Crate,
Expand Down
6 changes: 6 additions & 0 deletions crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@
use cast::{CastCheck, CastError};
pub(crate) use closure::{CaptureKind, CapturedItem, CapturedItemWithoutTy};

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum SizedTraitKind {
Sized,
MetaSized,

Check failure on line 92 in crates/hir-ty/src/infer.rs

View workflow job for this annotation

GitHub Actions / Rust (ubuntu-latest)

variant `MetaSized` is never constructed
}

/// The entry point of type inference.
pub(crate) fn infer_query(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
let _p = tracing::info_span!("infer_query").entered();
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-ty/src/infer/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl CastCheck {
}

if !self.cast_ty.data(Interner).flags.contains(TypeFlags::HAS_TY_INFER)
&& !table.is_sized(&self.cast_ty)
&& !table.has_trivial_sizedness(&self.cast_ty, super::SizedTraitKind::Sized)
{
return Err(InferenceDiagnostic::CastToUnsized {
expr: self.expr,
Expand Down Expand Up @@ -175,7 +175,7 @@ impl CastCheck {
// array-ptr-cast
CastTy::Ptr(t, m) => {
let t = table.eagerly_normalize_and_resolve_shallow_in(t);
if !table.is_sized(&t) {
if !table.has_trivial_sizedness(&t, super::SizedTraitKind::Sized) {
return Err(CastError::IllegalCast);
}
self.check_ref_cast(
Expand Down Expand Up @@ -369,7 +369,7 @@ enum PointerKind {
fn pointer_kind(ty: &Ty, table: &mut InferenceTable<'_>) -> Result<Option<PointerKind>, ()> {
let ty = table.eagerly_normalize_and_resolve_shallow_in(ty.clone());

if table.is_sized(&ty) {
if table.has_trivial_sizedness(&ty, super::SizedTraitKind::Sized) {
return Ok(Some(PointerKind::Thin));
}

Expand Down
19 changes: 13 additions & 6 deletions crates/hir-ty/src/infer/unify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
Lifetime, OpaqueTyId, ParamKind, ProjectionTy, ProjectionTyExt, Scalar, Solution, Substitution,
TraitEnvironment, TraitRef, Ty, TyBuilder, TyExt, TyKind, VariableKind, WhereClause,
consteval::unknown_const, db::HirDatabase, fold_generic_args, fold_tys_and_consts,
to_chalk_trait_id, traits::FnTrait,
infer::SizedTraitKind, to_chalk_trait_id, traits::FnTrait,
};

impl InferenceContext<'_> {
Expand Down Expand Up @@ -975,8 +975,8 @@ impl<'a> InferenceTable<'a> {
}

/// Check if given type is `Sized` or not
pub(crate) fn is_sized(&mut self, ty: &Ty) -> bool {
fn short_circuit_trivial_tys(ty: &Ty) -> Option<bool> {
pub(crate) fn has_trivial_sizedness(&mut self, ty: &Ty, sizedness: SizedTraitKind) -> bool {
fn short_circuit_trivial_tys(ty: &Ty, sizedness: SizedTraitKind) -> Option<bool> {
match ty.kind(Interner) {
TyKind::Scalar(..)
| TyKind::Ref(..)
Expand All @@ -985,14 +985,20 @@ impl<'a> InferenceTable<'a> {
| TyKind::FnDef(..)
| TyKind::Array(..)
| TyKind::Function(..) => Some(true),
TyKind::Slice(..) | TyKind::Str | TyKind::Dyn(..) => Some(false),
TyKind::Slice(..) | TyKind::Str | TyKind::Dyn(..) => Some(match sizedness {
SizedTraitKind::Sized => false,
SizedTraitKind::MetaSized => true,
}),
TyKind::Foreign(_) => Some(match sizedness {
SizedTraitKind::Sized | SizedTraitKind::MetaSized => false,
}),
_ => None,
}
}

let mut ty = ty.clone();
ty = self.eagerly_normalize_and_resolve_shallow_in(ty);
if let Some(sized) = short_circuit_trivial_tys(&ty) {
if let Some(sized) = short_circuit_trivial_tys(&ty, sizedness) {
return sized;
}

Expand All @@ -1015,7 +1021,7 @@ impl<'a> InferenceTable<'a> {
// as unsized by the chalk, so we do this manually.
ty = last_field_ty;
ty = self.eagerly_normalize_and_resolve_shallow_in(ty);
if let Some(sized) = short_circuit_trivial_tys(&ty) {
if let Some(sized) = short_circuit_trivial_tys(&ty, sizedness) {
return sized;
}
} else {
Expand All @@ -1024,6 +1030,7 @@ impl<'a> InferenceTable<'a> {
}
}

// FIXME(sized-hierarchy):
let Some(sized) = LangItem::Sized.resolve_trait(self.db, self.trait_env.krate) else {
return false;
};
Expand Down
26 changes: 20 additions & 6 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use hir_def::{
use hir_expand::name::Name;
use la_arena::{Arena, ArenaMap};
use rustc_hash::FxHashSet;
use stdx::{impl_from, never};
use stdx::{TupleExt, impl_from, never};
use triomphe::{Arc, ThinArc};

use crate::{
Expand Down Expand Up @@ -588,16 +588,28 @@ impl<'a> TyLoweringContext<'a> {
clause = Some(crate::wrap_empty_binders(WhereClause::Implemented(trait_ref)));
}
}
// FIXME(sized-hierarchy)
&TypeBound::Path(path, TraitBoundModifier::Maybe) => {
let sized_trait = LangItem::Sized.resolve_trait(self.db, self.resolver.krate());
// Don't lower associated type bindings as the only possible relaxed trait bound
// `?Sized` has no of them.
// If we got another trait here ignore the bound completely.
let trait_id = self
.lower_trait_ref_from_path(path, self_ty.clone())
.map(|(trait_ref, _)| trait_ref.hir_trait_id());
if trait_id == sized_trait {
let trait_id =
self.lower_trait_ref_from_path(path, self_ty.clone()).map(TupleExt::head);
if trait_id.as_ref().map(|trait_ref| trait_ref.hir_trait_id()) == sized_trait {
self.unsized_types.insert(self_ty);
clause = trait_id
.and_then(|it| {
Some(TraitRef {
trait_id: to_chalk_trait_id(
LangItem::MetaSized
.resolve_trait(self.db, self.resolver.krate())?,
),
substitution: it.substitution,
})
})
.map(WhereClause::Implemented)
.map(crate::wrap_empty_binders)
}
}
&TypeBound::Lifetime(l) => {
Expand Down Expand Up @@ -910,6 +922,7 @@ pub(crate) fn field_types_with_diagnostics_query(
(Arc::new(res), create_diagnostics(ctx.diagnostics))
}

// FIXME(sized-hierarchy)
/// This query exists only to be used when resolving short-hand associated types
/// like `T::Item`.
///
Expand Down Expand Up @@ -1130,6 +1143,7 @@ pub(crate) fn generic_predicates_without_parent_with_diagnostics_query(
generic_predicates_filtered_by(db, def, |_, d| d == def)
}

// FIXME(sized-hierarchy)
/// Resolve the where clause(s) of an item with generics,
/// except the ones inherited from the parent
fn generic_predicates_filtered_by<F>(
Expand Down Expand Up @@ -1163,7 +1177,6 @@ where
for pred in maybe_parent_generics.where_predicates() {
if filter(pred, maybe_parent_generics.def()) {
// We deliberately use `generics` and not `maybe_parent_generics` here. This is not a mistake!
// If we use the parent generics
predicates.extend(
ctx.lower_where_predicate(pred, false).map(|p| make_binders(db, &generics, p)),
);
Expand All @@ -1190,6 +1203,7 @@ where
)
}

// FIXME(sized-hierarchy)
/// Generate implicit `: Sized` predicates for all generics that has no `?Sized` bound.
/// Exception is Self of a trait def.
fn implicitly_sized_clauses<'db, 'a, 'subst: 'a>(
Expand Down
4 changes: 3 additions & 1 deletion crates/ide-assists/src/handlers/generate_enum_is_method.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::slice;

use ide_db::assists::GroupLabel;
use stdx::to_lower_snake_case;
use syntax::ast::HasVisibility;
Expand Down Expand Up @@ -52,7 +54,7 @@ pub(crate) fn generate_enum_is_method(acc: &mut Assists, ctx: &AssistContext<'_>
let fn_name = format!("is_{}", &to_lower_snake_case(&variant_name.text()));

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

let target = variant.syntax().text_range();
acc.add_group(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::slice;

use ide_db::assists::GroupLabel;
use itertools::Itertools;
use stdx::to_lower_snake_case;
Expand Down Expand Up @@ -148,7 +150,7 @@ fn generate_enum_projection_method(
let fn_name = format!("{fn_name_prefix}_{}", &to_lower_snake_case(&variant_name.text()));

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

let target = variant.syntax().text_range();
acc.add_group(
Expand Down
2 changes: 2 additions & 0 deletions crates/intern/src/symbol/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,8 @@ define_symbols! {
shr,
simd,
sized,
meta_sized,
pointee_sized,
skip,
slice_len_fn,
Some,
Expand Down
2 changes: 1 addition & 1 deletion crates/rust-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// Any toolchain less than this version will likely not work with rust-analyzer built from this revision.
pub const MINIMUM_SUPPORTED_TOOLCHAIN_VERSION: semver::Version = semver::Version {
major: 1,
minor: 78,
minor: 89,
patch: 0,
pre: semver::Prerelease::EMPTY,
build: semver::BuildMetadata::EMPTY,
Expand Down
Loading
Loading