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: 2 additions & 0 deletions crates/base-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ pub trait SourceDatabase: salsa::Database + std::fmt::Debug {
fn nonce_and_revision(&self) -> (Nonce, salsa::Revision);

fn line_column(&self, file: FileId, offset: TextSize) -> Result<(u32, u32), ()>;

fn as_dyn(&self) -> &dyn SourceDatabase;
}

static NEXT_NONCE: AtomicUsize = AtomicUsize::new(0);
Expand Down
4 changes: 4 additions & 0 deletions crates/hir-def/src/test_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ impl SourceDatabase for TestDB {
fn line_column(&self, _file: FileId, _offset: syntax::TextSize) -> Result<(u32, u32), ()> {
Err(())
}

fn as_dyn(&self) -> &dyn SourceDatabase {
self
}
}

impl TestDB {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

use std::fmt;

use base_db::SourceDatabase;
use hir_def::{TraitId, TypeAliasId};
use rustc_type_ir::inherent::{IntoKind, Ty as _};
use tracing::debug;

use crate::{
ParamEnvAndCrate, Span,
db::HirDatabase,
infer::InferenceContext,
next_solver::{
Canonical, DbInterner, ParamEnv, TraitRef, Ty, TyKind, TypingMode,
Expand All @@ -35,7 +35,7 @@ const AUTODEREF_RECURSION_LIMIT: usize = 20;
/// - a type won't be yielded more than once; in other words, the returned iterator will stop if it
/// detects a cycle in the deref chain.
pub fn autoderef<'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
env: ParamEnvAndCrate<'db>,
ty: Canonical<'db, Ty<'db>>,
) -> impl Iterator<Item = Ty<'db>> + use<'db> {
Expand Down
5 changes: 3 additions & 2 deletions crates/hir-ty/src/builtin_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::ops::ControlFlow;

use base_db::SourceDatabase;
use hir_def::{
AdtId, BuiltinDeriveImplId, BuiltinDeriveImplLoc, HasModule, LocalFieldId, TraitId,
TypeOrConstParamId, TypeParamId,
Expand Down Expand Up @@ -82,7 +83,7 @@ pub(crate) fn generics_of<'db>(
}
}

pub fn generic_params_count(db: &dyn HirDatabase, id: BuiltinDeriveImplId) -> usize {
pub fn generic_params_count(db: &dyn SourceDatabase, id: BuiltinDeriveImplId) -> usize {
let loc = id.loc(db);
let adt_params = GenericParams::of(db, loc.adt.into());
let extra_params_count = match loc.trait_ {
Expand Down Expand Up @@ -154,7 +155,7 @@ pub fn impl_trait<'db>(
}

#[salsa::tracked(returns(ref))]
pub fn predicates(db: &dyn HirDatabase, impl_: BuiltinDeriveImplId) -> GenericPredicates {
pub fn predicates(db: &dyn SourceDatabase, impl_: BuiltinDeriveImplId) -> GenericPredicates {
let loc = impl_.loc(db);
let generic_params = GenericParams::of(db, loc.adt.into());
let interner = DbInterner::new_with(db, loc.module(db).krate(db));
Expand Down
38 changes: 21 additions & 17 deletions crates/hir-ty/src/consteval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[cfg(test)]
mod tests;

use base_db::Crate;
use base_db::{Crate, SourceDatabase};
use hir_def::{
ConstId, EnumVariantId, ExpressionStoreOwnerId, HasModule, StaticId,
attrs::AttrFlags,
Expand Down Expand Up @@ -46,7 +46,7 @@ impl ConstEvalError<'_> {
pub fn pretty_print(
&self,
f: &mut String,
db: &dyn HirDatabase,
db: &dyn SourceDatabase,
span_formatter: impl Fn(span::FileId, span::TextRange) -> String,
display_target: DisplayTarget,
) -> std::result::Result<(), std::fmt::Error> {
Expand Down Expand Up @@ -214,7 +214,11 @@ pub(crate) fn literal_ty<'db>(
}

/// Interns a possibly-unknown target usize
pub fn usize_const<'db>(db: &'db dyn HirDatabase, value: Option<u128>, krate: Crate) -> Const<'db> {
pub fn usize_const<'db>(
db: &'db dyn SourceDatabase,
value: Option<u128>,
krate: Crate,
) -> Const<'db> {
let interner = DbInterner::new_no_crate(db);
let value = match value {
Some(value) => value,
Expand All @@ -236,7 +240,7 @@ pub fn allocation_as_usize(ec: Allocation<'_>) -> u128 {
u128::from_le_bytes(pad16(&ec.memory, false))
}

pub fn try_const_usize<'db>(db: &'db dyn HirDatabase, c: Const<'db>) -> Option<u128> {
pub fn try_const_usize<'db>(db: &'db dyn SourceDatabase, c: Const<'db>) -> Option<u128> {
match c.kind() {
ConstKind::Param(_) => None,
ConstKind::Infer(_) => None,
Expand Down Expand Up @@ -274,7 +278,7 @@ pub fn allocation_as_isize(ec: Allocation<'_>) -> i128 {
i128::from_le_bytes(pad16(&ec.memory, true))
}

pub fn try_const_isize<'db>(db: &'db dyn HirDatabase, c: Const<'db>) -> Option<i128> {
pub fn try_const_isize<'db>(db: &'db dyn SourceDatabase, c: Const<'db>) -> Option<i128> {
match c.kind() {
ConstKind::Param(_) => None,
ConstKind::Infer(_) => None,
Expand Down Expand Up @@ -323,7 +327,7 @@ pub(crate) enum CreateConstError<'db> {
}

pub(crate) fn path_to_const<'a, 'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
resolver: &Resolver<'db>,
generics: &dyn Fn() -> &'a Generics<'db>,
forbid_params_after: Option<u32>,
Expand Down Expand Up @@ -416,7 +420,7 @@ pub(crate) fn create_anon_const<'a, 'db>(

#[salsa::tracked(cycle_result = const_eval_discriminant_cycle_result)]
pub(crate) fn const_eval_discriminant_variant<'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
variant_id: EnumVariantId,
) -> Result<i128, ConstEvalError<'db>> {
let interner = DbInterner::new_no_crate(db);
Expand Down Expand Up @@ -452,15 +456,15 @@ pub(crate) fn const_eval_discriminant_variant<'db>(
}

fn const_eval_discriminant_cycle_result<'db>(
_: &'db dyn HirDatabase,
_: &'db dyn SourceDatabase,
_: salsa::Id,
_: EnumVariantId,
) -> Result<i128, ConstEvalError<'db>> {
Err(ConstEvalError::MirLowerError(MirLowerError::Loop))
}

pub(crate) fn const_eval<'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
def: ConstId,
subst: GenericArgs<'db>,
trait_env: Option<ParamEnvAndCrate<'db>>,
Expand All @@ -472,7 +476,7 @@ pub(crate) fn const_eval<'db>(

#[salsa::tracked(returns(ref), cycle_result = const_eval_cycle_result)]
pub(crate) fn const_eval_query<'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
def: ConstId,
subst: StoredGenericArgs,
trait_env: Option<StoredParamEnvAndCrate>,
Expand All @@ -488,7 +492,7 @@ pub(crate) fn const_eval<'db>(
}

pub(crate) fn const_eval_cycle_result<'db>(
_: &'db dyn HirDatabase,
_: &'db dyn SourceDatabase,
_: salsa::Id,
_: ConstId,
_: StoredGenericArgs,
Expand All @@ -499,7 +503,7 @@ pub(crate) fn const_eval<'db>(
}

pub(crate) fn anon_const_eval<'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
def: AnonConstId<'db>,
subst: GenericArgs<'db>,
trait_env: Option<ParamEnvAndCrate<'db>>,
Expand All @@ -511,7 +515,7 @@ pub(crate) fn anon_const_eval<'db>(

#[salsa::tracked(returns(ref), cycle_result = anon_const_eval_cycle_result)]
pub(crate) fn anon_const_eval_query<'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
def: AnonConstId<'db>,
subst: StoredGenericArgs,
trait_env: Option<StoredParamEnvAndCrate>,
Expand All @@ -530,7 +534,7 @@ pub(crate) fn anon_const_eval<'db>(
}

pub(crate) fn anon_const_eval_cycle_result<'db>(
_: &'db dyn HirDatabase,
_: &'db dyn SourceDatabase,
_: salsa::Id,
_: AnonConstId<'db>,
_: StoredGenericArgs,
Expand All @@ -541,7 +545,7 @@ pub(crate) fn anon_const_eval<'db>(
}

pub(crate) fn const_eval_static<'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
def: StaticId,
) -> Result<Allocation<'db>, ConstEvalError<'db>> {
return match const_eval_static_query(db, def) {
Expand All @@ -551,7 +555,7 @@ pub(crate) fn const_eval_static<'db>(

#[salsa::tracked(returns(ref), cycle_result = const_eval_static_cycle_result)]
pub(crate) fn const_eval_static_query<'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
def: StaticId,
) -> Result<StoredAllocation, ConstEvalError<'db>> {
let interner = DbInterner::new_no_crate(db);
Expand All @@ -566,7 +570,7 @@ pub(crate) fn const_eval_static<'db>(
}

pub(crate) fn const_eval_static_cycle_result<'db>(
_: &'db dyn HirDatabase,
_: &'db dyn SourceDatabase,
_: salsa::Id,
_: StaticId,
) -> Result<StoredAllocation, ConstEvalError<'db>> {
Expand Down
29 changes: 6 additions & 23 deletions crates/hir-ty/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ use crate::{
traits::{ParamEnvAndCrate, StoredParamEnvAndCrate},
};

#[salsa::db]
pub trait HirDatabase: SourceDatabase + 'static {
/// Manual implementation of upcasting from `dyn SourceDatabase` to `dyn HirDatabase`.
///
/// This function is needed because Rust can't perform this upcasting automatically
/// in the general case, as `Self` could be unsized.
fn as_dyn(&self) -> &dyn HirDatabase;

// region:mir

// FIXME: Collapse `mir_body_for_closure` into `mir_body`
Expand Down Expand Up @@ -334,17 +327,7 @@ pub trait HirDatabase: SourceDatabase + 'static {
}
}

#[salsa::db]
impl<T: SourceDatabase> HirDatabase for T {
fn as_dyn(&self) -> &dyn HirDatabase {
self
}
}

#[test]
fn hir_database_is_dyn_compatible() {
fn _assert_dyn_compatible(_: &dyn HirDatabase) {}
}
impl<T: SourceDatabase + ?Sized> HirDatabase for T {}

#[salsa::interned(debug, revisions = usize::MAX)]
#[derive(PartialOrd, Ord)]
Expand All @@ -367,7 +350,7 @@ pub struct InternedClosureId<'db> {

impl<'db> InternedClosureId<'db> {
#[inline]
pub fn new(db: &'db dyn HirDatabase, loc: InternedClosure<'db>) -> Self {
pub fn new(db: &'db dyn SourceDatabase, loc: InternedClosure<'db>) -> Self {
if cfg!(debug_assertions) {
let store = ExpressionStore::of(db, loc.owner.expression_store_owner(db));
let expr = &store[loc.expr];
Expand Down Expand Up @@ -395,7 +378,7 @@ pub struct InternedCoroutineId<'db> {

impl<'db> InternedCoroutineId<'db> {
#[inline]
pub fn new(db: &'db dyn HirDatabase, loc: InternedClosure<'db>) -> Self {
pub fn new(db: &'db dyn SourceDatabase, loc: InternedClosure<'db>) -> Self {
if cfg!(debug_assertions) {
let store = ExpressionStore::of(db, loc.owner.expression_store_owner(db));
let expr = &store[loc.expr];
Expand Down Expand Up @@ -424,7 +407,7 @@ pub struct InternedCoroutineClosureId<'db> {

impl<'db> InternedCoroutineClosureId<'db> {
#[inline]
pub fn new(db: &'db dyn HirDatabase, loc: InternedClosure<'db>) -> Self {
pub fn new(db: &'db dyn SourceDatabase, loc: InternedClosure<'db>) -> Self {
if cfg!(debug_assertions) {
let store = ExpressionStore::of(db, loc.owner.expression_store_owner(db));
let expr = &store[loc.expr];
Expand Down Expand Up @@ -492,7 +475,7 @@ impl HasResolver for AnonConstId<'_> {

impl<'db> AnonConstId<'db> {
pub fn all_from_signature(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
def: GenericDefId,
) -> ArrayVec<&'db [Self], 5> {
let mut result = ArrayVec::new();
Expand Down Expand Up @@ -540,7 +523,7 @@ pub enum GeneralConstId<'db> {
impl_from!(impl<'db> ConstId, StaticId, AnonConstId<'db> for GeneralConstId<'db>);

impl<'db> GeneralConstId<'db> {
pub fn generic_def(self, db: &'db dyn HirDatabase) -> Option<GenericDefId> {
pub fn generic_def(self, db: &'db dyn SourceDatabase) -> Option<GenericDefId> {
match self {
GeneralConstId::ConstId(it) => Some(it.into()),
GeneralConstId::StaticId(it) => Some(it.into()),
Expand Down
7 changes: 4 additions & 3 deletions crates/hir-ty/src/diagnostics/decl_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod case_conv;

use std::fmt;

use base_db::SourceDatabase;
use hir_def::{
AdtId, ConstId, EnumId, EnumVariantId, FunctionId, HasModule, ItemContainerId, Lookup,
ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, UnionId,
Expand Down Expand Up @@ -44,7 +45,7 @@ use crate::db::HirDatabase;

use self::case_conv::{to_camel_case, to_lower_snake_case, to_upper_snake_case};

pub fn incorrect_case(db: &dyn HirDatabase, owner: ModuleDefId) -> Vec<IncorrectCase> {
pub fn incorrect_case(db: &dyn SourceDatabase, owner: ModuleDefId) -> Vec<IncorrectCase> {
let _p = tracing::info_span!("incorrect_case").entered();
let mut validator = DeclValidator::new(db);
validator.validate_item(owner);
Expand Down Expand Up @@ -123,7 +124,7 @@ pub struct IncorrectCase {
}

pub(super) struct DeclValidator<'a> {
db: &'a dyn HirDatabase,
db: &'a dyn SourceDatabase,
pub(super) sink: Vec<IncorrectCase>,
}

Expand All @@ -135,7 +136,7 @@ struct Replacement {
}

impl<'a> DeclValidator<'a> {
pub(super) fn new(db: &'a dyn HirDatabase) -> DeclValidator<'a> {
pub(super) fn new(db: &'a dyn SourceDatabase) -> DeclValidator<'a> {
DeclValidator { db, sink: Vec::new() }
}

Expand Down
12 changes: 6 additions & 6 deletions crates/hir-ty/src/diagnostics/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::fmt;

use base_db::Crate;
use base_db::{Crate, SourceDatabase};
use either::Either;
use hir_def::{
AdtId, AssocItemId, CallableDefId, DefWithBodyId, HasModule, ItemContainerId, Lookup,
Expand Down Expand Up @@ -76,7 +76,7 @@ pub enum BodyValidationDiagnostic<'db> {

impl<'db> BodyValidationDiagnostic<'db> {
pub fn collect(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
owner: DefWithBodyId,
validate_lints: bool,
) -> Vec<BodyValidationDiagnostic<'db>> {
Expand Down Expand Up @@ -113,7 +113,7 @@ struct ExprValidator<'db> {

impl<'db> ExprValidator<'db> {
#[inline]
fn db(&self) -> &'db dyn HirDatabase {
fn db(&self) -> &'db dyn SourceDatabase {
self.infcx.interner.db
}

Expand Down Expand Up @@ -566,7 +566,7 @@ struct FilterMapNextChecker<'db> {
}

impl<'db> FilterMapNextChecker<'db> {
fn new(lang_items: &'db LangItems, db: &'db dyn HirDatabase) -> Self {
fn new(lang_items: &'db LangItems, db: &'db dyn SourceDatabase) -> Self {
// Find and store the FunctionIds for Iterator::filter_map and Iterator::next
let (next_function_id, filter_map_function_id) = match lang_items.IteratorNext {
Some(next_function_id) => (
Expand Down Expand Up @@ -622,7 +622,7 @@ impl<'db> FilterMapNextChecker<'db> {
}

pub fn record_literal_missing_fields<'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
infer: &InferenceResult<'db>,
id: ExprId,
expr: &Expr,
Expand Down Expand Up @@ -665,7 +665,7 @@ pub fn record_literal_missing_fields<'db>(
}

pub fn record_pattern_missing_fields<'db>(
db: &'db dyn HirDatabase,
db: &'db dyn SourceDatabase,
infer: &InferenceResult<'db>,
id: PatId,
pat: &Pat,
Expand Down
Loading