X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fhir_expand%2Fsrc%2Flib.rs;h=ba0f10151246519037a551231995e38bd905fb42;hb=0b53744f2d7e0694cd7207cca632fd6de1dc5bff;hp=2ccf4c56eebb887667a4a8d77cc15ad39c82f205;hpb=7b89d5ede23cbbbf4bef37b43e0d2d99752ddb51;p=rust.git diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 2ccf4c56eeb..ba0f1015124 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -166,6 +166,7 @@ pub enum MacroCallKind { /// Outer attributes are counted first, then inner attributes. This does not support /// out-of-line modules, which may have attributes spread across 2 files! invoc_attr_index: u32, + /// Whether this attribute is the `#[derive]` attribute. is_derive: bool, }, } @@ -219,9 +220,18 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option { let arg_tt = loc.kind.arg(db)?; + let macro_def = db.macro_def(loc.def).ok()?; + let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?; + let macro_arg = db.macro_arg(macro_file.macro_call_id)?; + let def = loc.def.ast_id().left().and_then(|id| { let def_tt = match id.to_node(db) { ast::Macro::MacroRules(mac) => mac.token_tree()?, + ast::Macro::MacroDef(_) + if matches!(*macro_def, TokenExpander::BuiltinAttr(_)) => + { + return None + } ast::Macro::MacroDef(mac) => mac.body()?, }; Some(InFile::new(id.file_id, def_tt)) @@ -239,10 +249,6 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option { _ => None, }); - let macro_def = db.macro_def(loc.def).ok()?; - let (parse, exp_map) = db.parse_macro_expansion(macro_file).value?; - let macro_arg = db.macro_arg(macro_file.macro_call_id)?; - Some(ExpansionInfo { expanded: InFile::new(self, parse.syntax_node()), arg: InFile::new(loc.kind.file_id(), arg_tt), @@ -257,16 +263,16 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option { } /// Indicate it is macro file generated for builtin derive - pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option> { + pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option> { match self.0 { HirFileIdRepr::FileId(_) => None, HirFileIdRepr::MacroFile(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); - let item = match loc.def.kind { + let attr = match loc.def.kind { MacroDefKind::BuiltInDerive(..) => loc.kind.to_node(db), _ => return None, }; - Some(item.with_value(ast::Item::cast(item.value.clone())?)) + Some(attr.with_value(ast::Attr::cast(attr.value.clone())?)) } } } @@ -292,7 +298,7 @@ pub fn is_include_macro(&self, db: &dyn db::AstDatabase) -> bool { } } - /// Return whether this file is an include macro + /// Return whether this file is an attr macro pub fn is_attr_macro(&self, db: &dyn db::AstDatabase) -> bool { match self.0 { HirFileIdRepr::MacroFile(macro_file) => { @@ -303,6 +309,18 @@ pub fn is_attr_macro(&self, db: &dyn db::AstDatabase) -> bool { } } + /// Return whether this file is the pseudo expansion of the derive attribute. + /// See [`crate::builtin_attr_macro::derive_attr_expand`]. + pub fn is_derive_attr_pseudo_expansion(&self, db: &dyn db::AstDatabase) -> bool { + match self.0 { + HirFileIdRepr::MacroFile(macro_file) => { + let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); + matches!(loc.kind, MacroCallKind::Attr { is_derive: true, .. }) + } + _ => false, + } + } + pub fn is_macro(self) -> bool { matches!(self.0, HirFileIdRepr::MacroFile(_)) } @@ -367,8 +385,29 @@ pub fn to_node(&self, db: &dyn db::AstDatabase) -> InFile { MacroCallKind::FnLike { ast_id, .. } => { ast_id.with_value(ast_id.to_node(db).syntax().clone()) } - MacroCallKind::Derive { ast_id, .. } => { - ast_id.with_value(ast_id.to_node(db).syntax().clone()) + MacroCallKind::Derive { ast_id, derive_attr_index, .. } => { + // FIXME: handle `cfg_attr` + ast_id.with_value(ast_id.to_node(db)).map(|it| { + it.doc_comments_and_attrs() + .nth(*derive_attr_index as usize) + .and_then(|it| match it { + Either::Left(attr) => Some(attr.syntax().clone()), + Either::Right(_) => None, + }) + .unwrap_or_else(|| it.syntax().clone()) + }) + } + MacroCallKind::Attr { ast_id, is_derive: true, invoc_attr_index, .. } => { + // FIXME: handle `cfg_attr` + ast_id.with_value(ast_id.to_node(db)).map(|it| { + it.doc_comments_and_attrs() + .nth(*invoc_attr_index as usize) + .and_then(|it| match it { + Either::Left(attr) => Some(attr.syntax().clone()), + Either::Right(_) => None, + }) + .unwrap_or_else(|| it.syntax().clone()) + }) } MacroCallKind::Attr { ast_id, .. } => { ast_id.with_value(ast_id.to_node(db).syntax().clone()) @@ -567,6 +606,9 @@ pub fn map_token_up( // Attributes are a bit special for us, they have two inputs, the input tokentree and the annotated item. let (token_map, tt) = match &loc.kind { + MacroCallKind::Attr { attr_args, is_derive: true, .. } => { + (&attr_args.1, self.attr_input_or_mac_def.clone()?.syntax().cloned()) + } MacroCallKind::Attr { attr_args, .. } => { // try unshifting the the token id, if unshifting fails, the token resides in the non-item attribute input // note that the `TokenExpander::map_id_up` earlier only unshifts for declarative macros, so we don't double unshift with this @@ -722,6 +764,13 @@ pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option { + pub fn upmap(self, db: &dyn db::AstDatabase) -> Option> { + let expansion = self.file_id.expansion_info(db)?; + expansion.map_token_up(db, self.as_ref()).map(|(it, _)| it) + } +} + fn ascend_node_border_tokens( db: &dyn db::AstDatabase, InFile { file_id, value: node }: InFile<&SyntaxNode>,