1 //! A lowering for `use`-paths (more generally, paths without angle-bracketed segments).
8 use crate::{db::AstDatabase, hygiene::Hygiene, name::Name};
11 use syntax::{ast, AstNode};
13 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
19 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
22 /// `self::` is `Super(0)`
25 /// Absolute path (::foo)
27 /// `$crate` from macro expansion
32 pub fn from_src(db: &dyn AstDatabase, path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
33 convert_path(db, None, path, hygiene)
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 }
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() }
46 pub fn segments(&self) -> &[Name] {
50 pub fn push_segment(&mut self, segment: Name) {
51 self.segments.push(segment);
54 pub fn pop_segment(&mut self) -> Option<Name> {
58 /// Returns the number of segments in the path (counting special segments like `$crate` and
60 pub fn len(&self) -> usize {
64 PathKind::Super(i) => i as usize,
67 PathKind::DollarCrate(_) => 1,
71 pub fn is_ident(&self) -> bool {
72 self.as_ident().is_some()
75 pub fn is_self(&self) -> bool {
76 self.kind == PathKind::Super(0) && self.segments.is_empty()
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 {
85 match &*self.segments {
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 {
99 first_segment = false;
104 PathKind::Plain => {}
105 PathKind::Super(0) => add_segment("self")?,
106 PathKind::Super(n) => {
108 add_segment("super")?;
111 PathKind::Crate => add_segment("crate")?,
112 PathKind::Abs => add_segment("")?,
113 PathKind::DollarCrate(_) => add_segment("$crate")?,
115 for segment in &self.segments {
119 first_segment = false;
120 write!(f, "{}", segment)?;
126 impl From<Name> for ModPath {
127 fn from(name: Name) -> ModPath {
128 ModPath::from_segments(PathKind::Plain, iter::once(name))
133 db: &dyn AstDatabase,
134 prefix: Option<ModPath>,
137 ) -> Option<ModPath> {
138 let prefix = match path.qualifier() {
139 Some(qual) => Some(convert_path(db, prefix, qual, hygiene)?),
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(|| {
151 segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs),
154 res.segments.push(name);
157 Either::Right(crate_id) => {
158 return Some(ModPath::from_segments(
159 PathKind::DollarCrate(crate_id),
165 ast::PathSegmentKind::CrateKw => {
166 if prefix.is_some() {
169 ModPath::from_segments(PathKind::Crate, iter::empty())
171 ast::PathSegmentKind::SelfKw => {
172 if prefix.is_some() {
175 ModPath::from_segments(PathKind::Super(0), iter::empty())
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,
184 ModPath::from_segments(PathKind::Super(nested_super_count + 1), iter::empty())
186 ast::PathSegmentKind::Type { .. } => {
187 // not allowed in imports
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);
207 pub use crate::name as __name;
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) => {};
224 compile_error!("Please register your known path in the path module")
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],)*
238 pub use crate::__path as path;