Skip to content
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: 2 additions & 2 deletions crates/hir/src/source_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ impl SourceAnalyzer {

fn expr_id(&self, db: &dyn HirDatabase, expr: &ast::Expr) -> Option<ExprId> {
let src = match expr {
ast::Expr::MacroCall(call) => {
self.expand_expr(db, InFile::new(self.file_id, call.clone()))?
ast::Expr::MacroExpr(expr) => {
self.expand_expr(db, InFile::new(self.file_id, expr.macro_call()?.clone()))?
}
_ => InFile::new(self.file_id, expr.clone()),
};
Expand Down
9 changes: 7 additions & 2 deletions crates/hir_def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,8 @@ impl ExprCollector<'_> {
None => self.alloc_expr(Expr::Missing, syntax_ptr),
}
}
ast::Expr::MacroCall(e) => {
ast::Expr::MacroExpr(e) => {
let e = e.macro_call()?;
let macro_ptr = AstPtr::new(&e);
let id = self.collect_macro_call(e, macro_ptr.clone(), true, |this, expansion| {
expansion.map(|it| this.collect_expr(it))
Expand Down Expand Up @@ -629,7 +630,11 @@ impl ExprCollector<'_> {
}
let has_semi = stmt.semicolon_token().is_some();
// Note that macro could be expended to multiple statements
if let Some(ast::Expr::MacroCall(m)) = stmt.expr() {
if let Some(ast::Expr::MacroExpr(e)) = stmt.expr() {
let m = match e.macro_call() {
Some(it) => it,
None => return,
};
let macro_ptr = AstPtr::new(&m);
let syntax_ptr = AstPtr::new(&stmt.expr().unwrap());

Expand Down
28 changes: 28 additions & 0 deletions crates/hir_def/src/body/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ fn main() { n_nuple!(1,2,3); }
);
}

#[test]
fn issue_3642_bad_macro_stackover() {
lower(
r#"
#[macro_export]
macro_rules! match_ast {
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };

(match ($node:expr) {
$( ast::$ast:ident($it:ident) => $res:expr, )*
_ => $catch_all:expr $(,)?
}) => {{
$( if let Some($it) = ast::$ast::cast($node.clone()) { $res } else )*
{ $catch_all }
}};
}

fn main() {
let anchor = match_ast! {
match parent {
as => {},
_ => return None
}
};
}"#,
);
}

#[test]
fn macro_resolve() {
// Regression test for a path resolution bug introduced with inner item handling.
Expand Down
24 changes: 24 additions & 0 deletions crates/hir_def/src/body/tests/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,27 @@ fn outer() {
"#]],
);
}

#[test]
fn stmt_macro_expansion_with_trailing_expr() {
cov_mark::check!(macro_stmt_with_trailing_macro_expr);
check_at(
r#"
macro_rules! mac {
() => { mac!($) };
($x:tt) => { fn inner() {} };
}
fn foo() {
mac!();
$0
}
"#,
expect![[r#"
block scope
inner: v

crate
foo: v
"#]],
)
}
35 changes: 23 additions & 12 deletions crates/hir_def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,33 @@ impl<'a> Ctx<'a> {
pub(super) fn lower_macro_stmts(mut self, stmts: ast::MacroStmts) -> ItemTree {
self.tree.top_level = stmts
.statements()
.filter_map(|stmt| match stmt {
ast::Stmt::Item(item) => Some(item),
// Macro calls can be both items and expressions. The syntax library always treats
// them as expressions here, so we undo that.
ast::Stmt::ExprStmt(es) => match es.expr()? {
ast::Expr::MacroCall(call) => {
cov_mark::hit!(macro_call_in_macro_stmts_is_added_to_item_tree);
Some(call.into())
}
.filter_map(|stmt| {
match stmt {
ast::Stmt::Item(item) => Some(item),
// Macro calls can be both items and expressions. The syntax library always treats
// them as expressions here, so we undo that.
ast::Stmt::ExprStmt(es) => match es.expr()? {
ast::Expr::MacroExpr(expr) => {
cov_mark::hit!(macro_call_in_macro_stmts_is_added_to_item_tree);
Some(expr.macro_call()?.into())
}
_ => None,
},
_ => None,
},
_ => None,
}
})
.flat_map(|item| self.lower_mod_item(&item))
.collect();

if let Some(ast::Expr::MacroExpr(tail_macro)) = stmts.expr() {
if let Some(call) = tail_macro.macro_call() {
cov_mark::hit!(macro_stmt_with_trailing_macro_expr);
if let Some(mod_item) = self.lower_mod_item(&call.into()) {
self.tree.top_level.push(mod_item);
}
}
}

self.tree
}

Expand All @@ -75,7 +86,7 @@ impl<'a> Ctx<'a> {
// Macro calls can be both items and expressions. The syntax library always treats
// them as expressions here, so we undo that.
ast::Stmt::ExprStmt(es) => match es.expr()? {
ast::Expr::MacroCall(call) => self.lower_mod_item(&call.into()),
ast::Expr::MacroExpr(expr) => self.lower_mod_item(&expr.macro_call()?.into()),
_ => None,
},
_ => None,
Expand Down
3 changes: 1 addition & 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 @@ -249,8 +249,7 @@ macro_rules! format_args {

fn main() {
let _ =
// +errors
format_args!("{} {:?}", a.);
format_args!/*+errors*/("{} {:?}", a.);
}
"#,
expect![[r##"
Expand Down
3 changes: 1 addition & 2 deletions crates/hir_def/src/macro_expansion_tests/mbe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,7 @@ macro_rules! m {
}

fn f() -> i32 {
// +tree
m!{}
m!/*+tree*/{}
}
"#,
expect![[r#"
Expand Down
29 changes: 13 additions & 16 deletions crates/hir_expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,16 @@ impl ExpandTo {
None => return ExpandTo::Statements,
};

// FIXME: macros in statement position are treated as expression statements, they should
// probably be their own statement kind. The *grand*parent indicates what's valid.
if parent.kind() == MACRO_EXPR
&& parent
.parent()
.map_or(true, |p| matches!(p.kind(), EXPR_STMT | STMT_LIST | MACRO_STMTS))
{
return ExpandTo::Statements;
}

match parent.kind() {
MACRO_ITEMS | SOURCE_FILE | ITEM_LIST => ExpandTo::Items,
MACRO_STMTS | EXPR_STMT | STMT_LIST => ExpandTo::Statements,
Expand All @@ -895,23 +905,10 @@ impl ExpandTo {
| CLOSURE_EXPR | FIELD_EXPR | FOR_EXPR | IF_EXPR | INDEX_EXPR | LET_EXPR
| MATCH_ARM | MATCH_EXPR | MATCH_GUARD | METHOD_CALL_EXPR | PAREN_EXPR | PATH_EXPR
| PREFIX_EXPR | RANGE_EXPR | RECORD_EXPR_FIELD | REF_EXPR | RETURN_EXPR | TRY_EXPR
| TUPLE_EXPR | WHILE_EXPR => ExpandTo::Expr,
| TUPLE_EXPR | WHILE_EXPR | MACRO_EXPR => ExpandTo::Expr,
_ => {
match ast::LetStmt::cast(parent) {
Some(let_stmt) => {
if let Some(true) = let_stmt.initializer().map(|it| it.syntax() == syn) {
ExpandTo::Expr
} else if let Some(true) = let_stmt.ty().map(|it| it.syntax() == syn) {
ExpandTo::Type
} else {
ExpandTo::Pattern
}
}
None => {
// Unknown , Just guess it is `Items`
ExpandTo::Items
}
}
// Unknown , Just guess it is `Items`
ExpandTo::Items
}
}
}
Expand Down
28 changes: 0 additions & 28 deletions crates/hir_ty/src/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,34 +444,6 @@ fn test() {
);
}

#[test]
fn issue_3642_bad_macro_stackover() {
check_no_mismatches(
r#"
#[macro_export]
macro_rules! match_ast {
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };

(match ($node:expr) {
$( ast::$ast:ident($it:ident) => $res:expr, )*
_ => $catch_all:expr $(,)?
}) => {{
$( if let Some($it) = ast::$ast::cast($node.clone()) { $res } else )*
{ $catch_all }
}};
}

fn main() {
let anchor = match_ast! {
match parent {
as => {},
_ => return None
}
};
}"#,
);
}

#[test]
fn issue_3999_slice() {
check_infer(
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/highlight_related.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn highlight_exit_points(
highlights.push(HighlightedRange { category: None, range: token.text_range() });
}
}
ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroCall(_) => {
ast::Expr::MethodCallExpr(_) | ast::Expr::CallExpr(_) | ast::Expr::MacroExpr(_) => {
if sema.type_of_expr(&expr).map_or(false, |ty| ty.original.is_never()) {
highlights.push(HighlightedRange {
category: None,
Expand Down
54 changes: 28 additions & 26 deletions crates/ide/src/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,20 @@ fn test() {
[email protected] "{"
[email protected] "\n "
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "assert"
[email protected] "!"
[email protected]
[email protected] "("
[email protected] "\"\n fn foo() {\n ..."
[email protected] ","
[email protected] " "
[email protected] "\"\""
[email protected] ")"
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "assert"
[email protected] "!"
[email protected]
[email protected] "("
[email protected] "\"\n fn foo() {\n ..."
[email protected] ","
[email protected] " "
[email protected] "\"\""
[email protected] ")"
[email protected] ";"
[email protected] "\n"
[email protected] "}"
Expand Down Expand Up @@ -214,19 +215,20 @@ fn test() {
}"#,
expect![[r#"
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "assert"
[email protected] "!"
[email protected]
[email protected] "("
[email protected] "\"\n fn foo() {\n ..."
[email protected] ","
[email protected] " "
[email protected] "\"\""
[email protected] ")"
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected] "assert"
[email protected] "!"
[email protected]
[email protected] "("
[email protected] "\"\n fn foo() {\n ..."
[email protected] ","
[email protected] " "
[email protected] "\"\""
[email protected] ")"
[email protected] ";"
"#]],
);
Expand Down
11 changes: 11 additions & 0 deletions crates/ide/src/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,17 @@ sdasdasdasdasd
);
}

#[test]
fn noop_in_item_position_with_macro() {
type_char_noop('{', r#"$0println!();"#);
type_char_noop(
'{',
r#"
fn main() $0println!("hello");
}"#,
);
}

#[test]
fn adds_closing_brace_for_use_tree() {
type_char(
Expand Down
2 changes: 1 addition & 1 deletion crates/ide_assists/src/handlers/convert_bool_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub(crate) fn convert_if_to_bool_then(acc: &mut Assists, ctx: &AssistContext) ->
| ast::Expr::ForExpr(_)
| ast::Expr::IfExpr(_)
| ast::Expr::LoopExpr(_)
| ast::Expr::MacroCall(_)
| ast::Expr::MacroExpr(_)
| ast::Expr::MatchExpr(_)
| ast::Expr::PrefixExpr(_)
| ast::Expr::RangeExpr(_)
Expand Down
8 changes: 4 additions & 4 deletions crates/ide_assists/src/handlers/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,8 @@ impl FunctionBody {
ast::Expr::PathExpr(path_expr) => {
cb(path_expr.path().and_then(|it| it.as_single_name_ref()))
}
ast::Expr::MacroCall(call) => {
if let Some(tt) = call.token_tree() {
ast::Expr::MacroExpr(expr) => {
if let Some(tt) = expr.macro_call().and_then(|call| call.token_tree()) {
tt.syntax()
.children_with_tokens()
.flat_map(SyntaxElement::into_token)
Expand Down Expand Up @@ -923,7 +923,7 @@ fn reference_is_exclusive(

/// checks if this expr requires `&mut` access, recurses on field access
fn expr_require_exclusive_access(ctx: &AssistContext, expr: &ast::Expr) -> Option<bool> {
if let ast::Expr::MacroCall(_) = expr {
if let ast::Expr::MacroExpr(_) = expr {
// FIXME: expand macro and check output for mutable usages of the variable?
return None;
}
Expand Down Expand Up @@ -1015,7 +1015,7 @@ fn path_element_of_reference(
None
})?;
stdx::always!(
matches!(path, ast::Expr::PathExpr(_) | ast::Expr::MacroCall(_)),
matches!(path, ast::Expr::PathExpr(_) | ast::Expr::MacroExpr(_)),
"unexpected expression type for variable usage: {:?}",
path
);
Expand Down
Loading