Repository: rust-analyzer/rust-analyzer
Pull request #7795 (2021-02-27 00:44:58)

Show docs on hover for keywords and primitives
![lAWFadkziX](https://user-images.githubusercontent.com/3757771/109369534-eeb4f500-789c-11eb-8f2b-2f9c4e129de3.gif)

It's a bit annoying that this requires the `SyntaxNode` and `Semantics` to be pulled through `hover_for_definition` just so we can get the `std` crate but I couldn't think of a better way.


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index ec5aecfa09..625c618831 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -111,15 +111,6 @@ version = "1.4.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b"
 
-[[package]]
-name = "camino"
-version = "1.0.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "9bb47ab72bdba43021afa16dc1ef4d80c980d366b17ed37ea8d2ebe2087075b9"
-dependencies = [
- "serde",
-]
-
 [[package]]
 name = "cargo-platform"
 version = "0.1.1"
@@ -131,11 +122,10 @@ dependencies = [
 
 [[package]]
 name = "cargo_metadata"
-version = "0.13.1"
+version = "0.12.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "081e3f0755c1f380c2d010481b6fa2e02973586d5f2b24eebb7a2a1d98b143d8"
+checksum = "7714a157da7991e23d90686b9524b9e12e0407a108647f52e9328f4b3d51ac7f"
 dependencies = [
- "camino",
  "cargo-platform",
  "semver",
  "semver-parser",
@@ -789,9 +779,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
 
 [[package]]
 name = "libc"
-version = "0.2.87"
+version = "0.2.86"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213"
+checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"
 
 [[package]]
 name = "libloading"
@@ -879,7 +869,6 @@ dependencies = [
  "profile",
  "rustc-hash",
  "smallvec",
- "stdx",
  "syntax",
  "test_utils",
  "tt",
@@ -1016,9 +1005,9 @@ checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4"
 
 [[package]]
 name = "once_cell"
-version = "1.7.1"
+version = "1.7.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "ea78b9742c52ac729753c1590e9adc5248ea9bdaf974597efd46c74cfaa5fb54"
+checksum = "10acf907b94fc1b1a152d08ef97e7759650268cf986bf127f387e602b02c7e5a"
 
 [[package]]
 name = "oorandom"
@@ -1127,9 +1116,9 @@ dependencies = [
 
 [[package]]
 name = "pin-project-lite"
-version = "0.2.5"
+version = "0.2.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0cf491442e4b033ed1c722cb9f0df5fcfcf4de682466c46469c36bc47dc5548a"
+checksum = "439697af366c49a6d0a010c56a0d97685bc140ce0d377b13a2ea2aa42d64a827"
 
 [[package]]
 name = "proc-macro-hack"
diff --git a/crates/flycheck/Cargo.toml b/crates/flycheck/Cargo.toml
index 2a1a21b28b..1d19c78869 100644
--- a/crates/flycheck/Cargo.toml
+++ b/crates/flycheck/Cargo.toml
@@ -12,9 +12,9 @@ doctest = false
 [dependencies]
 crossbeam-channel = "0.5.0"
 log = "0.4.8"
-cargo_metadata = "0.13"
+cargo_metadata = "0.12.2"
 serde_json = "1.0.48"
 jod-thread = "0.1.1"
 
 toolchain = { path = "../toolchain", version = "0.0.0" }
-stdx = { path = "../stdx", version = "0.0.0" }
+stdx = { path = "../stdx", version = "0.0.0"  }
diff --git a/crates/flycheck/src/lib.rs b/crates/flycheck/src/lib.rs
index e2a59497aa..e04208006f 100644
--- a/crates/flycheck/src/lib.rs
+++ b/crates/flycheck/src/lib.rs
@@ -194,7 +194,7 @@ impl FlycheckActor {
                     cargo_metadata::Message::BuildScriptExecuted(_)
                     | cargo_metadata::Message::BuildFinished(_)
                     | cargo_metadata::Message::TextLine(_)
-                    | _ => {}
+                    | cargo_metadata::Message::Unknown => {}
                 },
             }
         }
@@ -329,7 +329,8 @@ impl CargoActor {
             // Skip certain kinds of messages to only spend time on what's useful
             match &message {
                 cargo_metadata::Message::CompilerArtifact(artifact) if artifact.fresh => (),
-                cargo_metadata::Message::BuildScriptExecuted(_) => (),
+                cargo_metadata::Message::BuildScriptExecuted(_)
+                | cargo_metadata::Message::Unknown => (),
                 _ => self.sender.send(message).unwrap(),
             }
         }
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 769945c474..69fcdab07a 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -33,11 +33,11 @@ mod has_source;
 pub use crate::{
     attrs::{HasAttrs, Namespace},
     code_model::{
-        Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
-        ConstParam, Crate, CrateDependency, DefWithBody, Enum, Field, FieldSource, Function,
-        GenericDef, GenericParam, HasVisibility, Impl, Label, LifetimeParam, Local, MacroDef,
-        Module, ModuleDef, ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union,
-        Variant, VariantDef,
+        Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, BuiltinType, Callable,
+        CallableKind, Const, ConstParam, Crate, CrateDependency, DefWithBody, Enum, Field,
+        FieldSource, Function, GenericDef, GenericParam, HasVisibility, Impl, Label, LifetimeParam,
+        Local, MacroDef, Module, ModuleDef, ScopeDef, Static, Struct, Trait, Type, TypeAlias,
+        TypeParam, Union, Variant, VariantDef,
     },
     has_source::HasSource,
     semantics::{PathResolution, Semantics, SemanticsScope},
@@ -47,7 +47,6 @@ pub use hir_def::{
     adt::StructKind,
     attr::{Attrs, Documentation},
     body::scope::ExprScopes,
-    builtin_type::BuiltinType,
     find_path::PrefixKind,
     import_map,
     item_scope::ItemInNs,
diff --git a/crates/ide/src/folding_ranges.rs b/crates/ide/src/folding_ranges.rs
index 4b1b245628..45170dd29d 100644
--- a/crates/ide/src/folding_ranges.rs
+++ b/crates/ide/src/folding_ranges.rs
@@ -6,7 +6,7 @@ use syntax::{
     ast::{self, AstNode, AstToken, VisibilityOwner},
     Direction, NodeOrToken, SourceFile,
     SyntaxKind::{self, *},
-    SyntaxNode, TextRange, TextSize,
+    SyntaxNode, TextRange,
 };
 
 #[derive(Debug, PartialEq, Eq)]
@@ -16,7 +16,6 @@ pub enum FoldKind {
     Mods,
     Block,
     ArgList,
-    Region,
 }
 
 #[derive(Debug)]
@@ -30,8 +29,6 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
     let mut visited_comments = FxHashSet::default();
     let mut visited_imports = FxHashSet::default();
     let mut visited_mods = FxHashSet::default();
-    // regions can be nested, here is a LIFO buffer
-    let mut regions_starts: Vec<TextSize> = vec![];
 
     for element in file.syntax().descendants_with_tokens() {
         // Fold items that span multiple lines
@@ -51,25 +48,10 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec<Fold> {
                 // Fold groups of comments
                 if let Some(comment) = ast::Comment::cast(token) {
                     if !visited_comments.contains(&comment) {
-                        // regions are not real comments
-                        if comment.text().trim().starts_with("// region:") {
-                            regions_starts.push(comment.syntax().text_range().start());
-                        } else if comment.text().trim().starts_with("// endregion") {
-                            if let Some(region) = regions_starts.pop() {
-                                res.push(Fold {
-                                    range: TextRange::new(
-                                        region,
-                                        comment.syntax().text_range().end(),
-                                    ),
-                                    kind: FoldKind::Region,
-                                })
-                            }
-                        } else {
-                            if let Some(range) =
-                                contiguous_range_for_comment(comment, &mut visited_comments)
-                            {
-                                res.push(Fold { range, kind: FoldKind::Comment })
-                            }
+                        if let Some(range) =
+                            contiguous_range_for_comment(comment, &mut visited_comments)
+                        {
+                            res.push(Fold { range, kind: FoldKind::Comment })
                         }
                     }
                 }
@@ -193,16 +175,9 @@ fn contiguous_range_for_comment(
                 }
                 if let Some(c) = ast::Comment::cast(token) {
                     if c.kind() == group_kind {
-                        // regions are not real comments
-                        if c.text().trim().starts_with("// region:")
-                            || c.text().trim().starts_with("// endregion")
-                        {
-                            break;
-                        } else {
-                            visited.insert(c.clone());
-                            last = c;
-                            continue;
-                        }
+                        visited.insert(c.clone());
+                        last = c;
+                        continue;
                     }
                 }
                 // The comment group ends because either:
@@ -249,7 +224,6 @@ mod tests {
                 FoldKind::Mods => "mods",
                 FoldKind::Block => "block",
                 FoldKind::ArgList => "arglist",
-                FoldKind::Region => "region",
             };
             assert_eq!(kind, &attr.unwrap());
         }
@@ -441,19 +415,6 @@ fn foo<fold arglist>(
     x: i32,
     y: String,
 )</fold> {}
-"#,
-        )
-    }
-
-    #[test]
-    fn fold_region() {
-        check(
-            r#"
-// 1. some normal comment
-<fold region>// region: test
-// 2. some normal comment
-calling_function(x,y);
-// endregion: test</fold>
 "#,
         )
     }
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs
index 20b799490e..a9454cfa31 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -5,6 +5,7 @@ use hir::{
 use ide_db::{
     base_db::SourceDatabase,
     defs::{Definition, NameClass, NameRefClass},
+    helpers::FamousDefs,
     RootDatabase,
 };
 use itertools::Itertools;
@@ -107,16 +108,14 @@ pub(crate) fn hover(
         }
     };
     if let Some(definition) = definition {
-        if let Some(markup) = hover_for_definition(db, definition) {
-            let markup = markup.as_str();
-            let markup = if !markdown {
-                remove_markdown(markup)
-            } else if links_in_hover {
-                rewrite_links(db, markup, &definition)
-            } else {
-                remove_links(markup)
-            };
-            res.markup = Markup::from(markup);
+        let famous_defs = match &definition {
+            Definition::ModuleDef(ModuleDef::BuiltinType(_)) => {
+                Some(FamousDefs(&sema, sema.scope(&node).krate()))
+            }
+            _ => None,
+        };
+        if let Some(markup) = hover_for_definition(db, definition, famous_defs.as_ref()) {
+            res.markup = process_markup(sema.db, definition, &markup, links_in_hover, markdown);
             if let Some(action) = show_implementations_action(db, definition) {
                 res.actions.push(action);
             }
@@ -138,6 +137,9 @@ pub(crate) fn hover(
         // don't highlight the entire parent node on comment hover
         return None;
     }
+    if let res @ Some(_) = hover_for_keyword(&sema, links_in_hover, markdown, &token) {
+        return res;
+    }
 
     let node = token
         .ancestors()
@@ -272,6 +274,24 @@ fn hover_markup(
     }
 }
 
+fn process_markup(
+    db: &RootDatabase,
+    def: Definition,
+    markup: &Markup,
+    links_in_hover: bool,
+    markdown: bool,
+) -> Markup {
+    let markup = markup.as_str();
+    let markup = if !markdown {
+        remove_markdown(markup)
+    } else if links_in_hover {
+        rewrite_links(db, markup, &def)
+    } else {
+        remove_links(markup)
+    };
+    Markup::from(markup)
+}
+
 fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String> {
     match def {
         Definition::Field(f) => Some(f.parent_def(db).name(db)),
@@ -304,7 +324,11 @@ fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
     def.module(db).map(|module| render_path(db, module, definition_owner_name(db, def)))
 }
 
-fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
+fn hover_for_definition(
+    db: &RootDatabase,
+    def: Definition,
+    famous_defs: Option<&FamousDefs>,
+) -> Option<Markup> {
     let mod_path = definition_mod_path(db, &def);
     return match def {
         Definition::Macro(it) => {
@@ -339,7 +363,9 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
             ModuleDef::Static(it) => from_def_source(db, it, mod_path),
             ModuleDef::Trait(it) => from_def_source(db, it, mod_path),
             ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path),
-            ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it.name())),
+            ModuleDef::BuiltinType(it) => famous_defs
+                .and_then(|fd| hover_for_builtin(fd, it))
+                .or_else(|| Some(Markup::fenced_block(&it.name()))),
         },
         Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))),
         Definition::SelfType(impl_def) => {
@@ -380,11 +406,52 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
     }
 }
 
+fn hover_for_keyword(
+    sema: &Semantics<RootDatabase>,
+    links_in_hover: bool,
+    markdown: bool,
+    token: &SyntaxToken,
+) -> Option<RangeInfo<HoverResult>> {
+    if !token.kind().is_keyword() {
+        return None;
+    }
+    let famous_defs = FamousDefs(&sema, sema.scope(&token.parent()).krate());
+    // std exposes {}_keyword modules with docstrings on the root to document keywords
+    let keyword_mod = format!("{}_keyword", token.text());
+    let doc_owner = find_std_module(&famous_defs, &keyword_mod)?;
+    let docs = doc_owner.attrs(sema.db).docs()?;
+    let markup = process_markup(
+        sema.db,
+        Definition::ModuleDef(doc_owner.into()),
+        &hover_markup(Some(docs.into()), Some(token.text().into()), None)?,
+        links_in_hover,
+        markdown,
+    );
+    Some(RangeInfo::new(token.text_range(), HoverResult { markup, actions: Default::default() }))
+}
+
+fn hover_for_builtin(famous_defs: &FamousDefs, builtin: hir::BuiltinType) -> Option<Markup> {
+    // std exposes prim_{} modules with docstrings on the root to document the builtins
+    let primitive_mod = format!("prim_{}", builtin.name());
+    let doc_owner = find_std_module(famous_defs, &primitive_mod)?;
+    let docs = doc_owner.attrs(famous_defs.0.db).docs()?;
+    hover_markup(Some(docs.into()), Some(builtin.name().to_string()), None)
+}
+
+fn find_std_module(famous_defs: &FamousDefs, name: &str) -> Option<hir::Module> {
+    let db = famous_defs.0.db;
+    let std_crate = famous_defs.std()?;
+    let std_root_module = std_crate.root_module(db);
+    std_root_module
+        .children(db)
+        .find(|module| module.name(db).map_or(false, |module| module.to_string() == name))
+}
+
 fn pick_best(tokens: TokenAtOffset<SyntaxToken>) -> Option<SyntaxToken> {
     return tokens.max_by_key(priority);
     fn priority(n: &SyntaxToken) -> usize {
         match n.kind() {
-            IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] => 3,
+            IDENT | INT_NUMBER | LIFETIME_IDENT | T![self] | T![super] | T![crate] => 3,
             T!['('] | T![')'] => 2,
             kind if kind.is_trivia() => 0,
             _ => 1,
@@ -3523,6 +3590,48 @@ use foo::bar::{self$0};
 
                 But this should appear
             "#]],
+        )
+    }
+
+    #[test]
+    fn hover_keyword() {
+        let ra_fixture = r#"//- /main.rs crate:main deps:std
+fn f() { retur$0n; }"#;
+        let fixture = format!("{}\n{}", ra_fixture, FamousDefs::FIXTURE);
+        check(
+            &fixture,
+            expect![[r#"
+                *return*
+
+                ```rust
+                return
+                ```
+
+                ---
+
+                Docs for return_keyword
+            "#]],
+        );
+    }
+
+    #[test]
+    fn hover_builtin() {
+        let ra_fixture = r#"//- /main.rs crate:main deps:std
+cosnt _: &str$0 = ""; }"#;
+        let fixture = format!("{}\n{}", ra_fixture, FamousDefs::FIXTURE);
+        check(
+            &fixture,
+            expect![[r#"
+                *str*
+
+                ```rust
+                str
+                ```
+
+                ---
+
+                Docs for prim_str
+            "#]],
         );
     }
 }
diff --git a/crates/ide_assists/src/handlers/extract_variable.rs b/crates/ide_assists/src/handlers/extract_variable.rs
index 312ac7ac42..98f3dc6ca0 100644
--- a/crates/ide_assists/src/handlers/extract_variable.rs
+++ b/crates/ide_assists/src/handlers/extract_variable.rs
@@ -8,7 +8,7 @@ use syntax::{
 };
 use test_utils::mark;
 
-use crate::{utils::suggest_name, AssistContext, AssistId, AssistKind, Assists};
+use crate::{AssistContext, AssistId, AssistKind, Assists};
 
 // Assist: extract_variable
 //
@@ -54,7 +54,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
 
             let var_name = match &field_shorthand {
                 Some(it) => it.to_string(),
-                None => suggest_name::variable(&to_extract, &ctx.sema),
+                None => "var_name".to_string(),
             };
             let expr_range = match &field_shorthand {
                 Some(it) => it.syntax().text_range().cover(to_extract.syntax().text_range()),
@@ -274,8 +274,8 @@ fn foo() {
 "#,
             r#"
 fn foo() {
-    let $0bar = bar(1 + 1);
-    bar
+    let $0var_name = bar(1 + 1);
+    var_name
 }
 "#,
         )
@@ -401,8 +401,8 @@ fn main() {
 ",
             "
 fn main() {
-    let $0foo = bar.foo();
-    let v = foo;
+    let $0var_name = bar.foo();
+    let v = var_name;
 }
 ",
         );
@@ -556,202 +556,6 @@ fn main() {
         )
     }
 
-    #[test]
-    fn extract_var_name_from_type() {
-        check_assist(
-            extract_variable,
-            r#"
-struct Test(i32);
-
-fn foo() -> Test {
-    $0{ Test(10) }$0
-}
-"#,
-            r#"
-struct Test(i32);
-
-fn foo() -> Test {
-    let $0test = { Test(10) };
-    test
-}
-"#,
-        )
-    }
-
-    #[test]
-    fn extract_var_name_from_parameter() {
-        check_assist(
-            extract_variable,
-            r#"
-fn bar(test: u32, size: u32)
-
-fn foo() {
-    bar(1, $01+1$0);
-}
-"#,
-            r#"
-fn bar(test: u32, size: u32)
-
-fn foo() {
-    let $0size = 1+1;
-    bar(1, size);
-}
-"#,
-        )
-    }
-
-    #[test]
-    fn extract_var_parameter_name_has_precedence_over_type() {
-        check_assist(
-            extract_variable,
-            r#"
-struct TextSize(u32);
-fn bar(test: u32, size: TextSize)
-
-fn foo() {
-    bar(1, $0{ TextSize(1+1) }$0);
-}
-"#,
-            r#"
-struct TextSize(u32);
-fn bar(test: u32, size: TextSize)
-
-fn foo() {
-    let $0size = { TextSize(1+1) };
-    bar(1, size);
-}
-"#,
-        )
-    }
-
-    #[test]
-    fn extract_var_name_from_function() {
-        check_assist(
-            extract_variable,
-            r#"
-fn is_required(test: u32, size: u32) -> bool
-
-fn foo() -> bool {
-    $0is_required(1, 2)$0
-}
-"#,
-            r#"
-fn is_required(test: u32, size: u32) -> bool
-
-fn foo() -> bool {
-    let $0is_required = is_required(1, 2);
-    is_required
-}
-"#,
-        )
-    }
-
-    #[test]
-    fn extract_var_name_from_method() {
-        check_assist(
-            extract_variable,
-            r#"
-struct S;
-impl S {
-    fn bar(&self, n: u32) -> u32 { n }
-}
-
-fn foo() -> u32 {
-    $0S.bar(1)$0
-}
-"#,
-            r#"
-struct S;
-impl S {
-    fn bar(&self, n: u32) -> u32 { n }
-}
-
-fn foo() -> u32 {
-    let $0bar = S.bar(1);
-    bar
-}
-"#,
-        )
-    }
-
-    #[test]
-    fn extract_var_name_from_method_param() {
-        check_assist(
-            extract_variable,
-            r#"
-struct S;
-impl S {
-    fn bar(&self, n: u32, size: u32) { n }
-}
-
-fn foo() {
-    S.bar($01 + 1$0, 2)
-}
-"#,
-            r#"
-struct S;
-impl S {
-    fn bar(&self, n: u32, size: u32) { n }
-}
-
-fn foo() {
-    let $0n = 1 + 1;
-    S.bar(n, 2)
-}
-"#,
-        )
-    }
-
-    #[test]
-    fn extract_var_name_from_ufcs_method_param() {
-        check_assist(
-            extract_variable,
-            r#"
-struct S;
-impl S {
-    fn bar(&self, n: u32, size: u32) { n }
-}
-
-fn foo() {
-    S::bar(&S, $01 + 1$0, 2)
-}
-"#,
-            r#"
-struct S;
-impl S {
-    fn bar(&self, n: u32, size: u32) { n }
-}
-
-fn foo() {
-    let $0n = 1 + 1;
-    S::bar(&S, n, 2)
-}
-"#,
-        )
-    }
-
-    #[test]
-    fn extract_var_parameter_name_has_precedence_over_function() {
-        check_assist(
-            extract_variable,
-            r#"
-fn bar(test: u32, size: u32)
-
-fn foo() {
-    bar(1, $0symbol_size(1, 2)$0);
-}
-"#,
-            r#"
-fn bar(test: u32, size: u32)
-
-fn foo() {
-    let $0size = symbol_size(1, 2);
-    bar(1, size);
-}
-"#,
-        )
-    }
-
     #[test]
     fn test_extract_var_for_return_not_applicable() {
         check_assist_not_applicable(extract_variable, "fn foo() { $0return$0; } ");
diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs
index 62f959082e..880ab6fe3e 100644
--- a/crates/ide_assists/src/utils.rs
+++ b/crates/ide_assists/src/utils.rs
@@ -1,7 +1,5 @@
 //! Assorted functions shared by several assists.
 
-pub(crate) mod suggest_name;
-
 use std::ops;
 
 use ast::TypeBoundsOwner;
diff --git a/crates/ide_assists/src/utils/suggest_name.rs b/crates/ide_assists/src/utils/suggest_name.rs
deleted file mode 100644
index d37c626429..0000000000
--- a/crates/ide_assists/src/utils/suggest_name.rs
+++ /dev/null
@@ -1,877 +0,0 @@
-//! This module contains functions to suggest names for expressions, functions and other items
-
-use hir::Semantics;
-use ide_db::RootDatabase;
-use itertools::Itertools;
-use stdx::to_lower_snake_case;
-use syntax::{
-    ast::{self, NameOwner},
-    match_ast, AstNode,
-};
-
-/// Trait names, that will be ignored when in `impl Trait` and `dyn Trait`
-const USELESS_TRAITS: &[&str] = &["Send", "Sync", "Copy", "Clone", "Eq", "PartialEq"];
-/// Identifier names that won't be suggested, ever
-///
-/// **NOTE**: they all must be snake lower case
-const USELESS_NAMES: &[&str] =
-    &["new", "default", "option", "some", "none", "ok", "err", "str", "string"];
-/// Generic types replaced by their first argument
-///
-/// # Examples
-/// `Option<Name>` -> `Name`
-/// `Result<User, Error>` -> `User`
-const WRAPPER_TYPES: &[&str] = &["Box", "Option", "Result"];
-/// Prefixes to strip from methods names
-///
-/// # Examples
-/// `vec.as_slice()` -> `slice`
-/// `args.into_config()` -> `config`
-/// `bytes.to_vec()` -> `vec`
-const USELESS_METHOD_PREFIXES: &[&str] = &["into_", "as_", "to_"];
-/// Useless methods that are stripped from expression
-///
-/// # Examples
-/// `var.name().to_string()` -> `var.name()`
-const USELESS_METHODS: &[&str] = &[
-    "to_string",
-    "as_str",
-    "to_owned",
-    "as_ref",
-    "clone",
-    "cloned",
-    "expect",
-    "expect_none",
-    "unwrap",
-    "unwrap_none",
-    "unwrap_or",
-    "unwrap_or_default",
-    "unwrap_or_else",
-    "unwrap_unchecked",
-    "iter",
-    "into_iter",
-    "iter_mut",
-];
-
-/// Suggest name of variable for given expression
-///
-/// **NOTE**: it is caller's responsibility to guarantee uniqueness of the name.
-/// I.e. it doesn't look for names in scope.
-///
-/// # Current implementation
-///
-/// In current implementation, the function tries to get the name from
-/// the following sources:
-///
-/// * if expr is an argument to function/method, use paramter name
-/// * if expr is a function/method call, use function name
-/// * expression type name if it exists (E.g. `()`, `fn() -> ()` or `!` do not have names)
-/// * fallback: `var_name`
-///
-/// It also applies heuristics to filter out less informative names
-///
-/// Currently it sticks to the first name found.
-pub(crate) fn variable(expr: &ast::Expr, sema: &Semantics<'_, RootDatabase>) -> String {
-    // `from_param` does not benifit from stripping
-    // it need the largest context possible
-    // so we check firstmost
-    if let Some(name) = from_param(expr, sema) {
-        return name;
-    }
-
-    let mut next_expr = Some(expr.clone());
-    while let Some(expr) = next_expr {
-        let name = from_call(&expr).or_else(|| from_type(&expr, sema));
-        if let Some(name) = name {
-            return name;
-        }
-
-        match expr {
-            ast::Expr::RefExpr(inner) => next_expr = inner.expr(),
-            ast::Expr::BoxExpr(inner) => next_expr = inner.expr(),
-            ast::Expr::AwaitExpr(inner) => next_expr = inner.expr(),
-            // ast::Expr::BlockExpr(block) => expr = block.tail_expr(),
-            ast::Expr::CastExpr(inner) => next_expr = inner.expr(),
-            ast::Expr::MethodCallExpr(method) if is_useless_method(&method) => {
-                next_expr = method.receiver();
-            }
-            ast::Expr::ParenExpr(inner) => next_expr = inner.expr(),
-            ast::Expr::TryExpr(inner) => next_expr = inner.expr(),
-            ast::Expr::PrefixExpr(prefix) if prefix.op_kind() == Some(ast::PrefixOp::Deref) => {
-                next_expr = prefix.expr()
-            }
-            _ => break,
-        }
-    }
-
-    "var_name".to_string()
-}
-
-fn normalize(name: &str) -> Option<String> {
-    let name = to_lower_snake_case(name);
-
-    if USELESS_NAMES.contains(&name.as_str()) {
-        return None;
-    }
-
-    if !is_valid_name(&name) {
-        return None;
-    }
-
-    Some(name)
-}
-
-fn is_valid_name(name: &str) -> bool {
-    match syntax::lex_single_syntax_kind(name) {
-        Some((syntax::SyntaxKind::IDENT, _error)) => true,
-        _ => false,
-    }
-}
-
-fn is_useless_method(method: &ast::MethodCallExpr) -> bool {
-    let ident = method.name_ref().and_then(|it| it.ident_token());
-
-    if let Some(ident) = ident {
-        USELESS_METHODS.contains(&ident.text())
-    } else {
-        false
-    }
-}
-
-fn from_call(expr: &ast::Expr) -> Option<String> {
-    from_func_call(expr).or_else(|| from_method_call(expr))
-}
-
-fn from_func_call(expr: &ast::Expr) -> Option<String> {
-    let call = match expr {
-        ast::Expr::CallExpr(call) => call,
-        _ => return None,
-    };
-    let func = match call.expr()? {
-        ast::Expr::PathExpr(path) => path,
-        _ => return None,
-    };
-    let ident = func.path()?.segment()?.name_ref()?.ident_token()?;
-    normalize(ident.text())
-}
-
-fn from_method_call(expr: &ast::Expr) -> Option<String> {
-    let method = match expr {
-        ast::Expr::MethodCallExpr(call) => call,
-        _ => return None,
-    };
-    let ident = method.name_ref()?.ident_token()?;
-    let mut name = ident.text();
-
-    if USELESS_METHODS.contains(&name) {
-        return None;
-    }
-
-    for prefix in USELESS_METHOD_PREFIXES {
-        if let Some(suffix) = name.strip_prefix(prefix) {
-            name = suffix;
-            break;
-        }
-    }
-
-    normalize(&name)
-}
-
-fn from_param(expr: &ast::Expr, sema: &Semantics<'_, RootDatabase>) -> Option<String> {
-    let arg_list = expr.syntax().parent().and_then(ast::ArgList::cast)?;
-    let args_parent = arg_list.syntax().parent()?;
-    let func = match_ast! {
-        match args_parent {
-            ast::CallExpr(call) => {
-                let func = call.expr()?;
-                let func_ty = sema.type_of_expr(&func)?;
-                func_ty.as_callable(sema.db)?
-            },
-            ast::MethodCallExpr(method) => sema.resolve_method_call_as_callable(&method)?,
-            _ => return None,
-        }
-    };
-
-    let (idx, _) = arg_list.args().find_position(|it| it == expr).unwrap();
-    let (pat, _) = func.params(sema.db).into_iter().nth(idx)?;
-    let pat = match pat? {
-        either::Either::Right(pat) => pat,
-        _ => return None,
-    };
-    let name = var_name_from_pat(&pat)?;
-    normalize(&name.to_string())
-}
-
-fn var_name_from_pat(pat: &ast::Pat) -> Option<ast::Name> {
-    match pat {
-        ast::Pat::IdentPat(var) => var.name(),
-        ast::Pat::RefPat(ref_pat) => var_name_from_pat(&ref_pat.pat()?),
-        ast::Pat::BoxPat(box_pat) => var_name_from_pat(&box_pat.pat()?),
-        _ => None,
-    }
-}
-
-fn from_type(expr: &ast::Expr, sema: &Semantics<'_, RootDatabase>) -> Option<String> {
-    let ty = sema.type_of_expr(expr)?;
-    let ty = ty.remove_ref().unwrap_or(ty);
-
-    name_of_type(&ty, sema.db)
-}
-
-fn name_of_type(ty: &hir::Type, db: &RootDatabase) -> Option<String> {
-    let name = if let Some(adt) = ty.as_adt() {
-        let name = adt.name(db).to_string();
-
-        if WRAPPER_TYPES.contains(&name.as_str()) {
-            let inner_ty = ty.type_parameters().next()?;
-            return name_of_type(&inner_ty, db);
-        }
-
-        name
-    } else if let Some(trait_) = ty.as_dyn_trait() {
-        trait_name(&trait_, db)?
-    } else if let Some(traits) = ty.as_impl_traits(db) {
-        let mut iter = traits.into_iter().filter_map(|t| trait_name(&t, db));
-        let name = iter.next()?;
-        if iter.next().is_some() {
-            return None;
-        }
-        name
-    } else {
-        return None;
-    };
-    normalize(&name)
-}
-
-fn trait_name(trait_: &hir::Trait, db: &RootDatabase) -> Option<String> {
-    let name = trait_.name(db).to_string();
-    if USELESS_TRAITS.contains(&name.as_str()) {
-        return None;
-    }
-    Some(name)
-}
-
-#[cfg(test)]
-mod tests {
-    use super::*;
-
-    use crate::tests::check_name_suggestion;
-
-    mod from_func_call {
-        use super::*;
-
-        #[test]
-        fn no_args() {
-            check_name_suggestion(
-                |e, _| from_func_call(e),
-                r#"
-                fn foo() {
-                    $0bar()$0
-                }"#,
-                "bar",
-            );
-        }
-
-        #[test]
-        fn single_arg() {
-            check_name_suggestion(
-                |e, _| from_func_call(e),
-                r#"
-                fn foo() {
-                    $0bar(1)$0
-                }"#,
-                "bar",
-            );
-        }
-
-        #[test]
-        fn many_args() {
-            check_name_suggestion(
-                |e, _| from_func_call(e),
-                r#"
-                fn foo() {
-                    $0bar(1, 2, 3)$0
-                }"#,
-                "bar",
-            );
-        }
-
-        #[test]
-        fn path() {
-            check_name_suggestion(
-                |e, _| from_func_call(e),
-                r#"
-                fn foo() {
-                    $0i32::bar(1, 2, 3)$0
-                }"#,
-                "bar",
-            );
-        }
-
-        #[test]
-        fn generic_params() {
-            check_name_suggestion(
-                |e, _| from_func_call(e),
-                r#"
-                fn foo() {
-                    $0bar::<i32>(1, 2, 3)$0
-                }"#,
-                "bar",
-            );
-        }
-    }
-
-    mod from_method_call {
-        use super::*;
-
-        #[test]
-        fn no_args() {
-            check_name_suggestion(
-                |e, _| from_method_call(e),
-                r#"
-                fn foo() {
-                    $0bar.frobnicate()$0
-                }"#,
-                "frobnicate",
-            );
-        }
-
-        #[test]
-        fn generic_params() {
-            check_name_suggestion(
-                |e, _| from_method_call(e),
-                r#"
-                fn foo() {
-                    $0bar.frobnicate::<i32, u32>()$0
-                }"#,
-                "frobnicate",
-            );
-        }
-
-        #[test]
-        fn to_name() {
-            check_name_suggestion(
-                |e, _| from_method_call(e),
-                r#"
-                struct Args;
-                struct Config;
-                impl Args {
-                    fn to_config(&self) -> Config {}
-                }
-                fn foo() {
-                    $0Args.to_config()$0;
-                }"#,
-                "config",
-            );
-        }
-    }
-
-    mod from_param {
-        use crate::tests::check_name_suggestion_not_applicable;
-
-        use super::*;
-
-        #[test]
-        fn plain_func() {
-            check_name_suggestion(
-                from_param,
-                r#"
-                fn bar(n: i32, m: u32);
-                fn foo() {
-                    bar($01$0, 2)
-                }"#,
-                "n",
-            );
-        }
-
-        #[test]
-        fn mut_param() {
-            check_name_suggestion(
-                from_param,
-                r#"
-                fn bar(mut n: i32, m: u32);
-                fn foo() {
-                    bar($01$0, 2)
-                }"#,
-                "n",
-            );
-        }
-
-        #[test]
-        fn func_does_not_exist() {
-            check_name_suggestion_not_applicable(
-                from_param,
-                r#"
-                fn foo() {
-                    bar($01$0, 2)
-                }"#,
-            );
-        }
-
-        #[test]
-        fn unnamed_param() {
-            check_name_suggestion_not_applicable(
-                from_param,
-                r#"
-                fn bar(_: i32, m: u32);
-                fn foo() {
-                    bar($01$0, 2)
-                }"#,
-            );
-        }
-
-        #[test]
-        fn tuple_pat() {
-            check_name_suggestion_not_applicable(
-                from_param,
-                r#"
-                fn bar((n, k): (i32, i32), m: u32);
-                fn foo() {
-                    bar($0(1, 2)$0, 3)
-                }"#,
-            );
-        }
-
-        #[test]
-        fn ref_pat() {
-            check_name_suggestion(
-                from_param,
-                r#"
-                fn bar(&n: &i32, m: u32);
-                fn foo() {
-                    bar($0&1$0, 3)
-                }"#,
-                "n",
-            );
-        }
-
-        #[test]
-        fn box_pat() {
-            check_name_suggestion(
-                from_param,
-                r#"
-                fn bar(box n: &i32, m: u32);
-                fn foo() {
-                    bar($01$0, 3)
-                }"#,
-                "n",
-            );
-        }
-
-        #[test]
-        fn param_out_of_index() {
-            check_name_suggestion_not_applicable(
-                from_param,
-                r#"
-                fn bar(n: i32, m: u32);
-                fn foo() {
-                    bar(1, 2, $03$0)
-                }"#,
-            );
-        }
-
-        #[test]
-        fn generic_param_resolved() {
-            check_name_suggestion(
-                from_param,
-                r#"
-                fn bar<T>(n: T, m: u32);
-                fn foo() {
-                    bar($01$0, 2)
-                }"#,
-                "n",
-            );
-        }
-
-        #[test]
-        fn generic_param_unresolved() {
-            check_name_suggestion(
-                from_param,
-                r#"
-                fn bar<T>(n: T, m: u32);
-                fn foo<T>(x: T) {
-                    bar($0x$0, 2)
-                }"#,
-                "n",
-            );
-        }
-
-        #[test]
-        fn method() {
-            check_name_suggestion(
-                from_param,
-                r#"
-                struct S;
-                impl S {
-                    fn bar(&self, n: i32, m: u32);
-                }
-                fn foo() {
-                    S.bar($01$0, 2)
-                }"#,
-                "n",
-            );
-        }
-
-        #[test]
-        fn method_ufcs() {
-            check_name_suggestion(
-                from_param,
-                r#"
-                struct S;
-                impl S {
-                    fn bar(&self, n: i32, m: u32);
-                }
-                fn foo() {
-                    S::bar(&S, $01$0, 2)
-                }"#,
-                "n",
-            );
-        }
-
-        #[test]
-        fn method_self() {
-            check_name_suggestion_not_applicable(
-                from_param,
-                r#"
-                struct S;
-                impl S {
-                    fn bar(&self, n: i32, m: u32);
-                }
-                fn foo() {
-                    S::bar($0&S$0, 1, 2)
-                }"#,
-            );
-        }
-
-        #[test]
-        fn method_self_named() {
-            check_name_suggestion(
-                from_param,
-                r#"
-                struct S;
-                impl S {
-                    fn bar(strukt: &Self, n: i32, m: u32);
-                }
-                fn foo() {
-                    S::bar($0&S$0, 1, 2)
-                }"#,
-                "strukt",
-            );
-        }
-    }
-
-    mod from_type {
-        use crate::tests::check_name_suggestion_not_applicable;
-
-        use super::*;
-
-        #[test]
-        fn i32() {
-            check_name_suggestion_not_applicable(
-                from_type,
-                r#"
-                fn foo() {
-                    let _: i32 = $01$0;
-                }"#,
-            );
-        }
-
-        #[test]
-        fn u64() {
-            check_name_suggestion_not_applicable(
-                from_type,
-                r#"
-                fn foo() {
-                    let _: u64 = $01$0;
-                }"#,
-            );
-        }
-
-        #[test]
-        fn bool() {
-            check_name_suggestion_not_applicable(
-                from_type,
-                r#"
-                fn foo() {
-                    let _: bool = $0true$0;
-                }"#,
-            );
-        }
-
-        #[test]
-        fn struct_unit() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                struct Seed;
-                fn foo() {
-                    let _ = $0Seed$0;
-                }"#,
-                "seed",
-            );
-        }
-
-        #[test]
-        fn struct_unit_to_snake() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                struct SeedState;
-                fn foo() {
-                    let _ = $0SeedState$0;
-                }"#,
-                "seed_state",
-            );
-        }
-
-        #[test]
-        fn struct_single_arg() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                struct Seed(u32);
-                fn foo() {
-                    let _ = $0Seed(0)$0;
-                }"#,
-                "seed",
-            );
-        }
-
-        #[test]
-        fn struct_with_fields() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                struct Seed { value: u32 }
-                fn foo() {
-                    let _ = $0Seed { value: 0 }$0;
-                }"#,
-                "seed",
-            );
-        }
-
-        #[test]
-        fn enum_() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                enum Kind { A, B }
-                fn foo() {
-                    let _ = $0Kind::A$0;
-                }"#,
-                "kind",
-            );
-        }
-
-        #[test]
-        fn enum_generic_resolved() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                enum Kind<T> { A(T), B }
-                fn foo() {
-                    let _ = $0Kind::A(1)$0;
-                }"#,
-                "kind",
-            );
-        }
-
-        #[test]
-        fn enum_generic_unresolved() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                enum Kind<T> { A(T), B }
-                fn foo<T>(x: T) {
-                    let _ = $0Kind::A(x)$0;
-                }"#,
-                "kind",
-            );
-        }
-
-        #[test]
-        fn dyn_trait() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                trait DynHandler {}
-                fn bar() -> dyn DynHandler {}
-                fn foo() {
-                    $0bar()$0;
-                }"#,
-                "dyn_handler",
-            );
-        }
-
-        #[test]
-        fn impl_trait() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                trait StaticHandler {}
-                fn bar() -> impl StaticHandler {}
-                fn foo() {
-                    $0bar()$0;
-                }"#,
-                "static_handler",
-            );
-        }
-
-        #[test]
-        fn impl_trait_plus_clone() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                trait StaticHandler {}
-                trait Clone {}
-                fn bar() -> impl StaticHandler + Clone {}
-                fn foo() {
-                    $0bar()$0;
-                }"#,
-                "static_handler",
-            );
-        }
-
-        #[test]
-        fn impl_trait_plus_lifetime() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                trait StaticHandler {}
-                trait Clone {}
-                fn bar<'a>(&'a i32) -> impl StaticHandler + 'a {}
-                fn foo() {
-                    $0bar(&1)$0;
-                }"#,
-                "static_handler",
-            );
-        }
-
-        #[test]
-        fn impl_trait_plus_trait() {
-            check_name_suggestion_not_applicable(
-                from_type,
-                r#"
-                trait Handler {}
-                trait StaticHandler {}
-                fn bar() -> impl StaticHandler + Handler {}
-                fn foo() {
-                    $0bar()$0;
-                }"#,
-            );
-        }
-
-        #[test]
-        fn ref_value() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                struct Seed;
-                fn bar() -> &Seed {}
-                fn foo() {
-                    $0bar()$0;
-                }"#,
-                "seed",
-            );
-        }
-
-        #[test]
-        fn box_value() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                struct Box<T>(*const T);
-                struct Seed;
-                fn bar() -> Box<Seed> {}
-                fn foo() {
-                    $0bar()$0;
-                }"#,
-                "seed",
-            );
-        }
-
-        #[test]
-        fn box_generic() {
-            check_name_suggestion_not_applicable(
-                from_type,
-                r#"
-                struct Box<T>(*const T);
-                fn bar<T>() -> Box<T> {}
-                fn foo<T>() {
-                    $0bar::<T>()$0;
-                }"#,
-            );
-        }
-
-        #[test]
-        fn option_value() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                enum Option<T> { Some(T) }
-                struct Seed;
-                fn bar() -> Option<Seed> {}
-                fn foo() {
-                    $0bar()$0;
-                }"#,
-                "seed",
-            );
-        }
-
-        #[test]
-        fn result_value() {
-            check_name_suggestion(
-                from_type,
-                r#"
-                enum Result<T, E> { Ok(T), Err(E) }
-                struct Seed;
-                struct Error;
-                fn bar() -> Result<Seed, Error> {}
-                fn foo() {
-                    $0bar()$0;
-                }"#,
-                "seed",
-            );
-        }
-    }
-
-    mod variable {
-        use super::*;
-
-        #[test]
-        fn ref_call() {
-            check_name_suggestion(
-                |e, c| Some(variable(e, c)),
-                r#"
-                fn foo() {
-                    $0&bar(1, 3)$0
-                }"#,
-                "bar",
-            );
-        }
-
-        #[test]
-        fn name_to_string() {
-            check_name_suggestion(
-                |e, c| Some(variable(e, c)),
-                r#"
-                fn foo() {
-                    $0function.name().to_string()$0
-                }"#,
-                "name",
-            );
-        }
-
-        #[test]
-        fn nested_useless_method() {
-            check_name_suggestion(
-                |e, c| Some(variable(e, c)),
-                r#"
-                fn foo() {
-                    $0function.name().as_ref().unwrap().to_string()$0
-                }"#,
-                "name",
-            );
-        }
-    }
-}
diff --git a/crates/ide_db/src/helpers.rs b/crates/ide_db/src/helpers.rs
index f9de8ce0e6..3ff77400bb 100644
--- a/crates/ide_db/src/helpers.rs
+++ b/crates/ide_db/src/helpers.rs
@@ -41,6 +41,10 @@ pub struct FamousDefs<'a, 'b>(pub &'a Semantics<'b, RootDatabase>, pub Option<Cr
 impl FamousDefs<'_, '_> {
     pub const FIXTURE: &'static str = include_str!("helpers/famous_defs_fixture.rs");
 
+    pub fn std(&self) -> Option<Crate> {
+        self.find_crate("std")
+    }
+
     pub fn core(&self) -> Option<Crate> {
         self.find_crate("core")
     }
diff --git a/crates/ide_db/src/helpers/famous_defs_fixture.rs b/crates/ide_db/src/helpers/famous_defs_fixture.rs
index bb4e9666b1..d3464ae17b 100644
--- a/crates/ide_db/src/helpers/famous_defs_fixture.rs
+++ b/crates/ide_db/src/helpers/famous_defs_fixture.rs
@@ -129,3 +129,11 @@ pub mod prelude {
 }
 #[prelude_import]
 pub use prelude::*;
+//- /libstd.rs crate:std deps:core
+//! Signatures of traits, types and functions from the std lib for use in tests.
+
+/// Docs for return_keyword
+mod return_keyword {}
+
+/// Docs for prim_str
+mod prim_str {}
diff --git a/crates/mbe/Cargo.toml b/crates/mbe/Cargo.toml
index bb2656a80a..8b5cca22c7 100644
--- a/crates/mbe/Cargo.toml
+++ b/crates/mbe/Cargo.toml
@@ -18,7 +18,6 @@ syntax = { path = "../syntax", version = "0.0.0" }
 parser = { path = "../parser", version = "0.0.0" }
 tt = { path = "../tt", version = "0.0.0" }
 test_utils = { path = "../test_utils", version = "0.0.0" }
-stdx = { path = "../stdx", version = "0.0.0" }
 
 [dev-dependencies]
 profile = { path = "../profile" }
diff --git a/crates/mbe/src/benchmark.rs b/crates/mbe/src/benchmark.rs
index 503ad13556..6d81be8804 100644
--- a/crates/mbe/src/benchmark.rs
+++ b/crates/mbe/src/benchmark.rs
@@ -40,12 +40,18 @@ fn benchmark_expand_macro_rules() {
             .into_iter()
             .map(|(id, tt)| {
                 let res = rules[&id].expand(&tt);
-                assert!(res.err.is_none());
+                if res.err.is_some() {
+                    // FIXME:
+                    // Currently `invocation_fixtures` will generate some correct invocations but
+                    // cannot be expanded by mbe. We ignore errors here.
+                    // See: https://github.com/rust-analyzer/rust-analyzer/issues/4777
+                    eprintln!("err from {} {:?}", id, res.err);
+                }
                 res.value.token_trees.len()
             })
             .sum()
     };
-    assert_eq!(hash, 69413);
+    assert_eq!(hash, 66995);
 }
 
 fn macro_rules_fixtures() -> FxHashMap<String, MacroRules> {
@@ -71,7 +77,7 @@ fn macro_rules_fixtures_tt() -> FxHashMap<String, tt::Subtree> {
         .collect()
 }
 
-/// Generate random invocation fixtures from rules
+// Generate random invocation fixtures from rules
 fn invocation_fixtures(rules: &FxHashMap<String, MacroRules>) -> Vec<(String, tt::Subtree)> {
     let mut seed = 123456789;
     let mut res = Vec::new();
@@ -80,31 +86,11 @@ fn invocation_fixtures(rules: &FxHashMap<String, MacroRules>) -> Vec<(String, tt
         for rule in &it.rules {
             // Generate twice
             for _ in 0..2 {
-                // The input are generated by filling the `Op` randomly.
-                // However, there are some cases generated are ambiguous for expanding, for example:
-                // ```rust
-                // macro_rules! m {
-                //    ($($t:ident),* as $ty:ident) => {}
-                // }
-                // m!(as u32);  // error: local ambiguity: multiple parsing options: built-in NTs ident ('t') or 1 other option.
-                // ```
-                //
-                // So we just skip any error cases and try again
-                let mut try_cnt = 0;
-                loop {
-                    let mut subtree = tt::Subtree::default();
-                    for op in rule.lhs.iter() {
-                        collect_from_op(op, &mut subtree, &mut seed);
-                    }
-                    if it.expand(&subtree).err.is_none() {
-                        res.push((name.clone(), subtree));
-                        break;
-                    }
-                    try_cnt += 1;
-                    if try_cnt > 100 {
-                        panic!("invocaton fixture {} cannot be generated.\n", name);
-                    }
+                let mut subtree = tt::Subtree::default();
+                for op in rule.lhs.iter() {
+                    collect_from_op(op, &mut subtree, &mut seed);
                 }
+                res.push((name.clone(), subtree));
             }
         }
     }
diff --git a/crates/mbe/src/expander.rs b/crates/mbe/src/expander.rs
index 2efff8f52e..e7e14b3cce 100644
--- a/crates/mbe/src/expander.rs
+++ b/crates/mbe/src/expander.rs
@@ -5,7 +5,7 @@
 mod matcher;
 mod transcriber;
 
-use smallvec::SmallVec;
+use rustc_hash::FxHashMap;
 use syntax::SmolStr;
 
 use crate::{ExpandError, ExpandResult};
@@ -28,10 +28,10 @@ pub(crate) fn expand_rules(
                 return ExpandResult::ok(value);
             }
         }
-        // Use the rule if we matched more tokens, or bound variables count
+        // Use the rule if we matched more tokens, or had fewer errors
         if let Some((prev_match, _)) = &match_ {
-            if (new_match.unmatched_tts, -(new_match.bound_count as i32))
-                < (prev_match.unmatched_tts, -(prev_match.bound_count as i32))
+            if (new_match.unmatched_tts, new_match.err_count)
+                < (prev_match.unmatched_tts, prev_match.err_count)
             {
                 match_ = Some((new_match, rule));
             }
@@ -94,19 +94,19 @@ pub(crate) fn expand_rules(
 /// In other words, `Bindings` is a *multi* mapping from `SmolStr` to
 /// `tt::TokenTree`, where the index to select a particular `TokenTree` among
 /// many is not a plain `usize`, but an `&[usize]`.
-#[derive(Debug, Default, Clone, PartialEq, Eq)]
+#[derive(Debug, Default)]
 struct Bindings {
-    inner: SmallVec<[(SmolStr, Binding); 4]>,
+    inner: FxHashMap<SmolStr, Binding>,
 }
 
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Debug)]
 enum Binding {
     Fragment(Fragment),
     Nested(Vec<Binding>),
     Empty,
 }
 
-#[derive(Debug, Clone, PartialEq, Eq)]
+#[derive(Debug, Clone)]
 enum Fragment {
     /// token fragments are just copy-pasted into the output
     Tokens(tt::TokenTree),
diff --git a/crates/mbe/src/expander/matcher.rs b/crates/mbe/src/expander/matcher.rs
index 9d3d280552..e3bd4c09a0 100644
--- a/crates/mbe/src/expander/matcher.rs
+++ b/crates/mbe/src/expander/matcher.rs
@@ -1,74 +1,14 @@
-//! An NFA-based parser, which is porting from rustc mbe parsing code
-//!
-//! See https://github.com/rust-lang/rust/blob/70b18bc2cbac4712020019f5bf57c00905373205/compiler/rustc_expand/src/mbe/macro_parser.rs
-//! Here is a quick intro to how the parser works, copied from rustc:
-//!
-//! A 'position' is a dot in the middle of a matcher, usually represented as a
-//! dot. For example `· a $( a )* a b` is a position, as is `a $( · a )* a b`.
-//!
-//! The parser walks through the input a character at a time, maintaining a list
-//! of threads consistent with the current position in the input string: `cur_items`.
-//!
-//! As it processes them, it fills up `eof_items` with threads that would be valid if
-//! the macro invocation is now over, `bb_items` with threads that are waiting on
-//! a Rust non-terminal like `$e:expr`, and `next_items` with threads that are waiting
-//! on a particular token. Most of the logic concerns moving the · through the
-//! repetitions indicated by Kleene stars. The rules for moving the · without
-//! consuming any input are called epsilon transitions. It only advances or calls
-//! out to the real Rust parser when no `cur_items` threads remain.
-//!
-//! Example:
-//!
-//! ```text, ignore
-//! Start parsing a a a a b against [· a $( a )* a b].
-//!
-//! Remaining input: a a a a b
-//! next: [· a $( a )* a b]
-//!
-//! - - - Advance over an a. - - -
-//!
-//! Remaining input: a a a b
-//! cur: [a · $( a )* a b]
-//! Descend/Skip (first item).
-//! next: [a $( · a )* a b]  [a $( a )* · a b].
-//!
-//! - - - Advance over an a. - - -
-//!
-//! Remaining input: a a b
-//! cur: [a $( a · )* a b]  [a $( a )* a · b]
-//! Follow epsilon transition: Finish/Repeat (first item)
-//! next: [a $( a )* · a b]  [a $( · a )* a b]  [a $( a )* a · b]
-//!
-//! - - - Advance over an a. - - - (this looks exactly like the last step)
-//!
-//! Remaining input: a b
-//! cur: [a $( a · )* a b]  [a $( a )* a · b]
-//! Follow epsilon transition: Finish/Repeat (first item)
-//! next: [a $( a )* · a b]  [a $( · a )* a b]  [a $( a )* a · b]
-//!
-//! - - - Advance over an a. - - - (this looks exactly like the last step)
-//!
-//! Remaining input: b
-//! cur: [a $( a · )* a b]  [a $( a )* a · b]
-//! Follow epsilon transition: Finish/Repeat (first item)
-//! next: [a $( a )* · a b]  [a $( · a )* a b]  [a $( a )* a · b]
-//!
-//! - - - Advance over a b. - - -
-//!
-//! Remaining input: ''
-//! eof: [a $( a )* a b ·]
-//! ```
+//! FIXME: write short doc here
 
 use crate::{
     expander::{Binding, Bindings, Fragment},
-    parser::{Op, OpDelimited, OpDelimitedIter, RepeatKind, Separator},
+    parser::{Op, RepeatKind, Separator},
     tt_iter::TtIter,
     ExpandError, MetaTemplate,
 };
 
 use super::ExpandResult;
 use parser::FragmentKind::*;
-use smallvec::{smallvec, SmallVec};
 use syntax::SmolStr;
 
 impl Bindings {
@@ -76,19 +16,19 @@ impl Bindings {
         // FIXME: Do we have a better way to represent an empty token ?
         // Insert an empty subtree for empty token
         let tt = tt::Subtree::default().into();
-        self.inner.push((name.clone(), Binding::Fragment(Fragment::Tokens(tt))));
+        self.inner.insert(name.clone(), Binding::Fragment(Fragment::Tokens(tt)));
     }
 
     fn push_empty(&mut self, name: &SmolStr) {
-        self.inner.push((name.clone(), Binding::Empty));
+        self.inner.insert(name.clone(), Binding::Empty);
     }
 
     fn push_nested(&mut self, idx: usize, nested: Bindings) -> Result<(), ExpandError> {
         for (key, value) in nested.inner {
-            if self.get_mut(&key).is_none() {
-                self.inner.push((key.clone(), Binding::Nested(Vec::new())));
+            if !self.inner.contains_key(&key) {
+                self.inner.insert(key.clone(), Binding::Nested(Vec::new()));
             }
-            match self.get_mut(&key) {
+            match self.inner.get_mut(&key) {
                 Some(Binding::Nested(it)) => {
                     // insert empty nested bindings before this one
                     while it.len() < idx {
@@ -106,14 +46,6 @@ impl Bindings {
         }
         Ok(())
     }
-
-    fn get_mut(&mut self, name: &str) -> Option<&mut Binding> {
-        self.inner.iter_mut().find_map(|(n, b)| if n == name { Some(b) } else { None })
-    }
-
-    fn bindings(&self) -> impl Iterator<Item = &Binding> {
-        self.inner.iter().map(|(_, b)| b)
-    }
 }
 
 macro_rules! err {
@@ -125,7 +57,7 @@ macro_rules! err {
     };
 }
 
-#[derive(Clone, Debug, Default, PartialEq, Eq)]
+#[derive(Debug, Default)]
 pub(super) struct Match {
     pub(super) bindings: Bindings,
     /// We currently just keep the first error and count the rest to compare matches.
@@ -133,8 +65,6 @@ pub(super) struct Match {
     pub(super) err_count: usize,
     /// How many top-level token trees were left to match.
     pub(super) unmatched_tts: usize,
-    /// The number of bound variables
-    pub(super) bound_count: usize,
 }
 
 impl Match {
@@ -146,373 +76,72 @@ impl Match {
 }
 
 /// Matching errors are added to the `Match`.
-pub(super) fn match_(pattern: &MetaTemplate, input: &tt::Subtree) -> Match {
-    let mut res = match_loop(pattern, &input);
-    res.bound_count = count(res.bindings.bindings());
-    return res;
-
-    fn count<'a>(bindings: impl Iterator<Item = &'a Binding>) -> usize {
-        bindings
-            .map(|it| match it {
-                Binding::Fragment(_) => 1,
-                Binding::Empty => 1,
-                Binding::Nested(it) => count(it.iter()),
-            })
-            .sum()
-    }
-}
-
-#[derive(Debug, Clone)]
-struct MatchState<'t> {
-    /// The position of the "dot" in this matcher
-    dot: OpDelimitedIter<'t>,
-
-    /// Token subtree stack
-    /// When matching against matchers with nested delimited submatchers (e.g., `pat ( pat ( .. )
-    /// pat ) pat`), we need to keep track of the matchers we are descending into. This stack does
-    /// that where the bottom of the stack is the outermost matcher.
-    stack: SmallVec<[OpDelimitedIter<'t>; 4]>,
-
-    /// The "parent" matcher position if we are in a repetition. That is, the matcher position just
-    /// before we enter the repetition.
-    up: Option<Box<MatchState<'t>>>,
-
-    /// The separator if we are in a repetition.
-    sep: Option<Separator>,
-
-    /// The KleeneOp of this sequence if we are in a repetition.
-    sep_kind: Option<RepeatKind>,
-
-    /// Number of tokens of seperator parsed
-    sep_parsed: Option<usize>,
-
-    /// Matched meta variables bindings
-    bindings: SmallVec<[Bindings; 4]>,
-
-    /// Cached result of meta variable parsing
-    meta_result: Option<(TtIter<'t>, ExpandResult<Option<Fragment>>)>,
+pub(super) fn match_(pattern: &MetaTemplate, src: &tt::Subtree) -> Match {
+    let mut res = Match::default();
+    let mut src = TtIter::new(src);
 
-    /// Is error occuried in this state, will `poised` to "parent"
-    is_error: bool,
-}
+    match_tokens(&mut res, pattern, &mut src);
 
-/// Process the matcher positions of `cur_items` until it is empty. In the process, this will
-/// produce more items in `next_items`, `eof_items`, and `bb_items`.
-///
-/// For more info about the how this happens, see the module-level doc comments and the inline
-/// comments of this function.
-///
-/// # Parameters
-///
-/// - `src`: the current token of the parser.
-/// - `stack`: the "parent" frames of the token tree
-/// - `res`: the match result to store errors
-/// - `cur_items`: the set of current items to be processed. This should be empty by the end of a
-///   successful execution of this function.
-/// - `next_items`: the set of newly generated items. These are used to replenish `cur_items` in
-///   the function `parse`.
-/// - `eof_items`: the set of items that would be valid if this was the EOF.
-/// - `bb_items`: the set of items that are waiting for the black-box parser.
-/// - `error_items`: the set of items in errors, used for error-resilient parsing
-fn match_loop_inner<'t>(
-    src: TtIter<'t>,
-    stack: &[TtIter<'t>],
-    res: &mut Match,
-    cur_items: &mut SmallVec<[MatchState<'t>; 1]>,
-    bb_items: &mut SmallVec<[MatchState<'t>; 1]>,
-    next_items: &mut Vec<MatchState<'t>>,
-    eof_items: &mut SmallVec<[MatchState<'t>; 1]>,
-    error_items: &mut SmallVec<[MatchState<'t>; 1]>,
-) {
-    macro_rules! try_push {
-        ($items: expr, $it:expr) => {
-            if $it.is_error {
-                error_items.push($it);
-            } else {
-                $items.push($it);
-            }
-        };
+    if src.len() > 0 {
+        res.unmatched_tts += src.len();
+        res.add_err(err!("leftover tokens"));
     }
 
-    while let Some(mut item) = cur_items.pop() {
-        while item.dot.is_eof() {
-            match item.stack.pop() {
-                Some(frame) => {
-                    item.dot = frame;
-                    item.dot.next();
-                }
-                None => break,
-            }
-        }
-        let op = match item.dot.peek() {
-            None => {
-                // We are at or past the end of the matcher of `item`.
-                if item.up.is_some() {
-                    if item.sep_parsed.is_none() {
-                        // Get the `up` matcher
-                        let mut new_pos = *item.up.clone().unwrap();
-                        // Add matches from this repetition to the `matches` of `up`
-                        if let Some(bindings) = new_pos.bindings.last_mut() {
-                            for (i, b) in item.bindings.iter_mut().enumerate() {
-                                bindings.push_nested(i, b.clone()).unwrap();
-                            }
-                        }
-                        // Move the "dot" past the repetition in `up`
-                        new_pos.dot.next();
-                        new_pos.is_error = new_pos.is_error || item.is_error;
-                        cur_items.push(new_pos);
-                    }
-
-                    // Check if we need a separator.
-                    // We check the separator one by one
-                    let sep_idx = *item.sep_parsed.as_ref().unwrap_or(&0);
-                    let sep_len = item.sep.as_ref().map_or(0, Separator::tt_count);
-                    if item.sep.is_some() && sep_idx != sep_len {
-                        let sep = item.sep.as_ref().unwrap();
-                        if src.clone().expect_separator(&sep, sep_idx) {
-                            item.dot.next();
-                            item.sep_parsed = Some(sep_idx + 1);
-                            try_push!(next_items, item);
-                        }
-                    }
-                    // We don't need a separator. Move the "dot" back to the beginning of the matcher
-                    // and try to match again UNLESS we are only allowed to have _one_ repetition.
-                    else if item.sep_kind != Some(RepeatKind::ZeroOrOne) {
-                        item.dot = item.dot.reset();
-                        item.sep_parsed = None;
-                        item.bindings.push(Bindings::default());
-                        cur_items.push(item);
-                    }
-                } else {
-                    // If we are not in a repetition, then being at the end of a matcher means that we have
-                    // reached the potential end of the input.
-                    try_push!(eof_items, item);
-                }
-                continue;
-            }
-            Some(it) => it,
-        };
+    res
+}
 
-        // We are in the middle of a matcher.
+fn match_tokens(res: &mut Match, pattern: &MetaTemplate, src: &mut TtIter) {
+    for op in pattern.iter() {
         match op {
-            OpDelimited::Op(Op::Repeat { tokens, kind, separator }) => {
-                if matches!(kind, RepeatKind::ZeroOrMore | RepeatKind::ZeroOrOne) {
-                    let mut new_item = item.clone();
-                    new_item.dot.next();
-                    let mut vars = Vec::new();
-                    let bindings = new_item.bindings.last_mut().unwrap();
-                    collect_vars(&mut vars, tokens);
-                    for var in vars {
-                        bindings.push_empty(&var);
-                    }
-                    cur_items.push(new_item);
-                }
-                cur_items.push(MatchState {
-                    dot: tokens.iter_delimited(None),
-                    stack: Default::default(),
-                    up: Some(Box::new(item)),
-                    sep: separator.clone(),
-                    sep_kind: Some(*kind),
-                    sep_parsed: None,
-                    bindings: smallvec![Bindings::default()],
-                    meta_result: None,
-                    is_error: false,
-                })
-            }
-            OpDelimited::Op(Op::Subtree { tokens, delimiter }) => {
-                if let Ok(subtree) = src.clone().expect_subtree() {
-                    if subtree.delimiter_kind() == delimiter.map(|it| it.kind) {
-                        item.stack.push(item.dot);
-                        item.dot = tokens.iter_delimited(delimiter.as_ref());
-                        cur_items.push(item);
-                    }
-                }
-            }
-            OpDelimited::Op(Op::Var { kind, name, .. }) => {
-                if let Some(kind) = kind {
-                    let mut fork = src.clone();
-                    let match_res = match_meta_var(kind.as_str(), &mut fork);
-                    match match_res.err {
-                        None => {
-                            // Some meta variables are optional (e.g. vis)
-                            if match_res.value.is_some() {
-                                item.meta_result = Some((fork, match_res));
-                                try_push!(bb_items, item);
-                            } else {
-                                item.bindings.last_mut().unwrap().push_optional(name);
-                                item.dot.next();
-                                cur_items.push(item);
-                            }
-                        }
-                        Some(err) => {
-                            res.add_err(err);
-                            match match_res.value {
-                                Some(fragment) => {
-                                    item.bindings
-                                        .last_mut()
-                                        .unwrap()
-                                        .inner
-                                        .push((name.clone(), Binding::Fragment(fragment)));
-                                }
-                                _ => {}
-                            }
-                            item.is_error = true;
-                            error_items.push(item);
-                        }
-                    }
-                }
-            }
-            OpDelimited::Op(Op::Leaf(leaf)) => {
-                if let Err(err) = match_leaf(&leaf, &mut src.clone()) {
+            Op::Leaf(lhs) => {
+                if let Err(err) = match_leaf(lhs, src) {
                     res.add_err(err);
-                    item.is_error = true;
-                } else {
-                    item.dot.next();
+                    continue;
                 }
-                try_push!(next_items, item);
             }
-            OpDelimited::Open => {
-                if matches!(src.clone().next(), Some(tt::TokenTree::Subtree(..))) {
-                    item.dot.next();
-                    try_push!(next_items, item);
-                }
-            }
-            OpDelimited::Close => {
-                let is_delim_closed = src.peek_n(0).is_none() && !stack.is_empty();
-                if is_delim_closed {
-                    item.dot.next();
-                    try_push!(next_items, item);
-                }
-            }
-        }
-    }
-}
-
-fn match_loop(pattern: &MetaTemplate, src: &tt::Subtree) -> Match {
-    let mut src = TtIter::new(src);
-    let mut stack: SmallVec<[TtIter; 1]> = SmallVec::new();
-    let mut res = Match::default();
-    let mut error_reover_item = None;
-
-    let mut cur_items = smallvec![MatchState {
-        dot: pattern.iter_delimited(None),
-        stack: Default::default(),
-        up: None,
-        sep: None,
-        sep_kind: None,
-        sep_parsed: None,
-        bindings: smallvec![Bindings::default()],
-        is_error: false,
-        meta_result: None,
-    }];
-
-    let mut next_items = vec![];
-
-    loop {
-        let mut bb_items = SmallVec::new();
-        let mut eof_items = SmallVec::new();
-        let mut error_items = SmallVec::new();
-
-        stdx::always!(next_items.is_empty());
-
-        match_loop_inner(
-            src.clone(),
-            &stack,
-            &mut res,
-            &mut cur_items,
-            &mut bb_items,
-            &mut next_items,
-            &mut eof_items,
-            &mut error_items,
-        );
-        stdx::always!(cur_items.is_empty());
-
-        if error_items.len() > 0 {
-            error_reover_item = error_items.pop();
-        } else if eof_items.len() > 0 {
-            error_reover_item = Some(eof_items[0].clone());
-        }
-
-        // We need to do some post processing after the `match_loop_inner`.
-        // If we reached the EOF, check that there is EXACTLY ONE possible matcher. Otherwise,
-        // either the parse is ambiguous (which should never happen) or there is a syntax error.
-        if src.peek_n(0).is_none() && stack.is_empty() {
-            if eof_items.len() == 1 {
-                // remove all errors, because it is the correct answer !
-                res = Match::default();
-                res.bindings = eof_items[0].bindings[0].clone();
-            } else {
-                // Error recovery
-                if error_reover_item.is_some() {
-                    res.bindings = error_reover_item.unwrap().bindings[0].clone();
+            Op::Subtree { tokens, delimiter: delim } => {
+                let rhs = match src.expect_subtree() {
+                    Ok(s) => s,
+                    Err(()) => {
+                        res.add_err(err!("expected subtree"));
+                        continue;
+                    }
+                };
+                if delim.map(|it| it.kind) != rhs.delimiter_kind() {
+                    res.add_err(err!("mismatched delimiter"));
+                    continue;
                 }
-                res.add_err(ExpandError::UnexpectedToken);
-            }
-            return res;
-        }
-
-        // If there are no possible next positions AND we aren't waiting for the black-box parser,
-        // then there is a syntax error.
-        //
-        // Another possibility is that we need to call out to parse some rust nonterminal
-        // (black-box) parser. However, if there is not EXACTLY ONE of these, something is wrong.
-        if (bb_items.is_empty() && next_items.is_empty())
-            || (!bb_items.is_empty() && !next_items.is_empty())
-            || bb_items.len() > 1
-        {
-            res.unmatched_tts += src.len();
-            while let Some(it) = stack.pop() {
-                src = it;
-                res.unmatched_tts += src.len();
-            }
-            res.add_err(err!("leftover tokens"));
-
-            if let Some(mut error_reover_item) = error_reover_item {
-                res.bindings = error_reover_item.bindings.remove(0);
-            }
-            return res;
-        }
-        // Dump all possible `next_items` into `cur_items` for the next iteration.
-        else if !next_items.is_empty() {
-            // Now process the next token
-            cur_items.extend(next_items.drain(..));
-
-            match src.next() {
-                Some(tt::TokenTree::Subtree(subtree)) => {
-                    stack.push(src.clone());
-                    src = TtIter::new(subtree);
+                let mut src = TtIter::new(rhs);
+                match_tokens(res, tokens, &mut src);
+                if src.len() > 0 {
+                    res.add_err(err!("leftover tokens"));
                 }
-                None if !stack.is_empty() => src = stack.pop().unwrap(),
-                _ => (),
             }
-        }
-        // Finally, we have the case where we need to call the black-box parser to get some
-        // nonterminal.
-        else {
-            stdx::always!(bb_items.len() == 1);
-            let mut item = bb_items.pop().unwrap();
-
-            if let Some(OpDelimited::Op(Op::Var { name, .. })) = item.dot.peek() {
-                let (iter, match_res) = item.meta_result.take().unwrap();
-                let bindings = item.bindings.last_mut().unwrap();
-                match match_res.value {
+            Op::Var { name, kind, .. } => {
+                let kind = match kind {
+                    Some(k) => k,
+                    None => {
+                        res.add_err(ExpandError::UnexpectedToken);
+                        continue;
+                    }
+                };
+                let ExpandResult { value: matched, err: match_err } =
+                    match_meta_var(kind.as_str(), src);
+                match matched {
                     Some(fragment) => {
-                        bindings.inner.push((name.clone(), Binding::Fragment(fragment)));
+                        res.bindings.inner.insert(name.clone(), Binding::Fragment(fragment));
                     }
-                    None if match_res.err.is_none() => bindings.push_optional(name),
+                    None if match_err.is_none() => res.bindings.push_optional(name),
                     _ => {}
                 }
-                if let Some(err) = match_res.err {
+                if let Some(err) = match_err {
                     res.add_err(err);
                 }
-                src = iter.clone();
-                item.dot.next();
-            } else {
-                unreachable!()
             }
-            cur_items.push(item);
+            Op::Repeat { tokens: subtree, kind, separator } => {
+                match_repeat(res, subtree, *kind, separator, src);
+            }
         }
-        stdx::always!(!cur_items.is_empty());
     }
 }
 
@@ -544,6 +173,73 @@ fn match_leaf(lhs: &tt::Leaf, src: &mut TtIter) -> Result<(), ExpandError> {
     Ok(())
 }
 
+fn match_repeat(
+    res: &mut Match,
+    pattern: &MetaTemplate,
+    kind: RepeatKind,
+    separator: &Option<Separator>,
+    src: &mut TtIter,
+) {
+    // Dirty hack to make macro-expansion terminate.
+    // This should be replaced by a proper macro-by-example implementation
+    let mut limit = 65536;
+    let mut counter = 0;
+
+    for i in 0.. {
+        let mut fork = src.clone();
+
+        if let Some(separator) = &separator {
+            if i != 0 && !fork.eat_separator(separator) {
+                break;
+            }
+        }
+
+        let mut nested = Match::default();
+        match_tokens(&mut nested, pattern, &mut fork);
+        if nested.err.is_none() {
+            limit -= 1;
+            if limit == 0 {
+                log::warn!(
+                    "match_lhs exceeded repeat pattern limit => {:#?}\n{:#?}\n{:#?}\n{:#?}",
+                    pattern,
+                    src,
+                    kind,
+                    separator
+                );
+                break;
+            }
+            *src = fork;
+
+            if let Err(err) = res.bindings.push_nested(counter, nested.bindings) {
+                res.add_err(err);
+            }
+            counter += 1;
+            if counter == 1 {
+                if let RepeatKind::ZeroOrOne = kind {
+                    break;
+                }
+            }
+        } else {
+            break;
+        }
+    }
+
+    match (kind, counter) {
+        (RepeatKind::OneOrMore, 0) => {
+            res.add_err(ExpandError::UnexpectedToken);
+        }
+        (_, 0) => {
+            // Collect all empty variables in subtrees
+            let mut vars = Vec::new();
+            collect_vars(&mut vars, pattern);
+            for var in vars {
+                res.bindings.push_empty(&var)
+            }
+        }
+        _ => (),
+    }
+}
+
 fn match_meta_var(kind: &str, input: &mut TtIter) -> ExpandResult<Option<Fragment>> {
     let fragment = match kind {
         "path" => Path,
@@ -607,14 +303,14 @@ fn collect_vars(buf: &mut Vec<SmolStr>, pattern: &MetaTemplate) {
 }
 
 impl<'a> TtIter<'a> {
-    fn expect_separator(&mut self, separator: &Separator, idx: usize) -> bool {
+    fn eat_separator(&mut self, separator: &Separator) -> bool {
         let mut fork = self.clone();
         let ok = match separator {
-            Separator::Ident(lhs) if idx == 0 => match fork.expect_ident() {
+            Separator::Ident(lhs) => match fork.expect_ident() {
                 Ok(rhs) => rhs.text == lhs.text,
                 _ => false,
             },
-            Separator::Literal(lhs) if idx == 0 => match fork.expect_literal() {
+            Separator::Literal(lhs) => match fork.expect_literal() {
                 Ok(rhs) => match rhs {
                     tt::Leaf::Literal(rhs) => rhs.text == lhs.text,
                     tt::Leaf::Ident(rhs) => rhs.text == lhs.text,
@@ -622,11 +318,10 @@ impl<'a> TtIter<'a> {
                 },
                 _ => false,
             },
-            Separator::Puncts(lhss) if idx < lhss.len() => match fork.expect_punct() {
-                Ok(rhs) => rhs.char == lhss[idx].char,
+            Separator::Puncts(lhss) => lhss.iter().all(|lhs| match fork.expect_punct() {
+                Ok(rhs) => rhs.char == lhs.char,
                 _ => false,
-            },
-            _ => false,
+            }),
         };
         if ok {
             *self = fork;
diff --git a/crates/mbe/src/expander/transcriber.rs b/crates/mbe/src/expander/transcriber.rs
index ad9953a7d2..78368a33e5 100644
--- a/crates/mbe/src/expander/transcriber.rs
+++ b/crates/mbe/src/expander/transcriber.rs
@@ -13,17 +13,13 @@ use crate::{
 
 impl Bindings {
     fn contains(&self, name: &str) -> bool {
-        self.inner.iter().any(|(n, _)| n == name)
+        self.inner.contains_key(name)
     }
 
     fn get(&self, name: &str, nesting: &mut [NestingState]) -> Result<&Fragment, ExpandError> {
-        let mut b: &Binding = self
-            .inner
-            .iter()
-            .find_map(|(n, b)| if n == name { Some(b) } else { None })
-            .ok_or_else(|| {
-                ExpandError::BindingError(format!("could not find binding `{}`", name))
-            })?;
+        let mut b = self.inner.get(name).ok_or_else(|| {
+            ExpandError::BindingError(format!("could not find binding `{}`", name))
+        })?;
         for nesting_state in nesting.iter_mut() {
             nesting_state.hit = true;
             b = match b {
diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs
index f3d2da55a7..4c298f85fb 100644
--- a/crates/mbe/src/lib.rs
+++ b/crates/mbe/src/lib.rs
@@ -21,7 +21,7 @@ use test_utils::mark;
 pub use tt::{Delimiter, DelimiterKind, Punct};
 
 use crate::{
-    parser::{parse_pattern, parse_template, MetaTemplate, Op},
+    parser::{parse_pattern, parse_template, Op},
     tt_iter::TtIter,
 };
 
@@ -94,6 +94,15 @@ struct Rule {
     rhs: MetaTemplate,
 }
 
+#[derive(Clone, Debug, PartialEq, Eq)]
+struct MetaTemplate(Vec<Op>);
+
+impl<'a> MetaTemplate {
+    fn iter(&self) -> impl Iterator<Item = &Op> {
+        self.0.iter()
+    }
+}
+
 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
 struct Shift(u32);
 
diff --git a/crates/mbe/src/parser.rs b/crates/mbe/src/parser.rs
index 8671322e1f..f891ec29c6 100644
--- a/crates/mbe/src/parser.rs
+++ b/crates/mbe/src/parser.rs
@@ -5,75 +5,7 @@ use smallvec::SmallVec;
 use syntax::SmolStr;
 use tt::Delimiter;
 
-use crate::{tt_iter::TtIter, ParseError};
-
-#[derive(Clone, Debug, PartialEq, Eq)]
-pub(crate) struct MetaTemplate(pub(crate) Vec<Op>);
-
-#[derive(Debug, Clone, Copy)]
-pub(crate) enum OpDelimited<'a> {
-    Op(&'a Op),
-    Open,
-    Close,
-}
-
-#[derive(Debug, Clone, Copy)]
-pub(crate) struct OpDelimitedIter<'a> {
-    inner: &'a Vec<Op>,
-    delimited: Option<&'a Delimiter>,
-    idx: usize,
-}
-
-impl<'a> OpDelimitedIter<'a> {
-    pub(crate) fn is_eof(&self) -> bool {
-        let len = self.inner.len() + if self.delimited.is_some() { 2 } else { 0 };
-        self.idx >= len
-    }
-
-    pub(crate) fn peek(&self) -> Option<OpDelimited<'a>> {
-        match self.delimited {
-            None => self.inner.get(self.idx).map(OpDelimited::Op),
-            Some(_) => match self.idx {
-                0 => Some(OpDelimited::Open),
-                i if i == self.inner.len() + 1 => Some(OpDelimited::Close),
-                i => self.inner.get(i - 1).map(OpDelimited::Op),
-            },
-        }
-    }
-
-    pub(crate) fn reset(&self) -> Self {
-        Self { inner: &self.inner, idx: 0, delimited: self.delimited }
-    }
-}
-
-impl<'a> Iterator for OpDelimitedIter<'a> {
-    type Item = OpDelimited<'a>;
-
-    fn next(&mut self) -> Option<Self::Item> {
-        let res = self.peek();
-        self.idx += 1;
-        res
-    }
-
-    fn size_hint(&self) -> (usize, Option<usize>) {
-        let len = self.inner.len() + if self.delimited.is_some() { 2 } else { 0 };
-        let remain = len.checked_sub(self.idx).unwrap_or(0);
-        (remain, Some(remain))
-    }
-}
-
-impl<'a> MetaTemplate {
-    pub(crate) fn iter(&self) -> impl Iterator<Item = &Op> {
-        self.0.iter()
-    }
-
-    pub(crate) fn iter_delimited(
-        &'a self,
-        delimited: Option<&'a Delimiter>,
-    ) -> OpDelimitedIter<'a> {
-        OpDelimitedIter { inner: &self.0, idx: 0, delimited }
-    }
-}
+use crate::{tt_iter::TtIter, MetaTemplate, ParseError};
 
 #[derive(Clone, Debug, PartialEq, Eq)]
 pub(crate) enum Op {
@@ -115,16 +47,6 @@ impl PartialEq for Separator {
     }
 }
 
-impl Separator {
-    pub(crate) fn tt_count(&self) -> usize {
-        match self {
-            Separator::Literal(_) => 1,
-            Separator::Ident(_) => 1,
-            Separator::Puncts(it) => it.len(),
-        }
-    }
-}
-
 pub(crate) fn parse_template(template: &tt::Subtree) -> Result<Vec<Op>, ParseError> {
     parse_inner(&template, Mode::Template).into_iter().collect()
 }
diff --git a/crates/proc_macro_srv/Cargo.toml b/crates/proc_macro_srv/Cargo.toml
index 4c1b3036ae..6c8c289801 100644
--- a/crates/proc_macro_srv/Cargo.toml
+++ b/crates/proc_macro_srv/Cargo.toml
@@ -20,7 +20,7 @@ proc_macro_api = { path = "../proc_macro_api", version = "0.0.0" }
 test_utils = { path = "../test_utils", version = "0.0.0" }
 
 [dev-dependencies]
-cargo_metadata = "0.13"
+cargo_metadata = "0.12.2"
 
 # used as proc macro test targets
 serde_derive = "1.0.106"
diff --git a/crates/project_model/Cargo.toml b/crates/project_model/Cargo.toml
index fe3258332a..293cb5bfe1 100644
--- a/crates/project_model/Cargo.toml
+++ b/crates/project_model/Cargo.toml
@@ -12,7 +12,7 @@ doctest = false
 [dependencies]
 log = "0.4.8"
 rustc-hash = "1.1.0"
-cargo_metadata = "0.13"
+cargo_metadata = "0.12.2"
 serde = { version = "1.0.106", features = ["derive"] }
 serde_json = "1.0.48"
 anyhow = "1.0.26"
@@ -22,7 +22,7 @@ la-arena = { version = "0.2.0", path = "../../lib/arena" }
 cfg = { path = "../cfg", version = "0.0.0" }
 base_db = { path = "../base_db", version = "0.0.0" }
 toolchain = { path = "../toolchain", version = "0.0.0" }
-proc_macro_api = { path = "../proc_macro_api", version = "0.0.0" }
-paths = { path = "../paths", version = "0.0.0" }
-stdx = { path = "../stdx", version = "0.0.0" }
-profile = { path = "../profile", version = "0.0.0" }
+proc_macro_api =  { path = "../proc_macro_api", version = "0.0.0" }
+paths =  { path = "../paths", version = "0.0.0" }
+stdx =  { path = "../stdx", version = "0.0.0" }
+profile =  { path = "../profile", version = "0.0.0" }
diff --git a/crates/project_model/src/build_data.rs b/crates/project_model/src/build_data.rs
index 728a258eae..295b5f8eff 100644
--- a/crates/project_model/src/build_data.rs
+++ b/crates/project_model/src/build_data.rs
@@ -1,14 +1,14 @@
 //! Handles build script specific information
 
 use std::{
+    ffi::OsStr,
     io::BufReader,
-    path::PathBuf,
+    path::{Path, PathBuf},
     process::{Command, Stdio},
     sync::Arc,
 };
 
 use anyhow::Result;
-use cargo_metadata::camino::Utf8Path;
 use cargo_metadata::{BuildScript, Message};
 use itertools::Itertools;
 use paths::{AbsPath, AbsPathBuf};
@@ -162,8 +162,8 @@ fn collect_from_workspace(
                     let res = res.entry(package_id.repr.clone()).or_default();
                     // cargo_metadata crate returns default (empty) path for
                     // older cargos, which is not absolute, so work around that.
-                    if !out_dir.as_str().is_empty() {
-                        let out_dir = AbsPathBuf::assert(PathBuf::from(out_dir.into_os_string()));
+                    if out_dir != PathBuf::default() {
+                        let out_dir = AbsPathBuf::assert(out_dir);
                         res.out_dir = Some(out_dir);
                         res.cfgs = cfgs;
                     }
@@ -178,7 +178,7 @@ fn collect_from_workspace(
                         // Skip rmeta file
                         if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name))
                         {
-                            let filename = AbsPathBuf::assert(PathBuf::from(&filename));
+                            let filename = AbsPathBuf::assert(filename.clone());
                             let res = res.entry(package_id.repr.clone()).or_default();
                             res.proc_macro_dylib_path = Some(filename);
                         }
@@ -187,9 +187,9 @@ fn collect_from_workspace(
                 Message::CompilerMessage(message) => {
                     progress(message.target.name.clone());
                 }
+                Message::Unknown => (),
                 Message::BuildFinished(_) => {}
                 Message::TextLine(_) => {}
-                _ => {}
             }
         }
     }
@@ -209,8 +209,8 @@ fn collect_from_workspace(
 }
 
 // FIXME: File a better way to know if it is a dylib
-fn is_dylib(path: &Utf8Path) -> bool {
-    match path.extension().map(|e| e.to_string().to_lowercase()) {
+fn is_dylib(path: &Path) -> bool {
+    match path.extension().and_then(OsStr::to_str).map(|it| it.to_string().to_lowercase()) {
         None => false,
         Some(ext) => matches!(ext.as_str(), "dll" | "dylib" | "so"),
     }
@@ -227,7 +227,9 @@ fn inject_cargo_env(package: &cargo_metadata::Package, build_data: &mut BuildDat
 
     let mut manifest_dir = package.manifest_path.clone();
     manifest_dir.pop();
-    env.push(("CARGO_MANIFEST_DIR".into(), manifest_dir.into_string()));
+    if let Some(cargo_manifest_dir) = manifest_dir.to_str() {
+        env.push(("CARGO_MANIFEST_DIR".into(), cargo_manifest_dir.into()));
+    }
 
     // Not always right, but works for common cases.
     env.push(("CARGO".into(), "cargo".into()));
@@ -249,6 +251,7 @@ fn inject_cargo_env(package: &cargo_metadata::Package, build_data: &mut BuildDat
     env.push(("CARGO_PKG_REPOSITORY".into(), package.repository.clone().unwrap_or_default()));
     env.push(("CARGO_PKG_LICENSE".into(), package.license.clone().unwrap_or_default()));
 
-    let license_file = package.license_file.as_ref().map(|buf| buf.to_string()).unwrap_or_default();
+    let license_file =
+        package.license_file.as_ref().map(|buf| buf.display().to_string()).unwrap_or_default();
     env.push(("CARGO_PKG_LICENSE_FILE".into(), license_file));
 }
diff --git a/crates/project_model/src/cargo_workspace.rs b/crates/project_model/src/cargo_workspace.rs
index f7241b7113..1d8d34a0b8 100644
--- a/crates/project_model/src/cargo_workspace.rs
+++ b/crates/project_model/src/cargo_workspace.rs
@@ -1,6 +1,5 @@
 //! FIXME: write short doc here
 
-use std::path::PathBuf;
 use std::{convert::TryInto, ops, process::Command, sync::Arc};
 
 use anyhow::{Context, Result};
@@ -250,12 +249,11 @@ impl CargoWorkspace {
             let edition = edition
                 .parse::<Edition>()
                 .with_context(|| format!("Failed to parse edition {}", edition))?;
-
             let pkg = packages.alloc(PackageData {
                 id: id.repr.clone(),
                 name: name.clone(),
                 version: version.to_string(),
-                manifest: AbsPathBuf::assert(PathBuf::from(&manifest_path)),
+                manifest: AbsPathBuf::assert(manifest_path.clone()),
                 targets: Vec::new(),
                 is_member,
                 edition,
@@ -270,7 +268,7 @@ impl CargoWorkspace {
                 let tgt = targets.alloc(TargetData {
                     package: pkg,
                     name: meta_tgt.name.clone(),
-                    root: AbsPathBuf::assert(PathBuf::from(&meta_tgt.src_path)),
+                    root: AbsPathBuf::assert(meta_tgt.src_path.clone()),
                     kind: TargetKind::new(meta_tgt.kind.as_slice()),
                     is_proc_macro,
                 });
@@ -307,8 +305,7 @@ impl CargoWorkspace {
             packages[source].active_features.extend(node.features);
         }
 
-        let workspace_root =
-            AbsPathBuf::assert(PathBuf::from(meta.workspace_root.into_os_string()));
+        let workspace_root = AbsPathBuf::assert(meta.workspace_root);
         let build_data_config = BuildDataConfig::new(
             cargo_toml.to_path_buf(),
             config.clone(),
diff --git a/crates/rust-analyzer/src/diagnostics/to_proto.rs b/crates/rust-analyzer/src/diagnostics/to_proto.rs
index 0ad832c0ee..0ed87fe3e0 100644
--- a/crates/rust-analyzer/src/diagnostics/to_proto.rs
+++ b/crates/rust-analyzer/src/diagnostics/to_proto.rs
@@ -29,7 +29,7 @@ fn diagnostic_severity(
         },
         DiagnosticLevel::Note => lsp_types::DiagnosticSeverity::Information,
         DiagnosticLevel::Help => lsp_types::DiagnosticSeverity::Hint,
-        _ => return None,
+        DiagnosticLevel::Unknown => return None,
     };
     Some(res)
 }
diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs
index c1ca88df67..70cb7fbab5 100644
--- a/crates/rust-analyzer/src/to_proto.rs
+++ b/crates/rust-analyzer/src/to_proto.rs
@@ -474,7 +474,7 @@ pub(crate) fn folding_range(
     let kind = match fold.kind {
         FoldKind::Comment => Some(lsp_types::FoldingRangeKind::Comment),
         FoldKind::Imports => Some(lsp_types::FoldingRangeKind::Imports),
-        FoldKind::Mods | FoldKind::Block | FoldKind::ArgList | FoldKind::Region => None,
+        FoldKind::Mods | FoldKind::Block | FoldKind::ArgList => None,
     };
 
     let range = range(line_index, fold.range);


Test patch:
diff --git a/crates/ide_assists/src/tests.rs b/crates/ide_assists/src/tests.rs
index 66820058b6..b7f6167607 100644
--- a/crates/ide_assists/src/tests.rs
+++ b/crates/ide_assists/src/tests.rs
@@ -12,7 +12,7 @@ use ide_db::{
     RootDatabase,
 };
 use stdx::{format_to, trim_indent};
-use syntax::{ast, AstNode, TextRange};
+use syntax::TextRange;
 use test_utils::{assert_eq_text, extract_offset};
 
 use crate::{handlers::Handler, Assist, AssistConfig, AssistContext, AssistKind, Assists};
@@ -180,50 +180,6 @@ fn labels(assists: &[Assist]) -> String {
     labels.into_iter().collect::<String>()
 }
 
-pub(crate) type NameSuggestion = fn(&ast::Expr, &Semantics<'_, RootDatabase>) -> Option<String>;
-
-#[track_caller]
-pub(crate) fn check_name_suggestion(
-    suggestion: NameSuggestion,
-    ra_fixture: &str,
-    suggested_name: &str,
-) {
-    check_name(suggestion, ra_fixture, Some(suggested_name));
-}
-
-#[track_caller]
-pub(crate) fn check_name_suggestion_not_applicable(suggestion: NameSuggestion, ra_fixture: &str) {
-    check_name(suggestion, ra_fixture, None);
-}
-
-#[track_caller]
-fn check_name(suggestion: NameSuggestion, ra_fixture: &str, expected: Option<&str>) {
-    let (db, file_with_carret_id, range_or_offset) = RootDatabase::with_range_or_offset(ra_fixture);
-    let frange = FileRange { file_id: file_with_carret_id, range: range_or_offset.into() };
-
-    let sema = Semantics::new(&db);
-    let source_file = sema.parse(frange.file_id);
-    let element = source_file.syntax().covering_element(frange.range);
-    let expr =
-        element.ancestors().find_map(ast::Expr::cast).expect("selection is not an expression");
-    assert_eq!(
-        expr.syntax().text_range(),
-        frange.range,
-        "selection is not an expression(yet contained in one)"
-    );
-
-    let name = suggestion(&expr, &sema);
-
-    match (name, expected) {
-        (Some(name), Some(expected_name)) => {
-            assert_eq_text!(&name, expected_name);
-        }
-        (Some(_), None) => panic!("name suggestion should not be applicable"),
-        (None, Some(_)) => panic!("name suggestion is not applicable"),
-        (None, None) => (),
-    }
-}
-
 #[test]
 fn assist_order_field_struct() {
     let before = "struct Foo { $0bar: u32 }";
diff --git a/crates/mbe/src/tests.rs b/crates/mbe/src/tests.rs
index 5c641ebf28..f1eadcd1ee 100644
--- a/crates/mbe/src/tests.rs
+++ b/crates/mbe/src/tests.rs
@@ -456,17 +456,6 @@ fn test_match_group_with_multichar_sep() {
     .assert_expand_items("foo! (fn baz {true true} );", "fn baz () -> bool {true &&true}");
 }
 
-#[test]
-fn test_match_group_with_multichar_sep2() {
-    parse_macro(
-        r#"
-        macro_rules! foo {
-            (fn $name:ident {$($i:literal)&&*} ) => ( fn $name() -> bool { $($i)&&*} );
-        }"#,
-    )
-    .assert_expand_items("foo! (fn baz {true && true} );", "fn baz () -> bool {true &&true}");
-}
-
 #[test]
 fn test_match_group_zero_match() {
     parse_macro(
@@ -1278,18 +1267,6 @@ macro_rules! m {
     .is_some());
 }
 
-#[test]
-fn test_match_is_not_greedy() {
-    parse_macro(
-        r#"
-macro_rules! foo {
-    ($($i:ident $(,)*),*) => {};
-}
-"#,
-    )
-    .assert_expand_items(r#"foo!(a,b);"#, r#""#);
-}
-
 // The following tests are based on real world situations
 #[test]
 fn test_vec() {
diff --git a/crates/proc_macro_srv/src/tests/utils.rs b/crates/proc_macro_srv/src/tests/utils.rs
index 22813052df..196abb8fc2 100644
--- a/crates/proc_macro_srv/src/tests/utils.rs
+++ b/crates/proc_macro_srv/src/tests/utils.rs
@@ -8,7 +8,6 @@ use test_utils::assert_eq_text;
 
 mod fixtures {
     use cargo_metadata::Message;
-    use std::path::PathBuf;
     use std::process::Command;
 
     // Use current project metadata to get the proc-macro dylib path
@@ -25,7 +24,7 @@ mod fixtures {
                     if artifact.target.kind.contains(&"proc-macro".to_string()) {
                         let repr = format!("{} {}", crate_name, version);
                         if artifact.package_id.repr.starts_with(&repr) {
-                            return PathBuf::from(&artifact.filenames[0]);
+                            return artifact.filenames[0].clone();
                         }
                     }
                 }