Skip to content
Draft
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
251 changes: 175 additions & 76 deletions Cargo.lock

Large diffs are not rendered by default.

764 changes: 505 additions & 259 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/starpls_common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ edition = "2021"

[dependencies]
anyhow = "1.0.81"
salsa = { git = "https://github.com/withered-magic/salsa", package = "salsa-2022", rev = "91fdda90b344ef74e9bf35c3a5bb0fbae22ed6fb" }
salsa = "0.28.6"
starpls_bazel = { path = "../starpls_bazel" }
starpls_syntax = { path = "../starpls_syntax" }
13 changes: 12 additions & 1 deletion crates/starpls_common/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,15 @@ pub enum DiagnosticTag {
}

#[salsa::accumulator]
pub struct Diagnostics(Diagnostic);
pub struct Diagnostics(pub Diagnostic);

impl Diagnostics {
/// Accumulates the given diagnostic in the currently executing query.
pub fn push<Db>(db: &Db, diagnostic: Diagnostic)
where
Db: ?Sized + salsa::Database,
{
use salsa::Accumulator;
Diagnostics(diagnostic).accumulate(db)
}
}
69 changes: 27 additions & 42 deletions crates/starpls_common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Debug;
use std::path::PathBuf;

use salsa::Accumulator;
use starpls_bazel::APIContext;
use starpls_syntax::line_index as syntax_line_index;
use starpls_syntax::parse_module;
Expand All @@ -18,16 +19,6 @@ pub use crate::diagnostics::Severity;
mod diagnostics;
mod util;

#[salsa::jar(db = Db)]
pub struct Jar(
Diagnostics,
File,
LineIndexResult,
Parse,
parse,
line_index_query,
);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Dialect {
Standard,
Expand Down Expand Up @@ -63,7 +54,8 @@ pub enum ResolvedPath {
}

/// The base Salsa database. Supports file-related operations, like getting/setting file contents.
pub trait Db: salsa::DbWithJar<Jar> {
#[salsa::db]
pub trait Db: salsa::Database {
/// Creates a `File` in the database. This will overwrite the currently active
/// `File` for the given `FileId`, if it exists.
fn create_file(
Expand Down Expand Up @@ -109,12 +101,15 @@ pub enum FileInfo {
},
}

#[salsa::input]
#[salsa::input(debug)]
pub struct File {
#[returns(copy)]
pub id: FileId,
#[returns(copy)]
pub dialect: Dialect,
#[returns(clone)]
pub info: Option<FileInfo>,
#[return_ref]
#[returns(ref)]
pub contents: String,
}

Expand All @@ -133,12 +128,15 @@ impl File {
}

#[salsa::tracked]
pub struct Parse {
pub struct Parse<'db> {
#[returns(copy)]
pub file: File,
#[tracked]
#[returns(ref)]
module: ParseTree<Module>,
}

impl Parse {
impl<'db> Parse<'db> {
pub fn tree(&self, db: &dyn Db) -> Module {
self.module(db).tree()
}
Expand All @@ -148,39 +146,26 @@ impl Parse {
}
}

#[salsa::tracked]
pub fn parse(db: &dyn Db, file: File) -> Parse {
#[salsa::tracked(returns(clone))]
pub fn parse(db: &dyn Db, file: File) -> Parse<'_> {
let parse = parse_module(file.contents(db), &mut |err| {
Diagnostics::push(
db,
Diagnostic {
message: err.message,
range: FileRange {
file_id: file.id(db),
range: err.range,
},
severity: Severity::Error,
tags: None,
Diagnostics(Diagnostic {
message: err.message,
range: FileRange {
file_id: file.id(db),
range: err.range,
},
)
severity: Severity::Error,
tags: None,
})
.accumulate(db)
});
Parse::new(db, file, parse)
}

#[salsa::tracked]
struct LineIndexResult {
#[return_ref]
pub inner: LineIndex,
}

#[salsa::tracked]
fn line_index_query(db: &dyn Db, file: File) -> LineIndexResult {
let line_index = syntax_line_index(file.contents(db));
LineIndexResult::new(db, line_index)
}

pub fn line_index(db: &dyn Db, file: File) -> &LineIndex {
line_index_query(db, file).inner(db)
#[salsa::tracked(returns(ref))]
pub fn line_index(db: &dyn Db, file: File) -> LineIndex {
syntax_line_index(file.contents(db))
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion crates/starpls_hir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
crossbeam = "0.8.3"
dashmap = "5.5.3"
id-arena = "2.2.1"
salsa = { git = "https://github.com/withered-magic/salsa", package = "salsa-2022", rev = "91fdda90b344ef74e9bf35c3a5bb0fbae22ed6fb" }
salsa = "0.28.6"
starpls_bazel = { path = "../starpls_bazel" }
starpls_common = { path = "../starpls_common" }
starpls_intern = { path = "../starpls_intern" }
Expand Down
18 changes: 12 additions & 6 deletions crates/starpls_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,11 @@ impl Param {
}
}

#[salsa::tracked]
#[salsa::interned(debug, unsafe(no_lifetime), revisions = usize::MAX)]
pub struct LoadStmt {
#[return_ref]
#[returns(ref)]
pub(crate) module: Box<str>,
#[returns(clone)]
pub(crate) ptr: SyntaxNodePtr,
}

Expand Down Expand Up @@ -466,21 +467,26 @@ impl fmt::Display for Name {
}
}

#[salsa::interned]
#[salsa::interned(debug, unsafe(no_lifetime), revisions = usize::MAX)]
pub(crate) struct InternedString {
#[return_ref]
#[returns(ref)]
pub(crate) value: Box<str>,
}

/// Used for both function definitions and lambda expressions.
#[salsa::tracked]
#[salsa::interned(debug, unsafe(no_lifetime), revisions = usize::MAX)]
pub(crate) struct Function {
#[returns(clone)]
pub(crate) file: File,
#[returns(clone)]
pub(crate) name: Name,
#[returns(clone)]
pub(crate) ret_type_ref: Option<TypeRef>,
#[returns(clone)]
pub(crate) doc: Option<Box<str>>,
#[returns(clone)]
ptr: SyntaxNodePtr,
#[return_ref]
#[returns(ref)]
pub(crate) params: Box<[ParamId]>,
}

Expand Down
9 changes: 5 additions & 4 deletions crates/starpls_hir/src/def/codeflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,15 @@ pub(crate) fn lower_to_code_flow_graph(module: &Module, scopes: &Scopes) -> Code
}

#[salsa::tracked]
pub(crate) struct CodeFlowGraphResult {
#[return_ref]
pub(crate) struct CodeFlowGraphResult<'db> {
#[returns(ref)]
#[tracked]
pub(crate) cfg: CodeFlowGraph,
}

#[allow(unused)]
#[salsa::tracked]
pub(crate) fn code_flow_graph(db: &dyn Db, file: File) -> CodeFlowGraphResult {
#[salsa::tracked(returns(clone))]
pub(crate) fn code_flow_graph(db: &dyn Db, file: File) -> CodeFlowGraphResult<'_> {
let info = lower(db, file);
let scopes = module_scopes(db, file);
let cfg = lower_to_code_flow_graph(info.module(db), scopes.scopes(db));
Expand Down
18 changes: 11 additions & 7 deletions crates/starpls_hir/src/def/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,23 @@ pub(crate) enum ExecutionScopeId {
}

#[salsa::tracked]
pub(crate) struct ModuleScopes {
#[return_ref]
pub(crate) struct ModuleScopes<'db> {
#[returns(ref)]
#[tracked]
pub(crate) scopes: Scopes,
}

#[salsa::tracked]
pub(crate) fn module_scopes_query(db: &dyn Db, info: ModuleInfo) -> ModuleScopes {
#[salsa::tracked(returns(clone))]
pub(crate) fn module_scopes_query<'db>(
db: &'db dyn Db,
info: ModuleInfo<'db>,
) -> ModuleScopes<'db> {
let scopes = Scopes::new_for_module(db, info);
ModuleScopes::new(db, scopes)
}

#[salsa::tracked]
pub(crate) fn module_scopes(db: &dyn Db, file: File) -> ModuleScopes {
#[salsa::tracked(returns(clone))]
pub(crate) fn module_scopes(db: &dyn Db, file: File) -> ModuleScopes<'_> {
let info = lower(db, file);
module_scopes_query(db, info)
}
Expand Down Expand Up @@ -163,7 +167,7 @@ struct FunctionData {
}

impl Scopes {
fn new_for_module(db: &dyn Db, info: ModuleInfo) -> Self {
fn new_for_module(db: &dyn Db, info: ModuleInfo<'_>) -> Self {
ScopeCollector {
db,
deferred: VecDeque::new(),
Expand Down
59 changes: 17 additions & 42 deletions crates/starpls_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,53 +61,24 @@ mod test_database;
mod typeck;

#[salsa::tracked]
pub(crate) struct ModuleInfo {
pub(crate) struct ModuleInfo<'db> {
#[tracked]
#[returns(clone)]
pub file: File,
#[return_ref]
#[returns(ref)]
#[tracked]
pub module: Module,
#[return_ref]
#[returns(ref)]
#[tracked]
pub source_map: ModuleSourceMap,
}

#[salsa::jar(db = Db)]
pub struct Jar(
lower,
ModuleInfo,
def::Function,
def::LoadStmt,
def::InternedString,
def::codeflow::CodeFlowGraphResult,
def::codeflow::code_flow_graph,
def::scope::ModuleScopes,
def::scope::module_scopes,
def::scope::module_scopes_query,
typeck::builtins::BuiltinDefs,
typeck::builtins::BuiltinFunction,
typeck::builtins::BuiltinGlobals,
typeck::builtins::BuiltinProvider,
typeck::builtins::BuiltinProviders,
typeck::builtins::BuiltinType,
typeck::builtins::BuiltinTypes,
typeck::builtins::builtin_globals_query,
typeck::builtins::builtin_providers_query,
typeck::builtins::builtin_types_query,
typeck::builtins::CommonAttributes,
typeck::builtins::common_attributes_query,
typeck::intrinsics::Intrinsics,
typeck::intrinsics::IntrinsicClass,
typeck::intrinsics::IntrinsicFieldTypes,
typeck::intrinsics::IntrinsicFunction,
typeck::intrinsics::IntrinsicFunctions,
typeck::intrinsics::intrinsic_types,
typeck::intrinsics::intrinsic_field_types,
typeck::intrinsics::intrinsic_functions,
);

/// Documentation for the `Target` type defined by Bazel.
/// TODO(withered-magic): Find a better place to put this.
const TARGET_DOC: &str = "The BUILD target for a dependency. Appears in the fields of `ctx.attr` corresponding to dependency attributes (`label` or `label_list`).";

pub trait Db: salsa::DbWithJar<Jar> + starpls_common::Db {
#[salsa::db]
pub trait Db: salsa::Database + starpls_common::Db {
fn gcx(&self) -> &GlobalContext;
fn set_builtin_defs(&mut self, dialect: Dialect, builtins: Builtins, rules: Builtins);
fn get_builtin_defs(&self, dialect: &Dialect) -> BuiltinDefs;
Expand All @@ -121,7 +92,11 @@ pub trait Db: salsa::DbWithJar<Jar> + starpls_common::Db {
/// This does not include diagnostics from type inference, which are reported
/// by [`typeck::TyContext`] instead.
pub fn diagnostics_for_file(db: &dyn Db, file: File) -> impl Iterator<Item = Diagnostic> {
module_scopes::accumulated::<Diagnostics>(db, file).into_iter()
module_scopes::accumulated::<Diagnostics>(db, file)
.into_iter()
.map(|diagnostic| diagnostic.0.clone())
.collect::<Vec<_>>()
.into_iter()
}

pub struct Semantics<'a> {
Expand All @@ -133,7 +108,7 @@ impl<'a> Semantics<'a> {
Self { db }
}

pub fn parse(&self, file: File) -> Parse {
pub fn parse(&self, file: File) -> Parse<'a> {
parse(self.db, file)
}

Expand Down Expand Up @@ -860,8 +835,8 @@ impl From<scope::ScopeDef> for ScopeDef {
}
}

#[salsa::tracked]
pub(crate) fn lower(db: &dyn Db, file: File) -> ModuleInfo {
#[salsa::tracked(returns(clone))]
pub(crate) fn lower(db: &dyn Db, file: File) -> ModuleInfo<'_> {
let parse = parse(db, file);
let (module, source_map) = Module::new_with_source_map(db, file, parse.tree(db));
ModuleInfo::new(db, file, module, source_map)
Expand Down
8 changes: 6 additions & 2 deletions crates/starpls_hir/src/test_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::Arc;
use dashmap::mapref::entry::Entry;
use dashmap::DashMap;
use rustc_hash::FxHashMap;
use salsa::Setter;
use starpls_bazel::APIContext;
use starpls_bazel::Builtins;
use starpls_common::File;
Expand All @@ -24,8 +25,8 @@ use crate::Dialect;
use crate::GlobalContext;
use crate::InferenceOptions;

#[derive(Default)]
#[salsa::db(starpls_common::Jar, crate::Jar)]
#[derive(Default, Clone)]
#[salsa::db]
pub(crate) struct TestDatabase {
builtin_defs: Arc<DashMap<Dialect, BuiltinDefs>>,
storage: salsa::Storage<Self>,
Expand All @@ -35,8 +36,10 @@ pub(crate) struct TestDatabase {
pub(crate) gcx: Arc<GlobalContext>,
}

#[salsa::db]
impl salsa::Database for TestDatabase {}

#[salsa::db]
impl starpls_common::Db for TestDatabase {
fn create_file(
&mut self,
Expand Down Expand Up @@ -97,6 +100,7 @@ impl starpls_common::Db for TestDatabase {
}
}

#[salsa::db]
impl crate::Db for TestDatabase {
fn set_builtin_defs(&mut self, dialect: Dialect, builtins: Builtins, rules: Builtins) {
let defs = match self.builtin_defs.entry(dialect) {
Expand Down
Loading