]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | //! Transforms syntax into `Path` objects, ideally with accounting for hygiene |
2 | ||
9ffffee4 FG |
3 | use std::iter; |
4 | ||
353b0b11 | 5 | use crate::type_ref::ConstRefOrPath; |
064997fb FG |
6 | |
7 | use either::Either; | |
8 | use hir_expand::name::{name, AsName}; | |
9ffffee4 | 9 | use intern::Interned; |
064997fb FG |
10 | use syntax::ast::{self, AstNode, HasTypeBounds}; |
11 | ||
12 | use super::AssociatedTypeBinding; | |
13 | use crate::{ | |
14 | body::LowerCtx, | |
15 | path::{GenericArg, GenericArgs, ModPath, Path, PathKind}, | |
16 | type_ref::{LifetimeRef, TypeBound, TypeRef}, | |
17 | }; | |
18 | ||
19 | /// Converts an `ast::Path` to `Path`. Works with use trees. | |
20 | /// It correctly handles `$crate` based path from macro call. | |
21 | pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx<'_>) -> Option<Path> { | |
22 | let mut kind = PathKind::Plain; | |
23 | let mut type_anchor = None; | |
24 | let mut segments = Vec::new(); | |
25 | let mut generic_args = Vec::new(); | |
26 | let hygiene = ctx.hygiene(); | |
27 | loop { | |
28 | let segment = path.segment()?; | |
29 | ||
30 | if segment.coloncolon_token().is_some() { | |
31 | kind = PathKind::Abs; | |
32 | } | |
33 | ||
34 | match segment.kind()? { | |
35 | ast::PathSegmentKind::Name(name_ref) => { | |
36 | // FIXME: this should just return name | |
37 | match hygiene.name_ref_to_name(ctx.db.upcast(), name_ref) { | |
38 | Either::Left(name) => { | |
39 | let args = segment | |
40 | .generic_arg_list() | |
41 | .and_then(|it| lower_generic_args(ctx, it)) | |
42 | .or_else(|| { | |
43 | lower_generic_args_from_fn_path( | |
44 | ctx, | |
45 | segment.param_list(), | |
46 | segment.ret_type(), | |
47 | ) | |
48 | }) | |
49 | .map(Interned::new); | |
9ffffee4 FG |
50 | if let Some(_) = args { |
51 | generic_args.resize(segments.len(), None); | |
52 | generic_args.push(args); | |
53 | } | |
064997fb | 54 | segments.push(name); |
064997fb FG |
55 | } |
56 | Either::Right(crate_id) => { | |
57 | kind = PathKind::DollarCrate(crate_id); | |
58 | break; | |
59 | } | |
60 | } | |
61 | } | |
62 | ast::PathSegmentKind::SelfTypeKw => { | |
63 | segments.push(name![Self]); | |
064997fb FG |
64 | } |
65 | ast::PathSegmentKind::Type { type_ref, trait_ref } => { | |
66 | assert!(path.qualifier().is_none()); // this can only occur at the first segment | |
67 | ||
68 | let self_type = TypeRef::from_ast(ctx, type_ref?); | |
69 | ||
70 | match trait_ref { | |
71 | // <T>::foo | |
72 | None => { | |
73 | type_anchor = Some(Interned::new(self_type)); | |
74 | kind = PathKind::Plain; | |
75 | } | |
76 | // <T as Trait<A>>::Foo desugars to Trait<Self=T, A>::Foo | |
77 | Some(trait_ref) => { | |
78 | let Path { mod_path, generic_args: path_generic_args, .. } = | |
79 | Path::from_src(trait_ref.path()?, ctx)?; | |
80 | let num_segments = mod_path.segments().len(); | |
81 | kind = mod_path.kind; | |
82 | ||
83 | segments.extend(mod_path.segments().iter().cloned().rev()); | |
9ffffee4 FG |
84 | if let Some(path_generic_args) = path_generic_args { |
85 | generic_args.resize(segments.len() - num_segments, None); | |
86 | generic_args.extend(Vec::from(path_generic_args).into_iter().rev()); | |
87 | } else { | |
88 | generic_args.resize(segments.len(), None); | |
89 | } | |
90 | ||
91 | let self_type = GenericArg::Type(self_type); | |
064997fb FG |
92 | |
93 | // Insert the type reference (T in the above example) as Self parameter for the trait | |
9ffffee4 FG |
94 | let last_segment = generic_args.get_mut(segments.len() - num_segments)?; |
95 | *last_segment = Some(Interned::new(match last_segment.take() { | |
96 | Some(it) => GenericArgs { | |
97 | args: iter::once(self_type) | |
98 | .chain(it.args.iter().cloned()) | |
99 | .collect(), | |
100 | ||
101 | has_self_type: true, | |
102 | bindings: it.bindings.clone(), | |
103 | desugared_from_fn: it.desugared_from_fn, | |
104 | }, | |
105 | None => GenericArgs { | |
106 | args: Box::new([self_type]), | |
107 | has_self_type: true, | |
108 | ..GenericArgs::empty() | |
109 | }, | |
110 | })); | |
064997fb FG |
111 | } |
112 | } | |
113 | } | |
114 | ast::PathSegmentKind::CrateKw => { | |
115 | kind = PathKind::Crate; | |
116 | break; | |
117 | } | |
118 | ast::PathSegmentKind::SelfKw => { | |
119 | // don't break out if `self` is the last segment of a path, this mean we got a | |
120 | // use tree like `foo::{self}` which we want to resolve as `foo` | |
121 | if !segments.is_empty() { | |
122 | kind = PathKind::Super(0); | |
123 | break; | |
124 | } | |
125 | } | |
126 | ast::PathSegmentKind::SuperKw => { | |
127 | let nested_super_count = if let PathKind::Super(n) = kind { n } else { 0 }; | |
128 | kind = PathKind::Super(nested_super_count + 1); | |
129 | } | |
130 | } | |
131 | path = match qualifier(&path) { | |
132 | Some(it) => it, | |
133 | None => break, | |
134 | }; | |
135 | } | |
136 | segments.reverse(); | |
9ffffee4 FG |
137 | if !generic_args.is_empty() { |
138 | generic_args.resize(segments.len(), None); | |
139 | generic_args.reverse(); | |
140 | } | |
064997fb FG |
141 | |
142 | if segments.is_empty() && kind == PathKind::Plain && type_anchor.is_none() { | |
143 | // plain empty paths don't exist, this means we got a single `self` segment as our path | |
144 | kind = PathKind::Super(0); | |
145 | } | |
146 | ||
147 | // handle local_inner_macros : | |
148 | // Basically, even in rustc it is quite hacky: | |
149 | // https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456 | |
150 | // We follow what it did anyway :) | |
151 | if segments.len() == 1 && kind == PathKind::Plain { | |
152 | if let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) { | |
153 | if let Some(crate_id) = hygiene.local_inner_macros(ctx.db.upcast(), path) { | |
154 | kind = PathKind::DollarCrate(crate_id); | |
155 | } | |
156 | } | |
157 | } | |
158 | ||
159 | let mod_path = Interned::new(ModPath::from_segments(kind, segments)); | |
9ffffee4 FG |
160 | return Some(Path { |
161 | type_anchor, | |
162 | mod_path, | |
163 | generic_args: if generic_args.is_empty() { None } else { Some(generic_args.into()) }, | |
164 | }); | |
064997fb FG |
165 | |
166 | fn qualifier(path: &ast::Path) -> Option<ast::Path> { | |
167 | if let Some(q) = path.qualifier() { | |
168 | return Some(q); | |
169 | } | |
170 | // FIXME: this bottom up traversal is not too precise. | |
171 | // Should we handle do a top-down analysis, recording results? | |
172 | let use_tree_list = path.syntax().ancestors().find_map(ast::UseTreeList::cast)?; | |
173 | let use_tree = use_tree_list.parent_use_tree(); | |
174 | use_tree.path() | |
175 | } | |
176 | } | |
177 | ||
178 | pub(super) fn lower_generic_args( | |
179 | lower_ctx: &LowerCtx<'_>, | |
180 | node: ast::GenericArgList, | |
181 | ) -> Option<GenericArgs> { | |
182 | let mut args = Vec::new(); | |
183 | let mut bindings = Vec::new(); | |
184 | for generic_arg in node.generic_args() { | |
185 | match generic_arg { | |
186 | ast::GenericArg::TypeArg(type_arg) => { | |
187 | let type_ref = TypeRef::from_ast_opt(lower_ctx, type_arg.ty()); | |
188 | args.push(GenericArg::Type(type_ref)); | |
189 | } | |
190 | ast::GenericArg::AssocTypeArg(assoc_type_arg) => { | |
191 | if let Some(name_ref) = assoc_type_arg.name_ref() { | |
192 | let name = name_ref.as_name(); | |
487cf647 FG |
193 | let args = assoc_type_arg |
194 | .generic_arg_list() | |
195 | .and_then(|args| lower_generic_args(lower_ctx, args)) | |
196 | .map(Interned::new); | |
064997fb FG |
197 | let type_ref = assoc_type_arg.ty().map(|it| TypeRef::from_ast(lower_ctx, it)); |
198 | let bounds = if let Some(l) = assoc_type_arg.type_bound_list() { | |
199 | l.bounds() | |
200 | .map(|it| Interned::new(TypeBound::from_ast(lower_ctx, it))) | |
201 | .collect() | |
202 | } else { | |
9ffffee4 | 203 | Box::default() |
064997fb | 204 | }; |
487cf647 | 205 | bindings.push(AssociatedTypeBinding { name, args, type_ref, bounds }); |
064997fb FG |
206 | } |
207 | } | |
208 | ast::GenericArg::LifetimeArg(lifetime_arg) => { | |
209 | if let Some(lifetime) = lifetime_arg.lifetime() { | |
210 | let lifetime_ref = LifetimeRef::new(&lifetime); | |
211 | args.push(GenericArg::Lifetime(lifetime_ref)) | |
212 | } | |
213 | } | |
214 | ast::GenericArg::ConstArg(arg) => { | |
353b0b11 | 215 | let arg = ConstRefOrPath::from_expr_opt(arg.expr()); |
064997fb FG |
216 | args.push(GenericArg::Const(arg)) |
217 | } | |
218 | } | |
219 | } | |
220 | ||
221 | if args.is_empty() && bindings.is_empty() { | |
222 | return None; | |
223 | } | |
9ffffee4 FG |
224 | Some(GenericArgs { |
225 | args: args.into_boxed_slice(), | |
226 | has_self_type: false, | |
227 | bindings: bindings.into_boxed_slice(), | |
228 | desugared_from_fn: false, | |
229 | }) | |
064997fb FG |
230 | } |
231 | ||
232 | /// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y) | |
233 | /// -> Z` (which desugars to `Fn<(X, Y), Output=Z>`). | |
234 | fn lower_generic_args_from_fn_path( | |
235 | ctx: &LowerCtx<'_>, | |
236 | params: Option<ast::ParamList>, | |
237 | ret_type: Option<ast::RetType>, | |
238 | ) -> Option<GenericArgs> { | |
064997fb FG |
239 | let params = params?; |
240 | let mut param_types = Vec::new(); | |
241 | for param in params.params() { | |
242 | let type_ref = TypeRef::from_ast_opt(ctx, param.ty()); | |
243 | param_types.push(type_ref); | |
244 | } | |
9ffffee4 FG |
245 | let args = Box::new([GenericArg::Type(TypeRef::Tuple(param_types))]); |
246 | let bindings = if let Some(ret_type) = ret_type { | |
064997fb | 247 | let type_ref = TypeRef::from_ast_opt(ctx, ret_type.ty()); |
9ffffee4 | 248 | Box::new([AssociatedTypeBinding { |
064997fb | 249 | name: name![Output], |
487cf647 | 250 | args: None, |
064997fb | 251 | type_ref: Some(type_ref), |
9ffffee4 FG |
252 | bounds: Box::default(), |
253 | }]) | |
064997fb FG |
254 | } else { |
255 | // -> () | |
256 | let type_ref = TypeRef::Tuple(Vec::new()); | |
9ffffee4 | 257 | Box::new([AssociatedTypeBinding { |
064997fb | 258 | name: name![Output], |
487cf647 | 259 | args: None, |
064997fb | 260 | type_ref: Some(type_ref), |
9ffffee4 FG |
261 | bounds: Box::default(), |
262 | }]) | |
263 | }; | |
064997fb FG |
264 | Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: true }) |
265 | } |