1
- use std:: path:: Path ;
2
1
use std:: sync:: Arc ;
3
2
4
3
pub use jars:: { HasJar , HasJars } ;
@@ -7,12 +6,12 @@ pub use runtime::DbRuntime;
7
6
pub use storage:: JarsStorage ;
8
7
9
8
use 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 ;
16
15
17
16
mod jars;
18
17
mod query;
@@ -61,6 +60,8 @@ pub trait ParallelDatabase: Database + Send {
61
60
fn snapshot ( & self ) -> Snapshot < Self > ;
62
61
}
63
62
63
+ pub trait DbWithJar < Jar > : Database + HasJar < Jar > { }
64
+
64
65
/// Readonly snapshot of a database.
65
66
///
66
67
/// ## Dead locks
@@ -96,45 +97,24 @@ where
96
97
}
97
98
}
98
99
100
+ pub trait Upcast < T : ?Sized > {
101
+ fn upcast ( & self ) -> & T ;
102
+ }
103
+
99
104
// Red knot specific databases code.
100
105
101
- pub trait SourceDb : Database {
106
+ pub trait SourceDb : DbWithJar < SourceJar > {
102
107
// queries
103
108
fn file_id ( & self , path : & std:: path:: Path ) -> FileId ;
104
109
105
110
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 > ) ;
129
111
}
130
112
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 > { }
133
114
134
- fn lint_semantic ( & self , file_id : FileId ) -> QueryResult < Diagnostics > ;
135
- }
115
+ pub trait LintDb : SemanticDb + DbWithJar < LintJar > + Upcast < dyn SemanticDb > { }
136
116
137
- pub trait Db : LintDb { }
117
+ pub trait Db : LintDb + Upcast < dyn LintDb > { }
138
118
139
119
#[ derive( Debug , Default ) ]
140
120
pub struct SourceJar {
@@ -161,19 +141,10 @@ pub(crate) mod tests {
161
141
use std:: sync:: Arc ;
162
142
163
143
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 ,
166
146
} ;
167
147
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 } ;
177
148
178
149
use super :: { SemanticDb , SemanticJar } ;
179
150
@@ -223,56 +194,36 @@ pub(crate) mod tests {
223
194
fn file_path ( & self , file_id : FileId ) -> Arc < Path > {
224
195
self . files . path ( file_id)
225
196
}
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
- }
234
197
}
235
198
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 { }
244
200
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
247
204
}
205
+ }
248
206
249
- fn symbol_table ( & self , file_id : FileId ) -> QueryResult < Arc < SymbolTable > > {
250
- symbol_table ( self , file_id)
251
- }
207
+ impl SemanticDb for TestDb { }
252
208
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 { }
256
210
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
263
214
}
264
215
}
265
216
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 { }
270
218
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
273
222
}
274
223
}
275
224
225
+ impl DbWithJar < LintJar > for TestDb { }
226
+
276
227
impl HasJars for TestDb {
277
228
type Jars = ( SourceJar , SemanticJar , LintJar ) ;
278
229
0 commit comments