diff options
| author | Lukas Wirth <lukastw97@gmail.com> | 2022-02-22 12:32:15 +0100 |
|---|---|---|
| committer | Lukas Wirth <lukastw97@gmail.com> | 2022-02-22 12:32:27 +0100 |
| commit | 2e124d15fb72482257c8ef10369df77ee24d6a85 (patch) | |
| tree | aa4d9bc88295ee82d79126d5b28ceec52b6854e6 | |
| parent | b494795a423d91c8db06d09d6c6dc62840796e8d (diff) | |
| download | rust-2e124d15fb72482257c8ef10369df77ee24d6a85.tar.gz rust-2e124d15fb72482257c8ef10369df77ee24d6a85.zip | |
fix: Fix expand_macro always expanding the first listed derive
| -rw-r--r-- | crates/hir_expand/src/lib.rs | 3 | ||||
| -rw-r--r-- | crates/ide/src/expand_macro.rs | 17 | ||||
| -rw-r--r-- | crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs | 2 |
3 files changed, 18 insertions, 4 deletions
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index cc38faa1369..ba0f1015124 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -310,7 +310,8 @@ impl HirFileId { } /// Return whether this file is the pseudo expansion of the derive attribute. - pub fn is_derive_attr_macro(&self, db: &dyn db::AstDatabase) -> bool { + /// See [`crate::builtin_attr_macro::derive_attr_expand`]. + pub fn is_derive_attr_pseudo_expansion(&self, db: &dyn db::AstDatabase) -> bool { match self.0 { HirFileIdRepr::MacroFile(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); diff --git a/crates/ide/src/expand_macro.rs b/crates/ide/src/expand_macro.rs index f7326747253..7bb6b24a23d 100644 --- a/crates/ide/src/expand_macro.rs +++ b/crates/ide/src/expand_macro.rs @@ -42,7 +42,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< let derive = sema.descend_into_macros(tok.clone()).into_iter().find_map(|descended| { let hir_file = sema.hir_file_for(&descended.parent()?); - if !hir_file.is_derive_attr_macro(db) { + if !hir_file.is_derive_attr_pseudo_expansion(db) { return None; } @@ -55,7 +55,7 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option< .token_tree()? .token_trees_and_tokens() .filter_map(NodeOrToken::into_token) - .take_while(|it| it == &token) + .take_while(|it| it != &token) .filter(|it| it.kind() == T![,]) .count(); Some(ExpandedMacro { @@ -384,5 +384,18 @@ struct Foo {} "#]], ); + check( + r#" +//- minicore: copy, clone, derive + +#[derive(Copy, Cl$0one)] +struct Foo {} +"#, + expect![[r#" + Clone + impl < >core::clone::Clone for Foo< >{} + + "#]], + ); } } diff --git a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs index 8ac05bf5ff5..27f2960c7ed 100644 --- a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs @@ -49,7 +49,7 @@ pub(crate) fn replace_derive_with_manual_impl( let attr = ctx.find_node_at_offset_with_descend::<ast::Attr>()?; let path = attr.path()?; let hir_file = ctx.sema.hir_file_for(attr.syntax()); - if !hir_file.is_derive_attr_macro(ctx.db()) { + if !hir_file.is_derive_attr_pseudo_expansion(ctx.db()) { return None; } |
