Skip to content

Commit 669d34d

Browse files
authored
Merge pull request rust-lang#18899 from ChayimFriedman2/issue-18898
fix: Fix another issue with fixup reversing
2 parents d1b9176 + 37b089d commit 669d34d

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

src/tools/rust-analyzer/crates/hir-expand/src/builtin/quote.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ macro_rules! quote_impl__ {
102102
($span:ident $builder:ident # ) => {$crate::builtin::quote::__quote!(@PUNCT($span $builder) '#')};
103103
($span:ident $builder:ident $ ) => {$crate::builtin::quote::__quote!(@PUNCT($span $builder) '$')};
104104
($span:ident $builder:ident * ) => {$crate::builtin::quote::__quote!(@PUNCT($span $builder) '*')};
105+
($span:ident $builder:ident = ) => {$crate::builtin::quote::__quote!(@PUNCT($span $builder) '=')};
105106

106107
($span:ident $builder:ident $first:tt $($tail:tt)+ ) => {{
107108
$crate::builtin::quote::__quote!($span $builder $first);

src/tools/rust-analyzer/crates/hir-expand/src/fixup.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ fn transform_tt<'a, 'b>(
441441
};
442442
let len_diff = replacement.len() as i64 - old_len as i64;
443443
tt.splice(i..i + old_len, replacement.flat_tokens().iter().cloned());
444-
// `+1` for the loop.
445-
i = i.checked_add_signed(len_diff as isize + 1).unwrap();
444+
// Skip the newly inserted replacement, we don't want to visit it.
445+
i += replacement.len();
446446

447447
for &subtree_idx in &subtrees_stack {
448448
let tt::TokenTree::Subtree(subtree) = &mut tt[subtree_idx] else {

src/tools/rust-analyzer/crates/ide/src/inlay_hints.rs

+13
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,19 @@ fn foo() {
867867
let
868868
loop {}
869869
}
870+
"#,
871+
);
872+
}
873+
874+
#[test]
875+
fn regression_18898() {
876+
check(
877+
r#"
878+
//- proc_macros: issue_18898
879+
#[proc_macros::issue_18898]
880+
fn foo() {
881+
let
882+
}
870883
"#,
871884
);
872885
}

src/tools/rust-analyzer/crates/test-fixture/src/lib.rs

+69-3
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ impl ChangeFixture {
376376
}
377377
}
378378

379-
fn default_test_proc_macros() -> [(String, ProcMacro); 8] {
380-
[
379+
fn default_test_proc_macros() -> Box<[(String, ProcMacro)]> {
380+
Box::new([
381381
(
382382
r#"
383383
#[proc_macro_attribute]
@@ -498,7 +498,22 @@ pub fn issue_17479(input: TokenStream) -> TokenStream {
498498
disabled: false,
499499
},
500500
),
501-
]
501+
(
502+
r#"
503+
#[proc_macro_attribute]
504+
pub fn issue_18898(_attr: TokenStream, input: TokenStream) -> TokenStream {
505+
input
506+
}
507+
"#
508+
.into(),
509+
ProcMacro {
510+
name: Symbol::intern("issue_18898"),
511+
kind: ProcMacroKind::Bang,
512+
expander: sync::Arc::new(Issue18898ProcMacroExpander),
513+
disabled: false,
514+
},
515+
),
516+
])
502517
}
503518

504519
fn filter_test_proc_macros(
@@ -801,3 +816,54 @@ impl ProcMacroExpander for Issue17479ProcMacroExpander {
801816
})
802817
}
803818
}
819+
820+
// Reads ident type within string quotes, for issue #17479.
821+
#[derive(Debug)]
822+
struct Issue18898ProcMacroExpander;
823+
impl ProcMacroExpander for Issue18898ProcMacroExpander {
824+
fn expand(
825+
&self,
826+
subtree: &TopSubtree,
827+
_: Option<&TopSubtree>,
828+
_: &Env,
829+
def_site: Span,
830+
_: Span,
831+
_: Span,
832+
_: Option<String>,
833+
) -> Result<TopSubtree, ProcMacroExpansionError> {
834+
let span = subtree
835+
.token_trees()
836+
.flat_tokens()
837+
.last()
838+
.ok_or_else(|| ProcMacroExpansionError::Panic("malformed input".to_owned()))?
839+
.first_span();
840+
let overly_long_subtree = quote! {span =>
841+
{
842+
let a = 5;
843+
let a = 5;
844+
let a = 5;
845+
let a = 5;
846+
let a = 5;
847+
let a = 5;
848+
let a = 5;
849+
let a = 5;
850+
let a = 5;
851+
let a = 5;
852+
let a = 5;
853+
let a = 5;
854+
let a = 5;
855+
let a = 5;
856+
let a = 5;
857+
let a = 5;
858+
let a = 5;
859+
let a = 5;
860+
let a = 5;
861+
}
862+
};
863+
Ok(quote! { def_site =>
864+
fn foo() {
865+
#overly_long_subtree
866+
}
867+
})
868+
}
869+
}

0 commit comments

Comments
 (0)