Skip to content

Commit f3139d4

Browse files
bors[bot]Veykril
andauthored
Merge #7778
7778: Fix lowering trailing self paths in UseTrees r=Veykril a=Veykril Noticed that hovering over `self` in a use tree like `use foo::bar::{self}` showing documentation and such for the current module instead of `bar`. Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 4a9eec4 + ca7cd41 commit f3139d4

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

crates/hir_def/src/path/lower.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
101101
break;
102102
}
103103
ast::PathSegmentKind::SelfKw => {
104-
kind = PathKind::Super(0);
105-
break;
104+
// don't break out if `self` is the last segment of a path, this mean we got an
105+
// use tree like `foo::{self}` which we want to resolve as `foo`
106+
if !segments.is_empty() {
107+
kind = PathKind::Super(0);
108+
break;
109+
}
106110
}
107111
ast::PathSegmentKind::SuperKw => {
108112
let nested_super_count = if let PathKind::Super(n) = kind { n } else { 0 };
@@ -117,6 +121,11 @@ pub(super) fn lower_path(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path>
117121
segments.reverse();
118122
generic_args.reverse();
119123

124+
if segments.is_empty() && kind == PathKind::Plain && type_anchor.is_none() {
125+
// plain empty paths don't exist, this means we got a single `self` segment as our path
126+
kind = PathKind::Super(0);
127+
}
128+
120129
// handle local_inner_macros :
121130
// Basically, even in rustc it is quite hacky:
122131
// https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456

crates/ide/src/hover.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3496,4 +3496,33 @@ mod foo$0;
34963496
"#]],
34973497
);
34983498
}
3499+
3500+
#[test]
3501+
fn hover_self_in_use() {
3502+
check(
3503+
r#"
3504+
//! This should not appear
3505+
mod foo {
3506+
/// But this should appear
3507+
pub mod bar {}
3508+
}
3509+
use foo::bar::{self$0};
3510+
"#,
3511+
expect![[r#"
3512+
*self*
3513+
3514+
```rust
3515+
test::foo
3516+
```
3517+
3518+
```rust
3519+
pub mod bar
3520+
```
3521+
3522+
---
3523+
3524+
But this should appear
3525+
"#]],
3526+
);
3527+
}
34993528
}

0 commit comments

Comments
 (0)