1 //! Transforms syntax into `Path` objects, ideally with accounting for hygiene
5 use crate::intern::Interned;
8 use hir_expand::name::{name, AsName};
9 use syntax::ast::{self, AstNode, TypeBoundsOwner};
11 use super::AssociatedTypeBinding;
14 path::{GenericArg, GenericArgs, ModPath, Path, PathKind},
15 type_ref::{LifetimeRef, TypeBound, TypeRef},
18 pub(super) use lower_use::lower_use_tree;
20 /// Converts an `ast::Path` to `Path`. Works with use trees.
21 /// It correctly handles `$crate` based path from macro call.
22 pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx) -> Option<Path> {
23 let mut kind = PathKind::Plain;
24 let mut type_anchor = None;
25 let mut segments = Vec::new();
26 let mut generic_args = Vec::new();
27 let hygiene = ctx.hygiene();
29 let segment = path.segment()?;
31 if segment.coloncolon_token().is_some() {
35 match segment.kind()? {
36 ast::PathSegmentKind::Name(name_ref) => {
37 // FIXME: this should just return name
38 match hygiene.name_ref_to_name(ctx.db.upcast(), name_ref) {
39 Either::Left(name) => {
42 .and_then(|it| lower_generic_args(ctx, it))
44 lower_generic_args_from_fn_path(
52 generic_args.push(args)
54 Either::Right(crate_id) => {
55 kind = PathKind::DollarCrate(crate_id);
60 ast::PathSegmentKind::Type { type_ref, trait_ref } => {
61 assert!(path.qualifier().is_none()); // this can only occur at the first segment
63 let self_type = TypeRef::from_ast(ctx, type_ref?);
68 type_anchor = Some(Interned::new(self_type));
69 kind = PathKind::Plain;
71 // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo
73 let path = Path::from_src(trait_ref.path()?, ctx)?;
74 let mod_path = (*path.mod_path).clone();
75 let num_segments = path.mod_path.segments.len();
78 let mut prefix_segments = mod_path.segments;
79 prefix_segments.reverse();
80 segments.extend(prefix_segments);
82 let mut prefix_args = path.generic_args;
83 prefix_args.reverse();
84 generic_args.extend(prefix_args);
86 // Insert the type reference (T in the above example) as Self parameter for the trait
88 generic_args.iter_mut().rev().nth(num_segments.saturating_sub(1))?;
89 let mut args_inner = match last_segment {
90 Some(it) => it.as_ref().clone(),
91 None => GenericArgs::empty(),
93 args_inner.has_self_type = true;
94 args_inner.args.insert(0, GenericArg::Type(self_type));
95 *last_segment = Some(Interned::new(args_inner));
99 ast::PathSegmentKind::CrateKw => {
100 kind = PathKind::Crate;
103 ast::PathSegmentKind::SelfKw => {
104 // don't break out if `self` is the last segment of a path, this mean we got an
105 // use tree like `foo::{self}` which we want to resolve as `foo`
106 if !segments.is_empty() {
107 kind = PathKind::Super(0);
111 ast::PathSegmentKind::SuperKw => {
112 let nested_super_count = if let PathKind::Super(n) = kind { n } else { 0 };
113 kind = PathKind::Super(nested_super_count + 1);
116 path = match qualifier(&path) {
122 generic_args.reverse();
124 if segments.is_empty() && kind == PathKind::Plain && type_anchor.is_none() {
125 // plain empty paths don't exist, this means we got a single `self` segment as our path
126 kind = PathKind::Super(0);
129 // handle local_inner_macros :
130 // Basically, even in rustc it is quite hacky:
131 // https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456
132 // We follow what it did anyway :)
133 if segments.len() == 1 && kind == PathKind::Plain {
134 if let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) {
135 if let Some(crate_id) = hygiene.local_inner_macros(ctx.db.upcast(), path) {
136 kind = PathKind::DollarCrate(crate_id);
141 let mod_path = Interned::new(ModPath::from_segments(kind, segments));
142 return Some(Path { type_anchor, mod_path, generic_args });
144 fn qualifier(path: &ast::Path) -> Option<ast::Path> {
145 if let Some(q) = path.qualifier() {
148 // FIXME: this bottom up traversal is not too precise.
149 // Should we handle do a top-down analysis, recording results?
150 let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?;
151 let use_tree = use_tree_list.parent_use_tree();
156 pub(super) fn lower_generic_args(
157 lower_ctx: &LowerCtx,
158 node: ast::GenericArgList,
159 ) -> Option<GenericArgs> {
160 let mut args = Vec::new();
161 let mut bindings = Vec::new();
162 for generic_arg in node.generic_args() {
164 ast::GenericArg::TypeArg(type_arg) => {
165 let type_ref = TypeRef::from_ast_opt(lower_ctx, type_arg.ty());
166 args.push(GenericArg::Type(type_ref));
168 ast::GenericArg::AssocTypeArg(assoc_type_arg) => {
169 if let Some(name_ref) = assoc_type_arg.name_ref() {
170 let name = name_ref.as_name();
171 let type_ref = assoc_type_arg.ty().map(|it| TypeRef::from_ast(lower_ctx, it));
172 let bounds = if let Some(l) = assoc_type_arg.type_bound_list() {
174 .map(|it| Interned::new(TypeBound::from_ast(lower_ctx, it)))
179 bindings.push(AssociatedTypeBinding { name, type_ref, bounds });
182 ast::GenericArg::LifetimeArg(lifetime_arg) => {
183 if let Some(lifetime) = lifetime_arg.lifetime() {
184 let lifetime_ref = LifetimeRef::new(&lifetime);
185 args.push(GenericArg::Lifetime(lifetime_ref))
188 // constants are ignored for now.
189 ast::GenericArg::ConstArg(_) => (),
193 if args.is_empty() && bindings.is_empty() {
196 Some(GenericArgs { args, has_self_type: false, bindings })
199 /// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y)
200 /// -> Z` (which desugars to `Fn<(X, Y), Output=Z>`).
201 fn lower_generic_args_from_fn_path(
203 params: Option<ast::ParamList>,
204 ret_type: Option<ast::RetType>,
205 ) -> Option<GenericArgs> {
206 let mut args = Vec::new();
207 let mut bindings = Vec::new();
208 let params = params?;
209 let mut param_types = Vec::new();
210 for param in params.params() {
211 let type_ref = TypeRef::from_ast_opt(&ctx, param.ty());
212 param_types.push(type_ref);
214 let arg = GenericArg::Type(TypeRef::Tuple(param_types));
216 if let Some(ret_type) = ret_type {
217 let type_ref = TypeRef::from_ast_opt(&ctx, ret_type.ty());
218 bindings.push(AssociatedTypeBinding {
220 type_ref: Some(type_ref),
225 let type_ref = TypeRef::Tuple(Vec::new());
226 bindings.push(AssociatedTypeBinding {
228 type_ref: Some(type_ref),
232 Some(GenericArgs { args, has_self_type: false, bindings })