Skip to content

internal: Cleanup some syntax highlighting things #19257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 1, 2025
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
10 changes: 7 additions & 3 deletions crates/hir-def/src/macro_expansion_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use hir_expand::{
db::ExpandDatabase,
proc_macro::{ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind},
span_map::SpanMapRef,
InFile, MacroCallKind, MacroFileId, MacroFileIdExt,
InFile, MacroCallKind, MacroFileId, MacroFileIdExt, MacroKind,
};
use intern::Symbol;
use itertools::Itertools;
Expand Down Expand Up @@ -211,7 +211,11 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream

if let Some(src) = src {
if let Some(file_id) = src.file_id.macro_file() {
if file_id.is_attr_macro(&db) || file_id.is_custom_derive(&db) {
if let MacroKind::Derive
| MacroKind::DeriveBuiltIn
| MacroKind::Attr
| MacroKind::AttrBuiltIn = file_id.kind(&db)
{
let call = file_id.call_node(&db);
let mut show_spans = false;
let mut show_ctxt = false;
Expand All @@ -236,7 +240,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
for impl_id in def_map[local_id].scope.impls() {
let src = impl_id.lookup(&db).source(&db);
if let Some(macro_file) = src.file_id.macro_file() {
if macro_file.is_builtin_derive(&db) {
if let MacroKind::DeriveBuiltIn | MacroKind::Derive = macro_file.kind(&db) {
let pp = pretty_print_macro_expansion(
src.value.syntax().clone(),
db.span_map(macro_file.into()).as_ref(),
Expand Down
10 changes: 7 additions & 3 deletions crates/hir-expand/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, SyntaxToken, TextRange,

use crate::{
db::{self, ExpandDatabase},
map_node_range_up, map_node_range_up_rooted, span_for_offset, MacroFileIdExt,
map_node_range_up, map_node_range_up_rooted, span_for_offset, MacroFileIdExt, MacroKind,
};

/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
Expand Down Expand Up @@ -276,7 +276,11 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> {
HirFileIdRepr::FileId(file_id) => {
return Some(InRealFile { file_id, value: self.value.borrow().clone() })
}
HirFileIdRepr::MacroFile(m) if m.is_attr_macro(db) => m,
HirFileIdRepr::MacroFile(m)
if matches!(m.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn) =>
{
m
}
_ => return None,
};

Expand Down Expand Up @@ -453,7 +457,7 @@ impl<N: AstNode> InFile<N> {
}
HirFileIdRepr::MacroFile(m) => m,
};
if !file_id.is_attr_macro(db) {
if !matches!(file_id.kind(db), MacroKind::Attr | MacroKind::AttrBuiltIn) {
return None;
}

Expand Down
54 changes: 31 additions & 23 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,24 @@ impl HirFileIdExt for HirFileId {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacroKind {
/// `macro_rules!` or Macros 2.0 macro.
Declarative,
/// A built-in function-like macro.
DeclarativeBuiltIn,
/// A custom derive.
Derive,
/// A builtin-in derive.
DeriveBuiltIn,
/// A procedural attribute macro.
Attr,
/// A built-in attribute macro.
AttrBuiltIn,
/// A function-like procedural macro.
ProcMacro,
}

pub trait MacroFileIdExt {
fn is_env_or_option_env(&self, db: &dyn ExpandDatabase) -> bool;
fn is_include_like_macro(&self, db: &dyn ExpandDatabase) -> bool;
Expand All @@ -427,15 +445,12 @@ pub trait MacroFileIdExt {

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

fn is_builtin_derive(&self, db: &dyn ExpandDatabase) -> bool;
fn is_custom_derive(&self, db: &dyn ExpandDatabase) -> bool;
fn kind(&self, db: &dyn ExpandDatabase) -> MacroKind;

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

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

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

fn is_custom_derive(&self, db: &dyn ExpandDatabase) -> bool {
matches!(
db.lookup_intern_macro_call(self.macro_call_id).def.kind,
MacroDefKind::ProcMacro(_, _, ProcMacroKind::CustomDerive)
)
}

fn is_builtin_derive(&self, db: &dyn ExpandDatabase) -> bool {
matches!(
db.lookup_intern_macro_call(self.macro_call_id).def.kind,
MacroDefKind::BuiltInDerive(..)
)
fn kind(&self, db: &dyn ExpandDatabase) -> MacroKind {
match db.lookup_intern_macro_call(self.macro_call_id).def.kind {
MacroDefKind::Declarative(..) => MacroKind::Declarative,
MacroDefKind::BuiltIn(..) | MacroDefKind::BuiltInEager(..) => {
MacroKind::DeclarativeBuiltIn
}
MacroDefKind::BuiltInDerive(..) => MacroKind::DeriveBuiltIn,
MacroDefKind::ProcMacro(_, _, ProcMacroKind::CustomDerive) => MacroKind::Derive,
MacroDefKind::ProcMacro(_, _, ProcMacroKind::Attr) => MacroKind::Attr,
MacroDefKind::ProcMacro(_, _, ProcMacroKind::Bang) => MacroKind::ProcMacro,
MacroDefKind::BuiltInAttr(..) => MacroKind::AttrBuiltIn,
}
}

fn is_include_macro(&self, db: &dyn ExpandDatabase) -> bool {
Expand Down Expand Up @@ -507,13 +522,6 @@ impl MacroFileIdExt for MacroFileId {
}
}

fn is_attr_macro(&self, db: &dyn ExpandDatabase) -> bool {
matches!(
db.lookup_intern_macro_call(self.macro_call_id).def.kind,
MacroDefKind::BuiltInAttr(..) | MacroDefKind::ProcMacro(_, _, ProcMacroKind::Attr)
)
}

fn is_derive_attr_pseudo_expansion(&self, db: &dyn ExpandDatabase) -> bool {
let loc = db.lookup_intern_macro_call(self.macro_call_id);
loc.def.is_attribute_derive()
Expand Down
49 changes: 21 additions & 28 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub use {
name::Name,
prettify_macro_expansion,
proc_macro::{ProcMacros, ProcMacrosBuilder},
tt, ExpandResult, HirFileId, HirFileIdExt, MacroFileId, MacroFileIdExt,
tt, ExpandResult, HirFileId, HirFileIdExt, MacroFileId, MacroFileIdExt, MacroKind,
},
hir_ty::{
consteval::ConstEvalError,
Expand Down Expand Up @@ -699,7 +699,10 @@ impl Module {
let source_map = tree_source_maps.impl_(loc.id.value).item();
let node = &tree[loc.id.value];
let file_id = loc.id.file_id();
if file_id.macro_file().is_some_and(|it| it.is_builtin_derive(db.upcast())) {
if file_id
.macro_file()
.is_some_and(|it| it.kind(db.upcast()) == MacroKind::DeriveBuiltIn)
{
// these expansion come from us, diagnosing them is a waste of resources
// FIXME: Once we diagnose the inputs to builtin derives, we should at least extract those diagnostics somehow
continue;
Expand Down Expand Up @@ -3049,20 +3052,6 @@ impl BuiltinType {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacroKind {
/// `macro_rules!` or Macros 2.0 macro.
Declarative,
/// A built-in or custom derive.
Derive,
/// A built-in function-like macro.
BuiltIn,
/// A procedural attribute macro.
Attr,
/// A function-like procedural macro.
ProcMacro,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Macro {
pub(crate) id: MacroId,
Expand Down Expand Up @@ -3093,15 +3082,19 @@ impl Macro {
match self.id {
MacroId::Macro2Id(it) => match it.lookup(db.upcast()).expander {
MacroExpander::Declarative => MacroKind::Declarative,
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => {
MacroKind::DeclarativeBuiltIn
}
MacroExpander::BuiltInAttr(_) => MacroKind::AttrBuiltIn,
MacroExpander::BuiltInDerive(_) => MacroKind::DeriveBuiltIn,
},
MacroId::MacroRulesId(it) => match it.lookup(db.upcast()).expander {
MacroExpander::Declarative => MacroKind::Declarative,
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
MacroExpander::BuiltIn(_) | MacroExpander::BuiltInEager(_) => {
MacroKind::DeclarativeBuiltIn
}
MacroExpander::BuiltInAttr(_) => MacroKind::AttrBuiltIn,
MacroExpander::BuiltInDerive(_) => MacroKind::DeriveBuiltIn,
},
MacroId::ProcMacroId(it) => match it.lookup(db.upcast()).kind {
ProcMacroKind::CustomDerive => MacroKind::Derive,
Expand All @@ -3112,10 +3105,10 @@ impl Macro {
}

pub fn is_fn_like(&self, db: &dyn HirDatabase) -> bool {
match self.kind(db) {
MacroKind::Declarative | MacroKind::BuiltIn | MacroKind::ProcMacro => true,
MacroKind::Attr | MacroKind::Derive => false,
}
matches!(
self.kind(db),
MacroKind::Declarative | MacroKind::DeclarativeBuiltIn | MacroKind::ProcMacro
)
}

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

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

pub fn is_derive(&self, db: &dyn HirDatabase) -> bool {
matches!(self.kind(db), MacroKind::Derive)
matches!(self.kind(db), MacroKind::Derive | MacroKind::DeriveBuiltIn)
}
}

Expand Down
19 changes: 10 additions & 9 deletions crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,7 @@ impl<'db> SemanticsImpl<'db> {
})
}

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

Expand Down Expand Up @@ -551,10 +549,8 @@ impl<'db> SemanticsImpl<'db> {
res.is_empty().not().then_some(res)
}

pub fn is_attr_macro_call(&self, item: &ast::Item) -> bool {
let file_id = self.find_file(item.syntax()).file_id;
let src = InFile::new(file_id, item);
self.with_ctx(|ctx| ctx.item_to_macro_call(src).is_some())
pub fn is_attr_macro_call(&self, item: InFile<&ast::Item>) -> bool {
self.with_ctx(|ctx| ctx.item_to_macro_call(item).is_some())
}

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

// FIXME: Replace this with `resolve_macro_call2`
pub fn resolve_macro_call(&self, macro_call: &ast::MacroCall) -> Option<Macro> {
let macro_call = self.find_file(macro_call.syntax()).with_value(macro_call);
self.resolve_macro_call2(macro_call)
}

pub fn resolve_macro_call2(&self, macro_call: InFile<&ast::MacroCall>) -> Option<Macro> {
self.with_ctx(|ctx| {
ctx.macro_call_to_macro_call(macro_call)
.and_then(|call| macro_call_to_macro_id(ctx, call))
Expand All @@ -1538,8 +1539,8 @@ impl<'db> SemanticsImpl<'db> {
})
}

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

Expand Down
6 changes: 3 additions & 3 deletions crates/ide-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ pub enum SymbolKind {
impl From<hir::MacroKind> for SymbolKind {
fn from(it: hir::MacroKind) -> Self {
match it {
hir::MacroKind::Declarative | hir::MacroKind::BuiltIn => SymbolKind::Macro,
hir::MacroKind::Declarative | hir::MacroKind::DeclarativeBuiltIn => SymbolKind::Macro,
hir::MacroKind::ProcMacro => SymbolKind::ProcMacro,
hir::MacroKind::Derive => SymbolKind::Derive,
hir::MacroKind::Attr => SymbolKind::Attribute,
hir::MacroKind::Derive | hir::MacroKind::DeriveBuiltIn => SymbolKind::Derive,
hir::MacroKind::Attr | hir::MacroKind::AttrBuiltIn => SymbolKind::Attribute,
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/ide-db/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ impl Definition {
SearchScope::krate(db, module.krate())
}
}
hir::MacroKind::BuiltIn => SearchScope::crate_graph(db),
hir::MacroKind::AttrBuiltIn
| hir::MacroKind::DeriveBuiltIn
| hir::MacroKind::DeclarativeBuiltIn => SearchScope::crate_graph(db),
hir::MacroKind::Derive | hir::MacroKind::Attr | hir::MacroKind::ProcMacro => {
SearchScope::reverse_dependencies(db, module.krate())
}
Expand Down
5 changes: 3 additions & 2 deletions crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,13 @@ fn filename_and_frag_for_def(
}
Definition::Macro(mac) => match mac.kind(db) {
hir::MacroKind::Declarative
| hir::MacroKind::BuiltIn
| hir::MacroKind::AttrBuiltIn
| hir::MacroKind::DeclarativeBuiltIn
| hir::MacroKind::Attr
| hir::MacroKind::ProcMacro => {
format!("macro.{}.html", mac.name(db).as_str())
}
hir::MacroKind::Derive => {
hir::MacroKind::Derive | hir::MacroKind::DeriveBuiltIn => {
format!("derive.{}.html", mac.name(db).as_str())
}
},
Expand Down
10 changes: 5 additions & 5 deletions crates/ide/src/moniker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ pub(crate) fn def_to_kind(db: &RootDatabase, def: Definition) -> SymbolInformati

match def {
Definition::Macro(it) => match it.kind(db) {
MacroKind::Declarative => Macro,
MacroKind::Derive => Attribute,
MacroKind::BuiltIn => Macro,
MacroKind::Attr => Attribute,
MacroKind::ProcMacro => Macro,
MacroKind::Derive
| MacroKind::DeriveBuiltIn
| MacroKind::AttrBuiltIn
| MacroKind::Attr => Attribute,
MacroKind::Declarative | MacroKind::DeclarativeBuiltIn | MacroKind::ProcMacro => Macro,
},
Definition::Field(..) | Definition::TupleField(..) => Field,
Definition::Module(..) | Definition::Crate(..) => Module,
Expand Down
Loading
Loading