]> git.proxmox.com Git - rustc.git/blob - src/tools/rust-analyzer/crates/hir-def/src/path.rs
36d4c36a26894550135d732e08ab3f704ae55f7e
[rustc.git] / src / tools / rust-analyzer / crates / hir-def / src / path.rs
1 //! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`.
2 mod lower;
3
4 use std::{
5 fmt::{self, Display},
6 iter,
7 };
8
9 use crate::{
10 body::LowerCtx,
11 type_ref::{ConstScalarOrPath, LifetimeRef},
12 };
13 use hir_expand::name::Name;
14 use intern::Interned;
15 use syntax::ast;
16
17 use crate::type_ref::{TypeBound, TypeRef};
18
19 pub use hir_expand::mod_path::{path, ModPath, PathKind};
20
21 #[derive(Debug, Clone, PartialEq, Eq)]
22 pub enum ImportAlias {
23 /// Unnamed alias, as in `use Foo as _;`
24 Underscore,
25 /// Named alias
26 Alias(Name),
27 }
28
29 impl Display for ImportAlias {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self {
32 ImportAlias::Underscore => f.write_str("_"),
33 ImportAlias::Alias(name) => f.write_str(&name.to_smol_str()),
34 }
35 }
36 }
37
38 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
39 pub struct Path {
40 /// Type based path like `<T>::foo`.
41 /// Note that paths like `<Type as Trait>::foo` are desugared to `Trait::<Self=Type>::foo`.
42 type_anchor: Option<Interned<TypeRef>>,
43 mod_path: Interned<ModPath>,
44 /// Invariant: the same len as `self.mod_path.segments` or `None` if all segments are `None`.
45 generic_args: Option<Box<[Option<Interned<GenericArgs>>]>>,
46 }
47
48 /// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This
49 /// also includes bindings of associated types, like in `Iterator<Item = Foo>`.
50 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
51 pub struct GenericArgs {
52 pub args: Box<[GenericArg]>,
53 /// This specifies whether the args contain a Self type as the first
54 /// element. This is the case for path segments like `<T as Trait>`, where
55 /// `T` is actually a type parameter for the path `Trait` specifying the
56 /// Self type. Otherwise, when we have a path `Trait<X, Y>`, the Self type
57 /// is left out.
58 pub has_self_type: bool,
59 /// Associated type bindings like in `Iterator<Item = T>`.
60 pub bindings: Box<[AssociatedTypeBinding]>,
61 /// Whether these generic args were desugared from `Trait(Arg) -> Output`
62 /// parenthesis notation typically used for the `Fn` traits.
63 pub desugared_from_fn: bool,
64 }
65
66 /// An associated type binding like in `Iterator<Item = T>`.
67 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
68 pub struct AssociatedTypeBinding {
69 /// The name of the associated type.
70 pub name: Name,
71 /// The generic arguments to the associated type. e.g. For `Trait<Assoc<'a, T> = &'a T>`, this
72 /// would be `['a, T]`.
73 pub args: Option<Interned<GenericArgs>>,
74 /// The type bound to this associated type (in `Item = T`, this would be the
75 /// `T`). This can be `None` if there are bounds instead.
76 pub type_ref: Option<TypeRef>,
77 /// Bounds for the associated type, like in `Iterator<Item:
78 /// SomeOtherTrait>`. (This is the unstable `associated_type_bounds`
79 /// feature.)
80 pub bounds: Box<[Interned<TypeBound>]>,
81 }
82
83 /// A single generic argument.
84 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
85 pub enum GenericArg {
86 Type(TypeRef),
87 Lifetime(LifetimeRef),
88 Const(ConstScalarOrPath),
89 }
90
91 impl Path {
92 /// Converts an `ast::Path` to `Path`. Works with use trees.
93 /// It correctly handles `$crate` based path from macro call.
94 pub fn from_src(path: ast::Path, ctx: &LowerCtx<'_>) -> Option<Path> {
95 lower::lower_path(path, ctx)
96 }
97
98 /// Converts a known mod path to `Path`.
99 pub fn from_known_path(
100 path: ModPath,
101 generic_args: impl Into<Box<[Option<Interned<GenericArgs>>]>>,
102 ) -> Path {
103 let generic_args = generic_args.into();
104 assert_eq!(path.len(), generic_args.len());
105 Path { type_anchor: None, mod_path: Interned::new(path), generic_args: Some(generic_args) }
106 }
107
108 pub fn kind(&self) -> &PathKind {
109 &self.mod_path.kind
110 }
111
112 pub fn type_anchor(&self) -> Option<&TypeRef> {
113 self.type_anchor.as_deref()
114 }
115
116 pub fn segments(&self) -> PathSegments<'_> {
117 let s = PathSegments {
118 segments: self.mod_path.segments(),
119 generic_args: self.generic_args.as_deref(),
120 };
121 if let Some(generic_args) = s.generic_args {
122 assert_eq!(s.segments.len(), generic_args.len());
123 }
124 s
125 }
126
127 pub fn mod_path(&self) -> &ModPath {
128 &self.mod_path
129 }
130
131 pub fn qualifier(&self) -> Option<Path> {
132 if self.mod_path.is_ident() {
133 return None;
134 }
135 let res = Path {
136 type_anchor: self.type_anchor.clone(),
137 mod_path: Interned::new(ModPath::from_segments(
138 self.mod_path.kind,
139 self.mod_path.segments()[..self.mod_path.segments().len() - 1].iter().cloned(),
140 )),
141 generic_args: self.generic_args.as_ref().map(|it| it[..it.len() - 1].to_vec().into()),
142 };
143 Some(res)
144 }
145
146 pub fn is_self_type(&self) -> bool {
147 self.type_anchor.is_none()
148 && self.generic_args.as_deref().is_none()
149 && self.mod_path.is_Self()
150 }
151 }
152
153 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
154 pub struct PathSegment<'a> {
155 pub name: &'a Name,
156 pub args_and_bindings: Option<&'a GenericArgs>,
157 }
158
159 pub struct PathSegments<'a> {
160 segments: &'a [Name],
161 generic_args: Option<&'a [Option<Interned<GenericArgs>>]>,
162 }
163
164 impl<'a> PathSegments<'a> {
165 pub const EMPTY: PathSegments<'static> = PathSegments { segments: &[], generic_args: None };
166 pub fn is_empty(&self) -> bool {
167 self.len() == 0
168 }
169 pub fn len(&self) -> usize {
170 self.segments.len()
171 }
172 pub fn first(&self) -> Option<PathSegment<'a>> {
173 self.get(0)
174 }
175 pub fn last(&self) -> Option<PathSegment<'a>> {
176 self.get(self.len().checked_sub(1)?)
177 }
178 pub fn get(&self, idx: usize) -> Option<PathSegment<'a>> {
179 let res = PathSegment {
180 name: self.segments.get(idx)?,
181 args_and_bindings: self.generic_args.and_then(|it| it.get(idx)?.as_deref()),
182 };
183 Some(res)
184 }
185 pub fn skip(&self, len: usize) -> PathSegments<'a> {
186 PathSegments {
187 segments: &self.segments.get(len..).unwrap_or(&[]),
188 generic_args: self.generic_args.and_then(|it| it.get(len..)),
189 }
190 }
191 pub fn take(&self, len: usize) -> PathSegments<'a> {
192 PathSegments {
193 segments: &self.segments.get(..len).unwrap_or(&self.segments),
194 generic_args: self.generic_args.map(|it| it.get(..len).unwrap_or(it)),
195 }
196 }
197 pub fn iter(&self) -> impl Iterator<Item = PathSegment<'a>> {
198 self.segments
199 .iter()
200 .zip(self.generic_args.into_iter().flatten().chain(iter::repeat(&None)))
201 .map(|(name, args)| PathSegment { name, args_and_bindings: args.as_deref() })
202 }
203 }
204
205 impl GenericArgs {
206 pub(crate) fn from_ast(
207 lower_ctx: &LowerCtx<'_>,
208 node: ast::GenericArgList,
209 ) -> Option<GenericArgs> {
210 lower::lower_generic_args(lower_ctx, node)
211 }
212
213 pub(crate) fn empty() -> GenericArgs {
214 GenericArgs {
215 args: Box::default(),
216 has_self_type: false,
217 bindings: Box::default(),
218 desugared_from_fn: false,
219 }
220 }
221 }
222
223 impl From<Name> for Path {
224 fn from(name: Name) -> Path {
225 Path {
226 type_anchor: None,
227 mod_path: Interned::new(ModPath::from_segments(PathKind::Plain, iter::once(name))),
228 generic_args: None,
229 }
230 }
231 }
232
233 impl From<Name> for Box<Path> {
234 fn from(name: Name) -> Box<Path> {
235 Box::new(Path::from(name))
236 }
237 }