Skip to content

Commit 2fabcf0

Browse files
authored
Merge pull request #19257 from Veykril/push-myvwxskworsn
internal: Cleanup some syntax highlighting things
2 parents fe84446 + 716e9fd commit 2fabcf0

34 files changed

+485
-590
lines changed

crates/hir-def/src/macro_expansion_tests/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use hir_expand::{
2222
db::ExpandDatabase,
2323
proc_macro::{ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind},
2424
span_map::SpanMapRef,
25-
InFile, MacroCallKind, MacroFileId, MacroFileIdExt,
25+
InFile, MacroCallKind, MacroFileId, MacroFileIdExt, MacroKind,
2626
};
2727
use intern::Symbol;
2828
use itertools::Itertools;
@@ -211,7 +211,11 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
211211

212212
if let Some(src) = src {
213213
if let Some(file_id) = src.file_id.macro_file() {
214-
if file_id.is_attr_macro(&db) || file_id.is_custom_derive(&db) {
214+
if let MacroKind::Derive
215+
| MacroKind::DeriveBuiltIn
216+
| MacroKind::Attr
217+
| MacroKind::AttrBuiltIn = file_id.kind(&db)
218+
{
215219
let call = file_id.call_node(&db);
216220
let mut show_spans = false;
217221
let mut show_ctxt = false;
@@ -236,7 +240,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
236240
for impl_id in def_map[local_id].scope.impls() {
237241
let src = impl_id.lookup(&db).source(&db);
238242
if let Some(macro_file) = src.file_id.macro_file() {
239-
if macro_file.is_builtin_derive(&db) {
243+
if let MacroKind::DeriveBuiltIn | MacroKind::Derive = macro_file.kind(&db) {
240244
let pp = pretty_print_macro_expansion(
241245
src.value.syntax().clone(),
242246
db.span_map(macro_file.into()).as_ref(),

crates/hir-expand/src/files.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange,
1010

1111
use crate::{
1212
db::{self, ExpandDatabase},
13-
map_node_range_up, map_node_range_up_rooted, span_for_offset, MacroFileIdExt,
13+
map_node_range_up, map_node_range_up_rooted, span_for_offset, MacroFileIdExt, MacroKind,
1414
};
1515

1616
/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
@@ -276,7 +276,11 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> {
276276
HirFileIdRepr::FileId(file_id) => {
277277
return Some(InRealFile { file_id, value: self.value.borrow().clone() })
278278
}
279-
HirFileIdRepr::MacroFile(m) if m.is_attr_macro(db) => m,
279+
HirFileIdRepr::MacroFile(m)
280+
if matches!(m.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn) =>
281+
{
282+
m
283+
}
280284
_ => return None,
281285
};
282286

@@ -453,7 +457,7 @@ impl<N: AstNode> InFile<N> {
453457
}
454458
HirFileIdRepr::MacroFile(m) => m,
455459
};
456-
if !file_id.is_attr_macro(db) {
460+
if !matches!(file_id.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn) {
457461
return None;
458462
}
459463

crates/hir-expand/src/lib.rs

+31-23
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,24 @@ impl HirFileIdExt for HirFileId {
416416
}
417417
}
418418

419+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
420+
pub enum MacroKind {
421+
/// `macro_rules!` or Macros 2.0 macro.
422+
Declarative,
423+
/// A built-in function-like macro.
424+
DeclarativeBuiltIn,
425+
/// A custom derive.
426+
Derive,
427+
/// A builtin-in derive.
428+
DeriveBuiltIn,
429+
/// A procedural attribute macro.
430+
Attr,
431+
/// A built-in attribute macro.
432+
AttrBuiltIn,
433+
/// A function-like procedural macro.
434+
ProcMacro,
435+
}
436+
419437
pub trait MacroFileIdExt {
420438
fn is_env_or_option_env(&self, db: &dyn ExpandDatabase) -> bool;
421439
fn is_include_like_macro(&self, db: &dyn ExpandDatabase) -> bool;
@@ -427,15 +445,12 @@ pub trait MacroFileIdExt {
427445

428446
fn expansion_info(self, db: &dyn ExpandDatabase) -> ExpansionInfo;
429447

430-
fn is_builtin_derive(&self, db: &dyn ExpandDatabase) -> bool;
431-
fn is_custom_derive(&self, db: &dyn ExpandDatabase) -> bool;
448+
fn kind(&self, db: &dyn ExpandDatabase) -> MacroKind;
432449

433450
/// Return whether this file is an include macro
434451
fn is_include_macro(&self, db: &dyn ExpandDatabase) -> bool;
435452

436453
fn is_eager(&self, db: &dyn ExpandDatabase) -> bool;
437-
/// Return whether this file is an attr macro
438-
fn is_attr_macro(&self, db: &dyn ExpandDatabase) -> bool;
439454

440455
/// Return whether this file is the pseudo expansion of the derive attribute.
441456
/// See [`crate::builtin_attr_macro::derive_attr_expand`].
@@ -468,18 +483,18 @@ impl MacroFileIdExt for MacroFileId {
468483
ExpansionInfo::new(db, self)
469484
}
470485

471-
fn is_custom_derive(&self, db: &dyn ExpandDatabase) -> bool {
472-
matches!(
473-
db.lookup_intern_macro_call(self.macro_call_id).def.kind,
474-
MacroDefKind::ProcMacro(_, _, ProcMacroKind::CustomDerive)
475-
)
476-
}
477-
478-
fn is_builtin_derive(&self, db: &dyn ExpandDatabase) -> bool {
479-
matches!(
480-
db.lookup_intern_macro_call(self.macro_call_id).def.kind,
481-
MacroDefKind::BuiltInDerive(..)
482-
)
486+
fn kind(&self, db: &dyn ExpandDatabase) -> MacroKind {
487+
match db.lookup_intern_macro_call(self.macro_call_id).def.kind {
488+
MacroDefKind::Declarative(..) => MacroKind::Declarative,
489+
MacroDefKind::BuiltIn(..) | MacroDefKind::BuiltInEager(..) => {
490+
MacroKind::DeclarativeBuiltIn
491+
}
492+
MacroDefKind::BuiltInDerive(..) => MacroKind::DeriveBuiltIn,
493+
MacroDefKind::ProcMacro(_, _, ProcMacroKind::CustomDerive) => MacroKind::Derive,
494+
MacroDefKind::ProcMacro(_, _, ProcMacroKind::Attr) => MacroKind::Attr,
495+
MacroDefKind::ProcMacro(_, _, ProcMacroKind::Bang) => MacroKind::ProcMacro,
496+
MacroDefKind::BuiltInAttr(..) => MacroKind::AttrBuiltIn,
497+
}
483498
}
484499

485500
fn is_include_macro(&self, db: &dyn ExpandDatabase) -> bool {
@@ -507,13 +522,6 @@ impl MacroFileIdExt for MacroFileId {
507522
}
508523
}
509524

510-
fn is_attr_macro(&self, db: &dyn ExpandDatabase) -> bool {
511-
matches!(
512-
db.lookup_intern_macro_call(self.macro_call_id).def.kind,
513-
MacroDefKind::BuiltInAttr(..) | MacroDefKind::ProcMacro(_, _, ProcMacroKind::Attr)
514-
)
515-
}
516-
517525
fn is_derive_attr_pseudo_expansion(&self, db: &dyn ExpandDatabase) -> bool {
518526
let loc = db.lookup_intern_macro_call(self.macro_call_id);
519527
loc.def.is_attribute_derive()

crates/hir/src/lib.rs

+21-28
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub use {
142142
name::Name,
143143
prettify_macro_expansion,
144144
proc_macro::{ProcMacros, ProcMacrosBuilder},
145-
tt, ExpandResult, HirFileId, HirFileIdExt, MacroFileId, MacroFileIdExt,
145+
tt, ExpandResult, HirFileId, HirFileIdExt, MacroFileId, MacroFileIdExt, MacroKind,
146146
},
147147
hir_ty::{
148148
consteval::ConstEvalError,
@@ -699,7 +699,10 @@ impl Module {
699699
let source_map = tree_source_maps.impl_(loc.id.value).item();
700700
let node = &tree[loc.id.value];
701701
let file_id = loc.id.file_id();
702-
if file_id.macro_file().is_some_and(|it| it.is_builtin_derive(db.upcast())) {
702+
if file_id
703+
.macro_file()
704+
.is_some_and(|it| it.kind(db.upcast()) == MacroKind::DeriveBuiltIn)
705+
{
703706
// these expansion come from us, diagnosing them is a waste of resources
704707
// FIXME: Once we diagnose the inputs to builtin derives, we should at least extract those diagnostics somehow
705708
continue;
@@ -3049,20 +3052,6 @@ impl BuiltinType {
30493052
}
30503053
}
30513054

3052-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3053-
pub enum MacroKind {
3054-
/// `macro_rules!` or Macros 2.0 macro.
3055-
Declarative,
3056-
/// A built-in or custom derive.
3057-
Derive,
3058-
/// A built-in function-like macro.
3059-
BuiltIn,
3060-
/// A procedural attribute macro.
3061-
Attr,
3062-
/// A function-like procedural macro.
3063-
ProcMacro,
3064-
}
3065-
30663055
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
30673056
pub struct Macro {
30683057
pub(crate) id: MacroId,
@@ -3093,15 +3082,19 @@ impl Macro {
30933082
match self.id {
30943083
MacroId::Macro2Id(it) => match it.lookup(db.upcast()).expander {
30953084
MacroExpander::Declarative => MacroKind::Declarative,
3096-
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
3097-
MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
3098-
MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
3085+
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => {
3086+
MacroKind::DeclarativeBuiltIn
3087+
}
3088+
MacroExpander::BuiltInAttr(_) => MacroKind::AttrBuiltIn,
3089+
MacroExpander::BuiltInDerive(_) => MacroKind::DeriveBuiltIn,
30993090
},
31003091
MacroId::MacroRulesId(it) => match it.lookup(db.upcast()).expander {
31013092
MacroExpander::Declarative => MacroKind::Declarative,
3102-
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
3103-
MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
3104-
MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
3093+
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => {
3094+
MacroKind::DeclarativeBuiltIn
3095+
}
3096+
MacroExpander::BuiltInAttr(_) => MacroKind::AttrBuiltIn,
3097+
MacroExpander::BuiltInDerive(_) => MacroKind::DeriveBuiltIn,
31053098
},
31063099
MacroId::ProcMacroId(it) => match it.lookup(db.upcast()).kind {
31073100
ProcMacroKind::CustomDerive => MacroKind::Derive,
@@ -3112,10 +3105,10 @@ impl Macro {
31123105
}
31133106

31143107
pub fn is_fn_like(&self, db: &dyn HirDatabase) -> bool {
3115-
match self.kind(db) {
3116-
MacroKind::Declarative | MacroKind::BuiltIn | MacroKind::ProcMacro => true,
3117-
MacroKind::Attr | MacroKind::Derive => false,
3118-
}
3108+
matches!(
3109+
self.kind(db),
3110+
MacroKind::Declarative | MacroKind::DeclarativeBuiltIn | MacroKind::ProcMacro
3111+
)
31193112
}
31203113

31213114
pub fn is_builtin_derive(&self, db: &dyn HirDatabase) -> bool {
@@ -3155,11 +3148,11 @@ impl Macro {
31553148
}
31563149

31573150
pub fn is_attr(&self, db: &dyn HirDatabase) -> bool {
3158-
matches!(self.kind(db), MacroKind::Attr)
3151+
matches!(self.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn)
31593152
}
31603153

31613154
pub fn is_derive(&self, db: &dyn HirDatabase) -> bool {
3162-
matches!(self.kind(db), MacroKind::Derive)
3155+
matches!(self.kind(db), MacroKind::Derive | MacroKind::DeriveBuiltIn)
31633156
}
31643157
}
31653158

crates/hir/src/semantics.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,7 @@ impl<'db> SemanticsImpl<'db> {
508508
})
509509
}
510510

511-
pub fn is_derive_annotated(&self, adt: &ast::Adt) -> bool {
512-
let file_id = self.find_file(adt.syntax()).file_id;
513-
let adt = InFile::new(file_id, adt);
511+
pub fn is_derive_annotated(&self, adt: InFile<&ast::Adt>) -> bool {
514512
self.with_ctx(|ctx| ctx.has_derives(adt))
515513
}
516514

@@ -551,10 +549,8 @@ impl<'db> SemanticsImpl<'db> {
551549
res.is_empty().not().then_some(res)
552550
}
553551

554-
pub fn is_attr_macro_call(&self, item: &ast::Item) -> bool {
555-
let file_id = self.find_file(item.syntax()).file_id;
556-
let src = InFile::new(file_id, item);
557-
self.with_ctx(|ctx| ctx.item_to_macro_call(src).is_some())
552+
pub fn is_attr_macro_call(&self, item: InFile<&ast::Item>) -> bool {
553+
self.with_ctx(|ctx| ctx.item_to_macro_call(item).is_some())
558554
}
559555

560556
/// Expand the macro call with a different token tree, mapping the `token_to_map` down into the
@@ -1526,8 +1522,13 @@ impl<'db> SemanticsImpl<'db> {
15261522
self.analyze(field.syntax())?.resolve_record_pat_field(self.db, field)
15271523
}
15281524

1525+
// FIXME: Replace this with `resolve_macro_call2`
15291526
pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<Macro> {
15301527
let macro_call = self.find_file(macro_call.syntax()).with_value(macro_call);
1528+
self.resolve_macro_call2(macro_call)
1529+
}
1530+
1531+
pub fn resolve_macro_call2(&self, macro_call: InFile<&ast::MacroCall>) -> Option<Macro> {
15311532
self.with_ctx(|ctx| {
15321533
ctx.macro_call_to_macro_call(macro_call)
15331534
.and_then(|call| macro_call_to_macro_id(ctx, call))
@@ -1538,8 +1539,8 @@ impl<'db> SemanticsImpl<'db> {
15381539
})
15391540
}
15401541

1541-
pub fn is_proc_macro_call(&self, macro_call: &ast::MacroCall) -> bool {
1542-
self.resolve_macro_call(macro_call)
1542+
pub fn is_proc_macro_call(&self, macro_call: InFile<&ast::MacroCall>) -> bool {
1543+
self.resolve_macro_call2(macro_call)
15431544
.is_some_and(|m| matches!(m.id, MacroId::ProcMacroId(..)))
15441545
}
15451546

crates/ide-db/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ pub enum SymbolKind {
252252
impl From<hir::MacroKind> for SymbolKind {
253253
fn from(it: hir::MacroKind) -> Self {
254254
match it {
255-
hir::MacroKind::Declarative | hir::MacroKind::BuiltIn => SymbolKind::Macro,
255+
hir::MacroKind::Declarative | hir::MacroKind::DeclarativeBuiltIn => SymbolKind::Macro,
256256
hir::MacroKind::ProcMacro => SymbolKind::ProcMacro,
257-
hir::MacroKind::Derive => SymbolKind::Derive,
258-
hir::MacroKind::Attr => SymbolKind::Attribute,
257+
hir::MacroKind::Derive | hir::MacroKind::DeriveBuiltIn => SymbolKind::Derive,
258+
hir::MacroKind::Attr | hir::MacroKind::AttrBuiltIn => SymbolKind::Attribute,
259259
}
260260
}
261261
}

crates/ide-db/src/search.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ impl Definition {
373373
SearchScope::krate(db, module.krate())
374374
}
375375
}
376-
hir::MacroKind::BuiltIn => SearchScope::crate_graph(db),
376+
hir::MacroKind::AttrBuiltIn
377+
| hir::MacroKind::DeriveBuiltIn
378+
| hir::MacroKind::DeclarativeBuiltIn => SearchScope::crate_graph(db),
377379
hir::MacroKind::Derive | hir::MacroKind::Attr | hir::MacroKind::ProcMacro => {
378380
SearchScope::reverse_dependencies(db, module.krate())
379381
}

crates/ide/src/doc_links.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,13 @@ fn filename_and_frag_for_def(
635635
}
636636
Definition::Macro(mac) => match mac.kind(db) {
637637
hir::MacroKind::Declarative
638-
| hir::MacroKind::BuiltIn
638+
| hir::MacroKind::AttrBuiltIn
639+
| hir::MacroKind::DeclarativeBuiltIn
639640
| hir::MacroKind::Attr
640641
| hir::MacroKind::ProcMacro => {
641642
format!("macro.{}.html", mac.name(db).as_str())
642643
}
643-
hir::MacroKind::Derive => {
644+
hir::MacroKind::Derive | hir::MacroKind::DeriveBuiltIn => {
644645
format!("derive.{}.html", mac.name(db).as_str())
645646
}
646647
},

crates/ide/src/moniker.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ pub(crate) fn def_to_kind(db: &RootDatabase, def: Definition) -> SymbolInformati
184184

185185
match def {
186186
Definition::Macro(it) => match it.kind(db) {
187-
MacroKind::Declarative => Macro,
188-
MacroKind::Derive => Attribute,
189-
MacroKind::BuiltIn => Macro,
190-
MacroKind::Attr => Attribute,
191-
MacroKind::ProcMacro => Macro,
187+
MacroKind::Derive
188+
| MacroKind::DeriveBuiltIn
189+
| MacroKind::AttrBuiltIn
190+
| MacroKind::Attr => Attribute,
191+
MacroKind::Declarative | MacroKind::DeclarativeBuiltIn | MacroKind::ProcMacro => Macro,
192192
},
193193
Definition::Field(..) | Definition::TupleField(..) => Field,
194194
Definition::Module(..) | Definition::Crate(..) => Module,

0 commit comments

Comments
 (0)