Skip to content

Commit

Permalink
Auto merge of rust-lang#15903 - Veykril:inner-diag, r=Veykril
Browse files Browse the repository at this point in the history
Fix builtin line! expansion

`concat` expects only literals, not whole syntax nodes, so we need to expand as such
  • Loading branch information
bors committed Nov 15, 2023
2 parents 57ef70c + e8c4007 commit b8b4b22
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
4 changes: 2 additions & 2 deletions crates/hir-def/src/macro_expansion_tests/builtin_fn_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() { column!(); }
#[rustc_builtin_macro]
macro_rules! column {() => {}}
fn main() { 0 as u32; }
fn main() { 0u32; }
"#]],
);
}
Expand Down Expand Up @@ -74,7 +74,7 @@ fn main() { line!() }
#[rustc_builtin_macro]
macro_rules! line {() => {}}
fn main() { 0 as u32 }
fn main() { 0u32 }
"#]],
);
}
Expand Down
34 changes: 34 additions & 0 deletions crates/hir-def/src/macro_expansion_tests/mbe/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,3 +970,37 @@ builtin #format_args ("{}", &[0 2]);
"##]],
);
}

#[test]
fn eager_concat_line() {
check(
r#"
#[rustc_builtin_macro]
#[macro_export]
macro_rules! concat {}
#[rustc_builtin_macro]
#[macro_export]
macro_rules! line {}
fn main() {
concat!("event ", line!());
}
"#,
expect![[r##"
#[rustc_builtin_macro]
#[macro_export]
macro_rules! concat {}
#[rustc_builtin_macro]
#[macro_export]
macro_rules! line {}
fn main() {
"event 0u32";
}
"##]],
);
}
27 changes: 8 additions & 19 deletions crates/hir-expand/src/builtin_fn_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn find_builtin_macro(

register_builtin! {
LAZY:
(column, Column) => column_expand,
(column, Column) => line_expand,
(file, File) => file_expand,
(line, Line) => line_expand,
(module_path, ModulePath) => module_path_expand,
Expand Down Expand Up @@ -127,11 +127,13 @@ fn line_expand(
_tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
// dummy implementation for type-checking purposes
let expanded = quote! {
0 as u32
};

ExpandResult::ok(expanded)
ExpandResult::ok(tt::Subtree {
delimiter: tt::Delimiter::unspecified(),
token_trees: vec![tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
text: "0u32".into(),
span: tt::Span::UNSPECIFIED,
}))],
})
}

fn log_syntax_expand(
Expand Down Expand Up @@ -164,19 +166,6 @@ fn stringify_expand(
ExpandResult::ok(expanded)
}

fn column_expand(
_db: &dyn ExpandDatabase,
_id: MacroCallId,
_tt: &tt::Subtree,
) -> ExpandResult<tt::Subtree> {
// dummy implementation for type-checking purposes
let expanded = quote! {
0 as u32
};

ExpandResult::ok(expanded)
}

fn assert_expand(
_db: &dyn ExpandDatabase,
_id: MacroCallId,
Expand Down
6 changes: 2 additions & 4 deletions crates/hir-ty/src/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,7 @@ fn infer_builtin_macros_line() {
}
"#,
expect![[r#"
!0..1 '0': i32
!0..6 '0asu32': u32
!0..4 '0u32': u32
63..87 '{ ...!(); }': ()
73..74 'x': u32
"#]],
Expand Down Expand Up @@ -723,8 +722,7 @@ fn infer_builtin_macros_column() {
}
"#,
expect![[r#"
!0..1 '0': i32
!0..6 '0asu32': u32
!0..4 '0u32': u32
65..91 '{ ...!(); }': ()
75..76 'x': u32
"#]],
Expand Down
1 change: 0 additions & 1 deletion crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ impl GlobalState {
ws
})
.collect::<Vec<_>>();

// Workspaces are the same, but we've updated build data.
self.workspaces = Arc::new(workspaces);
} else {
Expand Down

0 comments on commit b8b4b22

Please sign in to comment.