Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d8ef6c2

Browse files
committed
Cleanup convert_path
1 parent 880baa9 commit d8ef6c2

File tree

4 files changed

+34
-42
lines changed

4 files changed

+34
-42
lines changed

crates/cfg/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl CfgDiff {
131131
/// of both.
132132
pub fn new(enable: Vec<CfgAtom>, disable: Vec<CfgAtom>) -> Option<CfgDiff> {
133133
let mut occupied = FxHashSet::default();
134-
if enable.iter().chain(disable.iter()).any(|item| occupied.insert(item)) {
134+
if enable.iter().chain(disable.iter()).any(|item| !occupied.insert(item)) {
135135
// was present
136136
return None;
137137
}

crates/hir-expand/src/attrs.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ impl Attr {
219219
}
220220

221221
fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree], id: AttrId) -> Option<Attr> {
222-
dbg!(tt);
223222
let span = tt.first()?.first_span();
224223
let path_end = tt
225224
.iter()

crates/hir-expand/src/db.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Defines database & queries for macro expansion.
22
3-
use base_db::{
4-
salsa::{self, debug::DebugQueryTable},
5-
CrateId, FileId, SourceDatabase,
6-
};
3+
use base_db::{salsa, CrateId, FileId, SourceDatabase};
74
use either::Either;
85
use limit::Limit;
96
use mbe::{syntax_node_to_token_tree, ValueResult};

crates/hir-expand/src/mod_path.rs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl ModPath {
5151
path: ast::Path,
5252
span_map: SpanMapRef<'_>,
5353
) -> Option<ModPath> {
54-
convert_path(db, None, path, span_map)
54+
convert_path(db, path, span_map)
5555
}
5656

5757
pub fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree]) -> Option<ModPath> {
@@ -199,22 +199,15 @@ fn display_fmt_path(
199199

200200
fn convert_path(
201201
db: &dyn ExpandDatabase,
202-
prefix: Option<ModPath>,
203202
path: ast::Path,
204203
span_map: SpanMapRef<'_>,
205204
) -> Option<ModPath> {
206-
let prefix = match path.qualifier() {
207-
Some(qual) => Some(convert_path(db, prefix, qual, span_map)?),
208-
None => prefix,
209-
};
205+
let mut segments = path.segments();
210206

211-
let segment = path.segment()?;
207+
let segment = &segments.next()?;
212208
let mut mod_path = match segment.kind()? {
213209
ast::PathSegmentKind::Name(name_ref) => {
214210
if name_ref.text() == "$crate" {
215-
if prefix.is_some() {
216-
return None;
217-
}
218211
ModPath::from_kind(
219212
resolve_crate_root(
220213
db,
@@ -224,48 +217,51 @@ fn convert_path(
224217
.unwrap_or(PathKind::Crate),
225218
)
226219
} else {
227-
let mut res = prefix.unwrap_or_else(|| {
228-
ModPath::from_kind(
229-
segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs),
230-
)
231-
});
220+
let mut res = ModPath::from_kind(
221+
segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs),
222+
);
232223
res.segments.push(name_ref.as_name());
233224
res
234225
}
235226
}
236227
ast::PathSegmentKind::SelfTypeKw => {
237-
if prefix.is_some() {
238-
return None;
239-
}
240228
ModPath::from_segments(PathKind::Plain, Some(known::SELF_TYPE))
241229
}
242-
ast::PathSegmentKind::CrateKw => {
243-
if prefix.is_some() {
244-
return None;
245-
}
246-
ModPath::from_segments(PathKind::Crate, iter::empty())
247-
}
248-
ast::PathSegmentKind::SelfKw => {
249-
if prefix.is_some() {
250-
return None;
251-
}
252-
ModPath::from_segments(PathKind::Super(0), iter::empty())
253-
}
230+
ast::PathSegmentKind::CrateKw => ModPath::from_segments(PathKind::Crate, iter::empty()),
231+
ast::PathSegmentKind::SelfKw => ModPath::from_segments(PathKind::Super(0), iter::empty()),
254232
ast::PathSegmentKind::SuperKw => {
255-
let nested_super_count = match prefix.map(|p| p.kind) {
256-
Some(PathKind::Super(n)) => n,
257-
Some(_) => return None,
258-
None => 0,
259-
};
233+
let mut deg = 1;
234+
let mut next_segment = None;
235+
while let Some(segment) = segments.next() {
236+
match segment.kind()? {
237+
ast::PathSegmentKind::SuperKw => deg += 1,
238+
ast::PathSegmentKind::Name(name) => {
239+
next_segment = Some(name.as_name());
240+
break;
241+
}
242+
ast::PathSegmentKind::Type { .. }
243+
| ast::PathSegmentKind::SelfTypeKw
244+
| ast::PathSegmentKind::SelfKw
245+
| ast::PathSegmentKind::CrateKw => return None,
246+
}
247+
}
260248

261-
ModPath::from_segments(PathKind::Super(nested_super_count + 1), iter::empty())
249+
ModPath::from_segments(PathKind::Super(deg), next_segment)
262250
}
263251
ast::PathSegmentKind::Type { .. } => {
264252
// not allowed in imports
265253
return None;
266254
}
267255
};
268256

257+
for segment in segments {
258+
let name = match segment.kind()? {
259+
ast::PathSegmentKind::Name(name) => name.as_name(),
260+
_ => return None,
261+
};
262+
mod_path.segments.push(name);
263+
}
264+
269265
// handle local_inner_macros :
270266
// Basically, even in rustc it is quite hacky:
271267
// https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456

0 commit comments

Comments
 (0)