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