Migrate Display impls to the next solver

This commit is contained in:
Chayim Refael Friedman 2025-10-14 21:30:34 +03:00
parent 2df61e02e4
commit 25ed1c5bfb
19 changed files with 689 additions and 1421 deletions

View file

@ -1,15 +1,14 @@
//! `TyBuilder`, a helper for building instances of `Ty` and related types.
use chalk_ir::{
AdtId, DebruijnIndex, Scalar,
cast::{Cast, CastTo, Caster},
DebruijnIndex, Scalar,
cast::{Cast, Caster},
};
use hir_def::{GenericDefId, GenericParamId, TraitId, TypeAliasId, builtin_type::BuiltinType};
use hir_def::{GenericDefId, GenericParamId, TraitId, builtin_type::BuiltinType};
use smallvec::SmallVec;
use crate::{
BoundVar, CallableSig, GenericArg, GenericArgData, Interner, ProjectionTy, Substitution,
TraitRef, Ty, TyDefId, TyExt, TyKind,
BoundVar, GenericArg, GenericArgData, Interner, Substitution, TraitRef, Ty, TyKind,
consteval::unknown_const_as_generic,
db::HirDatabase,
error_lifetime,
@ -19,18 +18,18 @@ use crate::{
DbInterner, EarlyBinder,
mapping::{ChalkToNextSolver, NextSolverToChalk},
},
primitive, to_assoc_type_id, to_chalk_trait_id,
primitive, to_chalk_trait_id,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParamKind {
pub(crate) enum ParamKind {
Type,
Lifetime,
Const(Ty),
}
/// This is a builder for `Ty` or anything that needs a `Substitution`.
pub struct TyBuilder<D> {
pub(crate) struct TyBuilder<D> {
/// The `data` field is used to keep track of what we're building (e.g. an
/// ADT, a `TraitRef`, ...).
data: D,
@ -60,10 +59,6 @@ impl<D> TyBuilder<D> {
Self { data, vec: SmallVec::with_capacity(param_kinds.len()), param_kinds, parent_subst }
}
fn new_empty(data: D) -> Self {
TyBuilder::new(data, SmallVec::new(), None)
}
fn build_internal(self) -> (D, Substitution) {
assert_eq!(
self.vec.len(),
@ -83,35 +78,15 @@ impl<D> TyBuilder<D> {
(self.data, subst)
}
pub fn build_into_subst(self) -> Substitution {
self.build_internal().1
}
pub fn push(mut self, arg: impl CastTo<GenericArg>) -> Self {
assert!(self.remaining() > 0);
let arg = arg.cast(Interner);
let expected_kind = &self.param_kinds[self.vec.len()];
let arg_kind = match arg.data(Interner) {
GenericArgData::Ty(_) => ParamKind::Type,
GenericArgData::Lifetime(_) => panic!("Got lifetime in TyBuilder::push"),
GenericArgData::Const(c) => {
let c = c.data(Interner);
ParamKind::Const(c.ty.clone())
}
};
assert_eq!(*expected_kind, arg_kind);
self.vec.push(arg);
self
}
pub fn remaining(&self) -> usize {
pub(crate) fn remaining(&self) -> usize {
self.param_kinds.len() - self.vec.len()
}
pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self {
pub(crate) fn fill_with_bound_vars(
self,
debruijn: DebruijnIndex,
starting_from: usize,
) -> Self {
// self.fill is inlined to make borrow checker happy
let mut this = self;
let other = &this.param_kinds[this.vec.len()..];
@ -129,22 +104,6 @@ impl<D> TyBuilder<D> {
this
}
pub fn fill_with_unknown(self) -> Self {
let interner = DbInterner::conjure();
// self.fill is inlined to make borrow checker happy
let mut this = self;
let filler = this.param_kinds[this.vec.len()..].iter().map(|x| match x {
ParamKind::Type => TyKind::Error.intern(Interner).cast(Interner),
ParamKind::Const(ty) => {
unknown_const_as_generic(ty.to_nextsolver(interner)).to_chalk(interner)
}
ParamKind::Lifetime => error_lifetime().cast(Interner),
});
this.vec.extend(filler.casted(Interner));
assert_eq!(this.remaining(), 0);
this
}
#[tracing::instrument(skip_all)]
pub(crate) fn fill_with_inference_vars(self, table: &mut InferenceTable<'_>) -> Self {
self.fill(|x| {
@ -157,7 +116,7 @@ impl<D> TyBuilder<D> {
})
}
pub fn fill(mut self, filler: impl FnMut(&ParamKind) -> GenericArg) -> Self {
pub(crate) fn fill(mut self, filler: impl FnMut(&ParamKind) -> GenericArg) -> Self {
self.vec.extend(self.param_kinds[self.vec.len()..].iter().map(filler));
assert_eq!(self.remaining(), 0);
self
@ -174,28 +133,11 @@ impl<D> TyBuilder<D> {
}
impl TyBuilder<()> {
pub fn unit() -> Ty {
TyKind::Tuple(0, Substitution::empty(Interner)).intern(Interner)
}
// FIXME: rustc's ty is dependent on the adt type, maybe we need to do that as well
pub fn discr_ty() -> Ty {
TyKind::Scalar(chalk_ir::Scalar::Int(chalk_ir::IntTy::I128)).intern(Interner)
}
pub fn bool() -> Ty {
TyKind::Scalar(chalk_ir::Scalar::Bool).intern(Interner)
}
pub fn usize() -> Ty {
pub(crate) fn usize() -> Ty {
TyKind::Scalar(chalk_ir::Scalar::Uint(chalk_ir::UintTy::Usize)).intern(Interner)
}
pub fn fn_ptr(sig: CallableSig) -> Ty {
TyKind::Function(sig.to_fn_ptr()).intern(Interner)
}
pub fn builtin(builtin: BuiltinType) -> Ty {
pub(crate) fn builtin(builtin: BuiltinType) -> Ty {
match builtin {
BuiltinType::Char => TyKind::Scalar(Scalar::Char).intern(Interner),
BuiltinType::Bool => TyKind::Scalar(Scalar::Bool).intern(Interner),
@ -212,16 +154,10 @@ impl TyBuilder<()> {
}
}
pub fn slice(argument: Ty) -> Ty {
TyKind::Slice(argument).intern(Interner)
}
pub fn placeholder_subst(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substitution {
let params = generics(db, def.into());
params.placeholder_subst(db)
}
pub fn unknown_subst(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substitution {
pub(crate) fn unknown_subst(
db: &dyn HirDatabase,
def: impl Into<GenericDefId>,
) -> Substitution {
let interner = DbInterner::conjure();
let params = generics(db, def.into());
Substitution::from_iter(
@ -239,7 +175,7 @@ impl TyBuilder<()> {
}
#[tracing::instrument(skip_all)]
pub fn subst_for_def(
pub(crate) fn subst_for_def(
db: &dyn HirDatabase,
def: impl Into<GenericDefId>,
parent_subst: Option<Substitution>,
@ -257,114 +193,25 @@ impl TyBuilder<()> {
TyBuilder::new((), params, parent_subst)
}
pub fn build(self) -> Substitution {
pub(crate) fn build(self) -> Substitution {
let ((), subst) = self.build_internal();
subst
}
}
impl TyBuilder<hir_def::AdtId> {
pub fn adt(db: &dyn HirDatabase, def: hir_def::AdtId) -> TyBuilder<hir_def::AdtId> {
TyBuilder::subst_for_def(db, def, None).with_data(def)
}
pub fn fill_with_defaults(
mut self,
db: &dyn HirDatabase,
mut fallback: impl FnMut() -> Ty,
) -> Self {
let interner = DbInterner::conjure();
// Note that we're building ADT, so we never have parent generic parameters.
let defaults = db.generic_defaults(self.data.into());
if let Some(defaults) = defaults.get(self.vec.len()..) {
for default_ty in defaults {
// NOTE(skip_binders): we only check if the arg type is error type.
if let Some(x) = default_ty.skip_binders().ty(Interner)
&& x.is_unknown()
{
self.vec.push(fallback().cast(Interner));
continue;
}
// Each default can only depend on the previous parameters.
self.vec.push(default_ty.clone().substitute(Interner, &*self.vec).cast(Interner));
}
}
// The defaults may be missing if no param has default, so fill that.
let filler = self.param_kinds[self.vec.len()..].iter().map(|x| match x {
ParamKind::Type => fallback().cast(Interner),
ParamKind::Const(ty) => {
unknown_const_as_generic(ty.to_nextsolver(interner)).to_chalk(interner)
}
ParamKind::Lifetime => error_lifetime().cast(Interner),
});
self.vec.extend(filler.casted(Interner));
self
}
pub fn build(self) -> Ty {
let (adt, subst) = self.build_internal();
TyKind::Adt(AdtId(adt), subst).intern(Interner)
}
}
pub struct Tuple(usize);
impl TyBuilder<Tuple> {
pub fn tuple(size: usize) -> TyBuilder<Tuple> {
TyBuilder::new(Tuple(size), std::iter::repeat_n(ParamKind::Type, size).collect(), None)
}
pub fn build(self) -> Ty {
let (Tuple(size), subst) = self.build_internal();
TyKind::Tuple(size, subst).intern(Interner)
}
pub fn tuple_with<I>(elements: I) -> Ty
where
I: IntoIterator<Item = Ty>,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
{
let elements = elements.into_iter();
let len = elements.len();
let mut b =
TyBuilder::new(Tuple(len), std::iter::repeat_n(ParamKind::Type, len).collect(), None);
for e in elements {
b = b.push(e);
}
b.build()
}
}
impl TyBuilder<TraitId> {
pub fn trait_ref(db: &dyn HirDatabase, def: TraitId) -> TyBuilder<TraitId> {
pub(crate) fn trait_ref(db: &dyn HirDatabase, def: TraitId) -> TyBuilder<TraitId> {
TyBuilder::subst_for_def(db, def, None).with_data(def)
}
pub fn build(self) -> TraitRef {
pub(crate) fn build(self) -> TraitRef {
let (trait_id, substitution) = self.build_internal();
TraitRef { trait_id: to_chalk_trait_id(trait_id), substitution }
}
}
impl TyBuilder<TypeAliasId> {
pub fn assoc_type_projection(
db: &dyn HirDatabase,
def: TypeAliasId,
parent_subst: Option<Substitution>,
) -> TyBuilder<TypeAliasId> {
TyBuilder::subst_for_def(db, def, parent_subst).with_data(def)
}
pub fn build(self) -> ProjectionTy {
let (type_alias, substitution) = self.build_internal();
ProjectionTy { associated_ty_id: to_assoc_type_id(type_alias), substitution }
}
}
impl<'db, T: rustc_type_ir::TypeFoldable<DbInterner<'db>>> TyBuilder<EarlyBinder<'db, T>> {
pub fn build(self, interner: DbInterner<'db>) -> T {
pub(crate) fn build(self, interner: DbInterner<'db>) -> T {
let (b, subst) = self.build_internal();
let args: crate::next_solver::GenericArgs<'db> = subst.to_nextsolver(interner);
b.instantiate(interner, args)
@ -372,24 +219,7 @@ impl<'db, T: rustc_type_ir::TypeFoldable<DbInterner<'db>>> TyBuilder<EarlyBinder
}
impl<'db> TyBuilder<EarlyBinder<'db, crate::next_solver::Ty<'db>>> {
pub fn def_ty(
db: &'db dyn HirDatabase,
def: TyDefId,
parent_subst: Option<Substitution>,
) -> TyBuilder<EarlyBinder<'db, crate::next_solver::Ty<'db>>> {
let poly_ty = db.ty(def);
let id: GenericDefId = match def {
TyDefId::BuiltinType(_) => {
assert!(parent_subst.is_none());
return TyBuilder::new_empty(poly_ty);
}
TyDefId::AdtId(id) => id.into(),
TyDefId::TypeAliasId(id) => id.into(),
};
TyBuilder::subst_for_def(db, id, parent_subst).with_data(poly_ty)
}
pub fn impl_self_ty(
pub(crate) fn impl_self_ty(
db: &'db dyn HirDatabase,
def: hir_def::ImplId,
) -> TyBuilder<EarlyBinder<'db, crate::next_solver::Ty<'db>>> {

View file

@ -3,47 +3,11 @@
use hir_def::{ItemContainerId, Lookup, TraitId};
use crate::{
Binders, CallableSig, DynTy, Interner, ProjectionTy, Substitution, TraitRef, Ty, TyKind,
db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, generics::generics,
to_chalk_trait_id, utils::ClosureSubst,
Binders, DynTy, Interner, ProjectionTy, Substitution, TraitRef, Ty, db::HirDatabase,
from_assoc_type_id, from_chalk_trait_id, generics::generics, to_chalk_trait_id,
};
pub(crate) trait TyExt {
fn is_unit(&self) -> bool;
fn is_unknown(&self) -> bool;
fn as_tuple(&self) -> Option<&Substitution>;
fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig>;
}
impl TyExt for Ty {
fn is_unit(&self) -> bool {
matches!(self.kind(Interner), TyKind::Tuple(0, _))
}
fn is_unknown(&self) -> bool {
matches!(self.kind(Interner), TyKind::Error)
}
fn as_tuple(&self) -> Option<&Substitution> {
match self.kind(Interner) {
TyKind::Tuple(_, substs) => Some(substs),
_ => None,
}
}
fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> {
match self.kind(Interner) {
TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)),
TyKind::FnDef(def, parameters) => Some(CallableSig::from_def(db, *def, parameters)),
TyKind::Closure(.., substs) => ClosureSubst(substs).sig_ty(db).callable_sig(db),
_ => None,
}
}
}
pub trait ProjectionTyExt {
pub(crate) trait ProjectionTyExt {
fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef;
fn trait_(&self, db: &dyn HirDatabase) -> TraitId;
fn self_type_parameter(&self, db: &dyn HirDatabase) -> Ty;
@ -88,7 +52,7 @@ impl DynTyExt for DynTy {
}
}
pub trait TraitRefExt {
pub(crate) trait TraitRefExt {
fn hir_trait_id(&self) -> TraitId;
}

View file

@ -24,7 +24,6 @@ use crate::{
lower::{Diagnostics, GenericDefaults, GenericPredicates},
method_resolution::{InherentImpls, TraitImpls, TyFingerprint},
mir::{BorrowckResult, MirBody, MirLowerError},
traits::NextTraitSolveResult,
};
#[query_group::query_group]

View file

@ -18,7 +18,7 @@ use hir_def::{
use hir_expand::name::Name;
use rustc_type_ir::inherent::{IntoKind, SliceLike};
use span::Edition;
use stdx::{always, never};
use stdx::{always, never, variance::PhantomCovariantLifetime};
use crate::{
InferenceResult,
@ -299,8 +299,8 @@ impl<'a, 'db> PatCtxt<'a, 'db> {
}
}
impl HirDisplay for Pat<'_> {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Pat<'db> {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
match &*self.kind {
PatKind::Wild => write!(f, "_"),
PatKind::Never => write!(f, "!"),
@ -356,7 +356,7 @@ impl HirDisplay for Pat<'_> {
.filter(|p| !matches!(*p.pattern.kind, PatKind::Wild))
.map(|p| {
printed += 1;
WriteWith(|f| {
WriteWith::new(|f| {
write!(
f,
"{}: ",
@ -382,7 +382,7 @@ impl HirDisplay for Pat<'_> {
if num_fields != 0 || variant.is_none() {
write!(f, "(")?;
let subpats = (0..num_fields).map(|i| {
WriteWith(move |f| {
WriteWith::new(move |f| {
let fid = LocalFieldId::from_raw((i as u32).into());
if let Some(p) = subpatterns.get(i)
&& p.field == fid
@ -420,15 +420,24 @@ impl HirDisplay for Pat<'_> {
}
}
struct WriteWith<F>(F)
struct WriteWith<'db, F>(F, PhantomCovariantLifetime<'db>)
where
F: Fn(&mut HirFormatter<'_>) -> Result<(), HirDisplayError>;
F: Fn(&mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError>;
impl<F> HirDisplay for WriteWith<F>
impl<'db, F> WriteWith<'db, F>
where
F: Fn(&mut HirFormatter<'_>) -> Result<(), HirDisplayError>,
F: Fn(&mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError>,
{
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
fn new(f: F) -> Self {
Self(f, PhantomCovariantLifetime::new())
}
}
impl<'db, F> HirDisplay<'db> for WriteWith<'db, F>
where
F: Fn(&mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError>,
{
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
(self.0)(f)
}
}

File diff suppressed because it is too large Load diff

View file

@ -130,11 +130,16 @@ impl Generics {
/// Returns total number of generic parameters in scope, including those from parent.
pub(crate) fn len(&self) -> usize {
let parent = self.parent_generics().map_or(0, Generics::len);
let parent = self.len_parent();
let child = self.params.len();
parent + child
}
#[inline]
pub(crate) fn len_parent(&self) -> usize {
self.parent_generics().map_or(0, Generics::len)
}
/// Returns numbers of generic parameters excluding those from parent.
pub(crate) fn len_self(&self) -> usize {
self.params.len()

View file

@ -55,8 +55,7 @@ use stdx::never;
use triomphe::Arc;
use crate::{
ImplTraitId, IncorrectGenericsLenKind, Interner, PathLoweringDiagnostic, TargetFeatures,
TraitEnvironment,
ImplTraitId, IncorrectGenericsLenKind, PathLoweringDiagnostic, TargetFeatures,
db::{HirDatabase, InternedClosureId, InternedOpaqueTyId},
generics::Generics,
infer::{
@ -77,7 +76,7 @@ use crate::{
DefineOpaqueTypes,
traits::{Obligation, ObligationCause},
},
mapping::{ChalkToNextSolver, NextSolverToChalk},
mapping::ChalkToNextSolver,
},
traits::FnTrait,
utils::TargetFeatureIsSafeInTarget,
@ -166,31 +165,6 @@ pub(crate) fn infer_cycle_result(
})
}
/// Fully normalize all the types found within `ty` in context of `owner` body definition.
///
/// This is appropriate to use only after type-check: it assumes
/// that normalization will succeed, for example.
#[tracing::instrument(level = "debug", skip(db))]
pub(crate) fn normalize(
db: &dyn HirDatabase,
trait_env: Arc<TraitEnvironment<'_>>,
ty: crate::Ty,
) -> crate::Ty {
// FIXME: TypeFlags::HAS_CT_PROJECTION is not implemented in chalk, so TypeFlags::HAS_PROJECTION only
// works for the type case, so we check array unconditionally. Remove the array part
// when the bug in chalk becomes fixed.
if !ty.data(Interner).flags.intersects(crate::TypeFlags::HAS_PROJECTION)
&& !matches!(ty.kind(Interner), crate::TyKind::Array(..))
{
return ty;
}
let mut table = unify::InferenceTable::new(db, trait_env);
let ty_with_vars = table.normalize_associated_types_in(ty.to_nextsolver(table.interner()));
table.select_obligations_where_possible();
table.resolve_completely(ty_with_vars).to_chalk(table.interner())
}
/// Binding modes inferred for patterns.
/// <https://doc.rust-lang.org/reference/patterns.html#binding-modes>
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]

View file

@ -81,22 +81,17 @@ use syntax::ast::{ConstArg, make};
use traits::FnTrait;
use triomphe::Arc;
#[cfg(not(debug_assertions))]
use crate::next_solver::ErrorGuaranteed;
use crate::{
builder::{ParamKind, TyBuilder},
chalk_ext::*,
db::HirDatabase,
display::{DisplayTarget, HirDisplay},
generics::Generics,
infer::unify::InferenceTable,
next_solver::{
DbInterner,
mapping::{ChalkToNextSolver, NextSolverToChalk, convert_ty_for_result},
},
next_solver::DbInterner,
};
pub use autoderef::autoderef;
pub use builder::{ParamKind, TyBuilder};
pub use chalk_ext::*;
pub use infer::{
Adjust, Adjustment, AutoBorrow, BindingMode, InferenceDiagnostic, InferenceResult,
InferenceTyDiagnosticSource, OverloadedDeref, PointerCast,
@ -156,7 +151,6 @@ pub(crate) type GenericArgData = chalk_ir::GenericArgData<Interner>;
pub(crate) type Ty = chalk_ir::Ty<Interner>;
pub type TyKind = chalk_ir::TyKind<Interner>;
pub(crate) type TypeFlags = chalk_ir::TypeFlags;
pub(crate) type DynTy = chalk_ir::DynTy<Interner>;
pub(crate) type FnPointer = chalk_ir::FnPointer<Interner>;
pub(crate) use chalk_ir::FnSubst; // a re-export so we don't lose the tuple constructor
@ -174,7 +168,6 @@ pub(crate) type ConstValue = chalk_ir::ConstValue<Interner>;
pub(crate) type Const = chalk_ir::Const<Interner>;
pub(crate) type ConstData = chalk_ir::ConstData<Interner>;
pub(crate) type ConcreteConst = chalk_ir::ConcreteConst<Interner>;
pub(crate) type TraitRef = chalk_ir::TraitRef<Interner>;
pub(crate) type QuantifiedWhereClause = Binders<WhereClause>;
@ -382,7 +375,7 @@ pub(crate) fn variable_kinds_from_iter(
/// A function signature as seen by type inference: Several parameter types and
/// one return type.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CallableSig {
pub(crate) struct CallableSig {
params_and_return: Arc<[Ty]>,
is_varargs: bool,
safety: Safety,
@ -534,112 +527,6 @@ impl FnAbi {
}
}
/// A polymorphic function signature.
pub type PolyFnSig = Binders<CallableSig>;
impl CallableSig {
pub fn from_params_and_return(
params: impl Iterator<Item = Ty>,
ret: Ty,
is_varargs: bool,
safety: Safety,
abi: FnAbi,
) -> CallableSig {
let mut params_and_return = Vec::with_capacity(params.size_hint().0 + 1);
params_and_return.extend(params);
params_and_return.push(ret);
CallableSig { params_and_return: params_and_return.into(), is_varargs, safety, abi }
}
pub fn from_def(db: &dyn HirDatabase, def: FnDefId, substs: &Substitution) -> CallableSig {
let callable_def = ToChalk::from_chalk(db, def);
let interner = DbInterner::new_with(db, None, None);
let args: crate::next_solver::GenericArgs<'_> = substs.to_nextsolver(interner);
let sig = db.callable_item_signature(callable_def);
sig.instantiate(interner, args).skip_binder().to_chalk(interner)
}
pub fn from_fn_ptr(fn_ptr: &FnPointer) -> CallableSig {
CallableSig {
// FIXME: what to do about lifetime params? -> return PolyFnSig
params_and_return: Arc::from_iter(
fn_ptr
.substitution
.clone()
.shifted_out_to(Interner, DebruijnIndex::ONE)
.expect("unexpected lifetime vars in fn ptr")
.0
.as_slice(Interner)
.iter()
.map(|arg| arg.assert_ty_ref(Interner).clone()),
),
is_varargs: fn_ptr.sig.variadic,
safety: fn_ptr.sig.safety,
abi: fn_ptr.sig.abi,
}
}
pub fn from_fn_sig_and_header<'db>(
interner: DbInterner<'db>,
sig: crate::next_solver::Binder<'db, rustc_type_ir::FnSigTys<DbInterner<'db>>>,
header: rustc_type_ir::FnHeader<DbInterner<'db>>,
) -> CallableSig {
CallableSig {
// FIXME: what to do about lifetime params? -> return PolyFnSig
params_and_return: Arc::from_iter(
sig.skip_binder()
.inputs_and_output
.iter()
.map(|t| convert_ty_for_result(interner, t)),
),
is_varargs: header.c_variadic,
safety: match header.safety {
next_solver::abi::Safety::Safe => chalk_ir::Safety::Safe,
next_solver::abi::Safety::Unsafe => chalk_ir::Safety::Unsafe,
},
abi: header.abi,
}
}
pub fn to_fn_ptr(&self) -> FnPointer {
FnPointer {
num_binders: 0,
sig: FnSig { abi: self.abi, safety: self.safety, variadic: self.is_varargs },
substitution: FnSubst(Substitution::from_iter(
Interner,
self.params_and_return.iter().cloned(),
)),
}
}
pub fn abi(&self) -> FnAbi {
self.abi
}
pub fn params(&self) -> &[Ty] {
&self.params_and_return[0..self.params_and_return.len() - 1]
}
pub fn ret(&self) -> &Ty {
&self.params_and_return[self.params_and_return.len() - 1]
}
}
impl TypeFoldable<Interner> for CallableSig {
fn try_fold_with<E>(
self,
folder: &mut dyn chalk_ir::fold::FallibleTypeFolder<Interner, Error = E>,
outer_binder: DebruijnIndex,
) -> Result<Self, E> {
let vec = self.params_and_return.to_vec();
let folded = vec.try_fold_with(folder, outer_binder)?;
Ok(CallableSig {
params_and_return: folded.into(),
is_varargs: self.is_varargs,
safety: self.safety,
abi: self.abi,
})
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum ImplTraitId {
ReturnTypeImplTrait(hir_def::FunctionId, ImplTraitIdx), // FIXME(next-solver): Should be crate::nextsolver::ImplTraitIdx.
@ -764,7 +651,12 @@ where
#[cfg(debug_assertions)]
let error = || Err(());
#[cfg(not(debug_assertions))]
let error = || Ok(crate::next_solver::Ty::new_error(self.interner, ErrorGuaranteed));
let error = || {
Ok(crate::next_solver::Ty::new_error(
self.interner,
crate::next_solver::ErrorGuaranteed,
))
};
match t.kind() {
crate::next_solver::TyKind::Error(_) => {

View file

@ -17,17 +17,20 @@ use std::{
use base_db::Crate;
use either::Either;
use hir_def::hir::generics::GenericParamDataRef;
use hir_def::item_tree::FieldsShape;
use hir_def::{
AdtId, AssocItemId, CallableDefId, ConstParamId, DefWithBodyId, EnumVariantId, FunctionId,
GenericDefId, GenericParamId, HasModule, ImplId, ItemContainerId, LocalFieldId, Lookup,
StructId, TraitId, TypeAliasId, TypeOrConstParamId, VariantId,
AdtId, AssocItemId, CallableDefId, ConstId, ConstParamId, DefWithBodyId, EnumVariantId,
FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
LocalFieldId, Lookup, StaticId, StructId, TraitId, TypeAliasId, TypeOrConstParamId,
TypeParamId, VariantId,
expr_store::{
ExpressionStore,
path::{GenericArg, Path},
},
hir::generics::{TypeOrConstParamData, WherePredicate},
hir::generics::{
GenericParamDataRef, TypeOrConstParamData, TypeParamData, TypeParamProvenance,
WherePredicate,
},
item_tree::FieldsShape,
lang_item::LangItem,
resolver::{HasResolver, LifetimeNs, Resolver, TypeNs},
signatures::{FunctionSignature, TraitFlags, TypeAliasFlags},
@ -36,7 +39,6 @@ use hir_def::{
TraitRef as HirTraitRef, TypeBound, TypeRef, TypeRefId,
},
};
use hir_def::{ConstId, LifetimeParamId, StaticId, TypeParamId};
use hir_expand::name::Name;
use intern::{Symbol, sym};
use la_arena::{Arena, ArenaMap, Idx};
@ -48,20 +50,17 @@ use rustc_type_ir::{
AliasTyKind, ConstKind, DebruijnIndex, ExistentialPredicate, ExistentialProjection,
ExistentialTraitRef, FnSig, OutlivesPredicate,
TyKind::{self},
TypeVisitableExt,
TypeFoldable, TypeFolder, TypeVisitableExt, Upcast,
inherent::{GenericArg as _, GenericArgs as _, IntoKind as _, Region as _, SliceLike, Ty as _},
};
use rustc_type_ir::{TypeFoldable, TypeFolder, Upcast};
use salsa::plumbing::AsId;
use smallvec::{SmallVec, smallvec};
use stdx::never;
use triomphe::Arc;
use crate::ValueTyDefId;
use crate::next_solver::ParamConst;
use crate::{
FnAbi, ImplTraitId, Interner, ParamKind, TraitEnvironment, TyDefId, TyLoweringDiagnostic,
TyLoweringDiagnosticKind,
TyLoweringDiagnosticKind, ValueTyDefId,
consteval::{intern_const_ref, path_to_const, unknown_const_as_generic},
db::HirDatabase,
generics::{Generics, generics, trait_self_param_idx},
@ -69,8 +68,8 @@ use crate::{
next_solver::{
AdtDef, AliasTy, Binder, BoundExistentialPredicates, BoundRegionKind, BoundTyKind,
BoundVarKind, BoundVarKinds, Clause, Clauses, Const, DbInterner, EarlyBinder,
EarlyParamRegion, ErrorGuaranteed, GenericArgs, ParamEnv, PolyFnSig, Predicate, Region,
SolverDefId, TraitPredicate, TraitRef, Ty, Tys,
EarlyParamRegion, ErrorGuaranteed, GenericArgs, ParamConst, ParamEnv, PolyFnSig, Predicate,
Region, SolverDefId, TraitPredicate, TraitRef, Ty, Tys,
abi::Safety,
mapping::{ChalkToNextSolver, convert_ty_for_result},
},
@ -187,8 +186,9 @@ pub struct TyLoweringContext<'db, 'a> {
pub(crate) unsized_types: FxHashSet<Ty<'db>>,
pub(crate) diagnostics: Vec<TyLoweringDiagnostic>,
lifetime_elision: LifetimeElisionKind<'db>,
/// We disallow referencing generic parameters that have an index greater than or equal to this number.
disallow_params_after: u32,
/// When lowering the defaults for generic params, this contains the index of the currently lowered param.
/// We disallow referring to later params, or to ADT's `Self`.
lowering_param_default: Option<u32>,
}
impl<'db, 'a> TyLoweringContext<'db, 'a> {
@ -213,7 +213,7 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
unsized_types: FxHashSet::default(),
diagnostics: Vec::new(),
lifetime_elision,
disallow_params_after: u32::MAX,
lowering_param_default: None,
}
}
@ -249,8 +249,8 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
self
}
pub(crate) fn disallow_params_after(&mut self, after: u32) {
self.disallow_params_after = after;
pub(crate) fn lowering_param_default(&mut self, index: u32) {
self.lowering_param_default = Some(index);
}
pub(crate) fn push_diagnostic(&mut self, type_ref: TypeRefId, kind: TyLoweringDiagnosticKind) {
@ -333,8 +333,13 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
self.generics.get_or_init(|| generics(self.db, self.def))
}
fn param_index_is_disallowed(&self, index: u32) -> bool {
self.lowering_param_default
.is_some_and(|disallow_params_after| index >= disallow_params_after)
}
fn type_param(&mut self, id: TypeParamId, index: u32, name: Symbol) -> Ty<'db> {
if index >= self.disallow_params_after {
if self.param_index_is_disallowed(index) {
// FIXME: Report an error.
Ty::new_error(self.interner, ErrorGuaranteed)
} else {
@ -343,7 +348,7 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
}
fn const_param(&mut self, id: ConstParamId, index: u32) -> Const<'db> {
if index >= self.disallow_params_after {
if self.param_index_is_disallowed(index) {
// FIXME: Report an error.
Const::error(self.interner)
} else {
@ -352,7 +357,7 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
}
fn region_param(&mut self, id: LifetimeParamId, index: u32) -> Region<'db> {
if index >= self.disallow_params_after {
if self.param_index_is_disallowed(index) {
// FIXME: Report an error.
Region::error(self.interner)
} else {
@ -394,7 +399,7 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
type_data
.name
.as_ref()
.map_or_else(|| sym::MISSING_NAME.clone(), |d| d.symbol().clone()),
.map_or_else(|| sym::MISSING_NAME, |d| d.symbol().clone()),
)
}
&TypeRef::RawPtr(inner, mutability) => {
@ -1603,8 +1608,6 @@ where
for pred in maybe_parent_generics.where_predicates() {
tracing::debug!(?pred);
if filter(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,
@ -1619,49 +1622,53 @@ where
let sized_trait = LangItem::Sized.resolve_trait(db, resolver.krate());
if let Some(sized_trait) = sized_trait {
let (mut generics, mut def_id) =
(crate::next_solver::generics::generics(db, def.into()), def);
loop {
if filter(def_id) {
let self_idx = trait_self_param_idx(db, def_id);
for (idx, p) in generics.own_params.iter().enumerate() {
if let Some(self_idx) = self_idx
&& p.index() as usize == self_idx
{
continue;
}
let GenericParamId::TypeParamId(param_id) = p.id else {
continue;
};
let idx = idx as u32 + generics.parent_count as u32;
let param_ty = Ty::new_param(interner, param_id, idx, p.name.clone());
if explicitly_unsized_tys.contains(&param_ty) {
continue;
}
let trait_ref = TraitRef::new_from_args(
interner,
sized_trait.into(),
GenericArgs::new_from_iter(interner, [param_ty.into()]),
);
let clause = Clause(Predicate::new(
interner,
Binder::dummy(rustc_type_ir::PredicateKind::Clause(
rustc_type_ir::ClauseKind::Trait(TraitPredicate {
trait_ref,
polarity: rustc_type_ir::PredicatePolarity::Positive,
}),
)),
));
predicates.push(clause);
}
let mut add_sized_clause = |param_idx, param_id, param_data| {
let (
GenericParamId::TypeParamId(param_id),
GenericParamDataRef::TypeParamData(param_data),
) = (param_id, param_data)
else {
return;
};
if param_data.provenance == TypeParamProvenance::TraitSelf {
return;
}
if let Some(g) = generics.parent {
generics = crate::next_solver::generics::generics(db, g.into());
def_id = g;
} else {
break;
let param_name = param_data
.name
.as_ref()
.map_or_else(|| sym::MISSING_NAME, |name| name.symbol().clone());
let param_ty = Ty::new_param(interner, param_id, param_idx, param_name);
if explicitly_unsized_tys.contains(&param_ty) {
return;
}
let trait_ref = TraitRef::new_from_args(
interner,
sized_trait.into(),
GenericArgs::new_from_iter(interner, [param_ty.into()]),
);
let clause = Clause(Predicate::new(
interner,
Binder::dummy(rustc_type_ir::PredicateKind::Clause(
rustc_type_ir::ClauseKind::Trait(TraitPredicate {
trait_ref,
polarity: rustc_type_ir::PredicatePolarity::Positive,
}),
)),
));
predicates.push(clause);
};
if generics.parent_generics().is_some_and(|parent| filter(parent.def())) {
generics.iter_parent().enumerate().for_each(|(param_idx, (param_id, param_data))| {
add_sized_clause(param_idx as u32, param_id, param_data);
});
}
if filter(def) {
let parent_params_len = generics.len_parent();
generics.iter_self().enumerate().for_each(|(param_idx, (param_id, param_data))| {
add_sized_clause((param_idx + parent_params_len) as u32, param_id, param_data);
});
}
}
@ -1860,10 +1867,7 @@ pub(crate) fn generic_defaults_with_diagnostics_query(
p: GenericParamDataRef<'_>,
generic_params: &Generics,
) -> (Option<EarlyBinder<'db, crate::next_solver::GenericArg<'db>>>, bool) {
// Each default can only refer to previous parameters.
// Type variable default referring to parameter coming
// after it is forbidden.
ctx.disallow_params_after(idx as u32);
ctx.lowering_param_default(idx as u32);
match p {
GenericParamDataRef::TypeParamData(p) => {
let ty = p.default.map(|ty| ctx.lower_ty(ty));

View file

@ -314,7 +314,9 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
self.lower_ty_relative_path(ty, Some(resolution), infer_args)
}
fn handle_type_ns_resolution(&mut self, resolution: &TypeNs) {
/// This returns whether to keep the resolution (`true`) of throw it (`false`).
#[must_use]
fn handle_type_ns_resolution(&mut self, resolution: &TypeNs) -> bool {
let mut prohibit_generics_on_resolved = |reason| {
if self.current_or_prev_segment.args_and_bindings.is_some() {
let segment = self.current_segment_u32();
@ -333,7 +335,13 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
prohibit_generics_on_resolved(GenericArgsProhibitedReason::TyParam)
}
TypeNs::AdtSelfType(_) => {
prohibit_generics_on_resolved(GenericArgsProhibitedReason::SelfTy)
prohibit_generics_on_resolved(GenericArgsProhibitedReason::SelfTy);
if self.ctx.lowering_param_default.is_some() {
// Generic defaults are not allowed to refer to `Self`.
// FIXME: Emit an error.
return false;
}
}
TypeNs::BuiltinType(_) => {
prohibit_generics_on_resolved(GenericArgsProhibitedReason::PrimitiveTy)
@ -346,6 +354,8 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
| TypeNs::TypeAliasId(_)
| TypeNs::TraitId(_) => {}
}
true
}
pub(crate) fn resolve_path_in_type_ns_fully(&mut self) -> Option<TypeNs> {
@ -379,11 +389,6 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
self.current_or_prev_segment =
segments.get(resolved_segment_idx).expect("should have resolved segment");
if matches!(self.path, Path::BarePath(..)) {
// Bare paths cannot have generics, so skip them as an optimization.
return Some((resolution, remaining_index));
}
for (i, mod_segment) in module_segments.iter().enumerate() {
if mod_segment.args_and_bindings.is_some() {
self.on_diagnostic(PathLoweringDiagnostic::GenericArgsProhibited {
@ -403,7 +408,9 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
});
}
self.handle_type_ns_resolution(&resolution);
if !self.handle_type_ns_resolution(&resolution) {
return None;
}
Some((resolution, remaining_index))
}
@ -475,7 +482,7 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
match resolution {
ValueNs::ImplSelf(_) => {
prohibit_generics_on_resolved(GenericArgsProhibitedReason::SelfTy)
prohibit_generics_on_resolved(GenericArgsProhibitedReason::SelfTy);
}
// FIXME: rustc generates E0107 (incorrect number of generic arguments) and not
// E0109 (generic arguments provided for a type that doesn't accept them) for
@ -499,7 +506,9 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
}
}
ResolveValueResult::Partial(resolution, _, _) => {
self.handle_type_ns_resolution(resolution);
if !self.handle_type_ns_resolution(resolution) {
return None;
}
}
};
Some(res)

View file

@ -118,10 +118,10 @@ enum LocalName<'db> {
Binding(Name, LocalId<'db>),
}
impl<'db> HirDisplay for LocalName<'db> {
impl<'db> HirDisplay<'db> for LocalName<'db> {
fn hir_fmt(
&self,
f: &mut crate::display::HirFormatter<'_>,
f: &mut crate::display::HirFormatter<'_, 'db>,
) -> Result<(), crate::display::HirDisplayError> {
match self {
LocalName::Unknown(l) => write!(f, "_{}", u32::from(l.into_raw())),
@ -489,7 +489,7 @@ impl<'a, 'db> MirPrettyCtx<'a, 'db> {
}
}
fn hir_display<'b, T: HirDisplay>(&self, ty: &'b T) -> impl Display + use<'a, 'b, 'db, T>
fn hir_display<'b, T: HirDisplay<'db>>(&self, ty: &'b T) -> impl Display + use<'a, 'b, 'db, T>
where
'db: 'b,
{

View file

@ -82,7 +82,11 @@ impl<'db> Const<'db> {
}
pub fn is_ct_infer(&self) -> bool {
matches!(&self.inner().internee, ConstKind::Infer(_))
matches!(self.kind(), ConstKind::Infer(_))
}
pub fn is_error(&self) -> bool {
matches!(self.kind(), ConstKind::Error(_))
}
pub fn is_trivially_wf(self) -> bool {

View file

@ -647,6 +647,26 @@ impl<'db> UpcastFrom<DbInterner<'db>, ty::OutlivesPredicate<DbInterner<'db>, Reg
PredicateKind::Clause(ClauseKind::RegionOutlives(from)).upcast(interner)
}
}
impl<'db> UpcastFrom<DbInterner<'db>, ty::OutlivesPredicate<DbInterner<'db>, Ty<'db>>>
for Clause<'db>
{
fn upcast_from(
from: ty::OutlivesPredicate<DbInterner<'db>, Ty<'db>>,
interner: DbInterner<'db>,
) -> Self {
Clause(from.upcast(interner))
}
}
impl<'db> UpcastFrom<DbInterner<'db>, ty::OutlivesPredicate<DbInterner<'db>, Region<'db>>>
for Clause<'db>
{
fn upcast_from(
from: ty::OutlivesPredicate<DbInterner<'db>, Region<'db>>,
interner: DbInterner<'db>,
) -> Self {
Clause(from.upcast(interner))
}
}
impl<'db> UpcastFrom<DbInterner<'db>, PolyRegionOutlivesPredicate<'db>> for Predicate<'db> {
fn upcast_from(from: PolyRegionOutlivesPredicate<'db>, tcx: DbInterner<'db>) -> Self {

View file

@ -12,22 +12,20 @@ use intern::sym;
use rustc_next_trait_solver::solve::{HasChanged, SolverDelegateEvalExt};
use rustc_type_ir::{
InferCtxtLike, TypingMode,
inherent::{IntoKind, SliceLike, Span as _, Ty as _},
inherent::{IntoKind, SliceLike, Span as _},
solve::Certainty,
};
use span::Edition;
use stdx::never;
use triomphe::Arc;
use crate::{
AliasEq, AliasTy, Canonical, DomainGoal, Goal, InEnvironment, Interner, ProjectionTy,
ProjectionTyExt, TraitRefExt, Ty, TyKind, TypeFlags, WhereClause,
AliasEq, AliasTy, Canonical, DomainGoal, Goal, InEnvironment, Interner, ProjectionTyExt,
TraitRefExt, TyKind, WhereClause,
db::HirDatabase,
from_assoc_type_id,
next_solver::{
DbInterner, GenericArg, ParamEnv, Predicate, SolverContext, Span,
infer::{DbInternerInferExt, InferCtxt, traits::ObligationCause},
mapping::{ChalkToNextSolver, NextSolverToChalk, convert_canonical_args_for_result},
mapping::{ChalkToNextSolver, convert_canonical_args_for_result},
obligation_ctxt::ObligationCtxt,
util::mini_canonicalize,
},
@ -94,47 +92,6 @@ pub fn structurally_normalize_ty<'db>(
ty.replace_infer_with_error(infcx.interner)
}
pub(crate) fn normalize_projection_query<'db>(
db: &'db dyn HirDatabase,
projection: ProjectionTy,
env: Arc<TraitEnvironment<'db>>,
) -> Ty {
if projection.substitution.iter(Interner).any(|arg| {
arg.ty(Interner)
.is_some_and(|ty| ty.data(Interner).flags.intersects(TypeFlags::HAS_TY_INFER))
}) {
never!(
"Invoking `normalize_projection_query` with a projection type containing inference var"
);
return TyKind::Error.intern(Interner);
}
let interner = DbInterner::new_with(db, Some(env.krate), env.block);
// FIXME(next-solver): I believe this should use `PostAnalysis` (this is only used for IDE things),
// but this causes some bug because of our incorrect impl of `type_of_opaque_hir_typeck()` for TAIT
// and async blocks.
let infcx = interner.infer_ctxt().build(TypingMode::Analysis {
defining_opaque_types_and_generators: crate::next_solver::SolverDefIds::new_from_iter(
interner,
[],
),
});
let alias_ty = crate::next_solver::Ty::new_alias(
interner,
rustc_type_ir::AliasTyKind::Projection,
crate::next_solver::AliasTy::new(
interner,
from_assoc_type_id(projection.associated_ty_id).into(),
<crate::Substitution as ChalkToNextSolver<crate::next_solver::GenericArgs<'_>>>::to_nextsolver(&projection.substitution, interner),
),
);
let mut ctxt = crate::next_solver::obligation_ctxt::ObligationCtxt::new(&infcx);
let normalized = ctxt
.structurally_normalize_ty(&ObligationCause::dummy(), env.env, alias_ty)
.unwrap_or(alias_ty);
normalized.replace_infer_with_error(interner).to_chalk(interner)
}
fn identity_subst(
binders: chalk_ir::CanonicalVarKinds<Interner>,
) -> chalk_ir::Canonical<chalk_ir::Substitution<Interner>> {
@ -165,45 +122,6 @@ fn identity_subst(
chalk_ir::Canonical { binders, value: identity_subst }
}
/// Solve a trait goal using next trait solver.
pub(crate) fn trait_solve_query(
db: &dyn HirDatabase,
krate: Crate,
block: Option<BlockId>,
goal: Canonical<InEnvironment<Goal>>,
) -> NextTraitSolveResult {
let _p = tracing::info_span!("trait_solve_query", detail = ?match &goal.value.goal.data(Interner) {
GoalData::DomainGoal(DomainGoal::Holds(WhereClause::Implemented(it))) => db
.trait_signature(it.hir_trait_id())
.name
.display(db, Edition::LATEST)
.to_string(),
GoalData::DomainGoal(DomainGoal::Holds(WhereClause::AliasEq(_))) => "alias_eq".to_owned(),
_ => "??".to_owned(),
})
.entered();
if let GoalData::DomainGoal(DomainGoal::Holds(WhereClause::AliasEq(AliasEq {
alias: AliasTy::Projection(projection_ty),
..
}))) = &goal.value.goal.data(Interner)
&& let TyKind::BoundVar(_) = projection_ty.self_type_parameter(db).kind(Interner)
{
// Hack: don't ask Chalk to normalize with an unknown self type, it'll say that's impossible
return NextTraitSolveResult::Uncertain(identity_subst(goal.binders.clone()));
}
// Chalk see `UnevaluatedConst` as a unique concrete value, but we see it as an alias for another const. So
// we should get rid of it when talking to chalk.
let goal = goal
.try_fold_with(&mut UnevaluatedConstEvaluatorFolder { db }, DebruijnIndex::INNERMOST)
.unwrap();
// We currently don't deal with universes (I think / hope they're not yet
// relevant for our use cases?)
next_trait_solve(db, krate, block, goal)
}
fn solve_nextsolver<'db>(
db: &'db dyn HirDatabase,
krate: Crate,

View file

@ -25,8 +25,7 @@ use smallvec::{SmallVec, smallvec};
use span::Edition;
use crate::{
ChalkTraitId, Const, ConstScalar, Interner, Substitution, TargetFeatures, TraitRef,
TraitRefExt, Ty,
ChalkTraitId, Const, ConstScalar, Interner, TargetFeatures, TraitRef, TraitRefExt,
consteval::unknown_const,
db::HirDatabase,
layout::{Layout, TagEncoding},
@ -192,19 +191,6 @@ pub(super) fn associated_type_by_name_including_super_traits(
})
}
pub(crate) struct ClosureSubst<'a>(pub(crate) &'a Substitution);
impl<'a> ClosureSubst<'a> {
pub(crate) fn sig_ty(&self, db: &dyn HirDatabase) -> Ty {
let interner = DbInterner::new_with(db, None, None);
let subst =
<Substitution as ChalkToNextSolver<crate::next_solver::GenericArgs<'_>>>::to_nextsolver(
self.0, interner,
);
subst.split_closure_args_untupled().closure_sig_as_fn_ptr_ty.to_chalk(interner)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Unsafety {
Safe,

View file

@ -11,14 +11,15 @@ use hir_def::{
type_ref::{TypeBound, TypeRef, TypeRefId},
};
use hir_ty::{
AliasEq, AliasTy, Interner, ProjectionTyExt, TraitRefExt, TyBuilder, TyKind, WhereClause,
db::HirDatabase,
display::{
HirDisplay, HirDisplayError, HirDisplayWithExpressionStore, HirFormatter, SizedByDefault,
hir_display_with_store, write_bounds_like_dyn_trait_with_prefix, write_visibility,
},
next_solver::ClauseKind,
};
use itertools::Itertools;
use rustc_type_ir::inherent::IntoKind;
use crate::{
Adt, AsAssocItem, AssocItem, AssocItemContainer, Const, ConstParam, Crate, Enum,
@ -27,8 +28,8 @@ use crate::{
TypeAlias, TypeNs, TypeOrConstParam, TypeParam, Union, Variant,
};
impl HirDisplay for Function {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Function {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
let db = f.db;
let data = db.function_signature(self.id);
let container = self.as_assoc_item(db).map(|it| it.container(db));
@ -184,7 +185,10 @@ impl HirDisplay for Function {
}
}
fn write_impl_header(impl_: &Impl, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
fn write_impl_header<'db>(
impl_: &Impl,
f: &mut HirFormatter<'_, 'db>,
) -> Result<(), HirDisplayError> {
let db = f.db;
f.write_str("impl")?;
@ -202,8 +206,8 @@ fn write_impl_header(impl_: &Impl, f: &mut HirFormatter<'_>) -> Result<(), HirDi
Ok(())
}
impl HirDisplay for SelfParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for SelfParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
let data = f.db.function_signature(self.func);
let param = *data.params.first().unwrap();
match &data.store[param] {
@ -228,8 +232,8 @@ impl HirDisplay for SelfParam {
}
}
impl HirDisplay for Adt {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Adt {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
match self {
Adt::Struct(it) => it.hir_fmt(f),
Adt::Union(it) => it.hir_fmt(f),
@ -238,8 +242,8 @@ impl HirDisplay for Adt {
}
}
impl HirDisplay for Struct {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Struct {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
let module_id = self.module(f.db).id;
// FIXME: Render repr if its set explicitly?
write_visibility(module_id, self.visibility(f.db), f)?;
@ -279,8 +283,8 @@ impl HirDisplay for Struct {
}
}
impl HirDisplay for Enum {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Enum {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
f.write_str("enum ")?;
write!(f, "{}", self.name(f.db).display(f.db, f.edition()))?;
@ -296,8 +300,8 @@ impl HirDisplay for Enum {
}
}
impl HirDisplay for Union {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Union {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
f.write_str("union ")?;
write!(f, "{}", self.name(f.db).display(f.db, f.edition()))?;
@ -312,12 +316,12 @@ impl HirDisplay for Union {
}
}
fn write_fields(
fn write_fields<'db>(
fields: &[Field],
has_where_clause: bool,
limit: usize,
in_line: bool,
f: &mut HirFormatter<'_>,
f: &mut HirFormatter<'_, 'db>,
) -> Result<(), HirDisplayError> {
let count = fields.len().min(limit);
let (indent, separator) = if in_line { ("", ' ') } else { (" ", '\n') };
@ -346,11 +350,11 @@ fn write_fields(
Ok(())
}
fn write_variants(
fn write_variants<'db>(
variants: &[Variant],
has_where_clause: bool,
limit: usize,
f: &mut HirFormatter<'_>,
f: &mut HirFormatter<'_, 'db>,
) -> Result<(), HirDisplayError> {
let count = variants.len().min(limit);
f.write_char(if !has_where_clause { ' ' } else { '\n' })?;
@ -386,23 +390,23 @@ fn write_variants(
Ok(())
}
impl HirDisplay for Field {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Field {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
write_visibility(self.parent.module(f.db).id, self.visibility(f.db), f)?;
write!(f, "{}: ", self.name(f.db).display(f.db, f.edition()))?;
self.ty(f.db).hir_fmt(f)
}
}
impl HirDisplay for TupleField {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for TupleField {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
write!(f, "pub {}: ", self.name().display(f.db, f.edition()))?;
self.ty(f.db).hir_fmt(f)
}
}
impl HirDisplay for Variant {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Variant {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
write!(f, "{}", self.name(f.db).display(f.db, f.edition()))?;
let data = self.id.fields(f.db);
match data.shape {
@ -431,20 +435,20 @@ impl HirDisplay for Variant {
}
}
impl HirDisplay for Type<'_> {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Type<'db> {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
self.ty.hir_fmt(f)
}
}
impl HirDisplay for TypeNs<'_> {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for TypeNs<'db> {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
self.ty.hir_fmt(f)
}
}
impl HirDisplay for ExternCrateDecl {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for ExternCrateDecl {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
f.write_str("extern crate ")?;
write!(f, "{}", self.name(f.db).display(f.db, f.edition()))?;
@ -455,8 +459,8 @@ impl HirDisplay for ExternCrateDecl {
}
}
impl HirDisplay for GenericParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for GenericParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
match self {
GenericParam::TypeParam(it) => it.hir_fmt(f),
GenericParam::ConstParam(it) => it.hir_fmt(f),
@ -465,8 +469,8 @@ impl HirDisplay for GenericParam {
}
}
impl HirDisplay for TypeOrConstParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for TypeOrConstParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
match self.split(f.db) {
either::Either::Left(it) => it.hir_fmt(f),
either::Either::Right(it) => it.hir_fmt(f),
@ -474,27 +478,22 @@ impl HirDisplay for TypeOrConstParam {
}
}
impl HirDisplay for TypeParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for TypeParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
let params = f.db.generic_params(self.id.parent());
let param_data = &params[self.id.local_id()];
let substs = TyBuilder::placeholder_subst(f.db, self.id.parent());
let krate = self.id.parent().krate(f.db).id;
let ty = TyKind::Placeholder(hir_ty::to_placeholder_idx_no_index(f.db, self.id.into()))
.intern(Interner);
let predicates = f.db.generic_predicates(self.id.parent());
let ty = self.ty(f.db).ty;
let predicates = f.db.generic_predicates_ns(self.id.parent());
let predicates = predicates
.iter()
.cloned()
.map(|pred| pred.substitute(Interner, &substs))
.filter(|wc| match wc.skip_binders() {
WhereClause::Implemented(tr) => tr.self_type_parameter(Interner) == ty,
WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(proj), ty: _ }) => {
proj.self_type_parameter(f.db) == ty
}
WhereClause::AliasEq(_) => false,
WhereClause::TypeOutlives(to) => to.ty == ty,
WhereClause::LifetimeOutlives(_) => false,
.instantiate_identity()
.into_iter()
.flatten()
.filter(|wc| match wc.kind().skip_binder() {
ClauseKind::Trait(tr) => tr.self_ty() == ty,
ClauseKind::Projection(proj) => proj.self_ty() == ty,
ClauseKind::TypeOutlives(to) => to.0 == ty,
_ => false,
})
.collect::<Vec<_>>();
@ -507,7 +506,7 @@ impl HirDisplay for TypeParam {
return write_bounds_like_dyn_trait_with_prefix(
f,
"impl",
Either::Left(&ty),
Either::Left(ty),
&predicates,
SizedByDefault::Sized { anchor: krate },
);
@ -523,23 +522,18 @@ impl HirDisplay for TypeParam {
}
let sized_trait = LangItem::Sized.resolve_trait(f.db, krate);
let has_only_sized_bound = predicates.iter().all(move |pred| match pred.skip_binders() {
WhereClause::Implemented(it) => Some(it.hir_trait_id()) == sized_trait,
_ => false,
});
let has_only_sized_bound =
predicates.iter().all(move |pred| match pred.kind().skip_binder() {
ClauseKind::Trait(it) => Some(it.def_id().0) == sized_trait,
_ => false,
});
let has_only_not_sized_bound = predicates.is_empty();
if !has_only_sized_bound || has_only_not_sized_bound {
let default_sized = SizedByDefault::Sized { anchor: krate };
write_bounds_like_dyn_trait_with_prefix(
f,
":",
Either::Left(
&hir_ty::TyKind::Placeholder(hir_ty::to_placeholder_idx_no_index(
f.db,
self.id.into(),
))
.intern(Interner),
),
Either::Left(ty),
&predicates,
default_sized,
)?;
@ -548,22 +542,22 @@ impl HirDisplay for TypeParam {
}
}
impl HirDisplay for LifetimeParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for LifetimeParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
write!(f, "{}", self.name(f.db).display(f.db, f.edition()))
}
}
impl HirDisplay for ConstParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for ConstParam {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
write!(f, "const {}: ", self.name(f.db).display(f.db, f.edition()))?;
self.ty(f.db).hir_fmt(f)
}
}
fn write_generic_params(
fn write_generic_params<'db>(
def: GenericDefId,
f: &mut HirFormatter<'_>,
f: &mut HirFormatter<'_, 'db>,
) -> Result<(), HirDisplayError> {
let (params, store) = f.db.generic_params_and_store(def);
if params.iter_lt().next().is_none()
@ -578,7 +572,7 @@ fn write_generic_params(
f.write_char('<')?;
let mut first = true;
let mut delim = |f: &mut HirFormatter<'_>| {
let mut delim = |f: &mut HirFormatter<'_, 'db>| {
if first {
first = false;
Ok(())
@ -622,9 +616,9 @@ fn write_generic_params(
Ok(())
}
fn write_where_clause(
fn write_where_clause<'db>(
def: GenericDefId,
f: &mut HirFormatter<'_>,
f: &mut HirFormatter<'_, 'db>,
) -> Result<bool, HirDisplayError> {
let (params, store) = f.db.generic_params_and_store(def);
if !has_disaplayable_predicates(f.db, &params, &store) {
@ -653,10 +647,10 @@ fn has_disaplayable_predicates(
})
}
fn write_where_predicates(
fn write_where_predicates<'db>(
params: &GenericParams,
store: &ExpressionStore,
f: &mut HirFormatter<'_>,
f: &mut HirFormatter<'_, 'db>,
) -> Result<(), HirDisplayError> {
use WherePredicate::*;
@ -717,8 +711,8 @@ fn write_where_predicates(
Ok(())
}
impl HirDisplay for Const {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Const {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
let db = f.db;
let container = self.as_assoc_item(db).map(|it| it.container(db));
let mut module = self.module(db);
@ -738,8 +732,8 @@ impl HirDisplay for Const {
}
}
impl HirDisplay for Static {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Static {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
let data = f.db.static_signature(self.id);
f.write_str("static ")?;
@ -752,14 +746,14 @@ impl HirDisplay for Static {
}
}
impl HirDisplay for TraitRef<'_> {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for TraitRef<'db> {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
self.trait_ref.hir_fmt(f)
}
}
impl HirDisplay for Trait {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Trait {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
// FIXME(trait-alias) needs special handling to print the equal sign
write_trait_header(self, f)?;
let def_id = GenericDefId::TraitId(self.id);
@ -798,7 +792,10 @@ impl HirDisplay for Trait {
}
}
fn write_trait_header(trait_: &Trait, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
fn write_trait_header<'db>(
trait_: &Trait,
f: &mut HirFormatter<'_, 'db>,
) -> Result<(), HirDisplayError> {
write_visibility(trait_.module(f.db).id, trait_.visibility(f.db), f)?;
let data = f.db.trait_signature(trait_.id);
if data.flags.contains(TraitFlags::UNSAFE) {
@ -812,8 +809,8 @@ fn write_trait_header(trait_: &Trait, f: &mut HirFormatter<'_>) -> Result<(), Hi
Ok(())
}
impl HirDisplay for TypeAlias {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for TypeAlias {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
write_visibility(self.module(f.db).id, self.visibility(f.db), f)?;
let data = f.db.type_alias_signature(self.id);
write!(f, "type {}", data.name.display(f.db, f.edition()))?;
@ -835,8 +832,8 @@ impl HirDisplay for TypeAlias {
}
}
impl HirDisplay for Module {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Module {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
match self.parent(f.db) {
Some(m) => write_visibility(m.id, self.visibility(f.db), f)?,
None => {
@ -853,8 +850,8 @@ impl HirDisplay for Module {
}
}
impl HirDisplay for Crate {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Crate {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
match self.display_name(f.db) {
Some(name) => write!(f, "extern crate {name}"),
None => f.write_str("extern crate {unknown}"),
@ -862,8 +859,8 @@ impl HirDisplay for Crate {
}
}
impl HirDisplay for Macro {
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
impl<'db> HirDisplay<'db> for Macro {
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
match self.id {
hir_def::MacroId::Macro2Id(_) => f.write_str("macro"),
hir_def::MacroId::MacroRulesId(_) => f.write_str("macro_rules!"),

View file

@ -9392,7 +9392,7 @@ fn main(a$0: T) {}
*a*
```rust
a: T
a: T<T>
```
---

View file

@ -384,7 +384,7 @@ fn def_to_non_local_moniker(
})
}
fn display<T: HirDisplay>(db: &RootDatabase, module: hir::Module, it: T) -> String {
fn display<'db, T: HirDisplay<'db>>(db: &'db RootDatabase, module: hir::Module, it: T) -> String {
match it.display_source_code(db, module.into(), true) {
Ok(result) => result,
// Fallback on display variant that always succeeds

View file

@ -382,7 +382,7 @@ impl ToNavFromAst for hir::Trait {
impl<D> TryToNav for D
where
D: HasSource + ToNavFromAst + Copy + HasDocs + HirDisplay + HasCrate,
D: HasSource + ToNavFromAst + Copy + HasDocs + for<'db> HirDisplay<'db> + HasCrate,
D::Ast: ast::HasName,
{
fn try_to_nav(