]> git.lizzy.rs Git - rust.git/blob - crates/hir_expand/src/mod_path.rs
Merge #11291
[rust.git] / crates / hir_expand / src / mod_path.rs
1 //! A lowering for `use`-paths (more generally, paths without angle-bracketed segments).
2
3 use std::{
4     fmt::{self, Display},
5     iter,
6 };
7
8 use crate::{db::AstDatabase, hygiene::Hygiene, name::Name};
9 use base_db::CrateId;
10 use either::Either;
11 use syntax::{ast, AstNode};
12
13 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
14 pub struct ModPath {
15     pub kind: PathKind,
16     segments: Vec<Name>,
17 }
18
19 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
20 pub enum PathKind {
21     Plain,
22     /// `self::` is `Super(0)`
23     Super(u8),
24     Crate,
25     /// Absolute path (::foo)
26     Abs,
27     /// `$crate` from macro expansion
28     DollarCrate(CrateId),
29 }
30
31 impl ModPath {
32     pub fn from_src(db: &dyn AstDatabase, path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
33         convert_path(db, None, path, hygiene)
34     }
35
36     pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath {
37         let segments = segments.into_iter().collect::<Vec<_>>();
38         ModPath { kind, segments }
39     }
40
41     /// Creates a `ModPath` from a `PathKind`, with no extra path segments.
42     pub const fn from_kind(kind: PathKind) -> ModPath {
43         ModPath { kind, segments: Vec::new() }
44     }
45
46     pub fn segments(&self) -> &[Name] {
47         &self.segments
48     }
49
50     pub fn push_segment(&mut self, segment: Name) {
51         self.segments.push(segment);
52     }
53
54     pub fn pop_segment(&mut self) -> Option<Name> {
55         self.segments.pop()
56     }
57
58     /// Returns the number of segments in the path (counting special segments like `$crate` and
59     /// `super`).
60     pub fn len(&self) -> usize {
61         self.segments.len()
62             + match self.kind {
63                 PathKind::Plain => 0,
64                 PathKind::Super(i) => i as usize,
65                 PathKind::Crate => 1,
66                 PathKind::Abs => 0,
67                 PathKind::DollarCrate(_) => 1,
68             }
69     }
70
71     pub fn is_ident(&self) -> bool {
72         self.as_ident().is_some()
73     }
74
75     pub fn is_self(&self) -> bool {
76         self.kind == PathKind::Super(0) && self.segments.is_empty()
77     }
78
79     /// If this path is a single identifier, like `foo`, return its name.
80     pub fn as_ident(&self) -> Option<&Name> {
81         if self.kind != PathKind::Plain {
82             return None;
83         }
84
85         match &*self.segments {
86             [name] => Some(name),
87             _ => None,
88         }
89     }
90 }
91
92 impl Display for ModPath {
93     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94         let mut first_segment = true;
95         let mut add_segment = |s| -> fmt::Result {
96             if !first_segment {
97                 f.write_str("::")?;
98             }
99             first_segment = false;
100             f.write_str(s)?;
101             Ok(())
102         };
103         match self.kind {
104             PathKind::Plain => {}
105             PathKind::Super(0) => add_segment("self")?,
106             PathKind::Super(n) => {
107                 for _ in 0..n {
108                     add_segment("super")?;
109                 }
110             }
111             PathKind::Crate => add_segment("crate")?,
112             PathKind::Abs => add_segment("")?,
113             PathKind::DollarCrate(_) => add_segment("$crate")?,
114         }
115         for segment in &self.segments {
116             if !first_segment {
117                 f.write_str("::")?;
118             }
119             first_segment = false;
120             write!(f, "{}", segment)?;
121         }
122         Ok(())
123     }
124 }
125
126 impl From<Name> for ModPath {
127     fn from(name: Name) -> ModPath {
128         ModPath::from_segments(PathKind::Plain, iter::once(name))
129     }
130 }
131
132 fn convert_path(
133     db: &dyn AstDatabase,
134     prefix: Option<ModPath>,
135     path: ast::Path,
136     hygiene: &Hygiene,
137 ) -> Option<ModPath> {
138     let prefix = match path.qualifier() {
139         Some(qual) => Some(convert_path(db, prefix, qual, hygiene)?),
140         None => prefix,
141     };
142
143     let segment = path.segment()?;
144     let mut mod_path = match segment.kind()? {
145         ast::PathSegmentKind::Name(name_ref) => {
146             match hygiene.name_ref_to_name(db, name_ref) {
147                 Either::Left(name) => {
148                     // no type args in use
149                     let mut res = prefix.unwrap_or_else(|| {
150                         ModPath::from_kind(
151                             segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs),
152                         )
153                     });
154                     res.segments.push(name);
155                     res
156                 }
157                 Either::Right(crate_id) => {
158                     return Some(ModPath::from_segments(
159                         PathKind::DollarCrate(crate_id),
160                         iter::empty(),
161                     ))
162                 }
163             }
164         }
165         ast::PathSegmentKind::CrateKw => {
166             if prefix.is_some() {
167                 return None;
168             }
169             ModPath::from_segments(PathKind::Crate, iter::empty())
170         }
171         ast::PathSegmentKind::SelfKw => {
172             if prefix.is_some() {
173                 return None;
174             }
175             ModPath::from_segments(PathKind::Super(0), iter::empty())
176         }
177         ast::PathSegmentKind::SuperKw => {
178             let nested_super_count = match prefix.map(|p| p.kind) {
179                 Some(PathKind::Super(n)) => n,
180                 Some(_) => return None,
181                 None => 0,
182             };
183
184             ModPath::from_segments(PathKind::Super(nested_super_count + 1), iter::empty())
185         }
186         ast::PathSegmentKind::Type { .. } => {
187             // not allowed in imports
188             return None;
189         }
190     };
191
192     // handle local_inner_macros :
193     // Basically, even in rustc it is quite hacky:
194     // https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456
195     // We follow what it did anyway :)
196     if mod_path.segments.len() == 1 && mod_path.kind == PathKind::Plain {
197         if let Some(_macro_call) = path.syntax().parent().and_then(ast::MacroCall::cast) {
198             if let Some(crate_id) = hygiene.local_inner_macros(db, path) {
199                 mod_path.kind = PathKind::DollarCrate(crate_id);
200             }
201         }
202     }
203
204     Some(mod_path)
205 }
206
207 pub use crate::name as __name;
208
209 #[macro_export]
210 macro_rules! __known_path {
211     (core::iter::IntoIterator) => {};
212     (core::iter::Iterator) => {};
213     (core::result::Result) => {};
214     (core::option::Option) => {};
215     (core::ops::Range) => {};
216     (core::ops::RangeFrom) => {};
217     (core::ops::RangeFull) => {};
218     (core::ops::RangeTo) => {};
219     (core::ops::RangeToInclusive) => {};
220     (core::ops::RangeInclusive) => {};
221     (core::future::Future) => {};
222     (core::ops::Try) => {};
223     ($path:path) => {
224         compile_error!("Please register your known path in the path module")
225     };
226 }
227
228 #[macro_export]
229 macro_rules! __path {
230     ($start:ident $(:: $seg:ident)*) => ({
231         $crate::__known_path!($start $(:: $seg)*);
232         $crate::mod_path::ModPath::from_segments($crate::mod_path::PathKind::Abs, vec![
233             $crate::mod_path::__name![$start], $($crate::mod_path::__name![$seg],)*
234         ])
235     });
236 }
237
238 pub use crate::__path as path;