Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #21397 from Shourya742/2026-01-03-fix-source-text
Fix source text
Lukas Wirth 4 months ago
parent 0b0d299 · parent 41f4245 · commit 13d71e0
-rw-r--r--crates/hir-def/src/macro_expansion_tests/mod.rs4
-rw-r--r--crates/hir-expand/src/proc_macro.rs4
-rw-r--r--crates/load-cargo/src/lib.rs53
-rw-r--r--crates/proc-macro-api/src/bidirectional_protocol/msg.rs2
-rw-r--r--crates/proc-macro-srv-cli/src/main_loop.rs5
-rw-r--r--crates/proc-macro-srv/src/lib.rs2
-rw-r--r--crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs8
-rw-r--r--crates/span/src/hygiene.rs3
-rw-r--r--crates/test-fixture/src/lib.rs20
9 files changed, 59 insertions, 42 deletions
diff --git a/crates/hir-def/src/macro_expansion_tests/mod.rs b/crates/hir-def/src/macro_expansion_tests/mod.rs
index 3f136bc591..c63f2c1d78 100644
--- a/crates/hir-def/src/macro_expansion_tests/mod.rs
+++ b/crates/hir-def/src/macro_expansion_tests/mod.rs
@@ -16,7 +16,7 @@ mod proc_macros;
use std::{any::TypeId, iter, ops::Range, sync};
-use base_db::{RootQueryDb, SourceDatabase};
+use base_db::RootQueryDb;
use expect_test::Expect;
use hir_expand::{
AstId, ExpansionInfo, InFile, MacroCallId, MacroCallKind, MacroKind,
@@ -387,7 +387,7 @@ struct IdentityWhenValidProcMacroExpander;
impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
subtree: &TopSubtree,
_: Option<&TopSubtree>,
_: &base_db::Env,
diff --git a/crates/hir-expand/src/proc_macro.rs b/crates/hir-expand/src/proc_macro.rs
index d2614aa5f1..467eae3122 100644
--- a/crates/hir-expand/src/proc_macro.rs
+++ b/crates/hir-expand/src/proc_macro.rs
@@ -4,7 +4,7 @@ use core::fmt;
use std::any::Any;
use std::{panic::RefUnwindSafe, sync};
-use base_db::{Crate, CrateBuilderId, CratesIdMap, Env, ProcMacroLoadingError, SourceDatabase};
+use base_db::{Crate, CrateBuilderId, CratesIdMap, Env, ProcMacroLoadingError};
use intern::Symbol;
use rustc_hash::FxHashMap;
use span::Span;
@@ -25,7 +25,7 @@ pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe + Any {
/// [`ProcMacroKind::Attr`]), environment variables, and span information.
fn expand(
&self,
- db: &dyn SourceDatabase,
+ db: &dyn ExpandDatabase,
subtree: &tt::TopSubtree,
attrs: Option<&tt::TopSubtree>,
env: &Env,
diff --git a/crates/load-cargo/src/lib.rs b/crates/load-cargo/src/lib.rs
index e01ce0b129..c302e266fe 100644
--- a/crates/load-cargo/src/lib.rs
+++ b/crates/load-cargo/src/lib.rs
@@ -11,15 +11,16 @@ extern crate rustc_driver as _;
use std::{any::Any, collections::hash_map::Entry, mem, path::Path, sync};
use crossbeam_channel::{Receiver, unbounded};
-use hir_expand::proc_macro::{
- ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind, ProcMacroLoadResult,
- ProcMacrosBuilder,
+use hir_expand::{
+ db::ExpandDatabase,
+ proc_macro::{
+ ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind, ProcMacroLoadResult,
+ ProcMacrosBuilder,
+ },
};
use ide_db::{
- ChangeWithProcMacros, FxHashMap, RootDatabase,
- base_db::{
- CrateGraphBuilder, Env, ProcMacroLoadingError, SourceDatabase, SourceRoot, SourceRootId,
- },
+ ChangeWithProcMacros, EditionedFileId, FxHashMap, RootDatabase,
+ base_db::{CrateGraphBuilder, Env, ProcMacroLoadingError, SourceRoot, SourceRootId},
prime_caches,
};
use itertools::Itertools;
@@ -530,7 +531,7 @@ struct Expander(proc_macro_api::ProcMacro);
impl ProcMacroExpander for Expander {
fn expand(
&self,
- db: &dyn SourceDatabase,
+ db: &dyn ExpandDatabase,
subtree: &tt::TopSubtree,
attrs: Option<&tt::TopSubtree>,
env: &Env,
@@ -541,30 +542,40 @@ impl ProcMacroExpander for Expander {
) -> Result<tt::TopSubtree, ProcMacroExpansionError> {
let mut cb = |req| match req {
SubRequest::LocalFilePath { file_id } => {
- let file = FileId::from_raw(file_id);
- let source_root_id = db.file_source_root(file).source_root_id(db);
+ let file_id = FileId::from_raw(file_id);
+ let source_root_id = db.file_source_root(file_id).source_root_id(db);
let source_root = db.source_root(source_root_id).source_root(db);
-
let name = source_root
- .path_for_file(&file)
+ .path_for_file(&file_id)
.and_then(|path| path.as_path())
.map(|path| path.to_string());
Ok(SubResponse::LocalFilePathResult { name })
}
- SubRequest::SourceText { file_id, start, end } => {
- let file = FileId::from_raw(file_id);
- let text = db.file_text(file).text(db);
- let slice = text.get(start as usize..end as usize).map(ToOwned::to_owned);
- Ok(SubResponse::SourceTextResult { text: slice })
+ SubRequest::SourceText { file_id, ast_id, start, end } => {
+ let raw_file_id = FileId::from_raw(file_id);
+ let editioned_file_id = span::EditionedFileId::from_raw(file_id);
+ let ast_id = span::ErasedFileAstId::from_raw(ast_id);
+ let hir_file_id = EditionedFileId::from_span_guess_origin(db, editioned_file_id);
+ let anchor_offset = db
+ .ast_id_map(hir_expand::HirFileId::FileId(hir_file_id))
+ .get_erased(ast_id)
+ .text_range()
+ .start();
+ let anchor_offset = u32::from(anchor_offset);
+ let abs_start = start + anchor_offset;
+ let abs_end = end + anchor_offset;
+ let source = db.file_text(raw_file_id).text(db);
+ let text = source.get(abs_start as usize..abs_end as usize).map(ToOwned::to_owned);
+
+ Ok(SubResponse::SourceTextResult { text })
}
SubRequest::FilePath { file_id } => {
- let file = FileId::from_raw(file_id);
- let source_root_id = db.file_source_root(file).source_root_id(db);
+ let file_id = FileId::from_raw(file_id);
+ let source_root_id = db.file_source_root(file_id).source_root_id(db);
let source_root = db.source_root(source_root_id).source_root(db);
-
let name = source_root
- .path_for_file(&file)
+ .path_for_file(&file_id)
.and_then(|path| path.as_path())
.map(|path| path.to_string())
.unwrap_or_default();
diff --git a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
index 558954f761..e41f8a5d7d 100644
--- a/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
+++ b/crates/proc-macro-api/src/bidirectional_protocol/msg.rs
@@ -11,7 +11,7 @@ use crate::{
#[derive(Debug, Serialize, Deserialize)]
pub enum SubRequest {
FilePath { file_id: u32 },
- SourceText { file_id: u32, start: u32, end: u32 },
+ SourceText { file_id: u32, ast_id: u32, start: u32, end: u32 },
LocalFilePath { file_id: u32 },
}
diff --git a/crates/proc-macro-srv-cli/src/main_loop.rs b/crates/proc-macro-srv-cli/src/main_loop.rs
index 8fe3e93e47..4891e07314 100644
--- a/crates/proc-macro-srv-cli/src/main_loop.rs
+++ b/crates/proc-macro-srv-cli/src/main_loop.rs
@@ -194,8 +194,9 @@ impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandl
}
}
- fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String> {
- match self.roundtrip(bidirectional::SubRequest::SourceText { file_id, start, end }) {
+ fn source_text(&mut self, file_id: u32, ast_id: u32, start: u32, end: u32) -> Option<String> {
+ match self.roundtrip(bidirectional::SubRequest::SourceText { file_id, ast_id, start, end })
+ {
Some(bidirectional::BidirectionalMessage::SubResponse(
bidirectional::SubResponse::SourceTextResult { text },
)) => text,
diff --git a/crates/proc-macro-srv/src/lib.rs b/crates/proc-macro-srv/src/lib.rs
index 8de712dbd3..d63aea947c 100644
--- a/crates/proc-macro-srv/src/lib.rs
+++ b/crates/proc-macro-srv/src/lib.rs
@@ -95,7 +95,7 @@ pub type ProcMacroClientHandle<'a> = &'a mut (dyn ProcMacroClientInterface + Syn
pub trait ProcMacroClientInterface {
fn file(&mut self, file_id: u32) -> String;
- fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String>;
+ fn source_text(&mut self, file_id: u32, ast_id: u32, start: u32, end: u32) -> Option<String>;
fn local_file(&mut self, file_id: u32) -> Option<String>;
}
diff --git a/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs b/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
index 7a9d655431..2ce3b717cb 100644
--- a/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
+++ b/crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
@@ -154,10 +154,16 @@ impl server::Span for RaSpanServer<'_> {
/// https://github.com/rust-lang/rust/pull/55780
fn source_text(&mut self, span: Self::Span) -> Option<String> {
let file_id = span.anchor.file_id;
+ let ast_id = span.anchor.ast_id;
let start: u32 = span.range.start().into();
let end: u32 = span.range.end().into();
- self.callback.as_mut()?.source_text(file_id.file_id().index(), start, end)
+ self.callback.as_mut()?.source_text(
+ file_id.file_id().index(),
+ ast_id.into_raw(),
+ start,
+ end,
+ )
}
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {
diff --git a/crates/span/src/hygiene.rs b/crates/span/src/hygiene.rs
index 6805417177..ea4f4c5efb 100644
--- a/crates/span/src/hygiene.rs
+++ b/crates/span/src/hygiene.rs
@@ -19,9 +19,8 @@
//! # The Call-site Hierarchy
//!
//! `ExpnData::call_site` in rustc, [`MacroCallLoc::call_site`] in rust-analyzer.
-use std::fmt;
-
use crate::Edition;
+use std::fmt;
/// A syntax context describes a hierarchy tracking order of macro definitions.
#[cfg(feature = "salsa")]
diff --git a/crates/test-fixture/src/lib.rs b/crates/test-fixture/src/lib.rs
index b9c389c769..d81f27d7c3 100644
--- a/crates/test-fixture/src/lib.rs
+++ b/crates/test-fixture/src/lib.rs
@@ -738,7 +738,7 @@ struct IdentityProcMacroExpander;
impl ProcMacroExpander for IdentityProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
subtree: &TopSubtree,
_: Option<&TopSubtree>,
_: &Env,
@@ -761,7 +761,7 @@ struct Issue18089ProcMacroExpander;
impl ProcMacroExpander for Issue18089ProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
subtree: &TopSubtree,
_: Option<&TopSubtree>,
_: &Env,
@@ -797,7 +797,7 @@ struct AttributeInputReplaceProcMacroExpander;
impl ProcMacroExpander for AttributeInputReplaceProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
_: &TopSubtree,
attrs: Option<&TopSubtree>,
_: &Env,
@@ -821,7 +821,7 @@ struct Issue18840ProcMacroExpander;
impl ProcMacroExpander for Issue18840ProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
fn_: &TopSubtree,
_: Option<&TopSubtree>,
_: &Env,
@@ -858,7 +858,7 @@ struct MirrorProcMacroExpander;
impl ProcMacroExpander for MirrorProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
input: &TopSubtree,
_: Option<&TopSubtree>,
_: &Env,
@@ -897,7 +897,7 @@ struct ShortenProcMacroExpander;
impl ProcMacroExpander for ShortenProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
input: &TopSubtree,
_: Option<&TopSubtree>,
_: &Env,
@@ -942,7 +942,7 @@ struct Issue17479ProcMacroExpander;
impl ProcMacroExpander for Issue17479ProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
subtree: &TopSubtree,
_: Option<&TopSubtree>,
_: &Env,
@@ -973,7 +973,7 @@ struct Issue18898ProcMacroExpander;
impl ProcMacroExpander for Issue18898ProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
subtree: &TopSubtree,
_: Option<&TopSubtree>,
_: &Env,
@@ -1027,7 +1027,7 @@ struct DisallowCfgProcMacroExpander;
impl ProcMacroExpander for DisallowCfgProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
subtree: &TopSubtree,
_: Option<&TopSubtree>,
_: &Env,
@@ -1059,7 +1059,7 @@ struct GenerateSuffixedTypeProcMacroExpander;
impl ProcMacroExpander for GenerateSuffixedTypeProcMacroExpander {
fn expand(
&self,
- _: &dyn SourceDatabase,
+ _: &dyn ExpandDatabase,
subtree: &TopSubtree,
_attrs: Option<&TopSubtree>,
_env: &Env,