Skip to content

Commit 13d71e0

Browse files
authored
Merge pull request #21397 from Shourya742/2026-01-03-fix-source-text
Fix source text
2 parents 0b0d299 + 41f4245 commit 13d71e0

9 files changed

Lines changed: 59 additions & 42 deletions

File tree

crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod proc_macros;
1616

1717
use std::{any::TypeId, iter, ops::Range, sync};
1818

19-
use base_db::{RootQueryDb, SourceDatabase};
19+
use base_db::RootQueryDb;
2020
use expect_test::Expect;
2121
use hir_expand::{
2222
AstId, ExpansionInfo, InFile, MacroCallId, MacroCallKind, MacroKind,
@@ -387,7 +387,7 @@ struct IdentityWhenValidProcMacroExpander;
387387
impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
388388
fn expand(
389389
&self,
390-
_: &dyn SourceDatabase,
390+
_: &dyn ExpandDatabase,
391391
subtree: &TopSubtree,
392392
_: Option<&TopSubtree>,
393393
_: &base_db::Env,

crates/hir-expand/src/proc_macro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::fmt;
44
use std::any::Any;
55
use std::{panic::RefUnwindSafe, sync};
66

7-
use base_db::{Crate, CrateBuilderId, CratesIdMap, Env, ProcMacroLoadingError, SourceDatabase};
7+
use base_db::{Crate, CrateBuilderId, CratesIdMap, Env, ProcMacroLoadingError};
88
use intern::Symbol;
99
use rustc_hash::FxHashMap;
1010
use span::Span;
@@ -25,7 +25,7 @@ pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe + Any {
2525
/// [`ProcMacroKind::Attr`]), environment variables, and span information.
2626
fn expand(
2727
&self,
28-
db: &dyn SourceDatabase,
28+
db: &dyn ExpandDatabase,
2929
subtree: &tt::TopSubtree,
3030
attrs: Option<&tt::TopSubtree>,
3131
env: &Env,

crates/load-cargo/src/lib.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ extern crate rustc_driver as _;
1111
use std::{any::Any, collections::hash_map::Entry, mem, path::Path, sync};
1212

1313
use crossbeam_channel::{Receiver, unbounded};
14-
use hir_expand::proc_macro::{
15-
ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind, ProcMacroLoadResult,
16-
ProcMacrosBuilder,
14+
use hir_expand::{
15+
db::ExpandDatabase,
16+
proc_macro::{
17+
ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind, ProcMacroLoadResult,
18+
ProcMacrosBuilder,
19+
},
1720
};
1821
use ide_db::{
19-
ChangeWithProcMacros, FxHashMap, RootDatabase,
20-
base_db::{
21-
CrateGraphBuilder, Env, ProcMacroLoadingError, SourceDatabase, SourceRoot, SourceRootId,
22-
},
22+
ChangeWithProcMacros, EditionedFileId, FxHashMap, RootDatabase,
23+
base_db::{CrateGraphBuilder, Env, ProcMacroLoadingError, SourceRoot, SourceRootId},
2324
prime_caches,
2425
};
2526
use itertools::Itertools;
@@ -530,7 +531,7 @@ struct Expander(proc_macro_api::ProcMacro);
530531
impl ProcMacroExpander for Expander {
531532
fn expand(
532533
&self,
533-
db: &dyn SourceDatabase,
534+
db: &dyn ExpandDatabase,
534535
subtree: &tt::TopSubtree,
535536
attrs: Option<&tt::TopSubtree>,
536537
env: &Env,
@@ -541,30 +542,40 @@ impl ProcMacroExpander for Expander {
541542
) -> Result<tt::TopSubtree, ProcMacroExpansionError> {
542543
let mut cb = |req| match req {
543544
SubRequest::LocalFilePath { file_id } => {
544-
let file = FileId::from_raw(file_id);
545-
let source_root_id = db.file_source_root(file).source_root_id(db);
545+
let file_id = FileId::from_raw(file_id);
546+
let source_root_id = db.file_source_root(file_id).source_root_id(db);
546547
let source_root = db.source_root(source_root_id).source_root(db);
547-
548548
let name = source_root
549-
.path_for_file(&file)
549+
.path_for_file(&file_id)
550550
.and_then(|path| path.as_path())
551551
.map(|path| path.to_string());
552552

553553
Ok(SubResponse::LocalFilePathResult { name })
554554
}
555-
SubRequest::SourceText { file_id, start, end } => {
556-
let file = FileId::from_raw(file_id);
557-
let text = db.file_text(file).text(db);
558-
let slice = text.get(start as usize..end as usize).map(ToOwned::to_owned);
559-
Ok(SubResponse::SourceTextResult { text: slice })
555+
SubRequest::SourceText { file_id, ast_id, start, end } => {
556+
let raw_file_id = FileId::from_raw(file_id);
557+
let editioned_file_id = span::EditionedFileId::from_raw(file_id);
558+
let ast_id = span::ErasedFileAstId::from_raw(ast_id);
559+
let hir_file_id = EditionedFileId::from_span_guess_origin(db, editioned_file_id);
560+
let anchor_offset = db
561+
.ast_id_map(hir_expand::HirFileId::FileId(hir_file_id))
562+
.get_erased(ast_id)
563+
.text_range()
564+
.start();
565+
let anchor_offset = u32::from(anchor_offset);
566+
let abs_start = start + anchor_offset;
567+
let abs_end = end + anchor_offset;
568+
let source = db.file_text(raw_file_id).text(db);
569+
let text = source.get(abs_start as usize..abs_end as usize).map(ToOwned::to_owned);
570+
571+
Ok(SubResponse::SourceTextResult { text })
560572
}
561573
SubRequest::FilePath { file_id } => {
562-
let file = FileId::from_raw(file_id);
563-
let source_root_id = db.file_source_root(file).source_root_id(db);
574+
let file_id = FileId::from_raw(file_id);
575+
let source_root_id = db.file_source_root(file_id).source_root_id(db);
564576
let source_root = db.source_root(source_root_id).source_root(db);
565-
566577
let name = source_root
567-
.path_for_file(&file)
578+
.path_for_file(&file_id)
568579
.and_then(|path| path.as_path())
569580
.map(|path| path.to_string())
570581
.unwrap_or_default();

crates/proc-macro-api/src/bidirectional_protocol/msg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
#[derive(Debug, Serialize, Deserialize)]
1212
pub enum SubRequest {
1313
FilePath { file_id: u32 },
14-
SourceText { file_id: u32, start: u32, end: u32 },
14+
SourceText { file_id: u32, ast_id: u32, start: u32, end: u32 },
1515
LocalFilePath { file_id: u32 },
1616
}
1717

crates/proc-macro-srv-cli/src/main_loop.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,9 @@ impl<C: Codec> proc_macro_srv::ProcMacroClientInterface for ProcMacroClientHandl
194194
}
195195
}
196196

197-
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String> {
198-
match self.roundtrip(bidirectional::SubRequest::SourceText { file_id, start, end }) {
197+
fn source_text(&mut self, file_id: u32, ast_id: u32, start: u32, end: u32) -> Option<String> {
198+
match self.roundtrip(bidirectional::SubRequest::SourceText { file_id, ast_id, start, end })
199+
{
199200
Some(bidirectional::BidirectionalMessage::SubResponse(
200201
bidirectional::SubResponse::SourceTextResult { text },
201202
)) => text,

crates/proc-macro-srv/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub type ProcMacroClientHandle<'a> = &'a mut (dyn ProcMacroClientInterface + Syn
9595

9696
pub trait ProcMacroClientInterface {
9797
fn file(&mut self, file_id: u32) -> String;
98-
fn source_text(&mut self, file_id: u32, start: u32, end: u32) -> Option<String>;
98+
fn source_text(&mut self, file_id: u32, ast_id: u32, start: u32, end: u32) -> Option<String>;
9999
fn local_file(&mut self, file_id: u32) -> Option<String>;
100100
}
101101

crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,16 @@ impl server::Span for RaSpanServer<'_> {
154154
/// https://github.com/rust-lang/rust/pull/55780
155155
fn source_text(&mut self, span: Self::Span) -> Option<String> {
156156
let file_id = span.anchor.file_id;
157+
let ast_id = span.anchor.ast_id;
157158
let start: u32 = span.range.start().into();
158159
let end: u32 = span.range.end().into();
159160

160-
self.callback.as_mut()?.source_text(file_id.file_id().index(), start, end)
161+
self.callback.as_mut()?.source_text(
162+
file_id.file_id().index(),
163+
ast_id.into_raw(),
164+
start,
165+
end,
166+
)
161167
}
162168

163169
fn parent(&mut self, _span: Self::Span) -> Option<Self::Span> {

crates/span/src/hygiene.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
//! # The Call-site Hierarchy
2020
//!
2121
//! `ExpnData::call_site` in rustc, [`MacroCallLoc::call_site`] in rust-analyzer.
22-
use std::fmt;
23-
2422
use crate::Edition;
23+
use std::fmt;
2524

2625
/// A syntax context describes a hierarchy tracking order of macro definitions.
2726
#[cfg(feature = "salsa")]

crates/test-fixture/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ struct IdentityProcMacroExpander;
738738
impl ProcMacroExpander for IdentityProcMacroExpander {
739739
fn expand(
740740
&self,
741-
_: &dyn SourceDatabase,
741+
_: &dyn ExpandDatabase,
742742
subtree: &TopSubtree,
743743
_: Option<&TopSubtree>,
744744
_: &Env,
@@ -761,7 +761,7 @@ struct Issue18089ProcMacroExpander;
761761
impl ProcMacroExpander for Issue18089ProcMacroExpander {
762762
fn expand(
763763
&self,
764-
_: &dyn SourceDatabase,
764+
_: &dyn ExpandDatabase,
765765
subtree: &TopSubtree,
766766
_: Option<&TopSubtree>,
767767
_: &Env,
@@ -797,7 +797,7 @@ struct AttributeInputReplaceProcMacroExpander;
797797
impl ProcMacroExpander for AttributeInputReplaceProcMacroExpander {
798798
fn expand(
799799
&self,
800-
_: &dyn SourceDatabase,
800+
_: &dyn ExpandDatabase,
801801
_: &TopSubtree,
802802
attrs: Option<&TopSubtree>,
803803
_: &Env,
@@ -821,7 +821,7 @@ struct Issue18840ProcMacroExpander;
821821
impl ProcMacroExpander for Issue18840ProcMacroExpander {
822822
fn expand(
823823
&self,
824-
_: &dyn SourceDatabase,
824+
_: &dyn ExpandDatabase,
825825
fn_: &TopSubtree,
826826
_: Option<&TopSubtree>,
827827
_: &Env,
@@ -858,7 +858,7 @@ struct MirrorProcMacroExpander;
858858
impl ProcMacroExpander for MirrorProcMacroExpander {
859859
fn expand(
860860
&self,
861-
_: &dyn SourceDatabase,
861+
_: &dyn ExpandDatabase,
862862
input: &TopSubtree,
863863
_: Option<&TopSubtree>,
864864
_: &Env,
@@ -897,7 +897,7 @@ struct ShortenProcMacroExpander;
897897
impl ProcMacroExpander for ShortenProcMacroExpander {
898898
fn expand(
899899
&self,
900-
_: &dyn SourceDatabase,
900+
_: &dyn ExpandDatabase,
901901
input: &TopSubtree,
902902
_: Option<&TopSubtree>,
903903
_: &Env,
@@ -942,7 +942,7 @@ struct Issue17479ProcMacroExpander;
942942
impl ProcMacroExpander for Issue17479ProcMacroExpander {
943943
fn expand(
944944
&self,
945-
_: &dyn SourceDatabase,
945+
_: &dyn ExpandDatabase,
946946
subtree: &TopSubtree,
947947
_: Option<&TopSubtree>,
948948
_: &Env,
@@ -973,7 +973,7 @@ struct Issue18898ProcMacroExpander;
973973
impl ProcMacroExpander for Issue18898ProcMacroExpander {
974974
fn expand(
975975
&self,
976-
_: &dyn SourceDatabase,
976+
_: &dyn ExpandDatabase,
977977
subtree: &TopSubtree,
978978
_: Option<&TopSubtree>,
979979
_: &Env,
@@ -1027,7 +1027,7 @@ struct DisallowCfgProcMacroExpander;
10271027
impl ProcMacroExpander for DisallowCfgProcMacroExpander {
10281028
fn expand(
10291029
&self,
1030-
_: &dyn SourceDatabase,
1030+
_: &dyn ExpandDatabase,
10311031
subtree: &TopSubtree,
10321032
_: Option<&TopSubtree>,
10331033
_: &Env,
@@ -1059,7 +1059,7 @@ struct GenerateSuffixedTypeProcMacroExpander;
10591059
impl ProcMacroExpander for GenerateSuffixedTypeProcMacroExpander {
10601060
fn expand(
10611061
&self,
1062-
_: &dyn SourceDatabase,
1062+
_: &dyn ExpandDatabase,
10631063
subtree: &TopSubtree,
10641064
_attrs: Option<&TopSubtree>,
10651065
_env: &Env,

0 commit comments

Comments
 (0)