]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_expand/src/lib.rs
Merge #11461
[rust.git] / crates / hir_expand / src / lib.rs
index b455fd59172a76e971e1139c4ff697583cd7260b..ba0f10151246519037a551231995e38bd905fb42 100644 (file)
 pub mod proc_macro;
 pub mod quote;
 pub mod eager;
+pub mod mod_path;
+mod fixup;
 
-use base_db::ProcMacroKind;
-use either::Either;
-
-pub use mbe::{ExpandError, ExpandResult, Origin};
+pub use mbe::{Origin, ValueResult};
 
-use std::{hash::Hash, iter, sync::Arc};
+use std::{fmt, hash::Hash, iter, sync::Arc};
 
-use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
+use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange, ProcMacroKind};
+use either::Either;
 use syntax::{
     algo::{self, skip_trivia_token},
     ast::{self, AstNode, HasDocComments},
     builtin_derive_macro::BuiltinDeriveExpander,
     builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
     db::TokenExpander,
+    mod_path::ModPath,
     proc_macro::ProcMacroExpander,
 };
 
+pub type ExpandResult<T> = ValueResult<T, ExpandError>;
+
+#[derive(Debug, PartialEq, Eq, Clone)]
+pub enum ExpandError {
+    UnresolvedProcMacro,
+    Mbe(mbe::ExpandError),
+    Other(Box<str>),
+}
+
+impl From<mbe::ExpandError> for ExpandError {
+    fn from(mbe: mbe::ExpandError) -> Self {
+        Self::Mbe(mbe)
+    }
+}
+
+impl fmt::Display for ExpandError {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match self {
+            ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc-macro"),
+            ExpandError::Mbe(it) => it.fmt(f),
+            ExpandError::Other(it) => f.write_str(it),
+        }
+    }
+}
+
 /// Input to the analyzer is a set of files, where each file is identified by
 /// `FileId` and contains source code. However, another source of source code in
 /// Rust are macros: each macro can be thought of as producing a "temporary
@@ -59,11 +85,13 @@ enum HirFileIdRepr {
     FileId(FileId),
     MacroFile(MacroFile),
 }
+
 impl From<FileId> for HirFileId {
     fn from(id: FileId) -> Self {
         HirFileId(HirFileIdRepr::FileId(id))
     }
 }
+
 impl From<MacroFile> for HirFileId {
     fn from(id: MacroFile) -> Self {
         HirFileId(HirFileIdRepr::MacroFile(id))
@@ -122,22 +150,24 @@ pub enum MacroCallKind {
     },
     Derive {
         ast_id: AstId<ast::Adt>,
-        derive_name: Box<str>,
         /// Syntactical index of the invoking `#[derive]` attribute.
         ///
         /// Outer attributes are counted first, then inner attributes. This does not support
         /// out-of-line modules, which may have attributes spread across 2 files!
         derive_attr_index: u32,
+        /// Index of the derive macro in the derive attribute
+        derive_index: u32,
     },
     Attr {
         ast_id: AstId<ast::Item>,
-        attr_name: Box<str>,
-        attr_args: (tt::Subtree, mbe::TokenMap),
+        attr_args: Arc<(tt::Subtree, mbe::TokenMap)>,
         /// Syntactical index of the invoking `#[attribute]`.
         ///
         /// 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,
     },
 }
 
@@ -149,8 +179,8 @@ pub fn original_file(self, db: &dyn db::AstDatabase) -> FileId {
             HirFileIdRepr::FileId(file_id) => file_id,
             HirFileIdRepr::MacroFile(macro_file) => {
                 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
-                let file_id = match &loc.eager {
-                    Some(EagerCallInfo { included_file: Some(file), .. }) => (*file).into(),
+                let file_id = match loc.eager {
+                    Some(EagerCallInfo { included_file: Some(file), .. }) => file.into(),
                     _ => loc.kind.file_id(),
                 };
                 file_id.original_file(db)
@@ -190,9 +220,18 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> {
 
                 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))
@@ -203,17 +242,13 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> {
                             .to_node(db)
                             .doc_comments_and_attrs()
                             .nth(invoc_attr_index as usize)
-                            .and_then(Either::right)?
+                            .and_then(Either::left)?
                             .token_tree()?;
                         Some(InFile::new(ast_id.file_id, tt))
                     }
                     _ => 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),
@@ -228,16 +263,16 @@ pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> {
     }
 
     /// Indicate it is macro file generated for builtin derive
-    pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option<InFile<ast::Item>> {
+    pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option<InFile<ast::Attr>> {
         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())?))
             }
         }
     }
@@ -247,10 +282,7 @@ pub fn is_custom_derive(&self, db: &dyn db::AstDatabase) -> bool {
             HirFileIdRepr::FileId(_) => false,
             HirFileIdRepr::MacroFile(macro_file) => {
                 let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
-                match loc.def.kind {
-                    MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _) => true,
-                    _ => false,
-                }
+                matches!(loc.def.kind, MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _))
             }
         }
     }
@@ -266,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) => {
@@ -277,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(_))
     }
@@ -300,15 +344,15 @@ pub fn as_lazy_macro(
     }
 
     pub fn ast_id(&self) -> Either<AstId<ast::Macro>, AstId<ast::Fn>> {
-        let id = match &self.kind {
-            MacroDefKind::ProcMacro(.., id) => return Either::Right(*id),
+        let id = match self.kind {
+            MacroDefKind::ProcMacro(.., id) => return Either::Right(id),
             MacroDefKind::Declarative(id)
             | MacroDefKind::BuiltIn(_, id)
             | MacroDefKind::BuiltInAttr(_, id)
             | MacroDefKind::BuiltInDerive(_, id)
             | MacroDefKind::BuiltInEager(_, id) => id,
         };
-        Either::Left(*id)
+        Either::Left(id)
     }
 
     pub fn is_proc_macro(&self) -> bool {
@@ -341,8 +385,29 @@ pub fn to_node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> {
             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())
@@ -357,20 +422,15 @@ pub fn to_node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> {
     /// get only the specific derive that is being referred to.
     pub fn original_call_range(self, db: &dyn db::AstDatabase) -> FileRange {
         let mut kind = self;
-        loop {
+        let file_id = loop {
             match kind.file_id().0 {
                 HirFileIdRepr::MacroFile(file) => {
                     kind = db.lookup_intern_macro_call(file.macro_call_id).kind;
                 }
-                _ => break,
+                HirFileIdRepr::FileId(file_id) => break file_id,
             }
-        }
-
-        // `call_id` is now the outermost macro call, so its location is in a real file.
-        let file_id = match kind.file_id().0 {
-            HirFileIdRepr::FileId(it) => it,
-            HirFileIdRepr::MacroFile(_) => unreachable!("encountered unexpected macro file"),
         };
+
         let range = match kind {
             MacroCallKind::FnLike { ast_id, .. } => ast_id.to_node(db).syntax().text_range(),
             MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
@@ -380,7 +440,7 @@ pub fn original_call_range(self, db: &dyn db::AstDatabase) -> FileRange {
                     .doc_comments_and_attrs()
                     .nth(derive_attr_index as usize)
                     .expect("missing derive")
-                    .expect_right("derive is a doc comment?")
+                    .expect_left("derive is a doc comment?")
                     .syntax()
                     .text_range()
             }
@@ -389,7 +449,7 @@ pub fn original_call_range(self, db: &dyn db::AstDatabase) -> FileRange {
                 .doc_comments_and_attrs()
                 .nth(invoc_attr_index as usize)
                 .expect("missing attribute")
-                .expect_right("attribute macro is a doc comment?")
+                .expect_left("attribute macro is a doc comment?")
                 .syntax()
                 .text_range(),
         };
@@ -411,6 +471,7 @@ fn expand_to(&self) -> ExpandTo {
         match self {
             MacroCallKind::FnLike { expand_to, .. } => *expand_to,
             MacroCallKind::Derive { .. } => ExpandTo::Items,
+            MacroCallKind::Attr { is_derive: true, .. } => ExpandTo::Statements,
             MacroCallKind::Attr { .. } => ExpandTo::Items, // is this always correct?
         }
     }
@@ -432,7 +493,7 @@ pub struct ExpansionInfo {
     attr_input_or_mac_def: Option<InFile<ast::TokenTree>>,
 
     macro_def: Arc<TokenExpander>,
-    macro_arg: Arc<(tt::Subtree, mbe::TokenMap)>,
+    macro_arg: Arc<(tt::Subtree, mbe::TokenMap, fixup::SyntaxFixupUndoInfo)>,
     /// A shift built from `macro_arg`'s subtree, relevant for attributes as the item is the macro arg
     /// and as such we need to shift tokens if they are part of an attributes input instead of their item.
     macro_arg_shift: mbe::Shift,
@@ -477,11 +538,11 @@ pub fn map_token_down(
 
             let token_range = token.value.text_range();
             match &loc.kind {
-                MacroCallKind::Attr { attr_args: (_, map), invoc_attr_index, .. } => {
+                MacroCallKind::Attr { attr_args, invoc_attr_index, is_derive, .. } => {
                     let attr = item
                         .doc_comments_and_attrs()
                         .nth(*invoc_attr_index as usize)
-                        .and_then(Either::right)?;
+                        .and_then(Either::left)?;
                     match attr.token_tree() {
                         Some(token_tree)
                             if token_tree.syntax().text_range().contains_range(token_range) =>
@@ -491,8 +552,13 @@ pub fn map_token_down(
                             let relative_range =
                                 token.value.text_range().checked_sub(attr_input_start)?;
                             // shift by the item's tree's max id
-                            let token_id =
-                                self.macro_arg_shift.shift(map.token_by_range(relative_range)?);
+                            let token_id = attr_args.1.token_by_range(relative_range)?;
+                            let token_id = if *is_derive {
+                                // we do not shift for `#[derive]`, as we only need to downmap the derive attribute tokens
+                                token_id
+                            } else {
+                                self.macro_arg_shift.shift(token_id)
+                            };
                             Some(token_id)
                         }
                         _ => None,
@@ -540,13 +606,16 @@ 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: (_, arg_token_map), .. } => {
+            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
                 match self.macro_arg_shift.unshift(token_id) {
                     Some(unshifted) => {
                         token_id = unshifted;
-                        (arg_token_map, self.attr_input_or_mac_def.clone()?.syntax().cloned())
+                        (&attr_args.1, self.attr_input_or_mac_def.clone()?.syntax().cloned())
                     }
                     None => (&self.macro_arg.1, self.arg.clone()),
                 }
@@ -572,7 +641,6 @@ pub fn map_token_up(
 /// `AstId` points to an AST node in any file.
 ///
 /// It is stable across reparses, and can be used as salsa key/value.
-// FIXME: isn't this just a `Source<FileAstId<N>>` ?
 pub type AstId<N> = InFile<FileAstId<N>>;
 
 impl<N: AstNode> AstId<N> {
@@ -600,7 +668,6 @@ pub fn new(file_id: HirFileId, value: T) -> InFile<T> {
         InFile { file_id, value }
     }
 
-    // Similarly, naming here is stupid...
     pub fn with_value<U>(&self, value: U) -> InFile<U> {
         InFile::new(self.file_id, value)
     }
@@ -697,30 +764,27 @@ pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option<FileRan
     }
 }
 
+impl InFile<SyntaxToken> {
+    pub fn upmap(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxToken>> {
+        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>,
 ) -> Option<InFile<(SyntaxToken, SyntaxToken)>> {
     let expansion = file_id.expansion_info(db)?;
 
-    // the input node has only one token ?
-    let first = skip_trivia_token(node.first_token()?, Direction::Next)?;
-    let last = skip_trivia_token(node.last_token()?, Direction::Prev)?;
-    let is_single_token = first == last;
-
-    node.descendants().find_map(|it| {
-        let first = skip_trivia_token(it.first_token()?, Direction::Next)?;
-        let first = ascend_call_token(db, &expansion, InFile::new(file_id, first))?;
+    let first_token = |node: &SyntaxNode| skip_trivia_token(node.first_token()?, Direction::Next);
+    let last_token = |node: &SyntaxNode| skip_trivia_token(node.last_token()?, Direction::Prev);
 
-        let last = skip_trivia_token(it.last_token()?, Direction::Prev)?;
-        let last = ascend_call_token(db, &expansion, InFile::new(file_id, last))?;
-
-        if (!is_single_token && first == last) || (first.file_id != last.file_id) {
-            return None;
-        }
-
-        Some(InFile::new(first.file_id, (first.value, last.value)))
-    })
+    let first = first_token(node)?;
+    let last = last_token(node)?;
+    let first = ascend_call_token(db, &expansion, InFile::new(file_id, first))?;
+    let last = ascend_call_token(db, &expansion, InFile::new(file_id, last))?;
+    (first.file_id == last.file_id).then(|| InFile::new(first.file_id, (first.value, last.value)))
 }
 
 fn ascend_call_token(
@@ -728,14 +792,14 @@ fn ascend_call_token(
     expansion: &ExpansionInfo,
     token: InFile<SyntaxToken>,
 ) -> Option<InFile<SyntaxToken>> {
-    let (mapped, origin) = expansion.map_token_up(db, token.as_ref())?;
-    if origin != Origin::Call {
-        return None;
-    }
-    if let Some(info) = mapped.file_id.expansion_info(db) {
-        return ascend_call_token(db, &info, mapped);
+    let mut mapping = expansion.map_token_up(db, token.as_ref())?;
+    while let (mapped, Origin::Call) = mapping {
+        match mapped.file_id.expansion_info(db) {
+            Some(info) => mapping = info.map_token_up(db, mapped.as_ref())?,
+            None => return Some(mapped),
+        }
     }
-    Some(mapped)
+    None
 }
 
 impl InFile<SyntaxToken> {
@@ -756,20 +820,28 @@ pub fn descendants<T: AstNode>(self) -> impl Iterator<Item = InFile<T>> {
     }
 
     pub fn original_ast_node(self, db: &dyn db::AstDatabase) -> Option<InFile<N>> {
-        match ascend_node_border_tokens(db, self.syntax()) {
-            Some(InFile { file_id, value: (first, last) }) => {
-                let original_file = file_id.original_file(db);
-                if file_id != original_file.into() {
-                    let range = first.text_range().cover(last.text_range());
-                    tracing::error!("Failed mapping up more for {:?}", range);
-                    return None;
-                }
-                let anc = algo::least_common_ancestor(&first.parent()?, &last.parent()?)?;
-                Some(InFile::new(file_id, anc.ancestors().find_map(N::cast)?))
+        // This kind of upmapping can only be achieved in attribute expanded files,
+        // as we don't have node inputs otherwise and  therefor can't find an `N` node in the input
+        if !self.file_id.is_macro() {
+            return Some(self);
+        } else if !self.file_id.is_attr_macro(db) {
+            return None;
+        }
+
+        if let Some(InFile { file_id, value: (first, last) }) =
+            ascend_node_border_tokens(db, self.syntax())
+        {
+            if file_id.is_macro() {
+                let range = first.text_range().cover(last.text_range());
+                tracing::error!("Failed mapping out of macro file for {:?}", range);
+                return None;
             }
-            _ if !self.file_id.is_macro() => Some(self),
-            _ => None,
+            // FIXME: This heuristic is brittle and with the right macro may select completely unrelated nodes
+            let anc = algo::least_common_ancestor(&first.parent()?, &last.parent()?)?;
+            let value = anc.ancestors().find_map(N::cast)?;
+            return Some(InFile::new(file_id, value));
         }
+        None
     }
 
     pub fn syntax(&self) -> InFile<&SyntaxNode> {
@@ -819,10 +891,10 @@ pub fn from_call_site(call: &ast::MacroCall) -> ExpandTo {
             MACRO_TYPE => ExpandTo::Type,
 
             ARG_LIST | TRY_EXPR | TUPLE_EXPR | PAREN_EXPR | ARRAY_EXPR | FOR_EXPR | PATH_EXPR
-            | CLOSURE_EXPR | CONDITION | BREAK_EXPR | RETURN_EXPR | MATCH_EXPR | MATCH_ARM
-            | MATCH_GUARD | RECORD_EXPR_FIELD | CALL_EXPR | INDEX_EXPR | METHOD_CALL_EXPR
-            | FIELD_EXPR | AWAIT_EXPR | CAST_EXPR | REF_EXPR | PREFIX_EXPR | RANGE_EXPR
-            | BIN_EXPR => ExpandTo::Expr,
+            | CLOSURE_EXPR | BREAK_EXPR | RETURN_EXPR | MATCH_EXPR | MATCH_ARM | MATCH_GUARD
+            | RECORD_EXPR_FIELD | CALL_EXPR | INDEX_EXPR | METHOD_CALL_EXPR | FIELD_EXPR
+            | AWAIT_EXPR | CAST_EXPR | REF_EXPR | PREFIX_EXPR | RANGE_EXPR | BIN_EXPR
+            | LET_EXPR => ExpandTo::Expr,
             LET_STMT => {
                 // FIXME: Handle LHS Pattern
                 ExpandTo::Expr
@@ -835,3 +907,8 @@ pub fn from_call_site(call: &ast::MacroCall) -> ExpandTo {
         }
     }
 }
+
+#[derive(Debug)]
+pub struct UnresolvedMacro {
+    pub path: ModPath,
+}