diff options
Diffstat (limited to 'crates/ra_hir_def/src/path.rs')
-rw-r--r-- | crates/ra_hir_def/src/path.rs | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 9e37ac416..00325cd99 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs | |||
@@ -18,6 +18,18 @@ pub struct ModPath { | |||
18 | pub segments: Vec<Name>, | 18 | pub segments: Vec<Name>, |
19 | } | 19 | } |
20 | 20 | ||
21 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
22 | pub enum PathKind { | ||
23 | Plain, | ||
24 | /// `self::` is `Super(0)` | ||
25 | Super(u8), | ||
26 | Crate, | ||
27 | /// Absolute path (::foo) | ||
28 | Abs, | ||
29 | /// `$crate` from macro expansion | ||
30 | DollarCrate(CrateId), | ||
31 | } | ||
32 | |||
21 | impl ModPath { | 33 | impl ModPath { |
22 | pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> { | 34 | pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> { |
23 | lower::lower_path(path, hygiene).map(|it| it.mod_path) | 35 | lower::lower_path(path, hygiene).map(|it| it.mod_path) |
@@ -70,6 +82,9 @@ impl ModPath { | |||
70 | 82 | ||
71 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 83 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
72 | pub struct Path { | 84 | pub struct Path { |
85 | /// Type based path like `<T>::foo`. | ||
86 | /// Note that paths like `<Type as Trait>::foo` are desugard to `Trait::<Self=Type>::foo`. | ||
87 | type_anchor: Option<Box<TypeRef>>, | ||
73 | mod_path: ModPath, | 88 | mod_path: ModPath, |
74 | /// Invariant: the same len as self.path.segments | 89 | /// Invariant: the same len as self.path.segments |
75 | generic_args: Vec<Option<Arc<GenericArgs>>>, | 90 | generic_args: Vec<Option<Arc<GenericArgs>>>, |
@@ -97,19 +112,6 @@ pub enum GenericArg { | |||
97 | // or lifetime... | 112 | // or lifetime... |
98 | } | 113 | } |
99 | 114 | ||
100 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
101 | pub enum PathKind { | ||
102 | Plain, | ||
103 | Super(u8), | ||
104 | Crate, | ||
105 | // Absolute path | ||
106 | Abs, | ||
107 | // Type based path like `<T>::foo` | ||
108 | Type(Box<TypeRef>), | ||
109 | // `$crate` from macro expansion | ||
110 | DollarCrate(CrateId), | ||
111 | } | ||
112 | |||
113 | impl Path { | 115 | impl Path { |
114 | /// Converts an `ast::Path` to `Path`. Works with use trees. | 116 | /// Converts an `ast::Path` to `Path`. Works with use trees. |
115 | /// DEPRECATED: It does not handle `$crate` from macro call. | 117 | /// DEPRECATED: It does not handle `$crate` from macro call. |
@@ -125,18 +127,17 @@ impl Path { | |||
125 | 127 | ||
126 | /// Converts an `ast::NameRef` into a single-identifier `Path`. | 128 | /// Converts an `ast::NameRef` into a single-identifier `Path`. |
127 | pub(crate) fn from_name_ref(name_ref: &ast::NameRef) -> Path { | 129 | pub(crate) fn from_name_ref(name_ref: &ast::NameRef) -> Path { |
128 | Path { mod_path: name_ref.as_name().into(), generic_args: vec![None] } | 130 | Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] } |
129 | } | ||
130 | |||
131 | /// `true` if this path is just a standalone `self` | ||
132 | pub fn is_self(&self) -> bool { | ||
133 | self.mod_path.is_self() | ||
134 | } | 131 | } |
135 | 132 | ||
136 | pub fn kind(&self) -> &PathKind { | 133 | pub fn kind(&self) -> &PathKind { |
137 | &self.mod_path.kind | 134 | &self.mod_path.kind |
138 | } | 135 | } |
139 | 136 | ||
137 | pub fn type_anchor(&self) -> Option<&TypeRef> { | ||
138 | self.type_anchor.as_ref().map(|it| &**it) | ||
139 | } | ||
140 | |||
140 | pub fn segments(&self) -> PathSegments<'_> { | 141 | pub fn segments(&self) -> PathSegments<'_> { |
141 | PathSegments { | 142 | PathSegments { |
142 | segments: self.mod_path.segments.as_slice(), | 143 | segments: self.mod_path.segments.as_slice(), |
@@ -153,6 +154,7 @@ impl Path { | |||
153 | return None; | 154 | return None; |
154 | } | 155 | } |
155 | let res = Path { | 156 | let res = Path { |
157 | type_anchor: self.type_anchor.clone(), | ||
156 | mod_path: ModPath { | 158 | mod_path: ModPath { |
157 | kind: self.mod_path.kind.clone(), | 159 | kind: self.mod_path.kind.clone(), |
158 | segments: self.mod_path.segments[..self.mod_path.segments.len() - 1].to_vec(), | 160 | segments: self.mod_path.segments[..self.mod_path.segments.len() - 1].to_vec(), |
@@ -225,6 +227,7 @@ impl GenericArgs { | |||
225 | impl From<Name> for Path { | 227 | impl From<Name> for Path { |
226 | fn from(name: Name) -> Path { | 228 | fn from(name: Name) -> Path { |
227 | Path { | 229 | Path { |
230 | type_anchor: None, | ||
228 | mod_path: ModPath::from_simple_segments(PathKind::Plain, iter::once(name)), | 231 | mod_path: ModPath::from_simple_segments(PathKind::Plain, iter::once(name)), |
229 | generic_args: vec![None], | 232 | generic_args: vec![None], |
230 | } | 233 | } |