]> git.lizzy.rs Git - rust.git/blob - crates/hir/src/source_analyzer.rs
`add_explicit_type` respects coercions
[rust.git] / crates / hir / src / source_analyzer.rs
1 //! Lookup hir elements using positions in the source code. This is a lossy
2 //! transformation: in general, a single source might correspond to several
3 //! modules, functions, etc, due to macros, cfgs and `#[path=]` attributes on
4 //! modules.
5 //!
6 //! So, this modules should not be used during hir construction, it exists
7 //! purely for "IDE needs".
8 use std::{iter::once, sync::Arc};
9
10 use hir_def::{
11     body::{
12         self,
13         scope::{ExprScopes, ScopeId},
14         Body, BodySourceMap,
15     },
16     expr::{ExprId, Pat, PatId},
17     path::{ModPath, Path, PathKind},
18     resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs},
19     AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, VariantId,
20 };
21 use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile};
22 use hir_ty::{
23     diagnostics::{record_literal_missing_fields, record_pattern_missing_fields},
24     InferenceResult, Interner, Substitution, TyExt, TyLoweringContext,
25 };
26 use syntax::{
27     ast::{self, AstNode},
28     SyntaxNode, TextRange, TextSize,
29 };
30
31 use crate::{
32     db::HirDatabase, semantics::PathResolution, Adt, BuiltinType, Const, Field, Function, Local,
33     MacroDef, ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Variant,
34 };
35 use base_db::CrateId;
36
37 /// `SourceAnalyzer` is a convenience wrapper which exposes HIR API in terms of
38 /// original source files. It should not be used inside the HIR itself.
39 #[derive(Debug)]
40 pub(crate) struct SourceAnalyzer {
41     pub(crate) file_id: HirFileId,
42     pub(crate) resolver: Resolver,
43     body: Option<Arc<Body>>,
44     body_source_map: Option<Arc<BodySourceMap>>,
45     infer: Option<Arc<InferenceResult>>,
46     scopes: Option<Arc<ExprScopes>>,
47 }
48
49 impl SourceAnalyzer {
50     pub(crate) fn new_for_body(
51         db: &dyn HirDatabase,
52         def: DefWithBodyId,
53         node: InFile<&SyntaxNode>,
54         offset: Option<TextSize>,
55     ) -> SourceAnalyzer {
56         let (body, source_map) = db.body_with_source_map(def);
57         let scopes = db.expr_scopes(def);
58         let scope = match offset {
59             None => scope_for(&scopes, &source_map, node),
60             Some(offset) => scope_for_offset(db, &scopes, &source_map, node.with_value(offset)),
61         };
62         let resolver = resolver_for_scope(db.upcast(), def, scope);
63         SourceAnalyzer {
64             resolver,
65             body: Some(body),
66             body_source_map: Some(source_map),
67             infer: Some(db.infer(def)),
68             scopes: Some(scopes),
69             file_id: node.file_id,
70         }
71     }
72
73     pub(crate) fn new_for_resolver(
74         resolver: Resolver,
75         node: InFile<&SyntaxNode>,
76     ) -> SourceAnalyzer {
77         SourceAnalyzer {
78             resolver,
79             body: None,
80             body_source_map: None,
81             infer: None,
82             scopes: None,
83             file_id: node.file_id,
84         }
85     }
86
87     fn expr_id(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option<ExprId> {
88         let src = match expr {
89             ast::Expr::MacroCall(call) => {
90                 self.expand_expr(db, InFile::new(self.file_id, call.clone()))?
91             }
92             _ => InFile::new(self.file_id, expr.clone()),
93         };
94         let sm = self.body_source_map.as_ref()?;
95         sm.node_expr(src.as_ref())
96     }
97
98     fn pat_id(&self, pat: &ast::Pat) -> Option<PatId> {
99         // FIXME: macros, see `expr_id`
100         let src = InFile { file_id: self.file_id, value: pat };
101         self.body_source_map.as_ref()?.node_pat(src)
102     }
103
104     fn expand_expr(
105         &self,
106         db: &dyn HirDatabase,
107         expr: InFile<ast::MacroCall>,
108     ) -> Option<InFile<ast::Expr>> {
109         let macro_file = self.body_source_map.as_ref()?.node_macro_file(expr.as_ref())?;
110         let expanded = db.parse_or_expand(macro_file)?;
111
112         let res = match ast::MacroCall::cast(expanded.clone()) {
113             Some(call) => self.expand_expr(db, InFile::new(macro_file, call))?,
114             _ => InFile::new(macro_file, ast::Expr::cast(expanded)?),
115         };
116         Some(res)
117     }
118
119     pub(crate) fn type_of_expr(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option<Type> {
120         let expr_id = self.expr_id(db, expr)?;
121         let ty = self.infer.as_ref()?[expr_id].clone();
122         Type::new_with_resolver(db, &self.resolver, ty)
123     }
124
125     pub(crate) fn type_of_expr_with_coercion(
126         &self,
127         db: &dyn HirDatabase,
128         expr: &ast::Expr,
129     ) -> Option<Type> {
130         let expr_id = self.expr_id(db, expr)?;
131         let infer = self.infer.as_ref()?;
132         let ty = infer
133             .expr_adjustments
134             .get(&expr_id)
135             .and_then(|adjusts| adjusts.last().map(|adjust| &adjust.target))
136             .unwrap_or_else(|| &infer[expr_id]);
137         Type::new_with_resolver(db, &self.resolver, ty.clone())
138     }
139
140     pub(crate) fn type_of_pat(&self, db: &dyn HirDatabase, pat: &ast::Pat) -> Option<Type> {
141         let pat_id = self.pat_id(pat)?;
142         let ty = self.infer.as_ref()?[pat_id].clone();
143         Type::new_with_resolver(db, &self.resolver, ty)
144     }
145
146     pub(crate) fn type_of_self(
147         &self,
148         db: &dyn HirDatabase,
149         param: &ast::SelfParam,
150     ) -> Option<Type> {
151         let src = InFile { file_id: self.file_id, value: param };
152         let pat_id = self.body_source_map.as_ref()?.node_self_param(src)?;
153         let ty = self.infer.as_ref()?[pat_id].clone();
154         Type::new_with_resolver(db, &self.resolver, ty)
155     }
156
157     pub(crate) fn resolve_method_call(
158         &self,
159         db: &dyn HirDatabase,
160         call: &ast::MethodCallExpr,
161     ) -> Option<(FunctionId, Substitution)> {
162         let expr_id = self.expr_id(db, &call.clone().into())?;
163         self.infer.as_ref()?.method_resolution(expr_id)
164     }
165
166     pub(crate) fn resolve_field(
167         &self,
168         db: &dyn HirDatabase,
169         field: &ast::FieldExpr,
170     ) -> Option<Field> {
171         let expr_id = self.expr_id(db, &field.clone().into())?;
172         self.infer.as_ref()?.field_resolution(expr_id).map(|it| it.into())
173     }
174
175     pub(crate) fn resolve_record_field(
176         &self,
177         db: &dyn HirDatabase,
178         field: &ast::RecordExprField,
179     ) -> Option<(Field, Option<Local>, Type)> {
180         let record_expr = ast::RecordExpr::cast(field.syntax().parent().and_then(|p| p.parent())?)?;
181         let expr = ast::Expr::from(record_expr);
182         let expr_id = self.body_source_map.as_ref()?.node_expr(InFile::new(self.file_id, &expr))?;
183
184         let local_name = field.field_name()?.as_name();
185         let local = if field.name_ref().is_some() {
186             None
187         } else {
188             let path = ModPath::from_segments(PathKind::Plain, once(local_name.clone()));
189             match self.resolver.resolve_path_in_value_ns_fully(db.upcast(), &path) {
190                 Some(ValueNs::LocalBinding(pat_id)) => {
191                     Some(Local { pat_id, parent: self.resolver.body_owner()? })
192                 }
193                 _ => None,
194             }
195         };
196         let (_, subst) = self.infer.as_ref()?.type_of_expr.get(expr_id)?.as_adt()?;
197         let variant = self.infer.as_ref()?.variant_resolution_for_expr(expr_id)?;
198         let variant_data = variant.variant_data(db.upcast());
199         let field = FieldId { parent: variant, local_id: variant_data.field(&local_name)? };
200         let field_ty =
201             db.field_types(variant).get(field.local_id)?.clone().substitute(&Interner, subst);
202         Some((field.into(), local, Type::new_with_resolver(db, &self.resolver, field_ty)?))
203     }
204
205     pub(crate) fn resolve_record_pat_field(
206         &self,
207         db: &dyn HirDatabase,
208         field: &ast::RecordPatField,
209     ) -> Option<Field> {
210         let field_name = field.field_name()?.as_name();
211         let record_pat = ast::RecordPat::cast(field.syntax().parent().and_then(|p| p.parent())?)?;
212         let pat_id = self.pat_id(&record_pat.into())?;
213         let variant = self.infer.as_ref()?.variant_resolution_for_pat(pat_id)?;
214         let variant_data = variant.variant_data(db.upcast());
215         let field = FieldId { parent: variant, local_id: variant_data.field(&field_name)? };
216         Some(field.into())
217     }
218
219     pub(crate) fn resolve_macro_call(
220         &self,
221         db: &dyn HirDatabase,
222         macro_call: InFile<&ast::MacroCall>,
223     ) -> Option<MacroDef> {
224         let ctx = body::LowerCtx::new(db.upcast(), macro_call.file_id);
225         let path = macro_call.value.path().and_then(|ast| Path::from_src(ast, &ctx))?;
226         self.resolver.resolve_path_as_macro(db.upcast(), path.mod_path()).map(|it| it.into())
227     }
228
229     pub(crate) fn resolve_bind_pat_to_const(
230         &self,
231         db: &dyn HirDatabase,
232         pat: &ast::IdentPat,
233     ) -> Option<ModuleDef> {
234         let pat_id = self.pat_id(&pat.clone().into())?;
235         let body = self.body.as_ref()?;
236         let path = match &body[pat_id] {
237             Pat::Path(path) => path,
238             _ => return None,
239         };
240         let res = resolve_hir_path(db, &self.resolver, path)?;
241         match res {
242             PathResolution::Def(def) => Some(def),
243             _ => None,
244         }
245     }
246
247     pub(crate) fn resolve_path(
248         &self,
249         db: &dyn HirDatabase,
250         path: &ast::Path,
251     ) -> Option<PathResolution> {
252         let parent = || path.syntax().parent();
253         let mut prefer_value_ns = false;
254         if let Some(path_expr) = parent().and_then(ast::PathExpr::cast) {
255             let expr_id = self.expr_id(db, &path_expr.into())?;
256             let infer = self.infer.as_ref()?;
257             if let Some(assoc) = infer.assoc_resolutions_for_expr(expr_id) {
258                 return Some(PathResolution::AssocItem(assoc.into()));
259             }
260             if let Some(VariantId::EnumVariantId(variant)) =
261                 infer.variant_resolution_for_expr(expr_id)
262             {
263                 return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
264             }
265             prefer_value_ns = true;
266         }
267
268         if let Some(path_pat) = parent().and_then(ast::PathPat::cast) {
269             let pat_id = self.pat_id(&path_pat.into())?;
270             if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) {
271                 return Some(PathResolution::AssocItem(assoc.into()));
272             }
273             if let Some(VariantId::EnumVariantId(variant)) =
274                 self.infer.as_ref()?.variant_resolution_for_pat(pat_id)
275             {
276                 return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
277             }
278         }
279
280         if let Some(rec_lit) = parent().and_then(ast::RecordExpr::cast) {
281             let expr_id = self.expr_id(db, &rec_lit.into())?;
282             if let Some(VariantId::EnumVariantId(variant)) =
283                 self.infer.as_ref()?.variant_resolution_for_expr(expr_id)
284             {
285                 return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
286             }
287         }
288
289         if let Some(pat) = parent()
290             .and_then(ast::RecordPat::cast)
291             .map(ast::Pat::from)
292             .or_else(|| parent().and_then(ast::TupleStructPat::cast).map(ast::Pat::from))
293         {
294             let pat_id = self.pat_id(&pat)?;
295             if let Some(VariantId::EnumVariantId(variant)) =
296                 self.infer.as_ref()?.variant_resolution_for_pat(pat_id)
297             {
298                 return Some(PathResolution::Def(ModuleDef::Variant(variant.into())));
299             }
300         }
301
302         // This must be a normal source file rather than macro file.
303         let hygiene = Hygiene::new(db.upcast(), self.file_id);
304         let ctx = body::LowerCtx::with_hygiene(db.upcast(), &hygiene);
305         let hir_path = Path::from_src(path.clone(), &ctx)?;
306
307         // Case where path is a qualifier of another path, e.g. foo::bar::Baz where we are
308         // trying to resolve foo::bar.
309         if let Some(outer_path) = parent().and_then(ast::Path::cast) {
310             if let Some(qualifier) = outer_path.qualifier() {
311                 if path == &qualifier {
312                     return resolve_hir_path_qualifier(db, &self.resolver, &hir_path);
313                 }
314             }
315         }
316         // Case where path is a qualifier of a use tree, e.g. foo::bar::{Baz, Qux} where we are
317         // trying to resolve foo::bar.
318         if let Some(use_tree) = parent().and_then(ast::UseTree::cast) {
319             if let Some(qualifier) = use_tree.path() {
320                 if path == &qualifier && use_tree.coloncolon_token().is_some() {
321                     return resolve_hir_path_qualifier(db, &self.resolver, &hir_path);
322                 }
323             }
324         }
325
326         if parent().map_or(false, |it| ast::Visibility::can_cast(it.kind())) {
327             resolve_hir_path_qualifier(db, &self.resolver, &hir_path)
328         } else {
329             resolve_hir_path_(db, &self.resolver, &hir_path, prefer_value_ns)
330         }
331     }
332
333     pub(crate) fn record_literal_missing_fields(
334         &self,
335         db: &dyn HirDatabase,
336         literal: &ast::RecordExpr,
337     ) -> Option<Vec<(Field, Type)>> {
338         let krate = self.resolver.krate()?;
339         let body = self.body.as_ref()?;
340         let infer = self.infer.as_ref()?;
341
342         let expr_id = self.expr_id(db, &literal.clone().into())?;
343         let substs = infer.type_of_expr[expr_id].as_adt()?.1;
344
345         let (variant, missing_fields, _exhaustive) =
346             record_literal_missing_fields(db, infer, expr_id, &body[expr_id])?;
347         let res = self.missing_fields(db, krate, substs, variant, missing_fields);
348         Some(res)
349     }
350
351     pub(crate) fn record_pattern_missing_fields(
352         &self,
353         db: &dyn HirDatabase,
354         pattern: &ast::RecordPat,
355     ) -> Option<Vec<(Field, Type)>> {
356         let krate = self.resolver.krate()?;
357         let body = self.body.as_ref()?;
358         let infer = self.infer.as_ref()?;
359
360         let pat_id = self.pat_id(&pattern.clone().into())?;
361         let substs = infer.type_of_pat[pat_id].as_adt()?.1;
362
363         let (variant, missing_fields, _exhaustive) =
364             record_pattern_missing_fields(db, infer, pat_id, &body[pat_id])?;
365         let res = self.missing_fields(db, krate, substs, variant, missing_fields);
366         Some(res)
367     }
368
369     fn missing_fields(
370         &self,
371         db: &dyn HirDatabase,
372         krate: CrateId,
373         substs: &Substitution,
374         variant: VariantId,
375         missing_fields: Vec<LocalFieldId>,
376     ) -> Vec<(Field, Type)> {
377         let field_types = db.field_types(variant);
378
379         missing_fields
380             .into_iter()
381             .map(|local_id| {
382                 let field = FieldId { parent: variant, local_id };
383                 let ty = field_types[local_id].clone().substitute(&Interner, substs);
384                 (field.into(), Type::new_with_resolver_inner(db, krate, &self.resolver, ty))
385             })
386             .collect()
387     }
388
389     pub(crate) fn expand(
390         &self,
391         db: &dyn HirDatabase,
392         macro_call: InFile<&ast::MacroCall>,
393     ) -> Option<HirFileId> {
394         let krate = self.resolver.krate()?;
395         let macro_call_id = macro_call.as_call_id(db.upcast(), krate, |path| {
396             self.resolver.resolve_path_as_macro(db.upcast(), &path)
397         })?;
398         Some(macro_call_id.as_file()).filter(|it| it.expansion_level(db.upcast()) < 64)
399     }
400
401     pub(crate) fn resolve_variant(
402         &self,
403         db: &dyn HirDatabase,
404         record_lit: ast::RecordExpr,
405     ) -> Option<VariantId> {
406         let infer = self.infer.as_ref()?;
407         let expr_id = self.expr_id(db, &record_lit.into())?;
408         infer.variant_resolution_for_expr(expr_id)
409     }
410 }
411
412 fn scope_for(
413     scopes: &ExprScopes,
414     source_map: &BodySourceMap,
415     node: InFile<&SyntaxNode>,
416 ) -> Option<ScopeId> {
417     node.value
418         .ancestors()
419         .filter_map(ast::Expr::cast)
420         .filter_map(|it| source_map.node_expr(InFile::new(node.file_id, &it)))
421         .find_map(|it| scopes.scope_for(it))
422 }
423
424 fn scope_for_offset(
425     db: &dyn HirDatabase,
426     scopes: &ExprScopes,
427     source_map: &BodySourceMap,
428     offset: InFile<TextSize>,
429 ) -> Option<ScopeId> {
430     scopes
431         .scope_by_expr()
432         .iter()
433         .filter_map(|(id, scope)| {
434             let source = source_map.expr_syntax(*id).ok()?;
435             // FIXME: correctly handle macro expansion
436             if source.file_id != offset.file_id {
437                 return None;
438             }
439             let root = source.file_syntax(db.upcast());
440             let node = source.value.to_node(&root);
441             Some((node.syntax().text_range(), scope))
442         })
443         // find containing scope
444         .min_by_key(|(expr_range, _scope)| {
445             (
446                 !(expr_range.start() <= offset.value && offset.value <= expr_range.end()),
447                 expr_range.len(),
448             )
449         })
450         .map(|(expr_range, scope)| {
451             adjust(db, scopes, source_map, expr_range, offset).unwrap_or(*scope)
452         })
453 }
454
455 // XXX: during completion, cursor might be outside of any particular
456 // expression. Try to figure out the correct scope...
457 fn adjust(
458     db: &dyn HirDatabase,
459     scopes: &ExprScopes,
460     source_map: &BodySourceMap,
461     expr_range: TextRange,
462     offset: InFile<TextSize>,
463 ) -> Option<ScopeId> {
464     let child_scopes = scopes
465         .scope_by_expr()
466         .iter()
467         .filter_map(|(id, scope)| {
468             let source = source_map.expr_syntax(*id).ok()?;
469             // FIXME: correctly handle macro expansion
470             if source.file_id != offset.file_id {
471                 return None;
472             }
473             let root = source.file_syntax(db.upcast());
474             let node = source.value.to_node(&root);
475             Some((node.syntax().text_range(), scope))
476         })
477         .filter(|&(range, _)| {
478             range.start() <= offset.value && expr_range.contains_range(range) && range != expr_range
479         });
480
481     child_scopes
482         .max_by(|&(r1, _), &(r2, _)| {
483             if r1.contains_range(r2) {
484                 std::cmp::Ordering::Greater
485             } else if r2.contains_range(r1) {
486                 std::cmp::Ordering::Less
487             } else {
488                 r1.start().cmp(&r2.start())
489             }
490         })
491         .map(|(_ptr, scope)| *scope)
492 }
493
494 #[inline]
495 pub(crate) fn resolve_hir_path(
496     db: &dyn HirDatabase,
497     resolver: &Resolver,
498     path: &Path,
499 ) -> Option<PathResolution> {
500     resolve_hir_path_(db, resolver, path, false)
501 }
502
503 fn resolve_hir_path_(
504     db: &dyn HirDatabase,
505     resolver: &Resolver,
506     path: &Path,
507     prefer_value_ns: bool,
508 ) -> Option<PathResolution> {
509     let types = || {
510         let (ty, unresolved) = match path.type_anchor() {
511             Some(type_ref) => {
512                 let (_, res) = TyLoweringContext::new(db, resolver).lower_ty_ext(type_ref);
513                 res.map(|ty_ns| (ty_ns, path.segments().first()))
514             }
515             None => {
516                 let (ty, remaining) =
517                     resolver.resolve_path_in_type_ns(db.upcast(), path.mod_path())?;
518                 match remaining {
519                     Some(remaining) if remaining > 1 => None,
520                     _ => Some((ty, path.segments().get(1))),
521                 }
522             }
523         }?;
524         let res = match ty {
525             TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
526             TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
527             TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => {
528                 PathResolution::Def(Adt::from(it).into())
529             }
530             TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
531             TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
532             TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
533             TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
534         };
535         match unresolved {
536             Some(unresolved) => res
537                 .assoc_type_shorthand_candidates(db, |name, alias| {
538                     (name == unresolved.name).then(|| alias)
539                 })
540                 .map(TypeAlias::from)
541                 .map(Into::into)
542                 .map(PathResolution::Def),
543             None => Some(res),
544         }
545     };
546
547     let body_owner = resolver.body_owner();
548     let values = || {
549         resolver.resolve_path_in_value_ns_fully(db.upcast(), path.mod_path()).and_then(|val| {
550             let res = match val {
551                 ValueNs::LocalBinding(pat_id) => {
552                     let var = Local { parent: body_owner?, pat_id };
553                     PathResolution::Local(var)
554                 }
555                 ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()),
556                 ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()),
557                 ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
558                 ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
559                 ValueNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
560                 ValueNs::ImplSelf(impl_id) => PathResolution::SelfType(impl_id.into()),
561                 ValueNs::GenericParam(it) => PathResolution::ConstParam(it.into()),
562             };
563             Some(res)
564         })
565     };
566
567     let items = || {
568         resolver
569             .resolve_module_path_in_items(db.upcast(), path.mod_path())
570             .take_types()
571             .map(|it| PathResolution::Def(it.into()))
572     };
573
574     let macros = || {
575         resolver
576             .resolve_path_as_macro(db.upcast(), path.mod_path())
577             .map(|def| PathResolution::Macro(def.into()))
578     };
579
580     if prefer_value_ns { values().or_else(types) } else { types().or_else(values) }
581         .or_else(items)
582         .or_else(macros)
583 }
584
585 /// Resolves a path where we know it is a qualifier of another path.
586 ///
587 /// For example, if we have:
588 /// ```
589 /// mod my {
590 ///     pub mod foo {
591 ///         struct Bar;
592 ///     }
593 ///
594 ///     pub fn foo() {}
595 /// }
596 /// ```
597 /// then we know that `foo` in `my::foo::Bar` refers to the module, not the function.
598 fn resolve_hir_path_qualifier(
599     db: &dyn HirDatabase,
600     resolver: &Resolver,
601     path: &Path,
602 ) -> Option<PathResolution> {
603     let items = resolver
604         .resolve_module_path_in_items(db.upcast(), path.mod_path())
605         .take_types()
606         .map(|it| PathResolution::Def(it.into()));
607
608     if items.is_some() {
609         return items;
610     }
611
612     resolver.resolve_path_in_type_ns_fully(db.upcast(), path.mod_path()).map(|ty| match ty {
613         TypeNs::SelfType(it) => PathResolution::SelfType(it.into()),
614         TypeNs::GenericParam(id) => PathResolution::TypeParam(TypeParam { id }),
615         TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()),
616         TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
617         TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
618         TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
619         TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
620     })
621 }