Skip to content

Commit b231594

Browse files
authored
Merge pull request #22383 from ChayimFriedman2/unimplemented-builtin
fix: Have a specific error for unimplemented builtin macros
2 parents 746dab2 + 01725a4 commit b231594

9 files changed

Lines changed: 53 additions & 10 deletions

File tree

crates/hir-def/src/db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
7575
MacroExpander::BuiltInAttr(it) => MacroDefKind::BuiltInAttr(in_file, it),
7676
MacroExpander::BuiltInDerive(it) => MacroDefKind::BuiltInDerive(in_file, it),
7777
MacroExpander::BuiltInEager(it) => MacroDefKind::BuiltInEager(in_file, it),
78+
MacroExpander::UnimplementedBuiltIn => MacroDefKind::UnimplementedBuiltIn(in_file),
7879
}
7980
};
8081

crates/hir-def/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ pub enum MacroExpander {
442442
BuiltInAttr(BuiltinAttrExpander),
443443
BuiltInDerive(BuiltinDeriveExpander),
444444
BuiltInEager(EagerExpander),
445+
UnimplementedBuiltIn,
445446
}
446447

447448
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

crates/hir-def/src/nameres.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ pub(crate) fn macro_styles_from_id(db: &dyn DefDatabase, macro_id: MacroId) -> M
850850
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroCallStyles::FN_LIKE,
851851
MacroExpander::BuiltInAttr(_) => MacroCallStyles::ATTR,
852852
MacroExpander::BuiltInDerive(_) => MacroCallStyles::DERIVE,
853+
MacroExpander::UnimplementedBuiltIn => MacroCallStyles::all(), // Unknown.
853854
}
854855
}
855856

crates/hir-def/src/nameres/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,7 +2562,7 @@ impl ModCollector<'_, '_> {
25622562
.def_map
25632563
.diagnostics
25642564
.push(DefDiagnostic::unimplemented_builtin_macro(self.module_id, f_ast_id));
2565-
return;
2565+
MacroExpander::UnimplementedBuiltIn
25662566
}
25672567
}
25682568
} else {
@@ -2641,7 +2641,7 @@ impl ModCollector<'_, '_> {
26412641
.def_map
26422642
.diagnostics
26432643
.push(DefDiagnostic::unimplemented_builtin_macro(self.module_id, f_ast_id));
2644-
return;
2644+
MacroExpander::UnimplementedBuiltIn
26452645
}
26462646
} else {
26472647
// Case 2: normal `macro`

crates/hir-expand/src/db.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub enum TokenExpander<'db> {
4343
BuiltInAttr(BuiltinAttrExpander),
4444
/// `derive(Copy)` and such.
4545
BuiltInDerive(BuiltinDeriveExpander),
46+
UnimplementedBuiltIn,
4647
/// The thing we love the most here in rust-analyzer -- procedural macros.
4748
ProcMacro(CustomProcMacroExpander),
4849
}
@@ -311,6 +312,7 @@ pub fn expand_speculative(
311312
it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
312313
}
313314
MacroDefKind::BuiltInAttr(_, it) => it.expand(db, actual_macro_call, &tt, span),
315+
MacroDefKind::UnimplementedBuiltIn(_) => expand_unimplemented_builtin_macro(span),
314316
};
315317

316318
let expand_to = loc.expand_to();
@@ -335,6 +337,13 @@ pub fn expand_speculative(
335337
Some((node.syntax_node(), token))
336338
}
337339

340+
fn expand_unimplemented_builtin_macro(span: Span) -> ExpandResult<tt::TopSubtree> {
341+
ExpandResult::new(
342+
tt::TopSubtree::empty(tt::DelimSpan::from_single(span)),
343+
ExpandError::other(span, "this built-in macro is not implemented"),
344+
)
345+
}
346+
338347
#[salsa::tracked(lru = 1024, returns(ref))]
339348
fn ast_id_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> AstIdMap {
340349
AstIdMap::from_source(&db.parse_or_expand(file_id))
@@ -538,6 +547,7 @@ impl<'db> TokenExpander<'db> {
538547
MacroDefKind::BuiltInDerive(_, expander) => TokenExpander::BuiltInDerive(expander),
539548
MacroDefKind::BuiltInEager(_, expander) => TokenExpander::BuiltInEager(expander),
540549
MacroDefKind::ProcMacro(_, expander, _) => TokenExpander::ProcMacro(expander),
550+
MacroDefKind::UnimplementedBuiltIn(_) => TokenExpander::UnimplementedBuiltIn,
541551
}
542552
}
543553
}
@@ -572,6 +582,9 @@ fn macro_expand<'db>(
572582
MacroDefKind::BuiltInDerive(_, it) => {
573583
it.expand(db, macro_call_id, arg, span).map_err(Into::into).zip_val(None)
574584
}
585+
MacroDefKind::UnimplementedBuiltIn(_) => {
586+
expand_unimplemented_builtin_macro(span).zip_val(None)
587+
}
575588
MacroDefKind::BuiltInEager(_, it) => {
576589
// This might look a bit odd, but we do not expand the inputs to eager macros here.
577590
// Eager macros inputs are expanded, well, eagerly when we collect the macro calls.

crates/hir-expand/src/eager.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,8 @@ fn eager_macro_recur(
246246
| MacroDefKind::BuiltIn(..)
247247
| MacroDefKind::BuiltInAttr(..)
248248
| MacroDefKind::BuiltInDerive(..)
249-
| MacroDefKind::ProcMacro(..) => {
249+
| MacroDefKind::ProcMacro(..)
250+
| MacroDefKind::UnimplementedBuiltIn(..) => {
250251
let ExpandResult { value: (parse, tm), err } = lazy_expand(
251252
db,
252253
&def,

crates/hir-expand/src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ pub enum MacroDefKind {
249249
BuiltInAttr(AstId<ast::Macro>, BuiltinAttrExpander),
250250
BuiltInDerive(AstId<ast::Macro>, BuiltinDeriveExpander),
251251
BuiltInEager(AstId<ast::Macro>, EagerExpander),
252+
UnimplementedBuiltIn(AstId<ast::Macro>),
252253
ProcMacro(AstId<ast::Fn>, CustomProcMacroExpander, ProcMacroKind),
253254
}
254255

@@ -265,7 +266,8 @@ impl MacroDefKind {
265266
| MacroDefKind::BuiltInAttr(id, _)
266267
| MacroDefKind::BuiltInDerive(id, _)
267268
| MacroDefKind::BuiltInEager(id, _)
268-
| MacroDefKind::Declarative(id, ..) => id.erase(),
269+
| MacroDefKind::Declarative(id, ..)
270+
| MacroDefKind::UnimplementedBuiltIn(id) => id.erase(),
269271
}
270272
}
271273
}
@@ -500,6 +502,7 @@ impl MacroCallId {
500502
MacroDefKind::ProcMacro(_, _, ProcMacroKind::Attr) => MacroKind::Attr,
501503
MacroDefKind::ProcMacro(_, _, ProcMacroKind::Bang) => MacroKind::ProcMacro,
502504
MacroDefKind::BuiltInAttr(..) => MacroKind::AttrBuiltIn,
505+
MacroDefKind::UnimplementedBuiltIn(..) => MacroKind::Declarative,
503506
}
504507
}
505508

@@ -551,7 +554,8 @@ impl MacroDefId {
551554
| MacroDefKind::BuiltIn(id, _)
552555
| MacroDefKind::BuiltInAttr(id, _)
553556
| MacroDefKind::BuiltInDerive(id, _)
554-
| MacroDefKind::BuiltInEager(id, _) => {
557+
| MacroDefKind::BuiltInEager(id, _)
558+
| MacroDefKind::UnimplementedBuiltIn(id) => {
555559
id.with_value(db.ast_id_map(id.file_id).get(id.value).text_range())
556560
}
557561
MacroDefKind::ProcMacro(id, _, _) => {
@@ -567,7 +571,8 @@ impl MacroDefId {
567571
| MacroDefKind::BuiltIn(id, _)
568572
| MacroDefKind::BuiltInAttr(id, _)
569573
| MacroDefKind::BuiltInDerive(id, _)
570-
| MacroDefKind::BuiltInEager(id, _) => Either::Left(id),
574+
| MacroDefKind::BuiltInEager(id, _)
575+
| MacroDefKind::UnimplementedBuiltIn(id) => Either::Left(id),
571576
}
572577
}
573578

@@ -577,9 +582,9 @@ impl MacroDefId {
577582

578583
pub fn is_attribute(&self) -> bool {
579584
match self.kind {
580-
MacroDefKind::BuiltInAttr(..) | MacroDefKind::ProcMacro(_, _, ProcMacroKind::Attr) => {
581-
true
582-
}
585+
MacroDefKind::BuiltInAttr(..)
586+
| MacroDefKind::ProcMacro(_, _, ProcMacroKind::Attr)
587+
| MacroDefKind::UnimplementedBuiltIn(_) => true,
583588
MacroDefKind::Declarative(_, styles) => styles.contains(MacroCallStyles::ATTR),
584589
_ => false,
585590
}
@@ -588,7 +593,8 @@ impl MacroDefId {
588593
pub fn is_derive(&self) -> bool {
589594
match self.kind {
590595
MacroDefKind::BuiltInDerive(..)
591-
| MacroDefKind::ProcMacro(_, _, ProcMacroKind::CustomDerive) => true,
596+
| MacroDefKind::ProcMacro(_, _, ProcMacroKind::CustomDerive)
597+
| MacroDefKind::UnimplementedBuiltIn(_) => true,
592598
MacroDefKind::Declarative(_, styles) => styles.contains(MacroCallStyles::DERIVE),
593599
_ => false,
594600
}
@@ -601,6 +607,7 @@ impl MacroDefId {
601607
| MacroDefKind::ProcMacro(_, _, ProcMacroKind::Bang)
602608
| MacroDefKind::BuiltInEager(..)
603609
| MacroDefKind::Declarative(..)
610+
| MacroDefKind::UnimplementedBuiltIn(_)
604611
)
605612
}
606613

crates/hir/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3612,6 +3612,7 @@ impl Macro {
36123612
}
36133613
MacroExpander::BuiltInAttr(_) => MacroKind::AttrBuiltIn,
36143614
MacroExpander::BuiltInDerive(_) => MacroKind::DeriveBuiltIn,
3615+
MacroExpander::UnimplementedBuiltIn => MacroKind::Declarative,
36153616
},
36163617
MacroId::MacroRulesId(it) => match it.lookup(db).expander {
36173618
MacroExpander::Declarative { .. } => MacroKind::Declarative,
@@ -3620,6 +3621,7 @@ impl Macro {
36203621
}
36213622
MacroExpander::BuiltInAttr(_) => MacroKind::AttrBuiltIn,
36223623
MacroExpander::BuiltInDerive(_) => MacroKind::DeriveBuiltIn,
3624+
MacroExpander::UnimplementedBuiltIn => MacroKind::Declarative,
36233625
},
36243626
MacroId::ProcMacroId(it) => match it.lookup(db).kind {
36253627
ProcMacroKind::CustomDerive => MacroKind::Derive,

crates/ide-diagnostics/src/handlers/macro_error.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,21 @@ fn it_works() {
327327
"#,
328328
);
329329
}
330+
331+
#[test]
332+
fn unimplemented_builtin_macro() {
333+
check_diagnostics(
334+
r#"
335+
#[rustc_builtin_macro]
336+
macro_rules! unimplemented_builtin_macro {
337+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ weak: unimplemented built-in macro
338+
() => {};
339+
}
340+
341+
#[unimplemented_builtin_macro]
342+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: this built-in macro is not implemented
343+
struct Foo;
344+
"#,
345+
);
346+
}
330347
}

0 commit comments

Comments
 (0)