diff options
Diffstat (limited to 'crates/hir_def/src')
-rw-r--r-- | crates/hir_def/src/find_path.rs | 10 | ||||
-rw-r--r-- | crates/hir_def/src/item_tree.rs | 8 | ||||
-rw-r--r-- | crates/hir_def/src/item_tree/lower.rs | 3 | ||||
-rw-r--r-- | crates/hir_def/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 4 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/path_resolution.rs | 20 | ||||
-rw-r--r-- | crates/hir_def/src/path.rs | 29 | ||||
-rw-r--r-- | crates/hir_def/src/resolver.rs | 12 | ||||
-rw-r--r-- | crates/hir_def/src/visibility.rs | 9 |
9 files changed, 58 insertions, 39 deletions
diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs index 94a1d567d..aa2c6e04e 100644 --- a/crates/hir_def/src/find_path.rs +++ b/crates/hir_def/src/find_path.rs | |||
@@ -36,13 +36,13 @@ const MAX_PATH_LEN: usize = 15; | |||
36 | 36 | ||
37 | impl ModPath { | 37 | impl ModPath { |
38 | fn starts_with_std(&self) -> bool { | 38 | fn starts_with_std(&self) -> bool { |
39 | self.segments.first() == Some(&known::std) | 39 | self.segments().first() == Some(&known::std) |
40 | } | 40 | } |
41 | 41 | ||
42 | // When std library is present, paths starting with `std::` | 42 | // When std library is present, paths starting with `std::` |
43 | // should be preferred over paths starting with `core::` and `alloc::` | 43 | // should be preferred over paths starting with `core::` and `alloc::` |
44 | fn can_start_with_std(&self) -> bool { | 44 | fn can_start_with_std(&self) -> bool { |
45 | let first_segment = self.segments.first(); | 45 | let first_segment = self.segments().first(); |
46 | first_segment == Some(&known::alloc) || first_segment == Some(&known::core) | 46 | first_segment == Some(&known::alloc) || first_segment == Some(&known::core) |
47 | } | 47 | } |
48 | } | 48 | } |
@@ -157,7 +157,7 @@ fn find_path_inner( | |||
157 | if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() { | 157 | if let Some(ModuleDefId::EnumVariantId(variant)) = item.as_module_def_id() { |
158 | if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) { | 158 | if let Some(mut path) = find_path(db, ItemInNs::Types(variant.parent.into()), from) { |
159 | let data = db.enum_data(variant.parent); | 159 | let data = db.enum_data(variant.parent); |
160 | path.segments.push(data.variants[variant.local_id].name.clone()); | 160 | path.push_segment(data.variants[variant.local_id].name.clone()); |
161 | return Some(path); | 161 | return Some(path); |
162 | } | 162 | } |
163 | // If this doesn't work, it seems we have no way of referring to the | 163 | // If this doesn't work, it seems we have no way of referring to the |
@@ -186,7 +186,7 @@ fn find_path_inner( | |||
186 | best_path_len - 1, | 186 | best_path_len - 1, |
187 | prefixed, | 187 | prefixed, |
188 | ) { | 188 | ) { |
189 | path.segments.push(name); | 189 | path.push_segment(name); |
190 | 190 | ||
191 | let new_path = if let Some(best_path) = best_path { | 191 | let new_path = if let Some(best_path) = best_path { |
192 | select_best_path(best_path, path, prefer_no_std) | 192 | select_best_path(best_path, path, prefer_no_std) |
@@ -215,7 +215,7 @@ fn find_path_inner( | |||
215 | prefixed, | 215 | prefixed, |
216 | )?; | 216 | )?; |
217 | mark::hit!(partially_imported); | 217 | mark::hit!(partially_imported); |
218 | path.segments.push(info.path.segments.last().unwrap().clone()); | 218 | path.push_segment(info.path.segments.last().unwrap().clone()); |
219 | Some(path) | 219 | Some(path) |
220 | }) | 220 | }) |
221 | }); | 221 | }); |
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 401556931..3233b1957 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs | |||
@@ -240,7 +240,7 @@ impl ItemVisibilities { | |||
240 | fn alloc(&mut self, vis: RawVisibility) -> RawVisibilityId { | 240 | fn alloc(&mut self, vis: RawVisibility) -> RawVisibilityId { |
241 | match &vis { | 241 | match &vis { |
242 | RawVisibility::Public => RawVisibilityId::PUB, | 242 | RawVisibility::Public => RawVisibilityId::PUB, |
243 | RawVisibility::Module(path) if path.segments.is_empty() => match &path.kind { | 243 | RawVisibility::Module(path) if path.segments().is_empty() => match &path.kind { |
244 | PathKind::Super(0) => RawVisibilityId::PRIV, | 244 | PathKind::Super(0) => RawVisibilityId::PRIV, |
245 | PathKind::Crate => RawVisibilityId::PUB_CRATE, | 245 | PathKind::Crate => RawVisibilityId::PUB_CRATE, |
246 | _ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()), | 246 | _ => RawVisibilityId(self.arena.alloc(vis).into_raw().into()), |
@@ -251,10 +251,8 @@ impl ItemVisibilities { | |||
251 | } | 251 | } |
252 | 252 | ||
253 | static VIS_PUB: RawVisibility = RawVisibility::Public; | 253 | static VIS_PUB: RawVisibility = RawVisibility::Public; |
254 | static VIS_PRIV: RawVisibility = | 254 | static VIS_PRIV: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Super(0))); |
255 | RawVisibility::Module(ModPath { kind: PathKind::Super(0), segments: Vec::new() }); | 255 | static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(ModPath::from_kind(PathKind::Crate)); |
256 | static VIS_PUB_CRATE: RawVisibility = | ||
257 | RawVisibility::Module(ModPath { kind: PathKind::Crate, segments: Vec::new() }); | ||
258 | 256 | ||
259 | #[derive(Default, Debug, Eq, PartialEq)] | 257 | #[derive(Default, Debug, Eq, PartialEq)] |
260 | struct GenericParamsStorage { | 258 | struct GenericParamsStorage { |
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index 93cdca55d..8f2f0b340 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs | |||
@@ -750,7 +750,8 @@ impl Ctx { | |||
750 | 750 | ||
751 | fn desugar_future_path(orig: TypeRef) -> Path { | 751 | fn desugar_future_path(orig: TypeRef) -> Path { |
752 | let path = path![core::future::Future]; | 752 | let path = path![core::future::Future]; |
753 | let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect(); | 753 | let mut generic_args: Vec<_> = |
754 | std::iter::repeat(None).take(path.segments().len() - 1).collect(); | ||
754 | let mut last = GenericArgs::empty(); | 755 | let mut last = GenericArgs::empty(); |
755 | let binding = | 756 | let binding = |
756 | AssociatedTypeBinding { name: name![Output], type_ref: Some(orig), bounds: Vec::new() }; | 757 | AssociatedTypeBinding { name: name![Output], type_ref: Some(orig), bounds: Vec::new() }; |
diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index 5dd3705b0..b50923747 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs | |||
@@ -662,7 +662,7 @@ impl AsMacroCall for AstIdWithPath<ast::Item> { | |||
662 | def.as_lazy_macro( | 662 | def.as_lazy_macro( |
663 | db.upcast(), | 663 | db.upcast(), |
664 | krate, | 664 | krate, |
665 | MacroCallKind::Attr(self.ast_id, self.path.segments.last()?.to_string()), | 665 | MacroCallKind::Attr(self.ast_id, self.path.segments().last()?.to_string()), |
666 | ) | 666 | ) |
667 | .into(), | 667 | .into(), |
668 | ) | 668 | ) |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index f904a97de..6bd41bc08 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -655,7 +655,7 @@ impl DefCollector<'_> { | |||
655 | } | 655 | } |
656 | } | 656 | } |
657 | } else { | 657 | } else { |
658 | match import.path.segments.last() { | 658 | match import.path.segments().last() { |
659 | Some(last_segment) => { | 659 | Some(last_segment) => { |
660 | let name = match &import.alias { | 660 | let name = match &import.alias { |
661 | Some(ImportAlias::Alias(name)) => Some(name.clone()), | 661 | Some(ImportAlias::Alias(name)) => Some(name.clone()), |
@@ -956,7 +956,7 @@ impl DefCollector<'_> { | |||
956 | let item_tree = self.db.item_tree(import.file_id); | 956 | let item_tree = self.db.item_tree(import.file_id); |
957 | let import_data = &item_tree[import.value]; | 957 | let import_data = &item_tree[import.value]; |
958 | 958 | ||
959 | match (import_data.path.segments.first(), &import_data.path.kind) { | 959 | match (import_data.path.segments().first(), &import_data.path.kind) { |
960 | (Some(krate), PathKind::Plain) | (Some(krate), PathKind::Abs) => { | 960 | (Some(krate), PathKind::Plain) | (Some(krate), PathKind::Abs) => { |
961 | if diagnosed_extern_crates.contains(krate) { | 961 | if diagnosed_extern_crates.contains(krate) { |
962 | continue; | 962 | continue; |
diff --git a/crates/hir_def/src/nameres/path_resolution.rs b/crates/hir_def/src/nameres/path_resolution.rs index 2a3ac5d7b..f2b59172d 100644 --- a/crates/hir_def/src/nameres/path_resolution.rs +++ b/crates/hir_def/src/nameres/path_resolution.rs | |||
@@ -149,7 +149,7 @@ impl DefMap { | |||
149 | path: &ModPath, | 149 | path: &ModPath, |
150 | shadow: BuiltinShadowMode, | 150 | shadow: BuiltinShadowMode, |
151 | ) -> ResolvePathResult { | 151 | ) -> ResolvePathResult { |
152 | let mut segments = path.segments.iter().enumerate(); | 152 | let mut segments = path.segments().iter().enumerate(); |
153 | let mut curr_per_ns: PerNs = match path.kind { | 153 | let mut curr_per_ns: PerNs = match path.kind { |
154 | PathKind::DollarCrate(krate) => { | 154 | PathKind::DollarCrate(krate) => { |
155 | if krate == self.krate { | 155 | if krate == self.krate { |
@@ -190,7 +190,7 @@ impl DefMap { | |||
190 | // BuiltinShadowMode wasn't Module, then we need to try | 190 | // BuiltinShadowMode wasn't Module, then we need to try |
191 | // resolving it as a builtin. | 191 | // resolving it as a builtin. |
192 | let prefer_module = | 192 | let prefer_module = |
193 | if path.segments.len() == 1 { shadow } else { BuiltinShadowMode::Module }; | 193 | if path.segments().len() == 1 { shadow } else { BuiltinShadowMode::Module }; |
194 | 194 | ||
195 | log::debug!("resolving {:?} in module", segment); | 195 | log::debug!("resolving {:?} in module", segment); |
196 | self.resolve_name_in_module(db, original_module, &segment, prefer_module) | 196 | self.resolve_name_in_module(db, original_module, &segment, prefer_module) |
@@ -203,10 +203,10 @@ impl DefMap { | |||
203 | None => match &self.block { | 203 | None => match &self.block { |
204 | Some(block) => { | 204 | Some(block) => { |
205 | // Look up remaining path in parent `DefMap` | 205 | // Look up remaining path in parent `DefMap` |
206 | let new_path = ModPath { | 206 | let new_path = ModPath::from_segments( |
207 | kind: PathKind::Super(lvl - i), | 207 | PathKind::Super(lvl - i), |
208 | segments: path.segments.clone(), | 208 | path.segments().to_vec(), |
209 | }; | 209 | ); |
210 | log::debug!("`super` path: {} -> {} in parent map", path, new_path); | 210 | log::debug!("`super` path: {} -> {} in parent map", path, new_path); |
211 | return block.parent.def_map(db).resolve_path_fp_with_macro( | 211 | return block.parent.def_map(db).resolve_path_fp_with_macro( |
212 | db, | 212 | db, |
@@ -258,10 +258,10 @@ impl DefMap { | |||
258 | curr_per_ns = match curr { | 258 | curr_per_ns = match curr { |
259 | ModuleDefId::ModuleId(module) => { | 259 | ModuleDefId::ModuleId(module) => { |
260 | if module.krate != self.krate { | 260 | if module.krate != self.krate { |
261 | let path = ModPath { | 261 | let path = ModPath::from_segments( |
262 | segments: path.segments[i..].to_vec(), | 262 | PathKind::Super(0), |
263 | kind: PathKind::Super(0), | 263 | path.segments()[i..].iter().cloned(), |
264 | }; | 264 | ); |
265 | log::debug!("resolving {:?} in other crate", path); | 265 | log::debug!("resolving {:?} in other crate", path); |
266 | let defp_map = module.def_map(db); | 266 | let defp_map = module.def_map(db); |
267 | let (def, s) = defp_map.resolve_path(db, module.local_id, &path, shadow); | 267 | let (def, s) = defp_map.resolve_path(db, module.local_id, &path, shadow); |
diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index 84ea09b53..0caad53d3 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs | |||
@@ -20,7 +20,7 @@ use crate::{ | |||
20 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | 20 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
21 | pub struct ModPath { | 21 | pub struct ModPath { |
22 | pub kind: PathKind, | 22 | pub kind: PathKind, |
23 | pub segments: Vec<Name>, | 23 | segments: Vec<Name>, |
24 | } | 24 | } |
25 | 25 | ||
26 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] | 26 | #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
@@ -53,6 +53,11 @@ impl ModPath { | |||
53 | ModPath { kind, segments } | 53 | ModPath { kind, segments } |
54 | } | 54 | } |
55 | 55 | ||
56 | /// Creates a `ModPath` from a `PathKind`, with no extra path segments. | ||
57 | pub const fn from_kind(kind: PathKind) -> ModPath { | ||
58 | ModPath { kind, segments: Vec::new() } | ||
59 | } | ||
60 | |||
56 | /// Calls `cb` with all paths, represented by this use item. | 61 | /// Calls `cb` with all paths, represented by this use item. |
57 | pub(crate) fn expand_use_item( | 62 | pub(crate) fn expand_use_item( |
58 | item_src: InFile<ast::Use>, | 63 | item_src: InFile<ast::Use>, |
@@ -64,6 +69,18 @@ impl ModPath { | |||
64 | } | 69 | } |
65 | } | 70 | } |
66 | 71 | ||
72 | pub fn segments(&self) -> &[Name] { | ||
73 | &self.segments | ||
74 | } | ||
75 | |||
76 | pub fn push_segment(&mut self, segment: Name) { | ||
77 | self.segments.push(segment); | ||
78 | } | ||
79 | |||
80 | pub fn pop_segment(&mut self) -> Option<Name> { | ||
81 | self.segments.pop() | ||
82 | } | ||
83 | |||
67 | /// Returns the number of segments in the path (counting special segments like `$crate` and | 84 | /// Returns the number of segments in the path (counting special segments like `$crate` and |
68 | /// `super`). | 85 | /// `super`). |
69 | pub fn len(&self) -> usize { | 86 | pub fn len(&self) -> usize { |
@@ -78,7 +95,7 @@ impl ModPath { | |||
78 | } | 95 | } |
79 | 96 | ||
80 | pub fn is_ident(&self) -> bool { | 97 | pub fn is_ident(&self) -> bool { |
81 | self.kind == PathKind::Plain && self.segments.len() == 1 | 98 | self.as_ident().is_some() |
82 | } | 99 | } |
83 | 100 | ||
84 | pub fn is_self(&self) -> bool { | 101 | pub fn is_self(&self) -> bool { |
@@ -87,10 +104,14 @@ impl ModPath { | |||
87 | 104 | ||
88 | /// If this path is a single identifier, like `foo`, return its name. | 105 | /// If this path is a single identifier, like `foo`, return its name. |
89 | pub fn as_ident(&self) -> Option<&Name> { | 106 | pub fn as_ident(&self) -> Option<&Name> { |
90 | if !self.is_ident() { | 107 | if self.kind != PathKind::Plain { |
91 | return None; | 108 | return None; |
92 | } | 109 | } |
93 | self.segments.first() | 110 | |
111 | match &*self.segments { | ||
112 | [name] => Some(name), | ||
113 | _ => None, | ||
114 | } | ||
94 | } | 115 | } |
95 | } | 116 | } |
96 | 117 | ||
diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs index 9021ea712..f9ad50301 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs | |||
@@ -164,7 +164,7 @@ impl Resolver { | |||
164 | db: &dyn DefDatabase, | 164 | db: &dyn DefDatabase, |
165 | path: &ModPath, | 165 | path: &ModPath, |
166 | ) -> Option<(TypeNs, Option<usize>)> { | 166 | ) -> Option<(TypeNs, Option<usize>)> { |
167 | let first_name = path.segments.first()?; | 167 | let first_name = path.segments().first()?; |
168 | let skip_to_mod = path.kind != PathKind::Plain; | 168 | let skip_to_mod = path.kind != PathKind::Plain; |
169 | for scope in self.scopes.iter().rev() { | 169 | for scope in self.scopes.iter().rev() { |
170 | match scope { | 170 | match scope { |
@@ -179,7 +179,7 @@ impl Resolver { | |||
179 | 179 | ||
180 | Scope::GenericParams { params, def } => { | 180 | Scope::GenericParams { params, def } => { |
181 | if let Some(local_id) = params.find_type_by_name(first_name) { | 181 | if let Some(local_id) = params.find_type_by_name(first_name) { |
182 | let idx = if path.segments.len() == 1 { None } else { Some(1) }; | 182 | let idx = if path.segments().len() == 1 { None } else { Some(1) }; |
183 | return Some(( | 183 | return Some(( |
184 | TypeNs::GenericParam(TypeParamId { local_id, parent: *def }), | 184 | TypeNs::GenericParam(TypeParamId { local_id, parent: *def }), |
185 | idx, | 185 | idx, |
@@ -188,13 +188,13 @@ impl Resolver { | |||
188 | } | 188 | } |
189 | Scope::ImplDefScope(impl_) => { | 189 | Scope::ImplDefScope(impl_) => { |
190 | if first_name == &name![Self] { | 190 | if first_name == &name![Self] { |
191 | let idx = if path.segments.len() == 1 { None } else { Some(1) }; | 191 | let idx = if path.segments().len() == 1 { None } else { Some(1) }; |
192 | return Some((TypeNs::SelfType(*impl_), idx)); | 192 | return Some((TypeNs::SelfType(*impl_), idx)); |
193 | } | 193 | } |
194 | } | 194 | } |
195 | Scope::AdtScope(adt) => { | 195 | Scope::AdtScope(adt) => { |
196 | if first_name == &name![Self] { | 196 | if first_name == &name![Self] { |
197 | let idx = if path.segments.len() == 1 { None } else { Some(1) }; | 197 | let idx = if path.segments().len() == 1 { None } else { Some(1) }; |
198 | return Some((TypeNs::AdtSelfType(*adt), idx)); | 198 | return Some((TypeNs::AdtSelfType(*adt), idx)); |
199 | } | 199 | } |
200 | } | 200 | } |
@@ -270,9 +270,9 @@ impl Resolver { | |||
270 | db: &dyn DefDatabase, | 270 | db: &dyn DefDatabase, |
271 | path: &ModPath, | 271 | path: &ModPath, |
272 | ) -> Option<ResolveValueResult> { | 272 | ) -> Option<ResolveValueResult> { |
273 | let n_segments = path.segments.len(); | 273 | let n_segments = path.segments().len(); |
274 | let tmp = name![self]; | 274 | let tmp = name![self]; |
275 | let first_name = if path.is_self() { &tmp } else { path.segments.first()? }; | 275 | let first_name = if path.is_self() { &tmp } else { path.segments().first()? }; |
276 | let skip_to_mod = path.kind != PathKind::Plain && !path.is_self(); | 276 | let skip_to_mod = path.kind != PathKind::Plain && !path.is_self(); |
277 | for scope in self.scopes.iter().rev() { | 277 | for scope in self.scopes.iter().rev() { |
278 | match scope { | 278 | match scope { |
diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs index e79a91102..38da3132b 100644 --- a/crates/hir_def/src/visibility.rs +++ b/crates/hir_def/src/visibility.rs | |||
@@ -22,8 +22,7 @@ pub enum RawVisibility { | |||
22 | 22 | ||
23 | impl RawVisibility { | 23 | impl RawVisibility { |
24 | pub(crate) const fn private() -> RawVisibility { | 24 | pub(crate) const fn private() -> RawVisibility { |
25 | let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() }; | 25 | RawVisibility::Module(ModPath::from_kind(PathKind::Super(0))) |
26 | RawVisibility::Module(path) | ||
27 | } | 26 | } |
28 | 27 | ||
29 | pub(crate) fn from_ast( | 28 | pub(crate) fn from_ast( |
@@ -59,15 +58,15 @@ impl RawVisibility { | |||
59 | RawVisibility::Module(path) | 58 | RawVisibility::Module(path) |
60 | } | 59 | } |
61 | ast::VisibilityKind::PubCrate => { | 60 | ast::VisibilityKind::PubCrate => { |
62 | let path = ModPath { kind: PathKind::Crate, segments: Vec::new() }; | 61 | let path = ModPath::from_kind(PathKind::Crate); |
63 | RawVisibility::Module(path) | 62 | RawVisibility::Module(path) |
64 | } | 63 | } |
65 | ast::VisibilityKind::PubSuper => { | 64 | ast::VisibilityKind::PubSuper => { |
66 | let path = ModPath { kind: PathKind::Super(1), segments: Vec::new() }; | 65 | let path = ModPath::from_kind(PathKind::Super(1)); |
67 | RawVisibility::Module(path) | 66 | RawVisibility::Module(path) |
68 | } | 67 | } |
69 | ast::VisibilityKind::PubSelf => { | 68 | ast::VisibilityKind::PubSelf => { |
70 | let path = ModPath { kind: PathKind::Plain, segments: Vec::new() }; | 69 | let path = ModPath::from_kind(PathKind::Plain); |
71 | RawVisibility::Module(path) | 70 | RawVisibility::Module(path) |
72 | } | 71 | } |
73 | ast::VisibilityKind::Pub => RawVisibility::Public, | 72 | ast::VisibilityKind::Pub => RawVisibility::Public, |