diff options
| author | Lukas Wirth <lukastw97@gmail.com> | 2024-01-25 10:23:00 +0100 |
|---|---|---|
| committer | Lukas Wirth <lukastw97@gmail.com> | 2024-01-26 19:28:39 +0100 |
| commit | d8ef6c24cc4e2aa594dcbd561f33fb995278cf17 (patch) | |
| tree | 48f46bb0ae246b3773ba267fb3cf497d22df3f82 | |
| parent | 880baa9e568c5aff70a49f02e517fce075e40b3f (diff) | |
| download | rust-d8ef6c24cc4e2aa594dcbd561f33fb995278cf17.tar.gz rust-d8ef6c24cc4e2aa594dcbd561f33fb995278cf17.zip | |
Cleanup `convert_path`
| -rw-r--r-- | crates/cfg/src/lib.rs | 2 | ||||
| -rw-r--r-- | crates/hir-expand/src/attrs.rs | 1 | ||||
| -rw-r--r-- | crates/hir-expand/src/db.rs | 5 | ||||
| -rw-r--r-- | crates/hir-expand/src/mod_path.rs | 68 |
4 files changed, 34 insertions, 42 deletions
diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs index 3b41de14335..454d6fc5384 100644 --- a/crates/cfg/src/lib.rs +++ b/crates/cfg/src/lib.rs @@ -131,7 +131,7 @@ impl CfgDiff { /// of both. pub fn new(enable: Vec<CfgAtom>, disable: Vec<CfgAtom>) -> Option<CfgDiff> { let mut occupied = FxHashSet::default(); - if enable.iter().chain(disable.iter()).any(|item| occupied.insert(item)) { + if enable.iter().chain(disable.iter()).any(|item| !occupied.insert(item)) { // was present return None; } diff --git a/crates/hir-expand/src/attrs.rs b/crates/hir-expand/src/attrs.rs index b7c50fd82aa..30d38299d99 100644 --- a/crates/hir-expand/src/attrs.rs +++ b/crates/hir-expand/src/attrs.rs @@ -219,7 +219,6 @@ impl Attr { } fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree], id: AttrId) -> Option<Attr> { - dbg!(tt); let span = tt.first()?.first_span(); let path_end = tt .iter() diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 08542c6430a..8c43017971f 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -1,9 +1,6 @@ //! Defines database & queries for macro expansion. -use base_db::{ - salsa::{self, debug::DebugQueryTable}, - CrateId, FileId, SourceDatabase, -}; +use base_db::{salsa, CrateId, FileId, SourceDatabase}; use either::Either; use limit::Limit; use mbe::{syntax_node_to_token_tree, ValueResult}; diff --git a/crates/hir-expand/src/mod_path.rs b/crates/hir-expand/src/mod_path.rs index 01ccebea201..0eb1fc1eb50 100644 --- a/crates/hir-expand/src/mod_path.rs +++ b/crates/hir-expand/src/mod_path.rs @@ -51,7 +51,7 @@ impl ModPath { path: ast::Path, span_map: SpanMapRef<'_>, ) -> Option<ModPath> { - convert_path(db, None, path, span_map) + convert_path(db, path, span_map) } pub fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree]) -> Option<ModPath> { @@ -199,22 +199,15 @@ fn display_fmt_path( fn convert_path( db: &dyn ExpandDatabase, - prefix: Option<ModPath>, path: ast::Path, span_map: SpanMapRef<'_>, ) -> Option<ModPath> { - let prefix = match path.qualifier() { - Some(qual) => Some(convert_path(db, prefix, qual, span_map)?), - None => prefix, - }; + let mut segments = path.segments(); - let segment = path.segment()?; + let segment = &segments.next()?; let mut mod_path = match segment.kind()? { ast::PathSegmentKind::Name(name_ref) => { if name_ref.text() == "$crate" { - if prefix.is_some() { - return None; - } ModPath::from_kind( resolve_crate_root( db, @@ -224,41 +217,36 @@ fn convert_path( .unwrap_or(PathKind::Crate), ) } else { - let mut res = prefix.unwrap_or_else(|| { - ModPath::from_kind( - segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs), - ) - }); + let mut res = ModPath::from_kind( + segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs), + ); res.segments.push(name_ref.as_name()); res } } ast::PathSegmentKind::SelfTypeKw => { - if prefix.is_some() { - return None; - } ModPath::from_segments(PathKind::Plain, Some(known::SELF_TYPE)) } - ast::PathSegmentKind::CrateKw => { - if prefix.is_some() { - return None; - } - ModPath::from_segments(PathKind::Crate, iter::empty()) - } - ast::PathSegmentKind::SelfKw => { - if prefix.is_some() { - return None; - } - ModPath::from_segments(PathKind::Super(0), iter::empty()) - } + ast::PathSegmentKind::CrateKw => ModPath::from_segments(PathKind::Crate, iter::empty()), + ast::PathSegmentKind::SelfKw => ModPath::from_segments(PathKind::Super(0), iter::empty()), ast::PathSegmentKind::SuperKw => { - let nested_super_count = match prefix.map(|p| p.kind) { - Some(PathKind::Super(n)) => n, - Some(_) => return None, - None => 0, - }; + let mut deg = 1; + let mut next_segment = None; + while let Some(segment) = segments.next() { + match segment.kind()? { + ast::PathSegmentKind::SuperKw => deg += 1, + ast::PathSegmentKind::Name(name) => { + next_segment = Some(name.as_name()); + break; + } + ast::PathSegmentKind::Type { .. } + | ast::PathSegmentKind::SelfTypeKw + | ast::PathSegmentKind::SelfKw + | ast::PathSegmentKind::CrateKw => return None, + } + } - ModPath::from_segments(PathKind::Super(nested_super_count + 1), iter::empty()) + ModPath::from_segments(PathKind::Super(deg), next_segment) } ast::PathSegmentKind::Type { .. } => { // not allowed in imports @@ -266,6 +254,14 @@ fn convert_path( } }; + for segment in segments { + let name = match segment.kind()? { + ast::PathSegmentKind::Name(name) => name.as_name(), + _ => return None, + }; + mod_path.segments.push(name); + } + // handle local_inner_macros : // Basically, even in rustc it is quite hacky: // https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456 |
