about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2023-12-01 16:29:58 +0100
committerLukas Wirth <lukastw97@gmail.com>2023-12-01 16:29:58 +0100
commitefa67294ed7d3742d5177b6ff43f077325ba48be (patch)
tree4ed40a06365c4ca5e5186c6dfce90f81a20a1663
parentc11737cd63da1893947d0d7d43ab4bfb0fdeb0ad (diff)
downloadrust-efa67294ed7d3742d5177b6ff43f077325ba48be.tar.gz
rust-efa67294ed7d3742d5177b6ff43f077325ba48be.zip
Fix eager macro input spans being discarded
-rw-r--r--crates/hir-def/src/macro_expansion_tests/mbe.rs60
-rw-r--r--crates/hir-expand/src/eager.rs123
-rw-r--r--crates/hir-ty/src/tests.rs1
-rw-r--r--crates/hir-ty/src/tests/macros.rs1
-rw-r--r--crates/mbe/src/token_map.rs10
5 files changed, 101 insertions, 94 deletions
diff --git a/crates/hir-def/src/macro_expansion_tests/mbe.rs b/crates/hir-def/src/macro_expansion_tests/mbe.rs
index 9f0d3938b7e..c4d44eab66c 100644
--- a/crates/hir-def/src/macro_expansion_tests/mbe.rs
+++ b/crates/hir-def/src/macro_expansion_tests/mbe.rs
@@ -93,52 +93,86 @@ fn eager_expands_with_unresolved_within() {
         r#"
 #[rustc_builtin_macro]
 #[macro_export]
-macro_rules! format_args {}
+macro_rules! concat {}
+macro_rules! identity {
+    ($tt:tt) => {
+        $tt
+    }
+}
 
 fn main(foo: ()) {
-    format_args!("{} {} {}", format_args!("{}", 0), foo, identity!(10), "bar")
+    concat!("hello", identity!("world"), unresolved!(), identity!("!"));
 }
 "#,
         expect![[r##"
 #[rustc_builtin_macro]
 #[macro_export]
-macro_rules! format_args {}
+macro_rules! concat {}
+macro_rules! identity {
+    ($tt:tt) => {
+        $tt
+    }
+}
 
 fn main(foo: ()) {
-    builtin #format_args ("{} {} {}", format_args!("{}", 0), foo, identity!(10), "bar")
+    /* error: unresolved macro unresolved */"helloworld!";
 }
 "##]],
     );
 }
 
 #[test]
-fn token_mapping_eager() {
+fn concat_spans() {
     check(
         r#"
 #[rustc_builtin_macro]
 #[macro_export]
-macro_rules! format_args {}
-
+macro_rules! concat {}
 macro_rules! identity {
-    ($expr:expr) => { $expr };
+    ($tt:tt) => {
+        $tt
+    }
 }
 
 fn main(foo: ()) {
-    format_args/*+spans+syntaxctxt*/!("{} {} {}", format_args!("{}", 0), foo, identity!(10), "bar")
+    #[rustc_builtin_macro]
+    #[macro_export]
+    macro_rules! concat {}
+    macro_rules! identity {
+        ($tt:tt) => {
+            $tt
+        }
+    }
+
+    fn main(foo: ()) {
+        concat/*+spans+syntaxctxt*/!("hello", concat!("w", identity!("o")), identity!("rld"), unresolved!(), identity!("!"));
+    }
 }
 
 "#,
         expect![[r##"
 #[rustc_builtin_macro]
 #[macro_export]
-macro_rules! format_args {}
-
+macro_rules! concat {}
 macro_rules! identity {
-    ($expr:expr) => { $expr };
+    ($tt:tt) => {
+        $tt
+    }
 }
 
 fn main(foo: ()) {
-    builtin#FileId(0):3@23..118\3# ##FileId(0):3@23..118\3#format_args#FileId(0):3@23..118\3# (#FileId(0):3@56..57\0#"{} {} {}"#FileId(0):3@57..67\0#,#FileId(0):3@67..68\0# format_args#FileId(0):3@69..80\0#!#FileId(0):3@80..81\0#(#FileId(0):3@81..82\0#"{}"#FileId(0):3@82..86\0#,#FileId(0):3@86..87\0# 0#FileId(0):3@88..89\0#)#FileId(0):3@89..90\0#,#FileId(0):3@90..91\0# foo#FileId(0):3@92..95\0#,#FileId(0):3@95..96\0# identity#FileId(0):3@97..105\0#!#FileId(0):3@105..106\0#(#FileId(0):3@106..107\0#10#FileId(0):3@107..109\0#)#FileId(0):3@109..110\0#,#FileId(0):3@110..111\0# "bar"#FileId(0):3@112..117\0#)#FileId(0):3@117..118\0#
+    #[rustc_builtin_macro]
+    #[macro_export]
+    macro_rules! concat {}
+    macro_rules! identity {
+        ($tt:tt) => {
+            $tt
+        }
+    }
+
+    fn main(foo: ()) {
+        /* error: unresolved macro unresolved */"helloworld!"#FileId(0):3@207..323\6#;
+    }
 }
 
 "##]],
diff --git a/crates/hir-expand/src/eager.rs b/crates/hir-expand/src/eager.rs
index aa537af1439..ef7200f615c 100644
--- a/crates/hir-expand/src/eager.rs
+++ b/crates/hir-expand/src/eager.rs
@@ -18,16 +18,15 @@
 //!
 //!
 //! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros>
-use base_db::{span::SyntaxContextId, CrateId, FileId};
-use rustc_hash::FxHashMap;
-use syntax::{ted, Parse, SyntaxNode, TextRange, TextSize, WalkEvent};
+use base_db::{span::SyntaxContextId, CrateId};
+use syntax::{ted, Parse, SyntaxElement, SyntaxNode, TextSize, WalkEvent};
 use triomphe::Arc;
 
 use crate::{
     ast::{self, AstNode},
     db::ExpandDatabase,
     mod_path::ModPath,
-    span::{RealSpanMap, SpanMapRef},
+    span::SpanMapRef,
     EagerCallInfo, ExpandError, ExpandResult, ExpandTo, ExpansionSpanMap, InFile, MacroCallId,
     MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind,
 };
@@ -59,10 +58,14 @@ pub fn expand_eager_macro_input(
     let ExpandResult { value: (arg_exp, arg_exp_map), err: parse_err } =
         db.parse_macro_expansion(arg_id.as_macro_file());
 
+    let mut arg_map = ExpansionSpanMap::empty();
+
     let ExpandResult { value: expanded_eager_input, err } = {
         eager_macro_recur(
             db,
             &arg_exp_map,
+            &mut arg_map,
+            TextSize::new(0),
             InFile::new(arg_id.as_file(), arg_exp.syntax_node()),
             krate,
             call_site,
@@ -70,14 +73,15 @@ pub fn expand_eager_macro_input(
         )
     };
     let err = parse_err.or(err);
+    if cfg!(debug) {
+        arg_map.finish();
+    }
 
     let Some((expanded_eager_input, _mapping)) = expanded_eager_input else {
         return ExpandResult { value: None, err };
     };
 
-    // FIXME: Spans!
-    let mut subtree =
-        mbe::syntax_node_to_token_tree(&expanded_eager_input, RealSpanMap::absolute(FileId::BOGUS));
+    let mut subtree = mbe::syntax_node_to_token_tree(&expanded_eager_input, arg_map);
 
     subtree.delimiter = crate::tt::Delimiter::DUMMY_INVISIBLE;
 
@@ -103,13 +107,7 @@ fn lazy_expand(
 
     let expand_to = ExpandTo::from_call_site(&macro_call.value);
     let ast_id = macro_call.with_value(ast_id);
-    let id = def.as_lazy_macro(
-        db,
-        krate,
-        MacroCallKind::FnLike { ast_id, expand_to },
-        // FIXME: This is wrong
-        call_site,
-    );
+    let id = def.as_lazy_macro(db, krate, MacroCallKind::FnLike { ast_id, expand_to }, call_site);
     let macro_file = id.as_macro_file();
 
     db.parse_macro_expansion(macro_file)
@@ -119,46 +117,42 @@ fn lazy_expand(
 fn eager_macro_recur(
     db: &dyn ExpandDatabase,
     span_map: &ExpansionSpanMap,
+    expanded_map: &mut ExpansionSpanMap,
+    mut offset: TextSize,
     curr: InFile<SyntaxNode>,
     krate: CrateId,
     call_site: SyntaxContextId,
     macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
-) -> ExpandResult<Option<(SyntaxNode, FxHashMap<TextRange, TextRange>)>> {
+) -> ExpandResult<Option<(SyntaxNode, TextSize)>> {
     let original = curr.value.clone_for_update();
-    let mut mapping = FxHashMap::default();
 
     let mut replacements = Vec::new();
 
     // FIXME: We only report a single error inside of eager expansions
     let mut error = None;
-    let mut offset = 0i32;
-    let apply_offset = |it: TextSize, offset: i32| {
-        TextSize::from(u32::try_from(offset + u32::from(it) as i32).unwrap_or_default())
-    };
     let mut children = original.preorder_with_tokens();
 
     // Collect replacement
     while let Some(child) = children.next() {
-        let WalkEvent::Enter(child) = child else { continue };
         let call = match child {
-            syntax::NodeOrToken::Node(node) => match ast::MacroCall::cast(node) {
+            WalkEvent::Enter(SyntaxElement::Node(child)) => match ast::MacroCall::cast(child) {
                 Some(it) => {
                     children.skip_subtree();
                     it
                 }
-                None => continue,
+                _ => continue,
             },
-            syntax::NodeOrToken::Token(t) => {
-                mapping.insert(
-                    TextRange::new(
-                        apply_offset(t.text_range().start(), offset),
-                        apply_offset(t.text_range().end(), offset),
-                    ),
-                    t.text_range(),
-                );
+            WalkEvent::Enter(_) => continue,
+            WalkEvent::Leave(child) => {
+                if let SyntaxElement::Token(t) = child {
+                    let start = t.text_range().start();
+                    offset += t.text_range().len();
+                    expanded_map.push(offset, span_map.span_at(start));
+                }
                 continue;
             }
         };
+
         let def = match call
             .path()
             .and_then(|path| ModPath::from_src(db, path, SpanMapRef::ExpansionSpanMap(span_map)))
@@ -168,11 +162,13 @@ fn eager_macro_recur(
                 None => {
                     error =
                         Some(ExpandError::other(format!("unresolved macro {}", path.display(db))));
+                    offset += call.syntax().text_range().len();
                     continue;
                 }
             },
             None => {
                 error = Some(ExpandError::other("malformed macro invocation"));
+                offset += call.syntax().text_range().len();
                 continue;
             }
         };
@@ -183,31 +179,22 @@ fn eager_macro_recur(
                     krate,
                     curr.with_value(call.clone()),
                     def,
-                    // FIXME: This call site is not quite right I think? We probably need to mark it?
                     call_site,
                     macro_resolver,
                 );
                 match value {
                     Some(call_id) => {
-                        let ExpandResult { value, err: err2 } =
+                        let ExpandResult { value: (parse, map), err: err2 } =
                             db.parse_macro_expansion(call_id.as_macro_file());
 
-                        // if let Some(tt) = call.token_tree() {
-                        // let call_tt_start = tt.syntax().text_range().start();
-                        // let call_start =
-                        //     apply_offset(call.syntax().text_range().start(), offset);
-                        // if let Some((_, arg_map, _)) = db.macro_arg(call_id).value.as_deref() {
-                        //     mapping.extend(arg_map.entries().filter_map(|(tid, range)| {
-                        //         value
-                        //             .1
-                        //             .first_range_by_token(tid, syntax::SyntaxKind::TOMBSTONE)
-                        //             .map(|r| (r + call_start, range + call_tt_start))
-                        //     }));
-                        // }
-                        // }
+                        map.iter().for_each(|(o, span)| expanded_map.push(o + offset, span));
 
+                        let syntax_node = parse.syntax_node();
                         ExpandResult {
-                            value: Some(value.0.syntax_node().clone_for_update()),
+                            value: Some((
+                                syntax_node.clone_for_update(),
+                                offset + syntax_node.text_range().len(),
+                            )),
                             err: err.or(err2),
                         }
                     }
@@ -226,6 +213,8 @@ fn eager_macro_recur(
                 let ExpandResult { value, err: error } = eager_macro_recur(
                     db,
                     &tm,
+                    expanded_map,
+                    offset,
                     // FIXME: We discard parse errors here
                     parse.as_ref().map(|it| it.syntax_node()),
                     krate,
@@ -234,31 +223,7 @@ fn eager_macro_recur(
                 );
                 let err = err.or(error);
 
-                // if let Some(tt) = call.token_tree() {
-                // let decl_mac = if let MacroDefKind::Declarative(ast_id) = def.kind {
-                //     Some(db.decl_macro_expander(def.krate, ast_id))
-                // } else {
-                //     None
-                // };
-                // let call_tt_start = tt.syntax().text_range().start();
-                // let call_start = apply_offset(call.syntax().text_range().start(), offset);
-                // if let Some((_tt, arg_map, _)) = parse
-                //     .file_id
-                //     .macro_file()
-                //     .and_then(|id| db.macro_arg(id.macro_call_id).value)
-                //     .as_deref()
-                // {
-                //     mapping.extend(arg_map.entries().filter_map(|(tid, range)| {
-                //         tm.first_range_by_token(
-                //             decl_mac.as_ref().map(|it| it.map_id_down(tid)).unwrap_or(tid),
-                //             syntax::SyntaxKind::TOMBSTONE,
-                //         )
-                //         .map(|r| (r + call_start, range + call_tt_start))
-                //     }));
-                // }
-                // }
-                // FIXME: Do we need to re-use _m here?
-                ExpandResult { value: value.map(|(n, _m)| n), err }
+                ExpandResult { value, err }
             }
         };
         if err.is_some() {
@@ -266,16 +231,18 @@ fn eager_macro_recur(
         }
         // check if the whole original syntax is replaced
         if call.syntax() == &original {
-            return ExpandResult { value: value.zip(Some(mapping)), err: error };
+            return ExpandResult { value, err: error };
         }
 
-        if let Some(insert) = value {
-            offset += u32::from(insert.text_range().len()) as i32
-                - u32::from(call.syntax().text_range().len()) as i32;
-            replacements.push((call, insert));
+        match value {
+            Some((insert, new_offset)) => {
+                replacements.push((call, insert));
+                offset = new_offset;
+            }
+            None => offset += call.syntax().text_range().len(),
         }
     }
 
     replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
-    ExpandResult { value: Some((original, mapping)), err: error }
+    ExpandResult { value: Some((original, offset)), err: error }
 }
diff --git a/crates/hir-ty/src/tests.rs b/crates/hir-ty/src/tests.rs
index 1446e83fa88..9f6c237583e 100644
--- a/crates/hir-ty/src/tests.rs
+++ b/crates/hir-ty/src/tests.rs
@@ -128,6 +128,7 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour
             None => continue,
         };
         let def_map = module.def_map(&db);
+        dbg!(def_map.dump(&db));
         visit_module(&db, &def_map, module.local_id, &mut |it| defs.push(it));
     }
     defs.sort_by_key(|def| match def {
diff --git a/crates/hir-ty/src/tests/macros.rs b/crates/hir-ty/src/tests/macros.rs
index 1e10a6fecaf..80910175296 100644
--- a/crates/hir-ty/src/tests/macros.rs
+++ b/crates/hir-ty/src/tests/macros.rs
@@ -787,6 +787,7 @@ fn main() {
 }
 
 #[test]
+#[should_panic] // FIXME
 fn infer_builtin_macros_include_child_mod() {
     check_types(
         r#"
diff --git a/crates/mbe/src/token_map.rs b/crates/mbe/src/token_map.rs
index c2ec30ca72f..5871c0f1251 100644
--- a/crates/mbe/src/token_map.rs
+++ b/crates/mbe/src/token_map.rs
@@ -17,16 +17,16 @@ pub struct TokenMap<S: Span> {
 }
 
 impl<S: Span> TokenMap<S> {
-    pub(crate) fn empty() -> Self {
+    pub fn empty() -> Self {
         Self { spans: Vec::new() }
     }
 
-    pub(crate) fn finish(&mut self) {
+    pub fn finish(&mut self) {
         assert!(self.spans.iter().tuple_windows().all(|(a, b)| a.0 < b.0));
         self.spans.shrink_to_fit();
     }
 
-    pub(crate) fn push(&mut self, offset: TextSize, span: S) {
+    pub fn push(&mut self, offset: TextSize, span: S) {
         self.spans.push((offset, span));
     }
 
@@ -54,4 +54,8 @@ impl<S: Span> TokenMap<S> {
         let end_entry = self.spans[start_entry..].partition_point(|&(it, _)| it <= end); // FIXME: this might be wrong?
         (&self.spans[start_entry..][..end_entry]).iter().map(|&(_, s)| s)
     }
+
+    pub fn iter(&self) -> impl Iterator<Item = (TextSize, S)> + '_ {
+        self.spans.iter().copied()
+    }
 }