Skip to content

fix: Prettify AST in PathTransform if it's coming from a macro #20103

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

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2199,6 +2199,10 @@ pub struct SemanticsScope<'db> {
}

impl<'db> SemanticsScope<'db> {
pub fn file_id(&self) -> HirFileId {
self.file_id
}

pub fn module(&self) -> Module {
Module { id: self.resolver.module() }
}
Expand Down
27 changes: 27 additions & 0 deletions crates/ide-completion/src/tests/item_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,30 @@ fn inside_extern_blocks() {
"#]],
)
}

#[test]
fn tokens_from_macro() {
check_edit(
"fn as_ref",
r#"
//- proc_macros: identity
//- minicore: as_ref
struct Foo;

#[proc_macros::identity]
impl<'a> AsRef<&'a i32> for Foo {
$0
}
"#,
r#"
struct Foo;

#[proc_macros::identity]
impl<'a> AsRef<&'a i32> for Foo {
fn as_ref(&self) -> &&'a i32 {
$0
}
}
"#,
);
}
31 changes: 27 additions & 4 deletions crates/ide-db/src/path_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

use crate::helpers::mod_path_to_ast;
use either::Either;
use hir::{AsAssocItem, HirDisplay, ImportPathConfig, ModuleDef, SemanticsScope};
use hir::{
AsAssocItem, HirDisplay, HirFileId, ImportPathConfig, ModuleDef, SemanticsScope,
prettify_macro_expansion,
};
use itertools::Itertools;
use rustc_hash::FxHashMap;
use span::Edition;
Expand Down Expand Up @@ -136,6 +139,25 @@ impl<'a> PathTransform<'a> {
}
}

fn prettify_target_node(&self, node: SyntaxNode) -> SyntaxNode {
match self.target_scope.file_id() {
HirFileId::FileId(_) => node,
HirFileId::MacroFile(file_id) => {
let db = self.target_scope.db;
prettify_macro_expansion(
db,
node,
&db.expansion_span_map(file_id),
self.target_scope.module().krate().into(),
)
}
}
}

fn prettify_target_ast<N: AstNode>(&self, node: N) -> N {
N::cast(self.prettify_target_node(node.syntax().clone())).unwrap()
}

fn build_ctx(&self) -> Ctx<'a> {
let db = self.source_scope.db;
let target_module = self.target_scope.module();
Expand Down Expand Up @@ -163,7 +185,7 @@ impl<'a> PathTransform<'a> {
.for_each(|(k, v)| match (k.split(db), v) {
(Either::Right(k), Some(TypeOrConst::Either(v))) => {
if let Some(ty) = v.ty() {
type_substs.insert(k, ty);
type_substs.insert(k, self.prettify_target_ast(ty));
}
}
(Either::Right(k), None) => {
Expand All @@ -178,7 +200,7 @@ impl<'a> PathTransform<'a> {
}
(Either::Left(k), Some(TypeOrConst::Either(v))) => {
if let Some(ty) = v.ty() {
const_substs.insert(k, ty.syntax().clone());
const_substs.insert(k, self.prettify_target_node(ty.syntax().clone()));
}
}
(Either::Left(k), Some(TypeOrConst::Const(v))) => {
Expand All @@ -189,7 +211,7 @@ impl<'a> PathTransform<'a> {
// and sometimes require slight modifications; see
// https://doc.rust-lang.org/reference/statements.html#expression-statements
// (default values in curly brackets can cause the same problem)
const_substs.insert(k, expr.syntax().clone());
const_substs.insert(k, self.prettify_target_node(expr.syntax().clone()));
}
}
(Either::Left(k), None) => {
Expand All @@ -204,6 +226,7 @@ impl<'a> PathTransform<'a> {
}
_ => (), // ignore mismatching params
});
// No need to prettify lifetimes, there's nothing to prettify.
let lifetime_substs: FxHashMap<_, _> = self
.generic_def
.into_iter()
Expand Down