1- use std:: path:: Path ;
21use std:: sync:: Arc ;
32
43pub use jars:: { HasJar , HasJars } ;
@@ -7,12 +6,12 @@ pub use runtime::DbRuntime;
76pub use storage:: JarsStorage ;
87
98use crate :: files:: FileId ;
10- use crate :: lint:: { Diagnostics , LintSemanticStorage , LintSyntaxStorage } ;
11- use crate :: module:: { Module , ModuleData , ModuleName , ModuleResolver , ModuleSearchPath } ;
12- use crate :: parse:: { Parsed , ParsedStorage } ;
13- use crate :: source:: { Source , SourceStorage } ;
14- use crate :: symbols:: { SymbolId , SymbolTable , SymbolTablesStorage } ;
15- use crate :: types:: { Type , TypeStore } ;
9+ use crate :: lint:: { LintSemanticStorage , LintSyntaxStorage } ;
10+ use crate :: module:: ModuleResolver ;
11+ use crate :: parse:: ParsedStorage ;
12+ use crate :: source:: SourceStorage ;
13+ use crate :: symbols:: SymbolTablesStorage ;
14+ use crate :: types:: TypeStore ;
1615
1716mod jars;
1817mod query;
@@ -61,6 +60,8 @@ pub trait ParallelDatabase: Database + Send {
6160 fn snapshot ( & self ) -> Snapshot < Self > ;
6261}
6362
63+ pub trait DbWithJar < Jar > : Database + HasJar < Jar > { }
64+
6465/// Readonly snapshot of a database.
6566///
6667/// ## Dead locks
@@ -96,45 +97,24 @@ where
9697 }
9798}
9899
100+ pub trait Upcast < T : ?Sized > {
101+ fn upcast ( & self ) -> & T ;
102+ }
103+
99104// Red knot specific databases code.
100105
101- pub trait SourceDb : Database {
106+ pub trait SourceDb : DbWithJar < SourceJar > {
102107 // queries
103108 fn file_id ( & self , path : & std:: path:: Path ) -> FileId ;
104109
105110 fn file_path ( & self , file_id : FileId ) -> Arc < std:: path:: Path > ;
106-
107- fn source ( & self , file_id : FileId ) -> QueryResult < Source > ;
108-
109- fn parse ( & self , file_id : FileId ) -> QueryResult < Parsed > ;
110- }
111-
112- pub trait SemanticDb : SourceDb {
113- // queries
114- fn resolve_module ( & self , name : ModuleName ) -> QueryResult < Option < Module > > ;
115-
116- fn file_to_module ( & self , file_id : FileId ) -> QueryResult < Option < Module > > ;
117-
118- fn path_to_module ( & self , path : & Path ) -> QueryResult < Option < Module > > ;
119-
120- fn symbol_table ( & self , file_id : FileId ) -> QueryResult < Arc < SymbolTable > > ;
121-
122- fn infer_symbol_type ( & self , file_id : FileId , symbol_id : SymbolId ) -> QueryResult < Type > ;
123-
124- // mutations
125-
126- fn add_module ( & mut self , path : & Path ) -> Option < ( Module , Vec < Arc < ModuleData > > ) > ;
127-
128- fn set_module_search_paths ( & mut self , paths : Vec < ModuleSearchPath > ) ;
129111}
130112
131- pub trait LintDb : SemanticDb {
132- fn lint_syntax ( & self , file_id : FileId ) -> QueryResult < Diagnostics > ;
113+ pub trait SemanticDb : SourceDb + DbWithJar < SemanticJar > + Upcast < dyn SourceDb > { }
133114
134- fn lint_semantic ( & self , file_id : FileId ) -> QueryResult < Diagnostics > ;
135- }
115+ pub trait LintDb : SemanticDb + DbWithJar < LintJar > + Upcast < dyn SemanticDb > { }
136116
137- pub trait Db : LintDb { }
117+ pub trait Db : LintDb + Upcast < dyn LintDb > { }
138118
139119#[ derive( Debug , Default ) ]
140120pub struct SourceJar {
@@ -161,19 +141,10 @@ pub(crate) mod tests {
161141 use std:: sync:: Arc ;
162142
163143 use crate :: db:: {
164- Database , DbRuntime , HasJar , HasJars , JarsStorage , LintDb , LintJar , QueryResult , SourceDb ,
165- SourceJar ,
144+ Database , DbRuntime , DbWithJar , HasJar , HasJars , JarsStorage , LintDb , LintJar , QueryResult ,
145+ SourceDb , SourceJar , Upcast ,
166146 } ;
167147 use crate :: files:: { FileId , Files } ;
168- use crate :: lint:: { lint_semantic, lint_syntax, Diagnostics } ;
169- use crate :: module:: {
170- add_module, file_to_module, path_to_module, resolve_module, set_module_search_paths,
171- Module , ModuleData , ModuleName , ModuleSearchPath ,
172- } ;
173- use crate :: parse:: { parse, Parsed } ;
174- use crate :: source:: { source_text, Source } ;
175- use crate :: symbols:: { symbol_table, SymbolId , SymbolTable } ;
176- use crate :: types:: { infer_symbol_type, Type } ;
177148
178149 use super :: { SemanticDb , SemanticJar } ;
179150
@@ -223,56 +194,36 @@ pub(crate) mod tests {
223194 fn file_path ( & self , file_id : FileId ) -> Arc < Path > {
224195 self . files . path ( file_id)
225196 }
226-
227- fn source ( & self , file_id : FileId ) -> QueryResult < Source > {
228- source_text ( self , file_id)
229- }
230-
231- fn parse ( & self , file_id : FileId ) -> QueryResult < Parsed > {
232- parse ( self , file_id)
233- }
234197 }
235198
236- impl SemanticDb for TestDb {
237- fn resolve_module ( & self , name : ModuleName ) -> QueryResult < Option < Module > > {
238- resolve_module ( self , name)
239- }
240-
241- fn file_to_module ( & self , file_id : FileId ) -> QueryResult < Option < Module > > {
242- file_to_module ( self , file_id)
243- }
199+ impl DbWithJar < SourceJar > for TestDb { }
244200
245- fn path_to_module ( & self , path : & Path ) -> QueryResult < Option < Module > > {
246- path_to_module ( self , path)
201+ impl Upcast < dyn SourceDb > for TestDb {
202+ fn upcast ( & self ) -> & ( dyn SourceDb + ' static ) {
203+ self
247204 }
205+ }
248206
249- fn symbol_table ( & self , file_id : FileId ) -> QueryResult < Arc < SymbolTable > > {
250- symbol_table ( self , file_id)
251- }
207+ impl SemanticDb for TestDb { }
252208
253- fn infer_symbol_type ( & self , file_id : FileId , symbol_id : SymbolId ) -> QueryResult < Type > {
254- infer_symbol_type ( self , file_id, symbol_id)
255- }
209+ impl DbWithJar < SemanticJar > for TestDb { }
256210
257- fn add_module ( & mut self , path : & Path ) -> Option < ( Module , Vec < Arc < ModuleData > > ) > {
258- add_module ( self , path)
259- }
260-
261- fn set_module_search_paths ( & mut self , paths : Vec < ModuleSearchPath > ) {
262- set_module_search_paths ( self , paths) ;
211+ impl Upcast < dyn SemanticDb > for TestDb {
212+ fn upcast ( & self ) -> & ( dyn SemanticDb + ' static ) {
213+ self
263214 }
264215 }
265216
266- impl LintDb for TestDb {
267- fn lint_syntax ( & self , file_id : FileId ) -> QueryResult < Diagnostics > {
268- lint_syntax ( self , file_id)
269- }
217+ impl LintDb for TestDb { }
270218
271- fn lint_semantic ( & self , file_id : FileId ) -> QueryResult < Diagnostics > {
272- lint_semantic ( self , file_id)
219+ impl Upcast < dyn LintDb > for TestDb {
220+ fn upcast ( & self ) -> & ( dyn LintDb + ' static ) {
221+ self
273222 }
274223 }
275224
225+ impl DbWithJar < LintJar > for TestDb { }
226+
276227 impl HasJars for TestDb {
277228 type Jars = ( SourceJar , SemanticJar , LintJar ) ;
278229
0 commit comments