about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crates/base-db/src/lib.rs2
-rw-r--r--crates/cfg/src/cfg_expr.rs22
-rw-r--r--crates/cfg/src/lib.rs8
-rw-r--r--crates/hir-def/src/attr.rs11
-rw-r--r--crates/hir-def/src/body.rs4
-rw-r--r--crates/hir-def/src/db.rs2
-rw-r--r--crates/hir-def/src/hir.rs2
-rw-r--r--crates/hir-def/src/lib.rs71
-rw-r--r--crates/hir-def/src/macro_expansion_tests/mbe.rs2
-rw-r--r--crates/hir-def/src/macro_expansion_tests/mod.rs4
-rw-r--r--crates/hir-def/src/nameres.rs4
-rw-r--r--crates/hir-def/src/nameres/attr_resolution.rs67
-rw-r--r--crates/hir-def/src/nameres/collector.rs10
-rw-r--r--crates/hir-expand/src/ast_id_map.rs9
-rw-r--r--crates/hir-expand/src/attrs.rs77
-rw-r--r--crates/hir-expand/src/builtin_attr_macro.rs8
-rw-r--r--crates/hir-expand/src/db.rs278
-rw-r--r--crates/hir-expand/src/declarative.rs177
-rw-r--r--crates/hir-expand/src/hygiene.rs40
-rw-r--r--crates/hir-expand/src/lib.rs9
-rw-r--r--crates/hir-expand/src/mod_path.rs118
-rw-r--r--crates/hir-expand/src/span_map.rs37
-rw-r--r--crates/hir-ty/src/diagnostics.rs2
-rw-r--r--crates/hir-ty/src/lib.rs4
-rw-r--r--crates/hir-ty/src/mir.rs4
-rw-r--r--crates/hir-ty/src/tests.rs14
-rw-r--r--crates/hir/src/attrs.rs66
-rw-r--r--crates/hir/src/lib.rs197
-rw-r--r--crates/ide-assists/src/lib.rs38
-rw-r--r--crates/ide-assists/src/utils.rs2
-rw-r--r--crates/ide-completion/src/completions.rs4
-rw-r--r--crates/ide-completion/src/completions/attribute.rs2
-rw-r--r--crates/ide-completion/src/lib.rs2
-rw-r--r--crates/ide-completion/src/render.rs8
-rw-r--r--crates/ide-completion/src/tests.rs2
-rw-r--r--crates/ide-db/src/lib.rs8
-rw-r--r--crates/ide-diagnostics/src/lib.rs14
-rw-r--r--crates/ide-ssr/src/lib.rs2
-rw-r--r--crates/ide/src/inlay_hints.rs4
-rw-r--r--crates/ide/src/lib.rs14
-rw-r--r--crates/ide/src/syntax_highlighting.rs6
-rw-r--r--crates/mbe/src/lib.rs4
-rw-r--r--crates/mbe/src/syntax_bridge.rs10
-rw-r--r--crates/parser/src/grammar.rs4
-rw-r--r--crates/parser/src/grammar/items.rs2
-rw-r--r--crates/parser/src/lib.rs8
-rw-r--r--crates/parser/src/tests.rs2
-rw-r--r--crates/proc-macro-srv/src/lib.rs2
-rw-r--r--crates/proc-macro-srv/src/server.rs2
-rw-r--r--crates/proc-macro-srv/src/server/token_id.rs2
-rw-r--r--crates/profile/src/lib.rs4
-rw-r--r--crates/project-model/src/lib.rs8
-rw-r--r--crates/rust-analyzer/src/cli.rs12
-rw-r--r--crates/rust-analyzer/src/lsp.rs4
-rw-r--r--crates/span/src/lib.rs3
-rw-r--r--crates/stdx/src/lib.rs6
-rw-r--r--crates/syntax/src/ast.rs12
-rw-r--r--crates/syntax/src/lib.rs12
-rw-r--r--editors/code/language-configuration.json6
-rw-r--r--lib/lsp-server/src/lib.rs6
-rw-r--r--rustfmt.toml2
-rw-r--r--xtask/src/main.rs6
62 files changed, 749 insertions, 723 deletions
diff --git a/crates/base-db/src/lib.rs b/crates/base-db/src/lib.rs
index 92d2b9c3f57..90da7efd4a8 100644
--- a/crates/base-db/src/lib.rs
+++ b/crates/base-db/src/lib.rs
@@ -2,8 +2,8 @@
 
 #![warn(rust_2018_idioms, unused_lifetimes)]
 
-mod input;
 mod change;
+mod input;
 
 use std::panic;
 
diff --git a/crates/cfg/src/cfg_expr.rs b/crates/cfg/src/cfg_expr.rs
index fb7505ba2dd..4be6ae7481d 100644
--- a/crates/cfg/src/cfg_expr.rs
+++ b/crates/cfg/src/cfg_expr.rs
@@ -18,28 +18,6 @@ pub enum CfgAtom {
     KeyValue { key: SmolStr, value: SmolStr },
 }
 
-impl CfgAtom {
-    /// Returns `true` when the atom comes from the target specification.
-    ///
-    /// If this returns `true`, then changing this atom requires changing the compilation target. If
-    /// it returns `false`, the atom might come from a build script or the build system.
-    pub fn is_target_defined(&self) -> bool {
-        match self {
-            CfgAtom::Flag(flag) => matches!(&**flag, "unix" | "windows"),
-            CfgAtom::KeyValue { key, value: _ } => matches!(
-                &**key,
-                "target_arch"
-                    | "target_os"
-                    | "target_env"
-                    | "target_family"
-                    | "target_endian"
-                    | "target_pointer_width"
-                    | "target_vendor" // NOTE: `target_feature` is left out since it can be configured via `-Ctarget-feature`
-            ),
-        }
-    }
-}
-
 impl fmt::Display for CfgAtom {
     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         match self {
diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs
index 6b178e7b04a..454d6fc5384 100644
--- a/crates/cfg/src/lib.rs
+++ b/crates/cfg/src/lib.rs
@@ -131,11 +131,9 @@ impl CfgDiff {
     /// of both.
     pub fn new(enable: Vec<CfgAtom>, disable: Vec<CfgAtom>) -> Option<CfgDiff> {
         let mut occupied = FxHashSet::default();
-        for item in enable.iter().chain(disable.iter()) {
-            if !occupied.insert(item) {
-                // was present
-                return None;
-            }
+        if enable.iter().chain(disable.iter()).any(|item| !occupied.insert(item)) {
+            // was present
+            return None;
         }
 
         Some(CfgDiff { enable, disable })
diff --git a/crates/hir-def/src/attr.rs b/crates/hir-def/src/attr.rs
index 8fbfcc81d28..a8e081705e3 100644
--- a/crates/hir-def/src/attr.rs
+++ b/crates/hir-def/src/attr.rs
@@ -32,6 +32,7 @@ use crate::{
     VariantId,
 };
 
+/// Desugared attributes of an item post `cfg_attr` expansion.
 #[derive(Default, Debug, Clone, PartialEq, Eq)]
 pub struct Attrs(RawAttrs);
 
@@ -228,7 +229,6 @@ pub enum DocAtom {
     KeyValue { key: SmolStr, value: SmolStr },
 }
 
-// Adapted from `CfgExpr` parsing code
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub enum DocExpr {
     Invalid,
@@ -448,10 +448,7 @@ impl AttrsWithOwner {
                 let map = db.fields_attrs_source_map(id.parent);
                 let file_id = id.parent.file_id(db);
                 let root = db.parse_or_expand(file_id);
-                let owner = match &map[id.local_id] {
-                    Either::Left(it) => ast::AnyHasAttrs::new(it.to_node(&root)),
-                    Either::Right(it) => ast::AnyHasAttrs::new(it.to_node(&root)),
-                };
+                let owner = ast::AnyHasAttrs::new(map[id.local_id].to_node(&root));
                 InFile::new(file_id, owner)
             }
             AttrDefId::AdtId(adt) => match adt {
@@ -634,7 +631,7 @@ fn attrs_from_item_tree_assoc<'db, N: ItemTreeModItemNode>(
 pub(crate) fn fields_attrs_source_map(
     db: &dyn DefDatabase,
     def: VariantId,
-) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>> {
+) -> Arc<ArenaMap<LocalFieldId, AstPtr<Either<ast::TupleField, ast::RecordField>>>> {
     let mut res = ArenaMap::default();
     let child_source = def.child_source(db);
 
@@ -643,7 +640,7 @@ pub(crate) fn fields_attrs_source_map(
             idx,
             variant
                 .as_ref()
-                .either(|l| Either::Left(AstPtr::new(l)), |r| Either::Right(AstPtr::new(r))),
+                .either(|l| AstPtr::new(l).wrap_left(), |r| AstPtr::new(r).wrap_right()),
         );
     }
 
diff --git a/crates/hir-def/src/body.rs b/crates/hir-def/src/body.rs
index 81132d73853..e4308c6b7f1 100644
--- a/crates/hir-def/src/body.rs
+++ b/crates/hir-def/src/body.rs
@@ -1,10 +1,10 @@
 //! Defines `Body`: a lowered representation of bodies of functions, statics and
 //! consts.
 mod lower;
+mod pretty;
+pub mod scope;
 #[cfg(test)]
 mod tests;
-pub mod scope;
-mod pretty;
 
 use std::ops::Index;
 
diff --git a/crates/hir-def/src/db.rs b/crates/hir-def/src/db.rs
index c9789ceb207..708abb53693 100644
--- a/crates/hir-def/src/db.rs
+++ b/crates/hir-def/src/db.rs
@@ -194,7 +194,7 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
     fn fields_attrs_source_map(
         &self,
         def: VariantId,
-    ) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>>;
+    ) -> Arc<ArenaMap<LocalFieldId, AstPtr<Either<ast::TupleField, ast::RecordField>>>>;
 
     #[salsa::invoke(AttrsWithOwner::attrs_query)]
     fn attrs(&self, def: AttrDefId) -> Attrs;
diff --git a/crates/hir-def/src/hir.rs b/crates/hir-def/src/hir.rs
index 1a33868a78c..ac44d379415 100644
--- a/crates/hir-def/src/hir.rs
+++ b/crates/hir-def/src/hir.rs
@@ -12,8 +12,8 @@
 //!
 //! See also a neighboring `body` module.
 
-pub mod type_ref;
 pub mod format_args;
+pub mod type_ref;
 
 use std::fmt;
 
diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs
index 790e3b414b6..243de663977 100644
--- a/crates/hir-def/src/lib.rs
+++ b/crates/hir-def/src/lib.rs
@@ -25,13 +25,13 @@ extern crate ra_ap_rustc_abi as rustc_abi;
 pub mod db;
 
 pub mod attr;
-pub mod path;
 pub mod builtin_type;
-pub mod per_ns;
 pub mod item_scope;
+pub mod path;
+pub mod per_ns;
 
-pub mod lower;
 pub mod expander;
+pub mod lower;
 
 pub mod dyn_map;
 
@@ -46,24 +46,24 @@ pub use self::hir::type_ref;
 pub mod body;
 pub mod resolver;
 
-mod trace;
 pub mod nameres;
+mod trace;
 
-pub mod src;
 pub mod child_by_source;
+pub mod src;
 
-pub mod visibility;
 pub mod find_path;
 pub mod import_map;
+pub mod visibility;
 
 pub use rustc_abi as layout;
 use triomphe::Arc;
 
 #[cfg(test)]
-mod test_db;
-#[cfg(test)]
 mod macro_expansion_tests;
 mod pretty;
+#[cfg(test)]
+mod test_db;
 
 use std::{
     hash::{Hash, Hasher},
@@ -73,7 +73,6 @@ use std::{
 use base_db::{impl_intern_key, salsa, CrateId, Edition};
 use hir_expand::{
     ast_id_map::{AstIdNode, FileAstId},
-    attrs::{Attr, AttrId, AttrInput},
     builtin_attr_macro::BuiltinAttrExpander,
     builtin_derive_macro::BuiltinDeriveExpander,
     builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
@@ -1274,60 +1273,6 @@ fn macro_call_as_call_id_with_eager(
     Ok(res)
 }
 
-fn derive_macro_as_call_id(
-    db: &dyn DefDatabase,
-    item_attr: &AstIdWithPath<ast::Adt>,
-    derive_attr_index: AttrId,
-    derive_pos: u32,
-    call_site: Span,
-    krate: CrateId,
-    resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
-) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
-    let (macro_id, def_id) = resolver(item_attr.path.clone())
-        .filter(|(_, def_id)| def_id.is_derive())
-        .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
-    let call_id = def_id.as_lazy_macro(
-        db.upcast(),
-        krate,
-        MacroCallKind::Derive {
-            ast_id: item_attr.ast_id,
-            derive_index: derive_pos,
-            derive_attr_index,
-        },
-        call_site,
-    );
-    Ok((macro_id, def_id, call_id))
-}
-
-fn attr_macro_as_call_id(
-    db: &dyn DefDatabase,
-    item_attr: &AstIdWithPath<ast::Item>,
-    macro_attr: &Attr,
-    krate: CrateId,
-    def: MacroDefId,
-) -> MacroCallId {
-    let arg = match macro_attr.input.as_deref() {
-        Some(AttrInput::TokenTree(tt)) => {
-            let mut tt = tt.as_ref().clone();
-            tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
-            Some(tt)
-        }
-
-        _ => None,
-    };
-
-    def.as_lazy_macro(
-        db.upcast(),
-        krate,
-        MacroCallKind::Attr {
-            ast_id: item_attr.ast_id,
-            attr_args: arg.map(Arc::new),
-            invoc_attr_index: macro_attr.id,
-        },
-        macro_attr.span,
-    )
-}
-
 #[derive(Debug)]
 pub struct UnresolvedMacro {
     pub path: hir_expand::mod_path::ModPath,
diff --git a/crates/hir-def/src/macro_expansion_tests/mbe.rs b/crates/hir-def/src/macro_expansion_tests/mbe.rs
index 6d365bd93c0..d0ae1f59f7c 100644
--- a/crates/hir-def/src/macro_expansion_tests/mbe.rs
+++ b/crates/hir-def/src/macro_expansion_tests/mbe.rs
@@ -1,11 +1,11 @@
 //! Tests specific to declarative macros, aka macros by example. This covers
 //! both stable `macro_rules!` macros as well as unstable `macro` macros.
 
-mod tt_conversion;
 mod matching;
 mod meta_syntax;
 mod metavar_expr;
 mod regression;
+mod tt_conversion;
 
 use expect_test::expect;
 
diff --git a/crates/hir-def/src/macro_expansion_tests/mod.rs b/crates/hir-def/src/macro_expansion_tests/mod.rs
index 550ce35f127..ec299405387 100644
--- a/crates/hir-def/src/macro_expansion_tests/mod.rs
+++ b/crates/hir-def/src/macro_expansion_tests/mod.rs
@@ -9,9 +9,9 @@
 //! write unit-tests (in fact, we used to do that), but that makes tests brittle
 //! and harder to understand.
 
-mod mbe;
-mod builtin_fn_macro;
 mod builtin_derive_macro;
+mod builtin_fn_macro;
+mod mbe;
 mod proc_macros;
 
 use std::{iter, ops::Range, sync};
diff --git a/crates/hir-def/src/nameres.rs b/crates/hir-def/src/nameres.rs
index 7eb2f3adddb..2295df16fdc 100644
--- a/crates/hir-def/src/nameres.rs
+++ b/crates/hir-def/src/nameres.rs
@@ -48,11 +48,11 @@
 //! the result
 
 pub mod attr_resolution;
-pub mod proc_macro;
-pub mod diagnostics;
 mod collector;
+pub mod diagnostics;
 mod mod_resolution;
 mod path_resolution;
+pub mod proc_macro;
 
 #[cfg(test)]
 mod tests;
diff --git a/crates/hir-def/src/nameres/attr_resolution.rs b/crates/hir-def/src/nameres/attr_resolution.rs
index 6288b8366bf..1cadae8c87c 100644
--- a/crates/hir-def/src/nameres/attr_resolution.rs
+++ b/crates/hir-def/src/nameres/attr_resolution.rs
@@ -1,16 +1,21 @@
 //! Post-nameres attribute resolution.
 
-use hir_expand::{attrs::Attr, MacroCallId};
+use base_db::CrateId;
+use hir_expand::{
+    attrs::{Attr, AttrId, AttrInput},
+    MacroCallId, MacroCallKind, MacroDefId,
+};
+use span::Span;
 use syntax::{ast, SmolStr};
+use triomphe::Arc;
 
 use crate::{
     attr::builtin::{find_builtin_attr_idx, TOOL_MODULES},
-    attr_macro_as_call_id,
     db::DefDatabase,
     item_scope::BuiltinShadowMode,
     nameres::path_resolution::ResolveMode,
-    path::{ModPath, PathKind},
-    AstIdWithPath, LocalModuleId, UnresolvedMacro,
+    path::{self, ModPath, PathKind},
+    AstIdWithPath, LocalModuleId, MacroId, UnresolvedMacro,
 };
 
 use super::{DefMap, MacroSubNs};
@@ -93,3 +98,57 @@ impl DefMap {
         false
     }
 }
+
+pub(super) fn attr_macro_as_call_id(
+    db: &dyn DefDatabase,
+    item_attr: &AstIdWithPath<ast::Item>,
+    macro_attr: &Attr,
+    krate: CrateId,
+    def: MacroDefId,
+) -> MacroCallId {
+    let arg = match macro_attr.input.as_deref() {
+        Some(AttrInput::TokenTree(tt)) => {
+            let mut tt = tt.as_ref().clone();
+            tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
+            Some(tt)
+        }
+
+        _ => None,
+    };
+
+    def.as_lazy_macro(
+        db.upcast(),
+        krate,
+        MacroCallKind::Attr {
+            ast_id: item_attr.ast_id,
+            attr_args: arg.map(Arc::new),
+            invoc_attr_index: macro_attr.id,
+        },
+        macro_attr.span,
+    )
+}
+
+pub(super) fn derive_macro_as_call_id(
+    db: &dyn DefDatabase,
+    item_attr: &AstIdWithPath<ast::Adt>,
+    derive_attr_index: AttrId,
+    derive_pos: u32,
+    call_site: Span,
+    krate: CrateId,
+    resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
+) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
+    let (macro_id, def_id) = resolver(item_attr.path.clone())
+        .filter(|(_, def_id)| def_id.is_derive())
+        .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
+    let call_id = def_id.as_lazy_macro(
+        db.upcast(),
+        krate,
+        MacroCallKind::Derive {
+            ast_id: item_attr.ast_id,
+            derive_index: derive_pos,
+            derive_attr_index,
+        },
+        call_site,
+    );
+    Ok((macro_id, def_id, call_id))
+}
diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs
index 1c0f4d4d35f..248d3213d5d 100644
--- a/crates/hir-def/src/nameres/collector.rs
+++ b/crates/hir-def/src/nameres/collector.rs
@@ -30,9 +30,7 @@ use triomphe::Arc;
 
 use crate::{
     attr::Attrs,
-    attr_macro_as_call_id,
     db::DefDatabase,
-    derive_macro_as_call_id,
     item_scope::{ImportId, ImportOrExternCrate, ImportType, PerNsGlobImports},
     item_tree::{
         self, ExternCrate, Fields, FileItemTreeId, ImportKind, ItemTree, ItemTreeId,
@@ -40,6 +38,7 @@ use crate::{
     },
     macro_call_as_call_id, macro_call_as_call_id_with_eager,
     nameres::{
+        attr_resolution::{attr_macro_as_call_id, derive_macro_as_call_id},
         diagnostics::DefDiagnostic,
         mod_resolution::ModDir,
         path_resolution::ReachedFixedPoint,
@@ -1245,7 +1244,9 @@ impl DefCollector<'_> {
                         MacroDefId { kind: MacroDefKind::BuiltInAttr(expander, _),.. }
                         if expander.is_derive()
                     ) {
-                        // Resolved to `#[derive]`
+                        // Resolved to `#[derive]`, we don't actually expand this attribute like
+                        // normal (as that would just be an identity expansion with extra output)
+                        // Instead we treat derive attributes special and apply them separately.
 
                         let item_tree = tree.item_tree(self.db);
                         let ast_adt_id: FileAstId<ast::Adt> = match *mod_item {
@@ -1284,7 +1285,8 @@ impl DefCollector<'_> {
                                 }
 
                                 // We treat the #[derive] macro as an attribute call, but we do not resolve it for nameres collection.
-                                // This is just a trick to be able to resolve the input to derives as proper paths.
+                                // This is just a trick to be able to resolve the input to derives
+                                // as proper paths in `Semantics`.
                                 // Check the comment in [`builtin_attr_macro`].
                                 let call_id = attr_macro_as_call_id(
                                     self.db,
diff --git a/crates/hir-expand/src/ast_id_map.rs b/crates/hir-expand/src/ast_id_map.rs
index 7bdd6db9321..530f10a0684 100644
--- a/crates/hir-expand/src/ast_id_map.rs
+++ b/crates/hir-expand/src/ast_id_map.rs
@@ -155,7 +155,14 @@ impl PartialEq for AstIdMap {
 impl Eq for AstIdMap {}
 
 impl AstIdMap {
-    pub(crate) fn from_source(node: &SyntaxNode) -> AstIdMap {
+    pub(crate) fn ast_id_map(
+        db: &dyn ExpandDatabase,
+        file_id: span::HirFileId,
+    ) -> triomphe::Arc<AstIdMap> {
+        triomphe::Arc::new(AstIdMap::from_source(&db.parse_or_expand(file_id)))
+    }
+
+    fn from_source(node: &SyntaxNode) -> AstIdMap {
         assert!(node.parent().is_none());
         let mut res = AstIdMap::default();
 
diff --git a/crates/hir-expand/src/attrs.rs b/crates/hir-expand/src/attrs.rs
index 67a318afd06..30d38299d99 100644
--- a/crates/hir-expand/src/attrs.rs
+++ b/crates/hir-expand/src/attrs.rs
@@ -117,14 +117,10 @@ impl RawAttrs {
                 None => return smallvec![attr.clone()],
             };
             let index = attr.id;
-            let attrs =
-                parts.enumerate().take(1 << AttrId::CFG_ATTR_BITS).filter_map(|(idx, attr)| {
-                    let tree = Subtree {
-                        delimiter: tt::Delimiter::invisible_spanned(attr.first()?.first_span()),
-                        token_trees: attr.to_vec(),
-                    };
-                    Attr::from_tt(db, &tree, index.with_cfg_attr(idx))
-                });
+            let attrs = parts
+                .enumerate()
+                .take(1 << AttrId::CFG_ATTR_BITS)
+                .filter_map(|(idx, attr)| Attr::from_tt(db, attr, index.with_cfg_attr(idx)));
 
             let cfg_options = &crate_graph[krate].cfg_options;
             let cfg = Subtree { delimiter: subtree.delimiter, token_trees: cfg.to_vec() };
@@ -222,12 +218,40 @@ impl Attr {
         Some(Attr { id, path, input, span })
     }
 
-    fn from_tt(db: &dyn ExpandDatabase, tt: &tt::Subtree, id: AttrId) -> Option<Attr> {
-        // FIXME: Unecessary roundtrip tt -> ast -> tt
-        let (parse, map) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MetaItem);
-        let ast = ast::Meta::cast(parse.syntax_node())?;
-
-        Self::from_src(db, ast, SpanMapRef::ExpansionSpanMap(&map), id)
+    fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree], id: AttrId) -> Option<Attr> {
+        let span = tt.first()?.first_span();
+        let path_end = tt
+            .iter()
+            .position(|tt| {
+                !matches!(
+                    tt,
+                    tt::TokenTree::Leaf(
+                        tt::Leaf::Punct(tt::Punct { char: ':' | '$', .. }) | tt::Leaf::Ident(_),
+                    )
+                )
+            })
+            .unwrap_or_else(|| tt.len());
+
+        let (path, input) = tt.split_at(path_end);
+        let path = Interned::new(ModPath::from_tt(db, path)?);
+
+        let input = match input.get(0) {
+            Some(tt::TokenTree::Subtree(tree)) => {
+                Some(Interned::new(AttrInput::TokenTree(Box::new(tree.clone()))))
+            }
+            Some(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct { char: '=', .. }))) => {
+                let input = match input.get(1) {
+                    Some(tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal { text, .. }))) => {
+                        //FIXME the trimming here isn't quite right, raw strings are not handled
+                        Some(Interned::new(AttrInput::Literal(text.trim_matches('"').into())))
+                    }
+                    _ => None,
+                };
+                input
+            }
+            _ => None,
+        };
+        Some(Attr { id, path, input, span })
     }
 
     pub fn path(&self) -> &ModPath {
@@ -277,29 +301,8 @@ impl Attr {
             .token_trees
             .split(|tt| matches!(tt, tt::TokenTree::Leaf(tt::Leaf::Punct(Punct { char: ',', .. }))))
             .filter_map(move |tts| {
-                if tts.is_empty() {
-                    return None;
-                }
-                // FIXME: This is necessarily a hack. It'd be nice if we could avoid allocation
-                // here or maybe just parse a mod path from a token tree directly
-                let subtree = tt::Subtree {
-                    delimiter: tt::Delimiter::invisible_spanned(tts.first()?.first_span()),
-                    token_trees: tts.to_vec(),
-                };
-                let (parse, span_map) =
-                    mbe::token_tree_to_syntax_node(&subtree, mbe::TopEntryPoint::MetaItem);
-                let meta = ast::Meta::cast(parse.syntax_node())?;
-                // Only simple paths are allowed.
-                if meta.eq_token().is_some() || meta.expr().is_some() || meta.token_tree().is_some()
-                {
-                    return None;
-                }
-                let path = meta.path()?;
-                let call_site = span_map.span_at(path.syntax().text_range().start());
-                Some((
-                    ModPath::from_src(db, path, SpanMapRef::ExpansionSpanMap(&span_map))?,
-                    call_site,
-                ))
+                let span = tts.first()?.first_span();
+                Some((ModPath::from_tt(db, tts)?, span))
             });
 
         Some(paths)
diff --git a/crates/hir-expand/src/builtin_attr_macro.rs b/crates/hir-expand/src/builtin_attr_macro.rs
index 55157abe671..dd2aa94ad01 100644
--- a/crates/hir-expand/src/builtin_attr_macro.rs
+++ b/crates/hir-expand/src/builtin_attr_macro.rs
@@ -48,11 +48,13 @@ impl BuiltinAttrExpander {
 
 register_builtin! { expand:
     (bench, Bench) => dummy_attr_expand,
+    (cfg, Cfg) => dummy_attr_expand,
+    (cfg_attr, CfgAttr) => dummy_attr_expand,
     (cfg_accessible, CfgAccessible) => dummy_attr_expand,
     (cfg_eval, CfgEval) => dummy_attr_expand,
-    (derive, Derive) => derive_attr_expand,
+    (derive, Derive) => derive_expand,
     // derive const is equivalent to derive for our proposes.
-    (derive_const, DeriveConst) => derive_attr_expand,
+    (derive_const, DeriveConst) => derive_expand,
     (global_allocator, GlobalAllocator) => dummy_attr_expand,
     (test, Test) => dummy_attr_expand,
     (test_case, TestCase) => dummy_attr_expand
@@ -91,7 +93,7 @@ fn dummy_attr_expand(
 /// always resolve as a derive without nameres recollecting them.
 /// So this hacky approach is a lot more friendly for us, though it does require a bit of support in
 /// [`hir::Semantics`] to make this work.
-fn derive_attr_expand(
+fn derive_expand(
     db: &dyn ExpandDatabase,
     id: MacroCallId,
     tt: &tt::Subtree,
diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs
index 2f8c0951b14..8c43017971f 100644
--- a/crates/hir-expand/src/db.rs
+++ b/crates/hir-expand/src/db.rs
@@ -1,16 +1,11 @@
 //! Defines database & queries for macro expansion.
 
-use std::sync::OnceLock;
-
-use base_db::{
-    salsa::{self, debug::DebugQueryTable},
-    CrateId, Edition, FileId, SourceDatabase, VersionReq,
-};
+use base_db::{salsa, CrateId, FileId, SourceDatabase};
 use either::Either;
 use limit::Limit;
 use mbe::{syntax_node_to_token_tree, ValueResult};
 use rustc_hash::FxHashSet;
-use span::{Span, SyntaxContextId};
+use span::SyntaxContextId;
 use syntax::{
     ast::{self, HasAttrs},
     AstNode, Parse, SyntaxError, SyntaxNode, SyntaxToken, T,
@@ -19,13 +14,14 @@ use triomphe::Arc;
 
 use crate::{
     ast_id_map::AstIdMap,
-    attrs::{collect_attrs, RawAttrs},
+    attrs::collect_attrs,
     builtin_attr_macro::pseudo_derive_attr_expansion,
     builtin_fn_macro::EagerExpander,
+    declarative::DeclarativeMacroExpander,
     fixup::{self, reverse_fixups, SyntaxFixupUndoInfo},
     hygiene::{
-        apply_mark, span_with_call_site_ctxt, span_with_def_site_ctxt, span_with_mixed_site_ctxt,
-        SyntaxContextData, Transparency,
+        span_with_call_site_ctxt, span_with_def_site_ctxt, span_with_mixed_site_ctxt,
+        SyntaxContextData,
     },
     proc_macro::ProcMacros,
     span_map::{RealSpanMap, SpanMap, SpanMapRef},
@@ -44,82 +40,6 @@ use crate::{
 static TOKEN_LIMIT: Limit = Limit::new(1_048_576);
 
 #[derive(Debug, Clone, Eq, PartialEq)]
-/// Old-style `macro_rules` or the new macros 2.0
-pub struct DeclarativeMacroExpander {
-    pub mac: mbe::DeclarativeMacro<span::Span>,
-    pub transparency: Transparency,
-}
-
-// FIXME: Remove this once we drop support for 1.76
-static REQUIREMENT: OnceLock<VersionReq> = OnceLock::new();
-
-impl DeclarativeMacroExpander {
-    pub fn expand(
-        &self,
-        db: &dyn ExpandDatabase,
-        tt: tt::Subtree,
-        call_id: MacroCallId,
-    ) -> ExpandResult<tt::Subtree> {
-        let loc = db.lookup_intern_macro_call(call_id);
-        let toolchain = &db.crate_graph()[loc.def.krate].toolchain;
-        let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
-            REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
-                &base_db::Version {
-                    pre: base_db::Prerelease::EMPTY,
-                    build: base_db::BuildMetadata::EMPTY,
-                    major: version.major,
-                    minor: version.minor,
-                    patch: version.patch,
-                },
-            )
-        });
-        match self.mac.err() {
-            Some(e) => ExpandResult::new(
-                tt::Subtree::empty(tt::DelimSpan { open: loc.call_site, close: loc.call_site }),
-                ExpandError::other(format!("invalid macro definition: {e}")),
-            ),
-            None => self
-                .mac
-                .expand(
-                    &tt,
-                    |s| s.ctx = apply_mark(db, s.ctx, call_id, self.transparency),
-                    new_meta_vars,
-                    loc.call_site,
-                )
-                .map_err(Into::into),
-        }
-    }
-
-    pub fn expand_unhygienic(
-        &self,
-        db: &dyn ExpandDatabase,
-        tt: tt::Subtree,
-        krate: CrateId,
-        call_site: Span,
-    ) -> ExpandResult<tt::Subtree> {
-        let toolchain = &db.crate_graph()[krate].toolchain;
-        let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
-            REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
-                &base_db::Version {
-                    pre: base_db::Prerelease::EMPTY,
-                    build: base_db::BuildMetadata::EMPTY,
-                    major: version.major,
-                    minor: version.minor,
-                    patch: version.patch,
-                },
-            )
-        });
-        match self.mac.err() {
-            Some(e) => ExpandResult::new(
-                tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
-                ExpandError::other(format!("invalid macro definition: {e}")),
-            ),
-            None => self.mac.expand(&tt, |_| (), new_meta_vars, call_site).map_err(Into::into),
-        }
-    }
-}
-
-#[derive(Debug, Clone, Eq, PartialEq)]
 pub enum TokenExpander {
     /// Old-style `macro_rules` or the new macros 2.0
     DeclarativeMacro(Arc<DeclarativeMacroExpander>),
@@ -141,6 +61,7 @@ pub trait ExpandDatabase: SourceDatabase {
     #[salsa::input]
     fn proc_macros(&self) -> Arc<ProcMacros>;
 
+    #[salsa::invoke(AstIdMap::ast_id_map)]
     fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>;
 
     /// Main public API -- parses a hir file, not caring whether it's a real
@@ -156,8 +77,10 @@ pub trait ExpandDatabase: SourceDatabase {
         macro_file: MacroFileId,
     ) -> ExpandResult<(Parse<SyntaxNode>, Arc<ExpansionSpanMap>)>;
     #[salsa::transparent]
+    #[salsa::invoke(SpanMap::new)]
     fn span_map(&self, file_id: HirFileId) -> SpanMap;
 
+    #[salsa::invoke(crate::span_map::real_span_map)]
     fn real_span_map(&self, file_id: FileId) -> Arc<RealSpanMap>;
 
     /// Macro ids. That's probably the tricksiest bit in rust-analyzer, and the
@@ -173,6 +96,7 @@ pub trait ExpandDatabase: SourceDatabase {
     #[salsa::transparent]
     fn setup_syntax_context_root(&self) -> ();
     #[salsa::transparent]
+    #[salsa::invoke(crate::hygiene::dump_syntax_contexts)]
     fn dump_syntax_contexts(&self) -> String;
 
     /// Lowers syntactic macro call to a token tree representation. That's a firewall
@@ -184,8 +108,10 @@ pub trait ExpandDatabase: SourceDatabase {
     ) -> ValueResult<Option<(Arc<tt::Subtree>, SyntaxFixupUndoInfo)>, Arc<Box<[SyntaxError]>>>;
     /// Fetches the expander for this macro.
     #[salsa::transparent]
+    #[salsa::invoke(TokenExpander::macro_expander)]
     fn macro_expander(&self, id: MacroDefId) -> TokenExpander;
     /// Fetches (and compiles) the expander of this decl macro.
+    #[salsa::invoke(DeclarativeMacroExpander::expander)]
     fn decl_macro_expander(
         &self,
         def_crate: CrateId,
@@ -203,36 +129,6 @@ pub trait ExpandDatabase: SourceDatabase {
     ) -> ExpandResult<Box<[SyntaxError]>>;
 }
 
-#[inline]
-pub fn span_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> SpanMap {
-    match file_id.repr() {
-        HirFileIdRepr::FileId(file_id) => SpanMap::RealSpanMap(db.real_span_map(file_id)),
-        HirFileIdRepr::MacroFile(m) => {
-            SpanMap::ExpansionSpanMap(db.parse_macro_expansion(m).value.1)
-        }
-    }
-}
-
-pub fn real_span_map(db: &dyn ExpandDatabase, file_id: FileId) -> Arc<RealSpanMap> {
-    use syntax::ast::HasModuleItem;
-    let mut pairs = vec![(syntax::TextSize::new(0), span::ROOT_ERASED_FILE_AST_ID)];
-    let ast_id_map = db.ast_id_map(file_id.into());
-    let tree = db.parse(file_id).tree();
-    // FIXME: Descend into modules and other item containing items that are not annotated with attributes
-    // and allocate pairs for those as well. This gives us finer grained span anchors resulting in
-    // better incrementality
-    pairs.extend(
-        tree.items()
-            .map(|item| (item.syntax().text_range().start(), ast_id_map.ast_id(&item).erase())),
-    );
-
-    Arc::new(RealSpanMap::from_file(
-        file_id,
-        pairs.into_boxed_slice(),
-        tree.syntax().text_range().end(),
-    ))
-}
-
 /// This expands the given macro call, but with different arguments. This is
 /// used for completion, where we want to see what 'would happen' if we insert a
 /// token. The `token_to_map` mapped down into the expansion, with the mapped
@@ -357,10 +253,6 @@ pub fn expand_speculative(
     Some((node.syntax_node(), token))
 }
 
-fn ast_id_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
-    Arc::new(AstIdMap::from_source(&db.parse_or_expand(file_id)))
-}
-
 fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> SyntaxNode {
     match file_id.repr() {
         HirFileIdRepr::FileId(file_id) => db.parse(file_id).syntax_node(),
@@ -412,7 +304,10 @@ fn parse_macro_expansion_error(
         .map(|it| it.0.errors().to_vec().into_boxed_slice())
 }
 
-fn parse_with_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> (Parse<SyntaxNode>, SpanMap) {
+pub(crate) fn parse_with_map(
+    db: &dyn ExpandDatabase,
+    file_id: HirFileId,
+) -> (Parse<SyntaxNode>, SpanMap) {
     match file_id.repr() {
         HirFileIdRepr::FileId(file_id) => {
             (db.parse(file_id).to_syntax(), SpanMap::RealSpanMap(db.real_span_map(file_id)))
@@ -581,100 +476,18 @@ fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<Sy
     .unwrap_or_default()
 }
 
-fn decl_macro_expander(
-    db: &dyn ExpandDatabase,
-    def_crate: CrateId,
-    id: AstId<ast::Macro>,
-) -> Arc<DeclarativeMacroExpander> {
-    let crate_data = &db.crate_graph()[def_crate];
-    let is_2021 = crate_data.edition >= Edition::Edition2021;
-    let (root, map) = parse_with_map(db, id.file_id);
-    let root = root.syntax_node();
-
-    let transparency = |node| {
-        // ... would be nice to have the item tree here
-        let attrs = RawAttrs::new(db, node, map.as_ref()).filter(db, def_crate);
-        match &*attrs
-            .iter()
-            .find(|it| {
-                it.path.as_ident().and_then(|it| it.as_str()) == Some("rustc_macro_transparency")
-            })?
-            .token_tree_value()?
-            .token_trees
-        {
-            [tt::TokenTree::Leaf(tt::Leaf::Ident(i)), ..] => match &*i.text {
-                "transparent" => Some(Transparency::Transparent),
-                "semitransparent" => Some(Transparency::SemiTransparent),
-                "opaque" => Some(Transparency::Opaque),
-                _ => None,
-            },
-            _ => None,
-        }
-    };
-    let toolchain = crate_data.toolchain.as_ref();
-    let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
-        REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
-            &base_db::Version {
-                pre: base_db::Prerelease::EMPTY,
-                build: base_db::BuildMetadata::EMPTY,
-                major: version.major,
-                minor: version.minor,
-                patch: version.patch,
-            },
-        )
-    });
-
-    let (mac, transparency) = match id.to_ptr(db).to_node(&root) {
-        ast::Macro::MacroRules(macro_rules) => (
-            match macro_rules.token_tree() {
-                Some(arg) => {
-                    let tt = mbe::syntax_node_to_token_tree(
-                        arg.syntax(),
-                        map.as_ref(),
-                        map.span_for_range(macro_rules.macro_rules_token().unwrap().text_range()),
-                    );
-
-                    mbe::DeclarativeMacro::parse_macro_rules(&tt, is_2021, new_meta_vars)
-                }
-                None => mbe::DeclarativeMacro::from_err(
-                    mbe::ParseError::Expected("expected a token tree".into()),
-                    is_2021,
-                ),
-            },
-            transparency(&macro_rules).unwrap_or(Transparency::SemiTransparent),
-        ),
-        ast::Macro::MacroDef(macro_def) => (
-            match macro_def.body() {
-                Some(arg) => {
-                    let tt = mbe::syntax_node_to_token_tree(
-                        arg.syntax(),
-                        map.as_ref(),
-                        map.span_for_range(macro_def.macro_token().unwrap().text_range()),
-                    );
-
-                    mbe::DeclarativeMacro::parse_macro2(&tt, is_2021, new_meta_vars)
-                }
-                None => mbe::DeclarativeMacro::from_err(
-                    mbe::ParseError::Expected("expected a token tree".into()),
-                    is_2021,
-                ),
-            },
-            transparency(&macro_def).unwrap_or(Transparency::Opaque),
-        ),
-    };
-    Arc::new(DeclarativeMacroExpander { mac, transparency })
-}
-
-fn macro_expander(db: &dyn ExpandDatabase, id: MacroDefId) -> TokenExpander {
-    match id.kind {
-        MacroDefKind::Declarative(ast_id) => {
-            TokenExpander::DeclarativeMacro(db.decl_macro_expander(id.krate, ast_id))
+impl TokenExpander {
+    fn macro_expander(db: &dyn ExpandDatabase, id: MacroDefId) -> TokenExpander {
+        match id.kind {
+            MacroDefKind::Declarative(ast_id) => {
+                TokenExpander::DeclarativeMacro(db.decl_macro_expander(id.krate, ast_id))
+            }
+            MacroDefKind::BuiltIn(expander, _) => TokenExpander::BuiltIn(expander),
+            MacroDefKind::BuiltInAttr(expander, _) => TokenExpander::BuiltInAttr(expander),
+            MacroDefKind::BuiltInDerive(expander, _) => TokenExpander::BuiltInDerive(expander),
+            MacroDefKind::BuiltInEager(expander, ..) => TokenExpander::BuiltInEager(expander),
+            MacroDefKind::ProcMacro(expander, ..) => TokenExpander::ProcMacro(expander),
         }
-        MacroDefKind::BuiltIn(expander, _) => TokenExpander::BuiltIn(expander),
-        MacroDefKind::BuiltInAttr(expander, _) => TokenExpander::BuiltInAttr(expander),
-        MacroDefKind::BuiltInDerive(expander, _) => TokenExpander::BuiltInDerive(expander),
-        MacroDefKind::BuiltInEager(expander, ..) => TokenExpander::BuiltInEager(expander),
-        MacroDefKind::ProcMacro(expander, ..) => TokenExpander::ProcMacro(expander),
     }
 }
 
@@ -862,40 +675,3 @@ fn check_tt_count(tt: &tt::Subtree) -> Result<(), ExpandResult<()>> {
 fn setup_syntax_context_root(db: &dyn ExpandDatabase) {
     db.intern_syntax_context(SyntaxContextData::root());
 }
-
-fn dump_syntax_contexts(db: &dyn ExpandDatabase) -> String {
-    let mut s = String::from("Expansions:");
-    let mut entries = InternMacroCallLookupQuery.in_db(db).entries::<Vec<_>>();
-    entries.sort_by_key(|e| e.key);
-    for e in entries {
-        let id = e.key;
-        let expn_data = e.value.as_ref().unwrap();
-        s.push_str(&format!(
-            "\n{:?}: parent: {:?}, call_site_ctxt: {:?}, def_site_ctxt: {:?}, kind: {:?}",
-            id,
-            expn_data.kind.file_id(),
-            expn_data.call_site,
-            SyntaxContextId::ROOT, // FIXME expn_data.def_site,
-            expn_data.kind.descr(),
-        ));
-    }
-
-    s.push_str("\n\nSyntaxContexts:\n");
-    let mut entries = InternSyntaxContextLookupQuery.in_db(db).entries::<Vec<_>>();
-    entries.sort_by_key(|e| e.key);
-    for e in entries {
-        struct SyntaxContextDebug<'a>(
-            &'a dyn ExpandDatabase,
-            SyntaxContextId,
-            &'a SyntaxContextData,
-        );
-
-        impl<'a> std::fmt::Debug for SyntaxContextDebug<'a> {
-            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-                self.2.fancy_debug(self.1, self.0, f)
-            }
-        }
-        stdx::format_to!(s, "{:?}\n", SyntaxContextDebug(db, e.key, &e.value.unwrap()));
-    }
-    s
-}
diff --git a/crates/hir-expand/src/declarative.rs b/crates/hir-expand/src/declarative.rs
new file mode 100644
index 00000000000..37084ee8b93
--- /dev/null
+++ b/crates/hir-expand/src/declarative.rs
@@ -0,0 +1,177 @@
+//! Compiled declarative macro expanders (`macro_rules!`` and `macro`)
+use std::sync::OnceLock;
+
+use base_db::{CrateId, Edition, VersionReq};
+use span::{MacroCallId, Span};
+use syntax::{ast, AstNode};
+use triomphe::Arc;
+
+use crate::{
+    attrs::RawAttrs,
+    db::ExpandDatabase,
+    hygiene::{apply_mark, Transparency},
+    tt, AstId, ExpandError, ExpandResult,
+};
+
+/// Old-style `macro_rules` or the new macros 2.0
+#[derive(Debug, Clone, Eq, PartialEq)]
+pub struct DeclarativeMacroExpander {
+    pub mac: mbe::DeclarativeMacro<span::Span>,
+    pub transparency: Transparency,
+}
+
+// FIXME: Remove this once we drop support for 1.76
+static REQUIREMENT: OnceLock<VersionReq> = OnceLock::new();
+
+impl DeclarativeMacroExpander {
+    pub fn expand(
+        &self,
+        db: &dyn ExpandDatabase,
+        tt: tt::Subtree,
+        call_id: MacroCallId,
+    ) -> ExpandResult<tt::Subtree> {
+        let loc = db.lookup_intern_macro_call(call_id);
+        let toolchain = &db.crate_graph()[loc.def.krate].toolchain;
+        let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
+            REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
+                &base_db::Version {
+                    pre: base_db::Prerelease::EMPTY,
+                    build: base_db::BuildMetadata::EMPTY,
+                    major: version.major,
+                    minor: version.minor,
+                    patch: version.patch,
+                },
+            )
+        });
+        match self.mac.err() {
+            Some(e) => ExpandResult::new(
+                tt::Subtree::empty(tt::DelimSpan { open: loc.call_site, close: loc.call_site }),
+                ExpandError::other(format!("invalid macro definition: {e}")),
+            ),
+            None => self
+                .mac
+                .expand(
+                    &tt,
+                    |s| s.ctx = apply_mark(db, s.ctx, call_id, self.transparency),
+                    new_meta_vars,
+                    loc.call_site,
+                )
+                .map_err(Into::into),
+        }
+    }
+
+    pub fn expand_unhygienic(
+        &self,
+        db: &dyn ExpandDatabase,
+        tt: tt::Subtree,
+        krate: CrateId,
+        call_site: Span,
+    ) -> ExpandResult<tt::Subtree> {
+        let toolchain = &db.crate_graph()[krate].toolchain;
+        let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
+            REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
+                &base_db::Version {
+                    pre: base_db::Prerelease::EMPTY,
+                    build: base_db::BuildMetadata::EMPTY,
+                    major: version.major,
+                    minor: version.minor,
+                    patch: version.patch,
+                },
+            )
+        });
+        match self.mac.err() {
+            Some(e) => ExpandResult::new(
+                tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
+                ExpandError::other(format!("invalid macro definition: {e}")),
+            ),
+            None => self.mac.expand(&tt, |_| (), new_meta_vars, call_site).map_err(Into::into),
+        }
+    }
+
+    pub(crate) fn expander(
+        db: &dyn ExpandDatabase,
+        def_crate: CrateId,
+        id: AstId<ast::Macro>,
+    ) -> Arc<DeclarativeMacroExpander> {
+        let crate_data = &db.crate_graph()[def_crate];
+        let is_2021 = crate_data.edition >= Edition::Edition2021;
+        let (root, map) = crate::db::parse_with_map(db, id.file_id);
+        let root = root.syntax_node();
+
+        let transparency = |node| {
+            // ... would be nice to have the item tree here
+            let attrs = RawAttrs::new(db, node, map.as_ref()).filter(db, def_crate);
+            match &*attrs
+                .iter()
+                .find(|it| {
+                    it.path.as_ident().and_then(|it| it.as_str())
+                        == Some("rustc_macro_transparency")
+                })?
+                .token_tree_value()?
+                .token_trees
+            {
+                [tt::TokenTree::Leaf(tt::Leaf::Ident(i)), ..] => match &*i.text {
+                    "transparent" => Some(Transparency::Transparent),
+                    "semitransparent" => Some(Transparency::SemiTransparent),
+                    "opaque" => Some(Transparency::Opaque),
+                    _ => None,
+                },
+                _ => None,
+            }
+        };
+        let toolchain = crate_data.toolchain.as_ref();
+        let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
+            REQUIREMENT.get_or_init(|| VersionReq::parse(">=1.76").unwrap()).matches(
+                &base_db::Version {
+                    pre: base_db::Prerelease::EMPTY,
+                    build: base_db::BuildMetadata::EMPTY,
+                    major: version.major,
+                    minor: version.minor,
+                    patch: version.patch,
+                },
+            )
+        });
+
+        let (mac, transparency) = match id.to_ptr(db).to_node(&root) {
+            ast::Macro::MacroRules(macro_rules) => (
+                match macro_rules.token_tree() {
+                    Some(arg) => {
+                        let tt = mbe::syntax_node_to_token_tree(
+                            arg.syntax(),
+                            map.as_ref(),
+                            map.span_for_range(
+                                macro_rules.macro_rules_token().unwrap().text_range(),
+                            ),
+                        );
+
+                        mbe::DeclarativeMacro::parse_macro_rules(&tt, is_2021, new_meta_vars)
+                    }
+                    None => mbe::DeclarativeMacro::from_err(
+                        mbe::ParseError::Expected("expected a token tree".into()),
+                        is_2021,
+                    ),
+                },
+                transparency(&macro_rules).unwrap_or(Transparency::SemiTransparent),
+            ),
+            ast::Macro::MacroDef(macro_def) => (
+                match macro_def.body() {
+                    Some(arg) => {
+                        let tt = mbe::syntax_node_to_token_tree(
+                            arg.syntax(),
+                            map.as_ref(),
+                            map.span_for_range(macro_def.macro_token().unwrap().text_range()),
+                        );
+
+                        mbe::DeclarativeMacro::parse_macro2(&tt, is_2021, new_meta_vars)
+                    }
+                    None => mbe::DeclarativeMacro::from_err(
+                        mbe::ParseError::Expected("expected a token tree".into()),
+                        is_2021,
+                    ),
+                },
+                transparency(&macro_def).unwrap_or(Transparency::Opaque),
+            ),
+        };
+        Arc::new(DeclarativeMacroExpander { mac, transparency })
+    }
+}
diff --git a/crates/hir-expand/src/hygiene.rs b/crates/hir-expand/src/hygiene.rs
index 57921543c4b..8ddaa3f3039 100644
--- a/crates/hir-expand/src/hygiene.rs
+++ b/crates/hir-expand/src/hygiene.rs
@@ -245,3 +245,43 @@ pub fn marks_rev(
     })
     .map(|ctx| ctx.outer_mark(db))
 }
+
+pub(crate) fn dump_syntax_contexts(db: &dyn ExpandDatabase) -> String {
+    use crate::db::{InternMacroCallLookupQuery, InternSyntaxContextLookupQuery};
+    use base_db::salsa::debug::DebugQueryTable;
+
+    let mut s = String::from("Expansions:");
+    let mut entries = InternMacroCallLookupQuery.in_db(db).entries::<Vec<_>>();
+    entries.sort_by_key(|e| e.key);
+    for e in entries {
+        let id = e.key;
+        let expn_data = e.value.as_ref().unwrap();
+        s.push_str(&format!(
+            "\n{:?}: parent: {:?}, call_site_ctxt: {:?}, def_site_ctxt: {:?}, kind: {:?}",
+            id,
+            expn_data.kind.file_id(),
+            expn_data.call_site,
+            SyntaxContextId::ROOT, // FIXME expn_data.def_site,
+            expn_data.kind.descr(),
+        ));
+    }
+
+    s.push_str("\n\nSyntaxContexts:\n");
+    let mut entries = InternSyntaxContextLookupQuery.in_db(db).entries::<Vec<_>>();
+    entries.sort_by_key(|e| e.key);
+    for e in entries {
+        struct SyntaxContextDebug<'a>(
+            &'a dyn ExpandDatabase,
+            SyntaxContextId,
+            &'a SyntaxContextData,
+        );
+
+        impl<'a> std::fmt::Debug for SyntaxContextDebug<'a> {
+            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+                self.2.fancy_debug(self.1, self.0, f)
+            }
+        }
+        stdx::format_to!(s, "{:?}\n", SyntaxContextDebug(db, e.key, &e.value.unwrap()));
+    }
+    s
+}
diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs
index bd216ccca82..05f12527a44 100644
--- a/crates/hir-expand/src/lib.rs
+++ b/crates/hir-expand/src/lib.rs
@@ -11,16 +11,18 @@ pub mod attrs;
 pub mod builtin_attr_macro;
 pub mod builtin_derive_macro;
 pub mod builtin_fn_macro;
+pub mod change;
 pub mod db;
+pub mod declarative;
 pub mod eager;
 pub mod files;
-pub mod change;
 pub mod hygiene;
 pub mod mod_path;
 pub mod name;
 pub mod proc_macro;
 pub mod quote;
 pub mod span_map;
+
 mod fixup;
 
 use attrs::collect_attrs;
@@ -167,7 +169,8 @@ pub struct MacroCallLoc {
     pub krate: CrateId,
     /// Some if this is a macro call for an eager macro. Note that this is `None`
     /// for the eager input macro file.
-    // FIXME: This seems bad to save in an interned structure
+    // FIXME: This is being interned, subtrees can vary quickly differ just slightly causing
+    // leakage problems here
     eager: Option<Arc<EagerCallInfo>>,
     pub kind: MacroCallKind,
     pub call_site: Span,
@@ -220,7 +223,7 @@ pub enum MacroCallKind {
     },
     Attr {
         ast_id: AstId<ast::Item>,
-        // FIXME: This is being interned, subtrees can very quickly differ just slightly causing
+        // FIXME: This is being interned, subtrees can vary quickly differ just slightly causing
         // leakage problems here
         attr_args: Option<Arc<tt::Subtree>>,
         /// Syntactical index of the invoking `#[attribute]`.
diff --git a/crates/hir-expand/src/mod_path.rs b/crates/hir-expand/src/mod_path.rs
index 47a587e407c..0eb1fc1eb50 100644
--- a/crates/hir-expand/src/mod_path.rs
+++ b/crates/hir-expand/src/mod_path.rs
@@ -10,6 +10,7 @@ use crate::{
     hygiene::{marks_rev, SyntaxContextExt, Transparency},
     name::{known, AsName, Name},
     span_map::SpanMapRef,
+    tt,
 };
 use base_db::CrateId;
 use smallvec::SmallVec;
@@ -39,7 +40,7 @@ pub enum PathKind {
     Crate,
     /// Absolute path (::foo)
     Abs,
-    // FIXME: Remove this
+    // FIXME: Can we remove this somehow?
     /// `$crate` from macro expansion
     DollarCrate(CrateId),
 }
@@ -50,11 +51,16 @@ impl ModPath {
         path: ast::Path,
         span_map: SpanMapRef<'_>,
     ) -> Option<ModPath> {
-        convert_path(db, None, path, span_map)
+        convert_path(db, path, span_map)
+    }
+
+    pub fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree]) -> Option<ModPath> {
+        convert_path_tt(db, tt)
     }
 
     pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath {
-        let segments = segments.into_iter().collect();
+        let mut segments: SmallVec<_> = segments.into_iter().collect();
+        segments.shrink_to_fit();
         ModPath { kind, segments }
     }
 
@@ -193,22 +199,15 @@ fn display_fmt_path(
 
 fn convert_path(
     db: &dyn ExpandDatabase,
-    prefix: Option<ModPath>,
     path: ast::Path,
     span_map: SpanMapRef<'_>,
 ) -> Option<ModPath> {
-    let prefix = match path.qualifier() {
-        Some(qual) => Some(convert_path(db, prefix, qual, span_map)?),
-        None => prefix,
-    };
+    let mut segments = path.segments();
 
-    let segment = path.segment()?;
+    let segment = &segments.next()?;
     let mut mod_path = match segment.kind()? {
         ast::PathSegmentKind::Name(name_ref) => {
             if name_ref.text() == "$crate" {
-                if prefix.is_some() {
-                    return None;
-                }
                 ModPath::from_kind(
                     resolve_crate_root(
                         db,
@@ -218,41 +217,36 @@ fn convert_path(
                     .unwrap_or(PathKind::Crate),
                 )
             } else {
-                let mut res = prefix.unwrap_or_else(|| {
-                    ModPath::from_kind(
-                        segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs),
-                    )
-                });
+                let mut res = ModPath::from_kind(
+                    segment.coloncolon_token().map_or(PathKind::Plain, |_| PathKind::Abs),
+                );
                 res.segments.push(name_ref.as_name());
                 res
             }
         }
         ast::PathSegmentKind::SelfTypeKw => {
-            if prefix.is_some() {
-                return None;
-            }
             ModPath::from_segments(PathKind::Plain, Some(known::SELF_TYPE))
         }
-        ast::PathSegmentKind::CrateKw => {
-            if prefix.is_some() {
-                return None;
-            }
-            ModPath::from_segments(PathKind::Crate, iter::empty())
-        }
-        ast::PathSegmentKind::SelfKw => {
-            if prefix.is_some() {
-                return None;
-            }
-            ModPath::from_segments(PathKind::Super(0), iter::empty())
-        }
+        ast::PathSegmentKind::CrateKw => ModPath::from_segments(PathKind::Crate, iter::empty()),
+        ast::PathSegmentKind::SelfKw => ModPath::from_segments(PathKind::Super(0), iter::empty()),
         ast::PathSegmentKind::SuperKw => {
-            let nested_super_count = match prefix.map(|p| p.kind) {
-                Some(PathKind::Super(n)) => n,
-                Some(_) => return None,
-                None => 0,
-            };
+            let mut deg = 1;
+            let mut next_segment = None;
+            while let Some(segment) = segments.next() {
+                match segment.kind()? {
+                    ast::PathSegmentKind::SuperKw => deg += 1,
+                    ast::PathSegmentKind::Name(name) => {
+                        next_segment = Some(name.as_name());
+                        break;
+                    }
+                    ast::PathSegmentKind::Type { .. }
+                    | ast::PathSegmentKind::SelfTypeKw
+                    | ast::PathSegmentKind::SelfKw
+                    | ast::PathSegmentKind::CrateKw => return None,
+                }
+            }
 
-            ModPath::from_segments(PathKind::Super(nested_super_count + 1), iter::empty())
+            ModPath::from_segments(PathKind::Super(deg), next_segment)
         }
         ast::PathSegmentKind::Type { .. } => {
             // not allowed in imports
@@ -260,6 +254,14 @@ fn convert_path(
         }
     };
 
+    for segment in segments {
+        let name = match segment.kind()? {
+            ast::PathSegmentKind::Name(name) => name.as_name(),
+            _ => return None,
+        };
+        mod_path.segments.push(name);
+    }
+
     // handle local_inner_macros :
     // Basically, even in rustc it is quite hacky:
     // https://github.com/rust-lang/rust/blob/614f273e9388ddd7804d5cbc80b8865068a3744e/src/librustc_resolve/macros.rs#L456
@@ -281,6 +283,46 @@ fn convert_path(
     Some(mod_path)
 }
 
+fn convert_path_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree]) -> Option<ModPath> {
+    let mut leafs = tt.iter().filter_map(|tt| match tt {
+        tt::TokenTree::Leaf(leaf) => Some(leaf),
+        tt::TokenTree::Subtree(_) => None,
+    });
+    let mut segments = smallvec::smallvec![];
+    let kind = match leafs.next()? {
+        tt::Leaf::Punct(tt::Punct { char: ':', .. }) => match leafs.next()? {
+            tt::Leaf::Punct(tt::Punct { char: ':', .. }) => PathKind::Abs,
+            _ => return None,
+        },
+        tt::Leaf::Ident(tt::Ident { text, span }) if text == "$crate" => {
+            resolve_crate_root(db, span.ctx).map(PathKind::DollarCrate).unwrap_or(PathKind::Crate)
+        }
+        tt::Leaf::Ident(tt::Ident { text, .. }) if text == "self" => PathKind::Super(0),
+        tt::Leaf::Ident(tt::Ident { text, .. }) if text == "super" => {
+            let mut deg = 1;
+            while let Some(tt::Leaf::Ident(tt::Ident { text, .. })) = leafs.next() {
+                if text != "super" {
+                    segments.push(Name::new_text_dont_use(text.clone()));
+                    break;
+                }
+                deg += 1;
+            }
+            PathKind::Super(deg)
+        }
+        tt::Leaf::Ident(tt::Ident { text, .. }) if text == "crate" => PathKind::Crate,
+        tt::Leaf::Ident(ident) => {
+            segments.push(Name::new_text_dont_use(ident.text.clone()));
+            PathKind::Plain
+        }
+        _ => return None,
+    };
+    segments.extend(leafs.filter_map(|leaf| match leaf {
+        ::tt::Leaf::Ident(ident) => Some(Name::new_text_dont_use(ident.text.clone())),
+        _ => None,
+    }));
+    Some(ModPath { kind, segments })
+}
+
 pub fn resolve_crate_root(db: &dyn ExpandDatabase, mut ctxt: SyntaxContextId) -> Option<CrateId> {
     // When resolving `$crate` from a `macro_rules!` invoked in a `macro`,
     // we don't want to pretend that the `macro_rules!` definition is in the `macro`
diff --git a/crates/hir-expand/src/span_map.rs b/crates/hir-expand/src/span_map.rs
index 4ec6e657f9e..8e624f5585d 100644
--- a/crates/hir-expand/src/span_map.rs
+++ b/crates/hir-expand/src/span_map.rs
@@ -1,10 +1,12 @@
 //! Span maps for real files and macro expansions.
-use span::Span;
-use syntax::TextRange;
+use span::{FileId, HirFileId, HirFileIdRepr, Span};
+use syntax::{AstNode, TextRange};
 use triomphe::Arc;
 
 pub use span::RealSpanMap;
 
+use crate::db::ExpandDatabase;
+
 pub type ExpansionSpanMap = span::SpanMap<Span>;
 
 /// Spanmap for a macro file or a real file
@@ -34,7 +36,6 @@ impl mbe::SpanMapper<Span> for SpanMapRef<'_> {
         self.span_for_range(range)
     }
 }
-
 impl SpanMap {
     pub fn span_for_range(&self, range: TextRange) -> Span {
         match self {
@@ -53,6 +54,16 @@ impl SpanMap {
             Self::RealSpanMap(span_map) => SpanMapRef::RealSpanMap(span_map),
         }
     }
+
+    #[inline]
+    pub(crate) fn new(db: &dyn ExpandDatabase, file_id: HirFileId) -> SpanMap {
+        match file_id.repr() {
+            HirFileIdRepr::FileId(file_id) => SpanMap::RealSpanMap(db.real_span_map(file_id)),
+            HirFileIdRepr::MacroFile(m) => {
+                SpanMap::ExpansionSpanMap(db.parse_macro_expansion(m).value.1)
+            }
+        }
+    }
 }
 
 impl SpanMapRef<'_> {
@@ -63,3 +74,23 @@ impl SpanMapRef<'_> {
         }
     }
 }
+
+pub(crate) fn real_span_map(db: &dyn ExpandDatabase, file_id: FileId) -> Arc<RealSpanMap> {
+    use syntax::ast::HasModuleItem;
+    let mut pairs = vec![(syntax::TextSize::new(0), span::ROOT_ERASED_FILE_AST_ID)];
+    let ast_id_map = db.ast_id_map(file_id.into());
+    let tree = db.parse(file_id).tree();
+    // FIXME: Descend into modules and other item containing items that are not annotated with attributes
+    // and allocate pairs for those as well. This gives us finer grained span anchors resulting in
+    // better incrementality
+    pairs.extend(
+        tree.items()
+            .map(|item| (item.syntax().text_range().start(), ast_id_map.ast_id(&item).erase())),
+    );
+
+    Arc::new(RealSpanMap::from_file(
+        file_id,
+        pairs.into_boxed_slice(),
+        tree.syntax().text_range().end(),
+    ))
+}
diff --git a/crates/hir-ty/src/diagnostics.rs b/crates/hir-ty/src/diagnostics.rs
index c1b3619009b..af4d2c9fc04 100644
--- a/crates/hir-ty/src/diagnostics.rs
+++ b/crates/hir-ty/src/diagnostics.rs
@@ -1,8 +1,8 @@
 //! Type inference-based diagnostics.
+mod decl_check;
 mod expr;
 mod match_check;
 mod unsafe_check;
-mod decl_check;
 
 pub use crate::diagnostics::{
     decl_check::{incorrect_case, CaseType, IncorrectCase},
diff --git a/crates/hir-ty/src/lib.rs b/crates/hir-ty/src/lib.rs
index 8d180f98617..54e91e7b29a 100644
--- a/crates/hir-ty/src/lib.rs
+++ b/crates/hir-ty/src/lib.rs
@@ -42,9 +42,9 @@ pub mod primitive;
 pub mod traits;
 
 #[cfg(test)]
-mod tests;
-#[cfg(test)]
 mod test_db;
+#[cfg(test)]
+mod tests;
 
 use std::{
     collections::hash_map::Entry,
diff --git a/crates/hir-ty/src/mir.rs b/crates/hir-ty/src/mir.rs
index 952a97e3d0f..494f1850b88 100644
--- a/crates/hir-ty/src/mir.rs
+++ b/crates/hir-ty/src/mir.rs
@@ -21,11 +21,11 @@ use hir_def::{
 };
 use la_arena::{Arena, ArenaMap, Idx, RawIdx};
 
+mod borrowck;
 mod eval;
 mod lower;
-mod borrowck;
-mod pretty;
 mod monomorphization;
+mod pretty;
 
 pub use borrowck::{borrowck_query, BorrowckResult, MutabilityReason};
 pub use eval::{
diff --git a/crates/hir-ty/src/tests.rs b/crates/hir-ty/src/tests.rs
index 671fd9ec3a4..9804910c878 100644
--- a/crates/hir-ty/src/tests.rs
+++ b/crates/hir-ty/src/tests.rs
@@ -1,14 +1,14 @@
-mod never_type;
 mod coercion;
+mod diagnostics;
+mod display_source_code;
+mod incremental;
+mod macros;
+mod method_resolution;
+mod never_type;
+mod patterns;
 mod regression;
 mod simple;
-mod patterns;
 mod traits;
-mod method_resolution;
-mod macros;
-mod display_source_code;
-mod incremental;
-mod diagnostics;
 
 use std::{collections::HashMap, env};
 
diff --git a/crates/hir/src/attrs.rs b/crates/hir/src/attrs.rs
index 7b9f895bc73..fc0a196df7c 100644
--- a/crates/hir/src/attrs.rs
+++ b/crates/hir/src/attrs.rs
@@ -2,7 +2,6 @@
 
 use std::ops::ControlFlow;
 
-use base_db::FileId;
 use hir_def::{
     attr::AttrsWithOwner,
     item_scope::ItemInNs,
@@ -11,12 +10,8 @@ use hir_def::{
     resolver::{HasResolver, Resolver, TypeNs},
     AssocItemId, AttrDefId, ModuleDefId,
 };
-use hir_expand::{
-    name::Name,
-    span_map::{RealSpanMap, SpanMapRef},
-};
+use hir_expand::{mod_path::PathKind, name::Name};
 use hir_ty::{db::HirDatabase, method_resolution};
-use syntax::{ast, AstNode};
 
 use crate::{
     Adt, AsAssocItem, AssocItem, BuiltinType, Const, ConstParam, DocLinkDef, Enum, ExternCrateDecl,
@@ -129,7 +124,7 @@ fn resolve_doc_path_on_(
         AttrDefId::GenericParamId(_) => return None,
     };
 
-    let mut modpath = modpath_from_str(db, link)?;
+    let mut modpath = modpath_from_str(link)?;
 
     let resolved = resolver.resolve_module_path_in_items(db.upcast(), &modpath);
     if resolved.is_none() {
@@ -305,34 +300,37 @@ fn as_module_def_if_namespace_matches(
     (ns.unwrap_or(expected_ns) == expected_ns).then(|| DocLinkDef::ModuleDef(def))
 }
 
-fn modpath_from_str(db: &dyn HirDatabase, link: &str) -> Option<ModPath> {
+fn modpath_from_str(link: &str) -> Option<ModPath> {
     // FIXME: this is not how we should get a mod path here.
     let try_get_modpath = |link: &str| {
-        let ast_path = ast::SourceFile::parse(&format!("type T = {link};"))
-            .syntax_node()
-            .descendants()
-            .find_map(ast::Path::cast)?;
-        if ast_path.syntax().text() != link {
-            return None;
-        }
-        ModPath::from_src(
-            db.upcast(),
-            ast_path,
-            SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId::BOGUS)),
-        )
+        let mut parts = link.split("::");
+        let mut first_segment = None;
+        let kind = match parts.next()? {
+            "" => PathKind::Abs,
+            "crate" => PathKind::Crate,
+            "self" => PathKind::Super(0),
+            "super" => {
+                let mut deg = 1;
+                while let Some(segment) = parts.next() {
+                    if segment == "super" {
+                        deg += 1;
+                    } else {
+                        first_segment = Some(segment);
+                        break;
+                    }
+                }
+                PathKind::Super(deg)
+            }
+            segment => {
+                first_segment = Some(segment);
+                PathKind::Plain
+            }
+        };
+        let parts = first_segment.into_iter().chain(parts).map(|segment| match segment.parse() {
+            Ok(idx) => Name::new_tuple_field(idx),
+            Err(_) => Name::new_text_dont_use(segment.into()),
+        });
+        Some(ModPath::from_segments(kind, parts))
     };
-
-    let full = try_get_modpath(link);
-    if full.is_some() {
-        return full;
-    }
-
-    // Tuple field names cannot be a part of `ModPath` usually, but rustdoc can
-    // resolve doc paths like `TupleStruct::0`.
-    // FIXME: Find a better way to handle these.
-    let (base, maybe_tuple_field) = link.rsplit_once("::")?;
-    let tuple_field = Name::new_tuple_field(maybe_tuple_field.parse().ok()?);
-    let mut modpath = try_get_modpath(base)?;
-    modpath.push_segment(tuple_field);
-    Some(modpath)
+    try_get_modpath(link)
 }
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index c50be5f1141..c332ab0050c 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -24,12 +24,12 @@
 mod semantics;
 mod source_analyzer;
 
-mod from_id;
 mod attrs;
+mod from_id;
 mod has_source;
 
-pub mod diagnostics;
 pub mod db;
+pub mod diagnostics;
 pub mod symbols;
 
 mod display;
@@ -70,13 +70,12 @@ use hir_ty::{
     primitive::UintTy,
     traits::FnTrait,
     AliasTy, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, ClosureId, GenericArg,
-    GenericArgData, Interner, ParamKind, QuantifiedWhereClause, Scalar, Substitution,
-    TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, ValueTyDefId,
-    WhereClause,
+    GenericArgData, InferenceDiagnostic, Interner, ParamKind, QuantifiedWhereClause, Scalar,
+    Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind,
+    ValueTyDefId, WhereClause,
 };
 use itertools::Itertools;
 use nameres::diagnostics::DefDiagnosticKind;
-use once_cell::unsync::Lazy;
 use rustc_hash::FxHashSet;
 use stdx::{impl_from, never};
 use syntax::{
@@ -1592,53 +1591,46 @@ impl DefWithBody {
         }
 
         for diag in source_map.diagnostics() {
-            match diag {
-                BodyDiagnostic::InactiveCode { node, cfg, opts } => acc.push(
-                    InactiveCode { node: *node, cfg: cfg.clone(), opts: opts.clone() }.into(),
-                ),
-                BodyDiagnostic::MacroError { node, message } => acc.push(
-                    MacroError {
-                        node: (*node).map(|it| it.into()),
-                        precise_location: None,
-                        message: message.to_string(),
-                    }
-                    .into(),
-                ),
-                BodyDiagnostic::UnresolvedProcMacro { node, krate } => acc.push(
-                    UnresolvedProcMacro {
-                        node: (*node).map(|it| it.into()),
-                        precise_location: None,
-                        macro_name: None,
-                        kind: MacroKind::ProcMacro,
-                        krate: *krate,
-                    }
-                    .into(),
-                ),
-                BodyDiagnostic::UnresolvedMacroCall { node, path } => acc.push(
-                    UnresolvedMacroCall {
-                        macro_call: (*node).map(|ast_ptr| ast_ptr.into()),
-                        precise_location: None,
-                        path: path.clone(),
-                        is_bang: true,
-                    }
-                    .into(),
-                ),
+            acc.push(match diag {
+                BodyDiagnostic::InactiveCode { node, cfg, opts } => {
+                    InactiveCode { node: *node, cfg: cfg.clone(), opts: opts.clone() }.into()
+                }
+                BodyDiagnostic::MacroError { node, message } => MacroError {
+                    node: (*node).map(|it| it.into()),
+                    precise_location: None,
+                    message: message.to_string(),
+                }
+                .into(),
+                BodyDiagnostic::UnresolvedProcMacro { node, krate } => UnresolvedProcMacro {
+                    node: (*node).map(|it| it.into()),
+                    precise_location: None,
+                    macro_name: None,
+                    kind: MacroKind::ProcMacro,
+                    krate: *krate,
+                }
+                .into(),
+                BodyDiagnostic::UnresolvedMacroCall { node, path } => UnresolvedMacroCall {
+                    macro_call: (*node).map(|ast_ptr| ast_ptr.into()),
+                    precise_location: None,
+                    path: path.clone(),
+                    is_bang: true,
+                }
+                .into(),
                 BodyDiagnostic::UnreachableLabel { node, name } => {
-                    acc.push(UnreachableLabel { node: *node, name: name.clone() }.into())
+                    UnreachableLabel { node: *node, name: name.clone() }.into()
                 }
                 BodyDiagnostic::UndeclaredLabel { node, name } => {
-                    acc.push(UndeclaredLabel { node: *node, name: name.clone() }.into())
+                    UndeclaredLabel { node: *node, name: name.clone() }.into()
                 }
-            }
+            });
         }
 
         let infer = db.infer(self.into());
-        let source_map = Lazy::new(|| db.body_with_source_map(self.into()).1);
         let expr_syntax = |expr| source_map.expr_syntax(expr).expect("unexpected synthetic");
         let pat_syntax = |pat| source_map.pat_syntax(pat).expect("unexpected synthetic");
         for d in &infer.diagnostics {
-            match d {
-                &hir_ty::InferenceDiagnostic::NoSuchField { field: expr, private } => {
+            acc.push(match d {
+                &InferenceDiagnostic::NoSuchField { field: expr, private } => {
                     let expr_or_pat = match expr {
                         ExprOrPatId::ExprId(expr) => {
                             source_map.field_syntax(expr).map(AstPtr::wrap_left)
@@ -1647,57 +1639,48 @@ impl DefWithBody {
                             source_map.pat_field_syntax(pat).map(AstPtr::wrap_right)
                         }
                     };
-                    acc.push(NoSuchField { field: expr_or_pat, private }.into())
+                    NoSuchField { field: expr_or_pat, private }.into()
                 }
-                &hir_ty::InferenceDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
-                    acc.push(
-                        MismatchedArgCount { call_expr: expr_syntax(call_expr), expected, found }
-                            .into(),
-                    )
+                &InferenceDiagnostic::MismatchedArgCount { call_expr, expected, found } => {
+                    MismatchedArgCount { call_expr: expr_syntax(call_expr), expected, found }.into()
                 }
-                &hir_ty::InferenceDiagnostic::PrivateField { expr, field } => {
+                &InferenceDiagnostic::PrivateField { expr, field } => {
                     let expr = expr_syntax(expr);
                     let field = field.into();
-                    acc.push(PrivateField { expr, field }.into())
+                    PrivateField { expr, field }.into()
                 }
-                &hir_ty::InferenceDiagnostic::PrivateAssocItem { id, item } => {
+                &InferenceDiagnostic::PrivateAssocItem { id, item } => {
                     let expr_or_pat = match id {
                         ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(AstPtr::wrap_left),
                         ExprOrPatId::PatId(pat) => pat_syntax(pat).map(AstPtr::wrap_right),
                     };
                     let item = item.into();
-                    acc.push(PrivateAssocItem { expr_or_pat, item }.into())
+                    PrivateAssocItem { expr_or_pat, item }.into()
                 }
-                hir_ty::InferenceDiagnostic::ExpectedFunction { call_expr, found } => {
+                InferenceDiagnostic::ExpectedFunction { call_expr, found } => {
                     let call_expr = expr_syntax(*call_expr);
-
-                    acc.push(
-                        ExpectedFunction {
-                            call: call_expr,
-                            found: Type::new(db, DefWithBodyId::from(self), found.clone()),
-                        }
-                        .into(),
-                    )
+                    ExpectedFunction {
+                        call: call_expr,
+                        found: Type::new(db, DefWithBodyId::from(self), found.clone()),
+                    }
+                    .into()
                 }
-                hir_ty::InferenceDiagnostic::UnresolvedField {
+                InferenceDiagnostic::UnresolvedField {
                     expr,
                     receiver,
                     name,
                     method_with_same_name_exists,
                 } => {
                     let expr = expr_syntax(*expr);
-
-                    acc.push(
-                        UnresolvedField {
-                            expr,
-                            name: name.clone(),
-                            receiver: Type::new(db, DefWithBodyId::from(self), receiver.clone()),
-                            method_with_same_name_exists: *method_with_same_name_exists,
-                        }
-                        .into(),
-                    )
+                    UnresolvedField {
+                        expr,
+                        name: name.clone(),
+                        receiver: Type::new(db, DefWithBodyId::from(self), receiver.clone()),
+                        method_with_same_name_exists: *method_with_same_name_exists,
+                    }
+                    .into()
                 }
-                hir_ty::InferenceDiagnostic::UnresolvedMethodCall {
+                InferenceDiagnostic::UnresolvedMethodCall {
                     expr,
                     receiver,
                     name,
@@ -1705,50 +1688,38 @@ impl DefWithBody {
                     assoc_func_with_same_name,
                 } => {
                     let expr = expr_syntax(*expr);
-
-                    acc.push(
-                        UnresolvedMethodCall {
-                            expr,
-                            name: name.clone(),
-                            receiver: Type::new(db, DefWithBodyId::from(self), receiver.clone()),
-                            field_with_same_name: field_with_same_name
-                                .clone()
-                                .map(|ty| Type::new(db, DefWithBodyId::from(self), ty)),
-                            assoc_func_with_same_name: *assoc_func_with_same_name,
-                        }
-                        .into(),
-                    )
+                    UnresolvedMethodCall {
+                        expr,
+                        name: name.clone(),
+                        receiver: Type::new(db, DefWithBodyId::from(self), receiver.clone()),
+                        field_with_same_name: field_with_same_name
+                            .clone()
+                            .map(|ty| Type::new(db, DefWithBodyId::from(self), ty)),
+                        assoc_func_with_same_name: *assoc_func_with_same_name,
+                    }
+                    .into()
                 }
-                &hir_ty::InferenceDiagnostic::UnresolvedAssocItem { id } => {
+                &InferenceDiagnostic::UnresolvedAssocItem { id } => {
                     let expr_or_pat = match id {
                         ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(AstPtr::wrap_left),
                         ExprOrPatId::PatId(pat) => pat_syntax(pat).map(AstPtr::wrap_right),
                     };
-                    acc.push(UnresolvedAssocItem { expr_or_pat }.into())
+                    UnresolvedAssocItem { expr_or_pat }.into()
                 }
-                &hir_ty::InferenceDiagnostic::BreakOutsideOfLoop {
-                    expr,
-                    is_break,
-                    bad_value_break,
-                } => {
+                &InferenceDiagnostic::BreakOutsideOfLoop { expr, is_break, bad_value_break } => {
                     let expr = expr_syntax(expr);
-                    acc.push(BreakOutsideOfLoop { expr, is_break, bad_value_break }.into())
+                    BreakOutsideOfLoop { expr, is_break, bad_value_break }.into()
                 }
-                hir_ty::InferenceDiagnostic::TypedHole { expr, expected } => {
+                InferenceDiagnostic::TypedHole { expr, expected } => {
                     let expr = expr_syntax(*expr);
-                    acc.push(
-                        TypedHole {
-                            expr,
-                            expected: Type::new(db, DefWithBodyId::from(self), expected.clone()),
-                        }
-                        .into(),
-                    )
+
+                    TypedHole {
+                        expr,
+                        expected: Type::new(db, DefWithBodyId::from(self), expected.clone()),
+                    }
+                    .into()
                 }
-                &hir_ty::InferenceDiagnostic::MismatchedTupleStructPatArgCount {
-                    pat,
-                    expected,
-                    found,
-                } => {
+                &InferenceDiagnostic::MismatchedTupleStructPatArgCount { pat, expected, found } => {
                     let expr_or_pat = match pat {
                         ExprOrPatId::ExprId(expr) => expr_syntax(expr).map(AstPtr::wrap_left),
                         ExprOrPatId::PatId(pat) => {
@@ -1762,11 +1733,9 @@ impl DefWithBody {
                             InFile { file_id, value: ptr }
                         }
                     };
-                    acc.push(
-                        MismatchedTupleStructPatArgCount { expr_or_pat, expected, found }.into(),
-                    )
+                    MismatchedTupleStructPatArgCount { expr_or_pat, expected, found }.into()
                 }
-            }
+            });
         }
         for (pat_or_expr, mismatch) in infer.type_mismatches() {
             let expr_or_pat = match pat_or_expr {
@@ -1805,8 +1774,6 @@ impl DefWithBody {
             }
         }
 
-        let hir_body = db.body(self.into());
-
         if let Ok(borrowck_results) = db.borrowck(self.into()) {
             for borrowck_result in borrowck_results.iter() {
                 let mir_body = &borrowck_result.mir_body;
@@ -1828,7 +1795,7 @@ impl DefWithBody {
                     )
                 }
                 let mol = &borrowck_result.mutability_of_locals;
-                for (binding_id, binding_data) in hir_body.bindings.iter() {
+                for (binding_id, binding_data) in body.bindings.iter() {
                     if binding_data.problems.is_some() {
                         // We should report specific diagnostics for these problems, not `need-mut` and `unused-mut`.
                         continue;
diff --git a/crates/ide-assists/src/lib.rs b/crates/ide-assists/src/lib.rs
index 1eb4903ab20..edcf52a9b38 100644
--- a/crates/ide-assists/src/lib.rs
+++ b/crates/ide-assists/src/lib.rs
@@ -111,6 +111,8 @@ mod handlers {
     mod add_label_to_loop;
     mod add_lifetime_to_type;
     mod add_missing_impl_members;
+    mod add_missing_match_arms;
+    mod add_return_type;
     mod add_turbo_fish;
     mod apply_demorgan;
     mod auto_import;
@@ -124,15 +126,15 @@ mod handlers {
     mod convert_iter_for_each_to_for;
     mod convert_let_else_to_match;
     mod convert_match_to_let_else;
+    mod convert_named_struct_to_tuple_struct;
     mod convert_nested_function_to_closure;
+    mod convert_to_guarded_return;
     mod convert_tuple_return_type_to_struct;
     mod convert_tuple_struct_to_named_struct;
-    mod convert_named_struct_to_tuple_struct;
-    mod convert_to_guarded_return;
     mod convert_two_arm_bool_match_to_matches_macro;
     mod convert_while_to_loop;
-    mod desugar_doc_comment;
     mod destructure_tuple_binding;
+    mod desugar_doc_comment;
     mod expand_glob_import;
     mod extract_expressions_from_format_string;
     mod extract_function;
@@ -140,7 +142,6 @@ mod handlers {
     mod extract_struct_from_enum_variant;
     mod extract_type_alias;
     mod extract_variable;
-    mod add_missing_match_arms;
     mod fix_visibility;
     mod flip_binexpr;
     mod flip_comma;
@@ -148,6 +149,7 @@ mod handlers {
     mod generate_constant;
     mod generate_default_from_enum_variant;
     mod generate_default_from_new;
+    mod generate_delegate_methods;
     mod generate_delegate_trait;
     mod generate_deref;
     mod generate_derive;
@@ -162,62 +164,60 @@ mod handlers {
     mod generate_is_empty_from_len;
     mod generate_mut_trait_impl;
     mod generate_new;
-    mod generate_delegate_methods;
     mod generate_trait_from_impl;
-    mod add_return_type;
     mod inline_call;
     mod inline_const_as_literal;
     mod inline_local_variable;
     mod inline_macro;
     mod inline_type_alias;
+    mod into_to_qualified_from;
+    mod introduce_named_generic;
     mod introduce_named_lifetime;
     mod invert_if;
     mod merge_imports;
     mod merge_match_arms;
+    mod merge_nested_if;
     mod move_bounds;
     mod move_const_to_impl;
+    mod move_from_mod_rs;
     mod move_guard;
     mod move_module_to_file;
     mod move_to_mod_rs;
-    mod move_from_mod_rs;
     mod number_representation;
     mod promote_local_to_const;
     mod pull_assignment_up;
-    mod qualify_path;
     mod qualify_method_call;
+    mod qualify_path;
     mod raw_string;
     mod remove_dbg;
     mod remove_mut;
+    mod remove_parentheses;
     mod remove_unused_imports;
     mod remove_unused_param;
-    mod remove_parentheses;
     mod reorder_fields;
     mod reorder_impl_items;
-    mod replace_try_expr_with_match;
+    mod replace_arith_op;
     mod replace_derive_with_manual_impl;
     mod replace_if_let_with_match;
     mod replace_is_method_with_if_let_method;
-    mod replace_method_eager_lazy;
-    mod replace_arith_op;
-    mod introduce_named_generic;
     mod replace_let_with_if_let;
+    mod replace_method_eager_lazy;
     mod replace_named_generic_with_impl;
     mod replace_qualified_name_with_use;
     mod replace_string_with_char;
+    mod replace_try_expr_with_match;
     mod replace_turbofish_with_explicit_type;
-    mod split_import;
-    mod unmerge_match_arm;
-    mod unwrap_tuple;
     mod sort_items;
+    mod split_import;
     mod toggle_ignore;
+    mod unmerge_match_arm;
     mod unmerge_use;
     mod unnecessary_async;
+    mod unqualify_method_call;
     mod unwrap_block;
     mod unwrap_result_return_type;
-    mod unqualify_method_call;
+    mod unwrap_tuple;
     mod wrap_return_type_in_result;
-    mod into_to_qualified_from;
-    mod merge_nested_if;
 
     pub(crate) fn all() -> &'static [Handler] {
         &[
diff --git a/crates/ide-assists/src/utils.rs b/crates/ide-assists/src/utils.rs
index 2420945f756..eeb3d80d07b 100644
--- a/crates/ide-assists/src/utils.rs
+++ b/crates/ide-assists/src/utils.rs
@@ -23,8 +23,8 @@ use syntax::{
 
 use crate::assist_context::{AssistContext, SourceChangeBuilder};
 
-pub(crate) mod suggest_name;
 mod gen_trait_fn_body;
+pub(crate) mod suggest_name;
 
 pub(crate) fn unwrap_trivial_block(block_expr: ast::BlockExpr) -> ast::Expr {
     extract_trivial_expression(&block_expr)
diff --git a/crates/ide-completion/src/completions.rs b/crates/ide-completion/src/completions.rs
index 4d3d0b4d1a6..ba3c0cf3fd6 100644
--- a/crates/ide-completion/src/completions.rs
+++ b/crates/ide-completion/src/completions.rs
@@ -2,8 +2,10 @@
 
 pub(crate) mod attribute;
 pub(crate) mod dot;
+pub(crate) mod env_vars;
 pub(crate) mod expr;
 pub(crate) mod extern_abi;
+pub(crate) mod extern_crate;
 pub(crate) mod field;
 pub(crate) mod flyimport;
 pub(crate) mod fn_param;
@@ -19,8 +21,6 @@ pub(crate) mod snippet;
 pub(crate) mod r#type;
 pub(crate) mod use_;
 pub(crate) mod vis;
-pub(crate) mod env_vars;
-pub(crate) mod extern_crate;
 
 use std::iter;
 
diff --git a/crates/ide-completion/src/completions/attribute.rs b/crates/ide-completion/src/completions/attribute.rs
index 8f7c3b5070b..a7a6cdebd36 100644
--- a/crates/ide-completion/src/completions/attribute.rs
+++ b/crates/ide-completion/src/completions/attribute.rs
@@ -25,8 +25,8 @@ use crate::{
 mod cfg;
 mod derive;
 mod lint;
-mod repr;
 mod macro_use;
+mod repr;
 
 pub(crate) use self::derive::complete_derive_path;
 
diff --git a/crates/ide-completion/src/lib.rs b/crates/ide-completion/src/lib.rs
index 6a98e109f6d..d26b6f431b5 100644
--- a/crates/ide-completion/src/lib.rs
+++ b/crates/ide-completion/src/lib.rs
@@ -8,9 +8,9 @@ mod context;
 mod item;
 mod render;
 
+mod snippet;
 #[cfg(test)]
 mod tests;
-mod snippet;
 
 use ide_db::{
     base_db::FilePosition,
diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs
index 6fd988bfc0f..ad26280ae74 100644
--- a/crates/ide-completion/src/render.rs
+++ b/crates/ide-completion/src/render.rs
@@ -1,14 +1,14 @@
 //! `render` module provides utilities for rendering completion suggestions
 //! into code pieces that will be presented to user.
 
-pub(crate) mod macro_;
-pub(crate) mod function;
 pub(crate) mod const_;
+pub(crate) mod function;
+pub(crate) mod literal;
+pub(crate) mod macro_;
 pub(crate) mod pattern;
 pub(crate) mod type_alias;
-pub(crate) mod variant;
 pub(crate) mod union_literal;
-pub(crate) mod literal;
+pub(crate) mod variant;
 
 use hir::{AsAssocItem, HasAttrs, HirDisplay, ModuleDef, ScopeDef, Type};
 use ide_db::{
diff --git a/crates/ide-completion/src/tests.rs b/crates/ide-completion/src/tests.rs
index f13754e2ded..c421be51a0d 100644
--- a/crates/ide-completion/src/tests.rs
+++ b/crates/ide-completion/src/tests.rs
@@ -12,8 +12,8 @@ mod attribute;
 mod expression;
 mod flyimport;
 mod fn_param;
-mod item_list;
 mod item;
+mod item_list;
 mod pattern;
 mod predicate;
 mod proc_macros;
diff --git a/crates/ide-db/src/lib.rs b/crates/ide-db/src/lib.rs
index 1cc1e363299..2881748dd47 100644
--- a/crates/ide-db/src/lib.rs
+++ b/crates/ide-db/src/lib.rs
@@ -9,6 +9,7 @@ mod apply_change;
 pub mod active_parameter;
 pub mod assists;
 pub mod defs;
+pub mod documentation;
 pub mod famous_defs;
 pub mod helpers;
 pub mod items_locator;
@@ -22,7 +23,6 @@ pub mod symbol_index;
 pub mod traits;
 pub mod ty_filter;
 pub mod use_trivial_constructor;
-pub mod documentation;
 
 pub mod imports {
     pub mod import_assets;
@@ -35,10 +35,10 @@ pub mod generated {
 }
 
 pub mod syntax_helpers {
-    pub mod node_ext;
-    pub mod insert_whitespace_into_node;
     pub mod format_string;
     pub mod format_string_exprs;
+    pub mod insert_whitespace_into_node;
+    pub mod node_ext;
 
     pub use parser::LexedStr;
 }
@@ -414,6 +414,6 @@ impl SnippetCap {
 
 #[cfg(test)]
 mod tests {
-    mod sourcegen_lints;
     mod line_index;
+    mod sourcegen_lints;
 }
diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs
index 7743b060c86..f35fc5b533a 100644
--- a/crates/ide-diagnostics/src/lib.rs
+++ b/crates/ide-diagnostics/src/lib.rs
@@ -44,30 +44,30 @@ mod handlers {
     pub(crate) mod private_assoc_item;
     pub(crate) mod private_field;
     pub(crate) mod replace_filter_map_next_with_find_map;
-    pub(crate) mod trait_impl_orphan;
     pub(crate) mod trait_impl_incorrect_safety;
     pub(crate) mod trait_impl_missing_assoc_item;
+    pub(crate) mod trait_impl_orphan;
     pub(crate) mod trait_impl_redundant_assoc_item;
-    pub(crate) mod typed_hole;
     pub(crate) mod type_mismatch;
+    pub(crate) mod typed_hole;
+    pub(crate) mod undeclared_label;
     pub(crate) mod unimplemented_builtin_macro;
+    pub(crate) mod unreachable_label;
     pub(crate) mod unresolved_assoc_item;
     pub(crate) mod unresolved_extern_crate;
     pub(crate) mod unresolved_field;
-    pub(crate) mod unresolved_method;
     pub(crate) mod unresolved_import;
     pub(crate) mod unresolved_macro_call;
+    pub(crate) mod unresolved_method;
     pub(crate) mod unresolved_module;
     pub(crate) mod unresolved_proc_macro;
-    pub(crate) mod undeclared_label;
-    pub(crate) mod unreachable_label;
     pub(crate) mod unused_variables;
 
     // The handlers below are unusual, the implement the diagnostics as well.
     pub(crate) mod field_shorthand;
-    pub(crate) mod useless_braces;
-    pub(crate) mod unlinked_file;
     pub(crate) mod json_is_not_rust;
+    pub(crate) mod unlinked_file;
+    pub(crate) mod useless_braces;
 }
 
 #[cfg(test)]
diff --git a/crates/ide-ssr/src/lib.rs b/crates/ide-ssr/src/lib.rs
index d756e7a63eb..b5bf510aeed 100644
--- a/crates/ide-ssr/src/lib.rs
+++ b/crates/ide-ssr/src/lib.rs
@@ -69,11 +69,11 @@
 // // foo($a, $b) ==>> ($a).foo($b)
 // ```
 
+mod fragments;
 mod from_comment;
 mod matching;
 mod nester;
 mod parsing;
-mod fragments;
 mod replacing;
 mod resolving;
 mod search;
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs
index 79fff15f050..46e5901852a 100644
--- a/crates/ide/src/inlay_hints.rs
+++ b/crates/ide/src/inlay_hints.rs
@@ -25,13 +25,13 @@ mod bind_pat;
 mod binding_mode;
 mod chaining;
 mod closing_brace;
-mod closure_ret;
 mod closure_captures;
+mod closure_ret;
 mod discriminant;
 mod fn_lifetime_fn;
+mod implicit_drop;
 mod implicit_static;
 mod param_name;
-mod implicit_drop;
 mod range_exclusive;
 
 #[derive(Clone, Debug, PartialEq, Eq)]
diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs
index a50aa2e6dfb..e9f42d47855 100644
--- a/crates/ide/src/lib.rs
+++ b/crates/ide/src/lib.rs
@@ -21,24 +21,25 @@ macro_rules! eprintln {
 mod fixture;
 
 mod markup;
-mod prime_caches;
 mod navigation_target;
+mod prime_caches;
 
 mod annotations;
 mod call_hierarchy;
-mod signature_help;
 mod doc_links;
-mod highlight_related;
 mod expand_macro;
 mod extend_selection;
+mod fetch_crates;
 mod file_structure;
 mod folding_ranges;
 mod goto_declaration;
 mod goto_definition;
 mod goto_implementation;
 mod goto_type_definition;
+mod highlight_related;
 mod hover;
 mod inlay_hints;
+mod interpret_function;
 mod join_lines;
 mod markdown_remove;
 mod matching_brace;
@@ -48,6 +49,8 @@ mod parent_module;
 mod references;
 mod rename;
 mod runnables;
+mod shuffle_crate_graph;
+mod signature_help;
 mod ssr;
 mod static_index;
 mod status;
@@ -56,12 +59,9 @@ mod syntax_tree;
 mod typing;
 mod view_crate_graph;
 mod view_hir;
-mod view_mir;
-mod interpret_function;
 mod view_item_tree;
-mod shuffle_crate_graph;
-mod fetch_crates;
 mod view_memory_layout;
+mod view_mir;
 
 use std::ffi::OsStr;
 
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs
index 3607c486d7d..8c6f5e2e9cb 100644
--- a/crates/ide/src/syntax_highlighting.rs
+++ b/crates/ide/src/syntax_highlighting.rs
@@ -3,11 +3,11 @@ pub(crate) mod tags;
 mod highlights;
 mod injector;
 
-mod highlight;
+mod escape;
 mod format;
-mod macro_;
+mod highlight;
 mod inject;
-mod escape;
+mod macro_;
 
 mod html;
 #[cfg(test)]
diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs
index f968a89a441..62fdce36892 100644
--- a/crates/mbe/src/lib.rs
+++ b/crates/mbe/src/lib.rs
@@ -8,11 +8,11 @@
 
 #![warn(rust_2018_idioms, unused_lifetimes)]
 
-mod parser;
 mod expander;
+mod parser;
 mod syntax_bridge;
-mod tt_iter;
 mod to_parser_input;
+mod tt_iter;
 
 #[cfg(test)]
 mod benchmark;
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs
index 8b57d7eeaf5..4513732902d 100644
--- a/crates/mbe/src/syntax_bridge.rs
+++ b/crates/mbe/src/syntax_bridge.rs
@@ -234,7 +234,7 @@ where
     let mut stack = NonEmptyVec::new(entry);
 
     while let Some((token, abs_range)) = conv.bump() {
-        let tt::Subtree { delimiter, token_trees: result } = stack.last_mut();
+        let tt::Subtree { delimiter, token_trees } = stack.last_mut();
 
         let tt = match token.as_leaf() {
             Some(leaf) => tt::TokenTree::Leaf(leaf.clone()),
@@ -243,7 +243,7 @@ where
                 COMMENT => {
                     let span = conv.span_for(abs_range);
                     if let Some(tokens) = conv.convert_doc_comment(&token, span) {
-                        result.extend(tokens);
+                        token_trees.extend(tokens);
                     }
                     continue;
                 }
@@ -317,7 +317,7 @@ where
                                 span: conv
                                     .span_for(TextRange::at(abs_range.start(), TextSize::of('\''))),
                             });
-                            result.push(apostrophe.into());
+                            token_trees.push(apostrophe.into());
 
                             let ident = tt::Leaf::from(tt::Ident {
                                 text: SmolStr::new(&token.to_text(conv)[1..]),
@@ -326,7 +326,7 @@ where
                                     abs_range.end(),
                                 )),
                             });
-                            result.push(ident.into());
+                            token_trees.push(ident.into());
                             continue;
                         }
                         _ => continue,
@@ -337,7 +337,7 @@ where
             },
         };
 
-        result.push(tt);
+        token_trees.push(tt);
     }
 
     // If we get here, we've consumed all input tokens.
diff --git a/crates/parser/src/grammar.rs b/crates/parser/src/grammar.rs
index 19da297b58c..53fda3ae4fd 100644
--- a/crates/parser/src/grammar.rs
+++ b/crates/parser/src/grammar.rs
@@ -30,12 +30,12 @@
 
 mod attributes;
 mod expressions;
+mod generic_args;
+mod generic_params;
 mod items;
 mod params;
 mod paths;
 mod patterns;
-mod generic_args;
-mod generic_params;
 mod types;
 
 use crate::{
diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs
index caf2a005a7d..243a219525a 100644
--- a/crates/parser/src/grammar/items.rs
+++ b/crates/parser/src/grammar/items.rs
@@ -1,5 +1,5 @@
-mod consts;
 mod adt;
+mod consts;
 mod traits;
 mod use_item;
 
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs
index ed0aec3cab3..3ca285e787e 100644
--- a/crates/parser/src/lib.rs
+++ b/crates/parser/src/lib.rs
@@ -26,15 +26,15 @@ extern crate ra_ap_rustc_lexer as rustc_lexer;
 #[cfg(feature = "in-rust-tree")]
 extern crate rustc_lexer;
 
-mod lexed_str;
-mod token_set;
-mod syntax_kind;
 mod event;
-mod parser;
 mod grammar;
 mod input;
+mod lexed_str;
 mod output;
+mod parser;
 mod shortcuts;
+mod syntax_kind;
+mod token_set;
 
 #[cfg(test)]
 mod tests;
diff --git a/crates/parser/src/tests.rs b/crates/parser/src/tests.rs
index 2fec765bd78..c65219b28dc 100644
--- a/crates/parser/src/tests.rs
+++ b/crates/parser/src/tests.rs
@@ -1,6 +1,6 @@
+mod prefix_entries;
 mod sourcegen_inline_tests;
 mod top_entries;
-mod prefix_entries;
 
 use std::{
     fmt::Write,
diff --git a/crates/proc-macro-srv/src/lib.rs b/crates/proc-macro-srv/src/lib.rs
index 67b9f57a163..460a96c07f3 100644
--- a/crates/proc-macro-srv/src/lib.rs
+++ b/crates/proc-macro-srv/src/lib.rs
@@ -21,8 +21,8 @@ extern crate proc_macro;
 extern crate rustc_driver as _;
 
 mod dylib;
-mod server;
 mod proc_macros;
+mod server;
 
 use std::{
     collections::{hash_map::Entry, HashMap},
diff --git a/crates/proc-macro-srv/src/server.rs b/crates/proc-macro-srv/src/server.rs
index 1854322ddb5..ff8fd295d88 100644
--- a/crates/proc-macro-srv/src/server.rs
+++ b/crates/proc-macro-srv/src/server.rs
@@ -13,9 +13,9 @@ use proc_macro::bridge;
 mod token_stream;
 pub use token_stream::TokenStream;
 
-pub mod token_id;
 pub mod rust_analyzer_span;
 mod symbol;
+pub mod token_id;
 pub use symbol::*;
 use tt::Spacing;
 
diff --git a/crates/proc-macro-srv/src/server/token_id.rs b/crates/proc-macro-srv/src/server/token_id.rs
index 12526ad4f3a..c83e09af0d6 100644
--- a/crates/proc-macro-srv/src/server/token_id.rs
+++ b/crates/proc-macro-srv/src/server/token_id.rs
@@ -206,7 +206,7 @@ impl server::TokenStream for TokenIdServer {
                     stream: if subtree.token_trees.is_empty() {
                         None
                     } else {
-                        Some(subtree.token_trees.into_iter().collect())
+                        Some(TokenStream { token_trees: subtree.token_trees })
                     },
                     span: bridge::DelimSpan::from_single(subtree.delimiter.open),
                 }),
diff --git a/crates/profile/src/lib.rs b/crates/profile/src/lib.rs
index fdd724e2aab..d86aa0c4144 100644
--- a/crates/profile/src/lib.rs
+++ b/crates/profile/src/lib.rs
@@ -2,11 +2,11 @@
 
 #![warn(rust_2018_idioms, unused_lifetimes)]
 
-mod stop_watch;
-mod memory_usage;
 #[cfg(feature = "cpu_profiler")]
 mod google_cpu_profiler;
 mod hprof;
+mod memory_usage;
+mod stop_watch;
 mod tree;
 
 use std::cell::RefCell;
diff --git a/crates/project-model/src/lib.rs b/crates/project-model/src/lib.rs
index 5f9b708289d..5114c9c016d 100644
--- a/crates/project-model/src/lib.rs
+++ b/crates/project-model/src/lib.rs
@@ -17,15 +17,15 @@
 
 #![warn(rust_2018_idioms, unused_lifetimes)]
 
-mod manifest_path;
+mod build_scripts;
 mod cargo_workspace;
 mod cfg_flag;
+mod manifest_path;
 mod project_json;
-mod sysroot;
-mod workspace;
 mod rustc_cfg;
-mod build_scripts;
+mod sysroot;
 pub mod target_data_layout;
+mod workspace;
 
 #[cfg(test)]
 mod tests;
diff --git a/crates/rust-analyzer/src/cli.rs b/crates/rust-analyzer/src/cli.rs
index de00c4192b4..00670f2cb4c 100644
--- a/crates/rust-analyzer/src/cli.rs
+++ b/crates/rust-analyzer/src/cli.rs
@@ -1,16 +1,16 @@
 //! Various batch processing tasks, intended primarily for debugging.
 
-pub mod flags;
-mod parse;
-mod symbols;
-mod highlight;
 mod analysis_stats;
 mod diagnostics;
-mod ssr;
+pub mod flags;
+mod highlight;
 mod lsif;
-mod scip;
+mod parse;
 mod run_tests;
 mod rustc_tests;
+mod scip;
+mod ssr;
+mod symbols;
 
 mod progress_report;
 
diff --git a/crates/rust-analyzer/src/lsp.rs b/crates/rust-analyzer/src/lsp.rs
index ac7e1a95e62..9e0d42faed4 100644
--- a/crates/rust-analyzer/src/lsp.rs
+++ b/crates/rust-analyzer/src/lsp.rs
@@ -2,11 +2,11 @@
 
 use core::fmt;
 
-pub(crate) mod utils;
-pub(crate) mod semantic_tokens;
 pub mod ext;
 pub(crate) mod from_proto;
+pub(crate) mod semantic_tokens;
 pub(crate) mod to_proto;
+pub(crate) mod utils;
 
 #[derive(Debug)]
 pub(crate) struct LspError {
diff --git a/crates/span/src/lib.rs b/crates/span/src/lib.rs
index f6569050b4a..6796dc41886 100644
--- a/crates/span/src/lib.rs
+++ b/crates/span/src/lib.rs
@@ -1,7 +1,4 @@
 //! File and span related types.
-// FIXME: This should be moved into its own crate to get rid of the dependency inversion, base-db
-// has business depending on tt, tt should depend on a span crate only (which unforunately will have
-// to depend on salsa)
 use std::fmt::{self, Write};
 
 use salsa::InternId;
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs
index cd5285295a2..07b78272281 100644
--- a/crates/stdx/src/lib.rs
+++ b/crates/stdx/src/lib.rs
@@ -6,13 +6,13 @@ use std::io as sio;
 use std::process::Command;
 use std::{cmp::Ordering, ops, time::Instant};
 
+pub mod anymap;
 mod macros;
-pub mod process;
-pub mod panic_context;
 pub mod non_empty_vec;
+pub mod panic_context;
+pub mod process;
 pub mod rand;
 pub mod thread;
-pub mod anymap;
 
 pub use always_assert::{always, never};
 pub use itertools;
diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs
index cc90d2dd1d5..e9ab7a4320b 100644
--- a/crates/syntax/src/ast.rs
+++ b/crates/syntax/src/ast.rs
@@ -1,15 +1,15 @@
 //! Abstract Syntax Tree, layered on top of untyped `SyntaxNode`s
 
-mod generated;
-mod traits;
-mod token_ext;
-mod node_ext;
-mod expr_ext;
-mod operators;
 pub mod edit;
 pub mod edit_in_place;
+mod expr_ext;
+mod generated;
 pub mod make;
+mod node_ext;
+mod operators;
 pub mod prec;
+mod token_ext;
+mod traits;
 
 use std::marker::PhantomData;
 
diff --git a/crates/syntax/src/lib.rs b/crates/syntax/src/lib.rs
index 21ed1310f56..62a0261d7a4 100644
--- a/crates/syntax/src/lib.rs
+++ b/crates/syntax/src/lib.rs
@@ -32,22 +32,22 @@ macro_rules! eprintln {
     ($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
 }
 
-mod syntax_node;
-mod syntax_error;
 mod parsing;
-mod validation;
 mod ptr;
-mod token_text;
+mod syntax_error;
+mod syntax_node;
 #[cfg(test)]
 mod tests;
+mod token_text;
+mod validation;
 
 pub mod algo;
 pub mod ast;
 #[doc(hidden)]
 pub mod fuzz;
-pub mod utils;
-pub mod ted;
 pub mod hacks;
+pub mod ted;
+pub mod utils;
 
 use std::marker::PhantomData;
 
diff --git a/editors/code/language-configuration.json b/editors/code/language-configuration.json
index 1c348b63f1a..a2af8b51a90 100644
--- a/editors/code/language-configuration.json
+++ b/editors/code/language-configuration.json
@@ -6,7 +6,9 @@
     "brackets": [
         ["{", "}"],
         ["[", "]"],
-        ["(", ")"]
+        ["(", ")"],
+        ["#[", "]"],
+        ["#![", "]"]
     ],
     "colorizedBracketPairs": [
         ["{", "}"],
@@ -17,6 +19,8 @@
         { "open": "{", "close": "}" },
         { "open": "[", "close": "]" },
         { "open": "(", "close": ")" },
+        { "open": "#[", "close": "]" },
+        { "open": "#![", "close": "]" },
         { "open": "\"", "close": "\"", "notIn": ["string"] },
         { "open": "/*", "close": " */" },
         { "open": "`", "close": "`", "notIn": ["string"] }
diff --git a/lib/lsp-server/src/lib.rs b/lib/lsp-server/src/lib.rs
index f717f8e0d4b..e476f8c2d13 100644
--- a/lib/lsp-server/src/lib.rs
+++ b/lib/lsp-server/src/lib.rs
@@ -6,11 +6,11 @@
 
 #![warn(rust_2018_idioms, unused_lifetimes)]
 
-mod msg;
-mod stdio;
 mod error;
-mod socket;
+mod msg;
 mod req_queue;
+mod socket;
+mod stdio;
 
 use std::{
     io,
diff --git a/rustfmt.toml b/rustfmt.toml
index 71007de81b9..20bf59547b8 100644
--- a/rustfmt.toml
+++ b/rustfmt.toml
@@ -1,2 +1,2 @@
-reorder_modules = false
+reorder_modules = true
 use_small_heuristics = "Max"
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index 49f8ae79baf..2d40ceb737d 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -12,11 +12,11 @@
 
 mod flags;
 
-mod install;
-mod release;
 mod dist;
-mod publish;
+mod install;
 mod metrics;
+mod publish;
+mod release;
 
 use anyhow::bail;
 use std::{