Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle self::super when lowering UseTree #16919

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 22 additions & 20 deletions crates/hir-expand/src/mod_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,26 @@ fn convert_path(
let mut segments = path.segments();

let segment = &segments.next()?;
let handle_super_kw = &mut |init_deg| {
let mut deg = init_deg;
let mut next_segment = None;
for segment in segments.by_ref() {
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,
}
}

Some(ModPath::from_segments(PathKind::Super(deg), next_segment))
};

let mut mod_path = match segment.kind()? {
ast::PathSegmentKind::Name(name_ref) => {
if name_ref.text() == "$crate" {
Expand All @@ -245,26 +265,8 @@ fn convert_path(
ModPath::from_segments(PathKind::Plain, Some(known::SELF_TYPE))
}
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 mut deg = 1;
let mut next_segment = None;
for segment in segments.by_ref() {
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(deg), next_segment)
}
ast::PathSegmentKind::SelfKw => handle_super_kw(0)?,
ast::PathSegmentKind::SuperKw => handle_super_kw(1)?,
ast::PathSegmentKind::Type { .. } => {
// not allowed in imports
return None;
Expand Down