diff options
Diffstat (limited to 'crates/ra_hir_def/src/nameres.rs')
-rw-r--r-- | crates/ra_hir_def/src/nameres.rs | 540 |
1 files changed, 539 insertions, 1 deletions
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 11ba8a777..fbd4248e6 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs | |||
@@ -1,5 +1,543 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! This module implements import-resolution/macro expansion algorithm. |
2 | //! | ||
3 | //! The result of this module is `CrateDefMap`: a data structure which contains: | ||
4 | //! | ||
5 | //! * a tree of modules for the crate | ||
6 | //! * for each module, a set of items visible in the module (directly declared | ||
7 | //! or imported) | ||
8 | //! | ||
9 | //! Note that `CrateDefMap` contains fully macro expanded code. | ||
10 | //! | ||
11 | //! Computing `CrateDefMap` can be partitioned into several logically | ||
12 | //! independent "phases". The phases are mutually recursive though, there's no | ||
13 | //! strict ordering. | ||
14 | //! | ||
15 | //! ## Collecting RawItems | ||
16 | //! | ||
17 | //! This happens in the `raw` module, which parses a single source file into a | ||
18 | //! set of top-level items. Nested imports are desugared to flat imports in | ||
19 | //! this phase. Macro calls are represented as a triple of (Path, Option<Name>, | ||
20 | //! TokenTree). | ||
21 | //! | ||
22 | //! ## Collecting Modules | ||
23 | //! | ||
24 | //! This happens in the `collector` module. In this phase, we recursively walk | ||
25 | //! tree of modules, collect raw items from submodules, populate module scopes | ||
26 | //! with defined items (so, we assign item ids in this phase) and record the set | ||
27 | //! of unresolved imports and macros. | ||
28 | //! | ||
29 | //! While we walk tree of modules, we also record macro_rules definitions and | ||
30 | //! expand calls to macro_rules defined macros. | ||
31 | //! | ||
32 | //! ## Resolving Imports | ||
33 | //! | ||
34 | //! We maintain a list of currently unresolved imports. On every iteration, we | ||
35 | //! try to resolve some imports from this list. If the import is resolved, we | ||
36 | //! record it, by adding an item to current module scope and, if necessary, by | ||
37 | //! recursively populating glob imports. | ||
38 | //! | ||
39 | //! ## Resolving Macros | ||
40 | //! | ||
41 | //! macro_rules from the same crate use a global mutable namespace. We expand | ||
42 | //! them immediately, when we collect modules. | ||
43 | //! | ||
44 | //! Macros from other crates (including proc-macros) can be used with | ||
45 | //! `foo::bar!` syntax. We handle them similarly to imports. There's a list of | ||
46 | //! unexpanded macros. On every iteration, we try to resolve each macro call | ||
47 | //! path and, upon success, we run macro expansion and "collect module" phase | ||
48 | //! on the result | ||
2 | 49 | ||
3 | // FIXME: review privacy of submodules | 50 | // FIXME: review privacy of submodules |
4 | pub mod raw; | 51 | pub mod raw; |
52 | pub mod per_ns; | ||
53 | pub mod collector; | ||
5 | pub mod mod_resolution; | 54 | pub mod mod_resolution; |
55 | |||
56 | #[cfg(test)] | ||
57 | mod tests; | ||
58 | |||
59 | use std::sync::Arc; | ||
60 | |||
61 | use hir_expand::{diagnostics::DiagnosticSink, name::Name, MacroDefId}; | ||
62 | use once_cell::sync::Lazy; | ||
63 | use ra_arena::Arena; | ||
64 | use ra_db::{CrateId, Edition, FileId}; | ||
65 | use ra_prof::profile; | ||
66 | use ra_syntax::ast; | ||
67 | use rustc_hash::{FxHashMap, FxHashSet}; | ||
68 | use test_utils::tested_by; | ||
69 | |||
70 | use crate::{ | ||
71 | builtin_type::BuiltinType, | ||
72 | db::DefDatabase2, | ||
73 | nameres::{diagnostics::DefDiagnostic, per_ns::PerNs, raw::ImportId}, | ||
74 | path::{Path, PathKind}, | ||
75 | AdtId, AstId, CrateModuleId, EnumVariantId, ModuleDefId, ModuleId, TraitId, | ||
76 | }; | ||
77 | |||
78 | /// Contains all top-level defs from a macro-expanded crate | ||
79 | #[derive(Debug, PartialEq, Eq)] | ||
80 | pub struct CrateDefMap { | ||
81 | krate: CrateId, | ||
82 | edition: Edition, | ||
83 | /// The prelude module for this crate. This either comes from an import | ||
84 | /// marked with the `prelude_import` attribute, or (in the normal case) from | ||
85 | /// a dependency (`std` or `core`). | ||
86 | prelude: Option<ModuleId>, | ||
87 | extern_prelude: FxHashMap<Name, ModuleDefId>, | ||
88 | root: CrateModuleId, | ||
89 | pub modules: Arena<CrateModuleId, ModuleData>, | ||
90 | |||
91 | /// Some macros are not well-behavior, which leads to infinite loop | ||
92 | /// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } } | ||
93 | /// We mark it down and skip it in collector | ||
94 | /// | ||
95 | /// FIXME: | ||
96 | /// Right now it only handle a poison macro in a single crate, | ||
97 | /// such that if other crate try to call that macro, | ||
98 | /// the whole process will do again until it became poisoned in that crate. | ||
99 | /// We should handle this macro set globally | ||
100 | /// However, do we want to put it as a global variable? | ||
101 | poison_macros: FxHashSet<MacroDefId>, | ||
102 | |||
103 | diagnostics: Vec<DefDiagnostic>, | ||
104 | } | ||
105 | |||
106 | impl std::ops::Index<CrateModuleId> for CrateDefMap { | ||
107 | type Output = ModuleData; | ||
108 | fn index(&self, id: CrateModuleId) -> &ModuleData { | ||
109 | &self.modules[id] | ||
110 | } | ||
111 | } | ||
112 | |||
113 | #[derive(Default, Debug, PartialEq, Eq)] | ||
114 | pub struct ModuleData { | ||
115 | pub parent: Option<CrateModuleId>, | ||
116 | pub children: FxHashMap<Name, CrateModuleId>, | ||
117 | pub scope: ModuleScope, | ||
118 | /// None for root | ||
119 | pub declaration: Option<AstId<ast::Module>>, | ||
120 | /// None for inline modules. | ||
121 | /// | ||
122 | /// Note that non-inline modules, by definition, live inside non-macro file. | ||
123 | pub definition: Option<FileId>, | ||
124 | } | ||
125 | |||
126 | #[derive(Debug, Default, PartialEq, Eq, Clone)] | ||
127 | pub struct ModuleScope { | ||
128 | pub items: FxHashMap<Name, Resolution>, | ||
129 | /// Macros visable in current module in legacy textual scope | ||
130 | /// | ||
131 | /// For macros invoked by an unquatified identifier like `bar!()`, `legacy_macros` will be searched in first. | ||
132 | /// If it yields no result, then it turns to module scoped `macros`. | ||
133 | /// It macros with name quatified with a path like `crate::foo::bar!()`, `legacy_macros` will be skipped, | ||
134 | /// and only normal scoped `macros` will be searched in. | ||
135 | /// | ||
136 | /// Note that this automatically inherit macros defined textually before the definition of module itself. | ||
137 | /// | ||
138 | /// Module scoped macros will be inserted into `items` instead of here. | ||
139 | // FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will | ||
140 | // be all resolved to the last one defined if shadowing happens. | ||
141 | legacy_macros: FxHashMap<Name, MacroDefId>, | ||
142 | } | ||
143 | |||
144 | static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| { | ||
145 | BuiltinType::ALL | ||
146 | .iter() | ||
147 | .map(|(name, ty)| { | ||
148 | (name.clone(), Resolution { def: PerNs::types(ty.clone().into()), import: None }) | ||
149 | }) | ||
150 | .collect() | ||
151 | }); | ||
152 | |||
153 | /// Legacy macros can only be accessed through special methods like `get_legacy_macros`. | ||
154 | /// Other methods will only resolve values, types and module scoped macros only. | ||
155 | impl ModuleScope { | ||
156 | pub fn entries<'a>(&'a self) -> impl Iterator<Item = (&'a Name, &'a Resolution)> + 'a { | ||
157 | //FIXME: shadowing | ||
158 | self.items.iter().chain(BUILTIN_SCOPE.iter()) | ||
159 | } | ||
160 | |||
161 | /// Iterate over all module scoped macros | ||
162 | pub fn macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { | ||
163 | self.items | ||
164 | .iter() | ||
165 | .filter_map(|(name, res)| res.def.get_macros().map(|macro_| (name, macro_))) | ||
166 | } | ||
167 | |||
168 | /// Iterate over all legacy textual scoped macros visable at the end of the module | ||
169 | pub fn legacy_macros<'a>(&'a self) -> impl Iterator<Item = (&'a Name, MacroDefId)> + 'a { | ||
170 | self.legacy_macros.iter().map(|(name, def)| (name, *def)) | ||
171 | } | ||
172 | |||
173 | /// Get a name from current module scope, legacy macros are not included | ||
174 | pub fn get(&self, name: &Name) -> Option<&Resolution> { | ||
175 | self.items.get(name).or_else(|| BUILTIN_SCOPE.get(name)) | ||
176 | } | ||
177 | |||
178 | pub fn traits<'a>(&'a self) -> impl Iterator<Item = TraitId> + 'a { | ||
179 | self.items.values().filter_map(|r| match r.def.take_types() { | ||
180 | Some(ModuleDefId::TraitId(t)) => Some(t), | ||
181 | _ => None, | ||
182 | }) | ||
183 | } | ||
184 | |||
185 | fn get_legacy_macro(&self, name: &Name) -> Option<MacroDefId> { | ||
186 | self.legacy_macros.get(name).copied() | ||
187 | } | ||
188 | } | ||
189 | |||
190 | #[derive(Debug, Clone, PartialEq, Eq, Default)] | ||
191 | pub struct Resolution { | ||
192 | /// None for unresolved | ||
193 | pub def: PerNs, | ||
194 | /// ident by which this is imported into local scope. | ||
195 | pub import: Option<ImportId>, | ||
196 | } | ||
197 | |||
198 | impl Resolution { | ||
199 | pub(crate) fn from_macro(macro_: MacroDefId) -> Self { | ||
200 | Resolution { def: PerNs::macros(macro_), import: None } | ||
201 | } | ||
202 | } | ||
203 | |||
204 | #[derive(Debug, Clone)] | ||
205 | struct ResolvePathResult { | ||
206 | resolved_def: PerNs, | ||
207 | segment_index: Option<usize>, | ||
208 | reached_fixedpoint: ReachedFixedPoint, | ||
209 | } | ||
210 | |||
211 | impl ResolvePathResult { | ||
212 | fn empty(reached_fixedpoint: ReachedFixedPoint) -> ResolvePathResult { | ||
213 | ResolvePathResult::with(PerNs::none(), reached_fixedpoint, None) | ||
214 | } | ||
215 | |||
216 | fn with( | ||
217 | resolved_def: PerNs, | ||
218 | reached_fixedpoint: ReachedFixedPoint, | ||
219 | segment_index: Option<usize>, | ||
220 | ) -> ResolvePathResult { | ||
221 | ResolvePathResult { resolved_def, reached_fixedpoint, segment_index } | ||
222 | } | ||
223 | } | ||
224 | |||
225 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
226 | enum ResolveMode { | ||
227 | Import, | ||
228 | Other, | ||
229 | } | ||
230 | |||
231 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
232 | enum ReachedFixedPoint { | ||
233 | Yes, | ||
234 | No, | ||
235 | } | ||
236 | |||
237 | impl CrateDefMap { | ||
238 | pub(crate) fn crate_def_map_query( | ||
239 | // Note that this doesn't have `+ AstDatabase`! | ||
240 | // This gurantess that `CrateDefMap` is stable across reparses. | ||
241 | db: &impl DefDatabase2, | ||
242 | krate: CrateId, | ||
243 | ) -> Arc<CrateDefMap> { | ||
244 | let _p = profile("crate_def_map_query"); | ||
245 | let def_map = { | ||
246 | let crate_graph = db.crate_graph(); | ||
247 | let edition = crate_graph.edition(krate); | ||
248 | let mut modules: Arena<CrateModuleId, ModuleData> = Arena::default(); | ||
249 | let root = modules.alloc(ModuleData::default()); | ||
250 | CrateDefMap { | ||
251 | krate, | ||
252 | edition, | ||
253 | extern_prelude: FxHashMap::default(), | ||
254 | prelude: None, | ||
255 | root, | ||
256 | modules, | ||
257 | poison_macros: FxHashSet::default(), | ||
258 | diagnostics: Vec::new(), | ||
259 | } | ||
260 | }; | ||
261 | let def_map = collector::collect_defs(db, def_map); | ||
262 | Arc::new(def_map) | ||
263 | } | ||
264 | |||
265 | pub fn krate(&self) -> CrateId { | ||
266 | self.krate | ||
267 | } | ||
268 | |||
269 | pub fn root(&self) -> CrateModuleId { | ||
270 | self.root | ||
271 | } | ||
272 | |||
273 | pub fn prelude(&self) -> Option<ModuleId> { | ||
274 | self.prelude | ||
275 | } | ||
276 | |||
277 | pub fn extern_prelude(&self) -> &FxHashMap<Name, ModuleDefId> { | ||
278 | &self.extern_prelude | ||
279 | } | ||
280 | |||
281 | pub fn add_diagnostics( | ||
282 | &self, | ||
283 | db: &impl DefDatabase2, | ||
284 | module: CrateModuleId, | ||
285 | sink: &mut DiagnosticSink, | ||
286 | ) { | ||
287 | self.diagnostics.iter().for_each(|it| it.add_to(db, module, sink)) | ||
288 | } | ||
289 | |||
290 | pub fn resolve_path( | ||
291 | &self, | ||
292 | db: &impl DefDatabase2, | ||
293 | original_module: CrateModuleId, | ||
294 | path: &Path, | ||
295 | ) -> (PerNs, Option<usize>) { | ||
296 | let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); | ||
297 | (res.resolved_def, res.segment_index) | ||
298 | } | ||
299 | |||
300 | // Returns Yes if we are sure that additions to `ItemMap` wouldn't change | ||
301 | // the result. | ||
302 | fn resolve_path_fp_with_macro( | ||
303 | &self, | ||
304 | db: &impl DefDatabase2, | ||
305 | mode: ResolveMode, | ||
306 | original_module: CrateModuleId, | ||
307 | path: &Path, | ||
308 | ) -> ResolvePathResult { | ||
309 | let mut segments = path.segments.iter().enumerate(); | ||
310 | let mut curr_per_ns: PerNs = match path.kind { | ||
311 | PathKind::DollarCrate(krate) => { | ||
312 | if krate == self.krate { | ||
313 | tested_by!(macro_dollar_crate_self); | ||
314 | PerNs::types(ModuleId { krate: self.krate, module_id: self.root }.into()) | ||
315 | } else { | ||
316 | let def_map = db.crate_def_map(krate); | ||
317 | let module = ModuleId { krate, module_id: def_map.root }; | ||
318 | tested_by!(macro_dollar_crate_other); | ||
319 | PerNs::types(module.into()) | ||
320 | } | ||
321 | } | ||
322 | PathKind::Crate => { | ||
323 | PerNs::types(ModuleId { krate: self.krate, module_id: self.root }.into()) | ||
324 | } | ||
325 | PathKind::Self_ => { | ||
326 | PerNs::types(ModuleId { krate: self.krate, module_id: original_module }.into()) | ||
327 | } | ||
328 | // plain import or absolute path in 2015: crate-relative with | ||
329 | // fallback to extern prelude (with the simplification in | ||
330 | // rust-lang/rust#57745) | ||
331 | // FIXME there must be a nicer way to write this condition | ||
332 | PathKind::Plain | PathKind::Abs | ||
333 | if self.edition == Edition::Edition2015 | ||
334 | && (path.kind == PathKind::Abs || mode == ResolveMode::Import) => | ||
335 | { | ||
336 | let segment = match segments.next() { | ||
337 | Some((_, segment)) => segment, | ||
338 | None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), | ||
339 | }; | ||
340 | log::debug!("resolving {:?} in crate root (+ extern prelude)", segment); | ||
341 | self.resolve_name_in_crate_root_or_extern_prelude(&segment.name) | ||
342 | } | ||
343 | PathKind::Plain => { | ||
344 | let segment = match segments.next() { | ||
345 | Some((_, segment)) => segment, | ||
346 | None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), | ||
347 | }; | ||
348 | log::debug!("resolving {:?} in module", segment); | ||
349 | self.resolve_name_in_module(db, original_module, &segment.name) | ||
350 | } | ||
351 | PathKind::Super => { | ||
352 | if let Some(p) = self.modules[original_module].parent { | ||
353 | PerNs::types(ModuleId { krate: self.krate, module_id: p }.into()) | ||
354 | } else { | ||
355 | log::debug!("super path in root module"); | ||
356 | return ResolvePathResult::empty(ReachedFixedPoint::Yes); | ||
357 | } | ||
358 | } | ||
359 | PathKind::Abs => { | ||
360 | // 2018-style absolute path -- only extern prelude | ||
361 | let segment = match segments.next() { | ||
362 | Some((_, segment)) => segment, | ||
363 | None => return ResolvePathResult::empty(ReachedFixedPoint::Yes), | ||
364 | }; | ||
365 | if let Some(def) = self.extern_prelude.get(&segment.name) { | ||
366 | log::debug!("absolute path {:?} resolved to crate {:?}", path, def); | ||
367 | PerNs::types(*def) | ||
368 | } else { | ||
369 | return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude | ||
370 | } | ||
371 | } | ||
372 | PathKind::Type(_) => { | ||
373 | // This is handled in `infer::infer_path_expr` | ||
374 | // The result returned here does not matter | ||
375 | return ResolvePathResult::empty(ReachedFixedPoint::Yes); | ||
376 | } | ||
377 | }; | ||
378 | |||
379 | for (i, segment) in segments { | ||
380 | let curr = match curr_per_ns.take_types() { | ||
381 | Some(r) => r, | ||
382 | None => { | ||
383 | // we still have path segments left, but the path so far | ||
384 | // didn't resolve in the types namespace => no resolution | ||
385 | // (don't break here because `curr_per_ns` might contain | ||
386 | // something in the value namespace, and it would be wrong | ||
387 | // to return that) | ||
388 | return ResolvePathResult::empty(ReachedFixedPoint::No); | ||
389 | } | ||
390 | }; | ||
391 | // resolve segment in curr | ||
392 | |||
393 | curr_per_ns = match curr { | ||
394 | ModuleDefId::ModuleId(module) => { | ||
395 | if module.krate != self.krate { | ||
396 | let path = | ||
397 | Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ }; | ||
398 | log::debug!("resolving {:?} in other crate", path); | ||
399 | let defp_map = db.crate_def_map(module.krate); | ||
400 | let (def, s) = defp_map.resolve_path(db, module.module_id, &path); | ||
401 | return ResolvePathResult::with( | ||
402 | def, | ||
403 | ReachedFixedPoint::Yes, | ||
404 | s.map(|s| s + i), | ||
405 | ); | ||
406 | } | ||
407 | |||
408 | // Since it is a qualified path here, it should not contains legacy macros | ||
409 | match self[module.module_id].scope.get(&segment.name) { | ||
410 | Some(res) => res.def, | ||
411 | _ => { | ||
412 | log::debug!("path segment {:?} not found", segment.name); | ||
413 | return ResolvePathResult::empty(ReachedFixedPoint::No); | ||
414 | } | ||
415 | } | ||
416 | } | ||
417 | ModuleDefId::AdtId(AdtId::EnumId(e)) => { | ||
418 | // enum variant | ||
419 | tested_by!(can_import_enum_variant); | ||
420 | let enum_data = db.enum_data(e); | ||
421 | match enum_data.variant(&segment.name) { | ||
422 | Some(local_id) => { | ||
423 | let variant = EnumVariantId { parent: e, local_id }; | ||
424 | PerNs::both(variant.into(), variant.into()) | ||
425 | } | ||
426 | None => { | ||
427 | return ResolvePathResult::with( | ||
428 | PerNs::types(e.into()), | ||
429 | ReachedFixedPoint::Yes, | ||
430 | Some(i), | ||
431 | ); | ||
432 | } | ||
433 | } | ||
434 | } | ||
435 | s => { | ||
436 | // could be an inherent method call in UFCS form | ||
437 | // (`Struct::method`), or some other kind of associated item | ||
438 | log::debug!( | ||
439 | "path segment {:?} resolved to non-module {:?}, but is not last", | ||
440 | segment.name, | ||
441 | curr, | ||
442 | ); | ||
443 | |||
444 | return ResolvePathResult::with( | ||
445 | PerNs::types(s), | ||
446 | ReachedFixedPoint::Yes, | ||
447 | Some(i), | ||
448 | ); | ||
449 | } | ||
450 | }; | ||
451 | } | ||
452 | ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None) | ||
453 | } | ||
454 | |||
455 | fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs { | ||
456 | let from_crate_root = | ||
457 | self[self.root].scope.get(name).map_or_else(PerNs::none, |res| res.def); | ||
458 | let from_extern_prelude = self.resolve_name_in_extern_prelude(name); | ||
459 | |||
460 | from_crate_root.or(from_extern_prelude) | ||
461 | } | ||
462 | |||
463 | pub(crate) fn resolve_name_in_module( | ||
464 | &self, | ||
465 | db: &impl DefDatabase2, | ||
466 | module: CrateModuleId, | ||
467 | name: &Name, | ||
468 | ) -> PerNs { | ||
469 | // Resolve in: | ||
470 | // - legacy scope of macro | ||
471 | // - current module / scope | ||
472 | // - extern prelude | ||
473 | // - std prelude | ||
474 | let from_legacy_macro = | ||
475 | self[module].scope.get_legacy_macro(name).map_or_else(PerNs::none, PerNs::macros); | ||
476 | let from_scope = self[module].scope.get(name).map_or_else(PerNs::none, |res| res.def); | ||
477 | let from_extern_prelude = | ||
478 | self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)); | ||
479 | let from_prelude = self.resolve_in_prelude(db, name); | ||
480 | |||
481 | from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude) | ||
482 | } | ||
483 | |||
484 | fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { | ||
485 | self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)) | ||
486 | } | ||
487 | |||
488 | fn resolve_in_prelude(&self, db: &impl DefDatabase2, name: &Name) -> PerNs { | ||
489 | if let Some(prelude) = self.prelude { | ||
490 | let keep; | ||
491 | let def_map = if prelude.krate == self.krate { | ||
492 | self | ||
493 | } else { | ||
494 | // Extend lifetime | ||
495 | keep = db.crate_def_map(prelude.krate); | ||
496 | &keep | ||
497 | }; | ||
498 | def_map[prelude.module_id].scope.get(name).map_or_else(PerNs::none, |res| res.def) | ||
499 | } else { | ||
500 | PerNs::none() | ||
501 | } | ||
502 | } | ||
503 | } | ||
504 | |||
505 | mod diagnostics { | ||
506 | use hir_expand::diagnostics::DiagnosticSink; | ||
507 | use ra_syntax::{ast, AstPtr}; | ||
508 | use relative_path::RelativePathBuf; | ||
509 | |||
510 | use crate::{db::DefDatabase2, diagnostics::UnresolvedModule, nameres::CrateModuleId, AstId}; | ||
511 | |||
512 | #[derive(Debug, PartialEq, Eq)] | ||
513 | pub(super) enum DefDiagnostic { | ||
514 | UnresolvedModule { | ||
515 | module: CrateModuleId, | ||
516 | declaration: AstId<ast::Module>, | ||
517 | candidate: RelativePathBuf, | ||
518 | }, | ||
519 | } | ||
520 | |||
521 | impl DefDiagnostic { | ||
522 | pub(super) fn add_to( | ||
523 | &self, | ||
524 | db: &impl DefDatabase2, | ||
525 | target_module: CrateModuleId, | ||
526 | sink: &mut DiagnosticSink, | ||
527 | ) { | ||
528 | match self { | ||
529 | DefDiagnostic::UnresolvedModule { module, declaration, candidate } => { | ||
530 | if *module != target_module { | ||
531 | return; | ||
532 | } | ||
533 | let decl = declaration.to_node(db); | ||
534 | sink.push(UnresolvedModule { | ||
535 | file: declaration.file_id(), | ||
536 | decl: AstPtr::new(&decl), | ||
537 | candidate: candidate.clone(), | ||
538 | }) | ||
539 | } | ||
540 | } | ||
541 | } | ||
542 | } | ||
543 | } | ||