Skip to content
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
2 changes: 1 addition & 1 deletion crates/hir-ty/src/builtin_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn predicates<'db>(db: &'db dyn HirDatabase, impl_: BuiltinDeriveImplId) ->
else {
// Malformed derive.
return GenericPredicates::from_explicit_own_predicates(StoredEarlyBinder::bind(
Clauses::default().store(),
Clauses::empty(interner).store(),
));
};
let duplicated_bounds =
Expand Down
13 changes: 5 additions & 8 deletions crates/hir-ty/src/consteval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,19 @@ use crate::{
db::HirDatabase,
display::DisplayTarget,
infer::InferenceContext,
lower::unknown_const,
mir::{MirEvalError, MirLowerError, pad16},
next_solver::{
Allocation, Const, ConstKind, Consts, DbInterner, ErrorGuaranteed, GenericArg, GenericArgs,
ScalarInt, StoredAllocation, StoredGenericArgs, Ty, TyKind, ValTreeKind, default_types,
Allocation, Const, ConstKind, Consts, DbInterner, GenericArg, GenericArgs, ScalarInt,
StoredAllocation, StoredGenericArgs, Ty, TyKind, ValTreeKind, default_types,
},
traits::StoredParamEnvAndCrate,
};

use super::mir::{interpret_mir, lower_body_to_mir};

pub fn unknown_const<'db>(_ty: Ty<'db>) -> Const<'db> {
Const::new(DbInterner::conjure(), rustc_type_ir::ConstKind::Error(ErrorGuaranteed))
}

pub fn unknown_const_as_generic<'db>(ty: Ty<'db>) -> GenericArg<'db> {
unknown_const(ty).into()
pub fn unknown_const_as_generic<'db>(interner: DbInterner<'db>, ty: Ty<'db>) -> GenericArg<'db> {
unknown_const(interner, ty).into()
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ fn render_const_scalar<'db>(
memory_map: &MemoryMap<'db>,
ty: Ty<'db>,
) -> Result {
let param_env = ParamEnv::empty();
let param_env = ParamEnv::empty(f.interner);
let infcx = f.interner.infer_ctxt().build(TypingMode::PostAnalysis);
let ty = infcx.at(&ObligationCause::new(), param_env).deeply_normalize(ty).unwrap_or(ty);
render_const_scalar_inner(f, b, memory_map, ty, param_env)
Expand Down Expand Up @@ -1021,7 +1021,7 @@ fn render_const_scalar_from_valtree<'db>(
ty: Ty<'db>,
valtree: ValTree<'db>,
) -> Result {
let param_env = ParamEnv::empty();
let param_env = ParamEnv::empty(f.interner);
let infcx = f.interner.infer_ctxt().build(TypingMode::PostAnalysis);
let ty = infcx.at(&ObligationCause::new(), param_env).deeply_normalize(ty).unwrap_or(ty);
render_const_scalar_from_valtree_inner(f, ty, valtree, param_env)
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-ty/src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2218,8 +2218,8 @@ impl<'body, 'db> InferenceContext<'body, 'db> {
return (self.err_ty(), None);
};
let args = path_ctx.substs_from_path_segment(it.into(), true, None, false);
let interner = path_ctx.ctx.interner;
drop(ctx);
let interner = DbInterner::conjure();
let ty = self.db.ty(it.into()).instantiate(interner, args);
let ty = self.insert_type_vars(ty);

Expand Down
12 changes: 6 additions & 6 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub(crate) enum GenericPredicateSource {
#[derive(Debug)]
pub struct TyLoweringContext<'db, 'a> {
pub db: &'db dyn HirDatabase,
interner: DbInterner<'db>,
pub(crate) interner: DbInterner<'db>,
types: &'db crate::next_solver::DefaultAny<'db>,
lang_items: &'db LangItems,
resolver: &'a Resolver<'db>,
Expand Down Expand Up @@ -404,7 +404,7 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> {
}

pub(crate) fn lower_path_as_const(&mut self, path: &Path, const_type: Ty<'db>) -> Const<'db> {
self.path_to_const(path).unwrap_or_else(|| unknown_const(const_type))
self.path_to_const(path).unwrap_or_else(|| unknown_const(self.interner, const_type))
}

fn generics(&self) -> &Generics<'db> {
Expand Down Expand Up @@ -1133,8 +1133,8 @@ pub(crate) fn lower_mutability(m: hir_def::type_ref::Mutability) -> Mutability {
}
}

fn unknown_const(_ty: Ty<'_>) -> Const<'_> {
Const::new(DbInterner::conjure(), ConstKind::Error(ErrorGuaranteed))
pub(crate) fn unknown_const<'db>(interner: DbInterner<'db>, _ty: Ty<'db>) -> Const<'db> {
Const::new(interner, rustc_type_ir::ConstKind::Error(ErrorGuaranteed))
}

pub(crate) type Diagnostics = Option<ThinArc<(), TyLoweringDiagnostic>>;
Expand Down Expand Up @@ -2055,13 +2055,13 @@ impl<'db> GenericPredicates {

/// A cycle can occur from malformed code.
fn generic_predicates_cycle_result(
_db: &dyn HirDatabase,
db: &dyn HirDatabase,
_: salsa::Id,
_def: GenericDefId,
) -> (GenericPredicates, Diagnostics) {
(
GenericPredicates::from_explicit_own_predicates(StoredEarlyBinder::bind(
Clauses::default().store(),
Clauses::empty(DbInterner::new_no_crate(db)).store(),
)),
None,
)
Expand Down
22 changes: 13 additions & 9 deletions crates/hir-ty/src/lower/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ use stdx::never;
use crate::{
GenericArgsProhibitedReason, IncorrectGenericsLenKind, PathGenericsSource,
PathLoweringDiagnostic, TyDefId, ValueTyDefId,
consteval::{unknown_const, unknown_const_as_generic},
consteval::unknown_const_as_generic,
db::HirDatabase,
generics::{Generics, generics},
lower::{
AssocTypeShorthandResolution, GenericPredicateSource, LifetimeElisionKind,
PathDiagnosticCallbackData,
PathDiagnosticCallbackData, unknown_const,
},
next_solver::{
Binder, Clause, Const, DbInterner, EarlyBinder, ErrorGuaranteed, GenericArg, GenericArgs,
Expand All @@ -57,7 +57,7 @@ pub(crate) struct PathDiagnosticCallback<'a, 'db> {
}

pub(crate) struct PathLoweringContext<'a, 'b, 'db> {
ctx: &'a mut TyLoweringContext<'db, 'b>,
pub(crate) ctx: &'a mut TyLoweringContext<'db, 'b>,
on_diagnostic: PathDiagnosticCallback<'a, 'db>,
path: &'a Path,
segments: PathSegments<'a>,
Expand Down Expand Up @@ -739,7 +739,7 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
) -> Const<'db> {
match arg {
TypeLikeConst::Path(path) => self.ctx.ctx.lower_path_as_const(path, const_ty),
TypeLikeConst::Infer => unknown_const(const_ty),
TypeLikeConst::Infer => unknown_const(self.ctx.ctx.interner, const_ty),
}
}

Expand Down Expand Up @@ -781,7 +781,10 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
let GenericParamId::ConstParamId(const_id) = param_id else {
unreachable!("non-const param ID for const param");
};
unknown_const_as_generic(const_param_ty_query(self.ctx.ctx.db, const_id))
unknown_const_as_generic(
self.ctx.ctx.interner,
const_param_ty_query(self.ctx.ctx.db, const_id),
)
}
}
}
Expand All @@ -791,9 +794,10 @@ impl<'a, 'b, 'db> PathLoweringContext<'a, 'b, 'db> {
GenericParamId::TypeParamId(_) => {
Ty::new_error(self.ctx.ctx.interner, ErrorGuaranteed).into()
}
GenericParamId::ConstParamId(const_id) => {
unknown_const_as_generic(const_param_ty_query(self.ctx.ctx.db, const_id))
}
GenericParamId::ConstParamId(const_id) => unknown_const_as_generic(
self.ctx.ctx.interner,
const_param_ty_query(self.ctx.ctx.db, const_id),
),
GenericParamId::LifetimeParamId(_) => {
Region::new(self.ctx.ctx.interner, rustc_type_ir::ReError(ErrorGuaranteed))
.into()
Expand Down Expand Up @@ -1332,7 +1336,7 @@ fn unknown_subst<'db>(interner: DbInterner<'db>, def: impl Into<GenericDefId>) -
params.iter_id().map(|id| match id {
GenericParamId::TypeParamId(_) => Ty::new_error(interner, ErrorGuaranteed).into(),
GenericParamId::ConstParamId(id) => {
unknown_const_as_generic(const_param_ty_query(interner.db(), id))
unknown_const_as_generic(interner, const_param_ty_query(interner.db(), id))
}
GenericParamId::LifetimeParamId(_) => Region::error(interner).into(),
}),
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/next_solver/infer/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ impl<'db> PredicateObligation<'db> {
/// Flips the polarity of the inner predicate.
///
/// Given `T: Trait` predicate it returns `T: !Trait` and given `T: !Trait` returns `T: Trait`.
pub fn flip_polarity(&self, _interner: DbInterner<'db>) -> Option<PredicateObligation<'db>> {
pub fn flip_polarity(&self, interner: DbInterner<'db>) -> Option<PredicateObligation<'db>> {
Some(PredicateObligation {
cause: self.cause.clone(),
param_env: self.param_env,
predicate: self.predicate.flip_polarity()?,
predicate: self.predicate.flip_polarity(interner)?,
recursion_depth: self.recursion_depth,
})
}
Expand Down
1 change: 1 addition & 0 deletions crates/hir-ty/src/next_solver/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ unsafe impl Sync for DbInterner<'_> {}

impl<'db> DbInterner<'db> {
// FIXME(next-solver): remove this method
#[doc(hidden)]
pub fn conjure() -> DbInterner<'db> {
// Here we can not reinit the cache since we do that when we attach the db.
crate::with_attached_db(|db| DbInterner {
Expand Down
15 changes: 4 additions & 11 deletions crates/hir-ty/src/next_solver/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl<'db> Predicate<'db> {
/// Flips the polarity of a Predicate.
///
/// Given `T: Trait` predicate it returns `T: !Trait` and given `T: !Trait` returns `T: Trait`.
pub fn flip_polarity(self) -> Option<Predicate<'db>> {
pub fn flip_polarity(self, interner: DbInterner<'db>) -> Option<Predicate<'db>> {
let kind = self
.kind()
.map_bound(|kind| match kind {
Expand All @@ -244,7 +244,7 @@ impl<'db> Predicate<'db> {
})
.transpose()?;

Some(Predicate::new(DbInterner::conjure(), kind))
Some(Predicate::new(interner, kind))
}
}

Expand Down Expand Up @@ -354,13 +354,6 @@ impl<'db> rustc_type_ir::inherent::SliceLike for Clauses<'db> {
}
}

impl<'db> Default for Clauses<'db> {
#[inline]
fn default() -> Self {
Clauses::empty(DbInterner::conjure())
}
}

impl<'db> rustc_type_ir::inherent::Clauses<DbInterner<'db>> for Clauses<'db> {}

impl<'db> rustc_type_ir::TypeSuperFoldable<DbInterner<'db>> for Clauses<'db> {
Expand Down Expand Up @@ -443,8 +436,8 @@ pub struct ParamEnv<'db> {
}

impl<'db> ParamEnv<'db> {
pub fn empty() -> Self {
ParamEnv { clauses: Clauses::empty(DbInterner::conjure()) }
pub fn empty(interner: DbInterner<'db>) -> Self {
ParamEnv { clauses: Clauses::empty(interner) }
}

pub fn clauses(self) -> Clauses<'db> {
Expand Down
58 changes: 34 additions & 24 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1657,9 +1657,11 @@ impl Enum {

/// The type of the enum variant bodies.
pub fn variant_body_ty<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> {
let interner = DbInterner::new_no_crate(db);
let krate = self.id.lookup(db).container.krate(db);
let interner = DbInterner::new_with(db, krate);
Type::new_for_crate(
self.id.lookup(db).container.krate(db),
db,
krate,
match EnumSignature::variant_body_type(db, self.id) {
layout::IntegerType::Pointer(sign) => match sign {
true => Ty::new_int(interner, rustc_type_ir::IntTy::Isize),
Expand Down Expand Up @@ -2229,8 +2231,11 @@ impl DefWithBody {
mir::MirSpan::Unknown => continue,
};
acc.push(
MovedOutOfRef { ty: Type::new_for_crate(krate, moof.ty.as_ref()), span }
.into(),
MovedOutOfRef {
ty: Type::new_for_crate(db, krate, moof.ty.as_ref()),
span,
}
.into(),
)
}
let mol = &borrowck_result.mutability_of_locals;
Expand Down Expand Up @@ -3425,7 +3430,7 @@ impl BuiltinType {
pub fn ty<'db>(self, db: &'db dyn HirDatabase) -> Type<'db> {
let core = Crate::core(db).map(|core| core.id).unwrap_or_else(|| all_crates(db)[0]);
let interner = DbInterner::new_no_crate(db);
Type::new_for_crate(core, Ty::from_builtin_type(interner, self.inner))
Type::new_for_crate(db, core, Ty::from_builtin_type(interner, self.inner))
}

pub fn name(self) -> Name {
Expand Down Expand Up @@ -5463,8 +5468,13 @@ impl<'db> Type<'db> {
Type { env: environment, ty }
}

pub(crate) fn new_for_crate(krate: base_db::Crate, ty: Ty<'db>) -> Self {
Type { env: empty_param_env(krate), ty }
pub(crate) fn new_for_crate(
db: &'db dyn HirDatabase,
krate: base_db::Crate,
ty: Ty<'db>,
) -> Self {
let interner = DbInterner::new_with(db, krate);
Type { env: ParamEnvAndCrate { param_env: ParamEnv::empty(interner), krate }, ty }
}

fn new(db: &'db dyn HirDatabase, lexical_env: impl HasResolver, ty: Ty<'db>) -> Self {
Expand Down Expand Up @@ -5517,15 +5527,18 @@ impl<'db> Type<'db> {
Type::new(db, def, ty.instantiate(interner, args))
}

pub fn new_slice(ty: Self) -> Self {
let interner = DbInterner::conjure();
pub fn new_slice(db: &'db dyn HirDatabase, ty: Self) -> Self {
let interner = DbInterner::new_no_crate(db);
Type { env: ty.env, ty: Ty::new_slice(interner, ty.ty) }
}

pub fn new_tuple(krate: base_db::Crate, tys: &[Self]) -> Self {
pub fn new_tuple(db: &'db dyn HirDatabase, krate: base_db::Crate, tys: &[Self]) -> Self {
let tys = tys.iter().map(|it| it.ty);
let interner = DbInterner::conjure();
Type { env: empty_param_env(krate), ty: Ty::new_tup_from_iter(interner, tys) }
let interner = DbInterner::new_with(db, krate);
Type {
env: ParamEnvAndCrate { param_env: ParamEnv::empty(interner), krate },
ty: Ty::new_tup_from_iter(interner, tys),
}
}

pub fn is_unit(&self) -> bool {
Expand Down Expand Up @@ -5634,8 +5647,8 @@ impl<'db> Type<'db> {
Some((self.derived(ty), m))
}

pub fn add_reference(&self, mutability: Mutability) -> Self {
let interner = DbInterner::conjure();
pub fn add_reference(&self, db: &'db dyn HirDatabase, mutability: Mutability) -> Self {
let interner = DbInterner::new_no_crate(db);
let ty_mutability = match mutability {
Mutability::Shared => hir_ty::next_solver::Mutability::Not,
Mutability::Mut => hir_ty::next_solver::Mutability::Mut,
Expand Down Expand Up @@ -5953,9 +5966,9 @@ impl<'db> Type<'db> {
}
}

pub fn fingerprint_for_trait_impl(&self) -> Option<SimplifiedType> {
pub fn fingerprint_for_trait_impl(&self, db: &'db dyn HirDatabase) -> Option<SimplifiedType> {
fast_reject::simplify_type(
DbInterner::conjure(),
DbInterner::new_no_crate(db),
self.ty,
fast_reject::TreatParams::AsRigid,
)
Expand Down Expand Up @@ -6563,7 +6576,7 @@ impl<'db> TypeNs<'db> {
let predicate = hir_ty::next_solver::Predicate::new(infcx.interner, pred_kind);
let goal = hir_ty::next_solver::Goal::new(
infcx.interner,
hir_ty::next_solver::ParamEnv::empty(),
hir_ty::next_solver::ParamEnv::empty(infcx.interner),
predicate,
);
let res = hir_ty::traits::next_trait_solve_in_ctxt(&infcx, goal);
Expand Down Expand Up @@ -7349,9 +7362,10 @@ fn param_env_from_resolver<'db>(
resolver: &Resolver<'_>,
) -> ParamEnvAndCrate<'db> {
ParamEnvAndCrate {
param_env: resolver
.generic_def()
.map_or_else(ParamEnv::empty, |generic_def| db.trait_environment(generic_def.into())),
param_env: resolver.generic_def().map_or_else(
|| ParamEnv::empty(DbInterner::new_no_crate(db)),
|generic_def| db.trait_environment(generic_def.into()),
),
krate: resolver.krate(),
}
}
Expand All @@ -7370,9 +7384,5 @@ fn body_param_env_from_has_crate<'db>(
ParamEnvAndCrate { param_env: db.trait_environment(id.into()), krate: id.krate(db) }
}

fn empty_param_env<'db>(krate: base_db::Crate) -> ParamEnvAndCrate<'db> {
ParamEnvAndCrate { param_env: ParamEnv::empty(), krate }
}

pub use hir_ty::next_solver;
pub use hir_ty::setup_tracing;
9 changes: 5 additions & 4 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,14 @@ impl<'db> SourceAnalyzer<'db> {
}

fn trait_environment(&self, db: &'db dyn HirDatabase) -> ParamEnvAndCrate<'db> {
self.param_and(self.body_or_sig.as_ref().map_or_else(ParamEnv::empty, |body_or_sig| {
match *body_or_sig {
self.param_and(self.body_or_sig.as_ref().map_or_else(
|| ParamEnv::empty(DbInterner::new_no_crate(db)),
|body_or_sig| match *body_or_sig {
BodyOrSig::Body { def, .. } => db.trait_environment(def.into()),
BodyOrSig::VariantFields { def, .. } => db.trait_environment(def.into()),
BodyOrSig::Sig { def, .. } => db.trait_environment(def.into()),
}
}))
},
))
}

pub(crate) fn expr_id(&self, expr: ast::Expr) -> Option<ExprOrPatId> {
Expand Down
Loading