Skip to content

Commit 70fa270

Browse files
committed
Auto merge of rust-lang#15401 - Veykril:disabled-proc-macro, r=Veykril
internal: Turn unresolved proc macro expansions into missing expressions Reduces the amount of type related errors one gets when proc macro expansion is disabled.
2 parents 5e1394e + 042be32 commit 70fa270

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

Diff for: crates/hir-def/src/body/lower.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -913,15 +913,14 @@ impl ExprCollector<'_> {
913913
self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
914914
}
915915

916-
fn collect_macro_call<F, T, U>(
916+
fn collect_macro_call<T, U>(
917917
&mut self,
918918
mcall: ast::MacroCall,
919919
syntax_ptr: AstPtr<ast::MacroCall>,
920920
record_diagnostics: bool,
921-
collector: F,
921+
collector: impl FnOnce(&mut Self, Option<T>) -> U,
922922
) -> U
923923
where
924-
F: FnOnce(&mut Self, Option<T>) -> U,
925924
T: ast::AstNode,
926925
{
927926
// File containing the macro call. Expansion errors will be attached here.

Diff for: crates/hir-def/src/expander.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,26 @@ impl Expander {
164164
return ExpandResult { value: None, err };
165165
};
166166

167-
Self::enter_expand_inner(db, call_id, err).map(|value| {
168-
value.and_then(|InFile { file_id, value }| {
169-
let parse = value.cast::<T>()?;
170-
171-
self.recursion_depth += 1;
172-
self.hygiene = Hygiene::new(db.upcast(), file_id);
173-
let old_file_id = std::mem::replace(&mut self.current_file_id, file_id);
174-
let mark =
175-
Mark { file_id: old_file_id, bomb: DropBomb::new("expansion mark dropped") };
176-
Some((mark, parse))
177-
})
178-
})
167+
let res = Self::enter_expand_inner(db, call_id, err);
168+
match res.err {
169+
// If proc-macro is disabled or unresolved, we want to expand to a missing expression
170+
// instead of an empty tree which might end up in an empty block.
171+
Some(ExpandError::UnresolvedProcMacro(_)) => res.map(|_| None),
172+
_ => res.map(|value| {
173+
value.and_then(|InFile { file_id, value }| {
174+
let parse = value.cast::<T>()?;
175+
176+
self.recursion_depth += 1;
177+
self.hygiene = Hygiene::new(db.upcast(), file_id);
178+
let old_file_id = std::mem::replace(&mut self.current_file_id, file_id);
179+
let mark = Mark {
180+
file_id: old_file_id,
181+
bomb: DropBomb::new("expansion mark dropped"),
182+
};
183+
Some((mark, parse))
184+
})
185+
}),
186+
}
179187
}
180188
}
181189

Diff for: crates/ide-assists/src/handlers/inline_macro.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
3737
pub(crate) fn inline_macro(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
3838
let unexpanded = ctx.find_node_at_offset::<ast::MacroCall>()?;
3939
let expanded = insert_ws_into(ctx.sema.expand(&unexpanded)?.clone_for_update());
40-
4140
let text_range = unexpanded.syntax().text_range();
4241

4342
acc.add(
44-
AssistId("inline_macro", AssistKind::RefactorRewrite),
43+
AssistId("inline_macro", AssistKind::RefactorInline),
4544
format!("Inline macro"),
4645
text_range,
4746
|builder| builder.replace(text_range, expanded.to_string()),

Diff for: crates/parser/src/grammar/items.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,6 @@ fn macro_rules(p: &mut Parser<'_>, m: Marker) {
328328
p.bump_remap(T![macro_rules]);
329329
p.expect(T![!]);
330330

331-
if p.at(IDENT) {
332-
name(p);
333-
}
334331
// Special-case `macro_rules! try`.
335332
// This is a hack until we do proper edition support
336333

@@ -340,6 +337,8 @@ fn macro_rules(p: &mut Parser<'_>, m: Marker) {
340337
let m = p.start();
341338
p.bump_remap(IDENT);
342339
m.complete(p, NAME);
340+
} else {
341+
name(p);
343342
}
344343

345344
match p.current() {

0 commit comments

Comments
 (0)