Auto merge of #15847 - wasd96040501:feat/preview_adt, r=<try>
feat: preview adt field when hover
Closes #13977
![20231108194345_rec_](https://github.com/rust-lang/rust-analyzer/assets/14040068/95894c4b-de6e-4ca4-98b3-6ab4559d0950)
diff --git a/Cargo.lock b/Cargo.lock
index 0a3d386..fcb188c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1454,12 +1454,12 @@
[[package]]
name = "ra-ap-rustc_abi"
-version = "0.14.0"
+version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a51b7a02377b3246ec5c095b852b5cf1678bd9ed6b572b2a79efbf7ad711c292"
+checksum = "7082716cb2bbcd8b5f062fe950cbbc87f3aba022d6da4168db35af6732a7f15d"
dependencies = [
"bitflags 1.3.2",
- "ra-ap-rustc_index",
+ "ra-ap-rustc_index 0.18.0",
"tracing",
]
@@ -1474,6 +1474,16 @@
]
[[package]]
+name = "ra-ap-rustc_index"
+version = "0.18.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "19e14b1fc835d6992b128a03a3f3a8365ba9f03e1c656a1670305f63f30d786d"
+dependencies = [
+ "arrayvec",
+ "smallvec",
+]
+
+[[package]]
name = "ra-ap-rustc_lexer"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1484,13 +1494,23 @@
]
[[package]]
+name = "ra-ap-rustc_lexer"
+version = "0.18.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1762abb25eb1e37c1823f62b5da0821bbcd870812318db084c9516c2f78d2dcd"
+dependencies = [
+ "unicode-properties",
+ "unicode-xid",
+]
+
+[[package]]
name = "ra-ap-rustc_parse_format"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "207b5ac1a21d4926695e03b605ffb9f63d4968e0488e9197c04c512c37303aa7"
dependencies = [
- "ra-ap-rustc_index",
- "ra-ap-rustc_lexer",
+ "ra-ap-rustc_index 0.14.0",
+ "ra-ap-rustc_lexer 0.14.0",
]
[[package]]
@@ -1614,8 +1634,8 @@
version = "0.0.0"
dependencies = [
"ra-ap-rustc_abi",
- "ra-ap-rustc_index",
- "ra-ap-rustc_lexer",
+ "ra-ap-rustc_index 0.18.0",
+ "ra-ap-rustc_lexer 0.18.0",
"ra-ap-rustc_parse_format",
]
diff --git a/crates/base-db/src/lib.rs b/crates/base-db/src/lib.rs
index af204e4..c5c4afa 100644
--- a/crates/base-db/src/lib.rs
+++ b/crates/base-db/src/lib.rs
@@ -75,7 +75,7 @@
#[salsa::input]
fn crate_graph(&self) -> Arc<CrateGraph>;
- /// The crate graph.
+ /// The proc macros.
#[salsa::input]
fn proc_macros(&self) -> Arc<ProcMacros>;
}
diff --git a/crates/hir-def/src/data/adt.rs b/crates/hir-def/src/data/adt.rs
index 76c8d9a..b163112 100644
--- a/crates/hir-def/src/data/adt.rs
+++ b/crates/hir-def/src/data/adt.rs
@@ -178,7 +178,7 @@
}
}
- Some(ReprOptions { int, align: max_align, pack: min_pack, flags })
+ Some(ReprOptions { int, align: max_align, pack: min_pack, flags, field_shuffle_seed: 0 })
}
impl StructData {
diff --git a/crates/ide-assists/src/handlers/generate_mut_trait_impl.rs b/crates/ide-assists/src/handlers/generate_mut_trait_impl.rs
new file mode 100644
index 0000000..cb8ef39
--- /dev/null
+++ b/crates/ide-assists/src/handlers/generate_mut_trait_impl.rs
@@ -0,0 +1,202 @@
+use ide_db::famous_defs::FamousDefs;
+use syntax::{
+ ast::{self, make},
+ ted, AstNode,
+};
+
+use crate::{AssistContext, AssistId, AssistKind, Assists};
+
+// FIXME: Generate proper `index_mut` method body refer to `index` method body may impossible due to the unpredicable case [#15581].
+// Here just leave the `index_mut` method body be same as `index` method body, user can modify it manually to meet their need.
+
+// Assist: generate_mut_trait_impl
+//
+// Adds a IndexMut impl from the `Index` trait.
+//
+// ```
+// # //- minicore: index
+// pub enum Axis { X = 0, Y = 1, Z = 2 }
+//
+// impl<T> core::ops::Index$0<Axis> for [T; 3] {
+// type Output = T;
+//
+// fn index(&self, index: Axis) -> &Self::Output {
+// &self[index as usize]
+// }
+// }
+// ```
+// ->
+// ```
+// pub enum Axis { X = 0, Y = 1, Z = 2 }
+//
+// $0impl<T> core::ops::IndexMut<Axis> for [T; 3] {
+// fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
+// &self[index as usize]
+// }
+// }
+//
+// impl<T> core::ops::Index<Axis> for [T; 3] {
+// type Output = T;
+//
+// fn index(&self, index: Axis) -> &Self::Output {
+// &self[index as usize]
+// }
+// }
+// ```
+pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
+ let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update();
+
+ let trait_ = impl_def.trait_()?;
+ if let ast::Type::PathType(trait_path) = trait_.clone() {
+ let trait_type = ctx.sema.resolve_trait(&trait_path.path()?)?;
+ let scope = ctx.sema.scope(trait_path.syntax())?;
+ if trait_type != FamousDefs(&ctx.sema, scope.krate()).core_convert_Index()? {
+ return None;
+ }
+ }
+
+ // Index -> IndexMut
+ let index_trait = impl_def
+ .syntax()
+ .descendants()
+ .filter_map(ast::NameRef::cast)
+ .find(|it| it.text() == "Index")?;
+ ted::replace(
+ index_trait.syntax(),
+ make::path_segment(make::name_ref("IndexMut")).clone_for_update().syntax(),
+ );
+
+ // index -> index_mut
+ let trait_method_name = impl_def
+ .syntax()
+ .descendants()
+ .filter_map(ast::Name::cast)
+ .find(|it| it.text() == "index")?;
+ ted::replace(trait_method_name.syntax(), make::name("index_mut").clone_for_update().syntax());
+
+ let type_alias = impl_def.syntax().descendants().find_map(ast::TypeAlias::cast)?;
+ ted::remove(type_alias.syntax());
+
+ // &self -> &mut self
+ let mut_self_param = make::mut_self_param();
+ let self_param: ast::SelfParam =
+ impl_def.syntax().descendants().find_map(ast::SelfParam::cast)?;
+ ted::replace(self_param.syntax(), mut_self_param.clone_for_update().syntax());
+
+ // &Self::Output -> &mut Self::Output
+ let ret_type = impl_def.syntax().descendants().find_map(ast::RetType::cast)?;
+ ted::replace(
+ ret_type.syntax(),
+ make::ret_type(make::ty("&mut Self::Output")).clone_for_update().syntax(),
+ );
+
+ let fn_ = impl_def.assoc_item_list()?.assoc_items().find_map(|it| match it {
+ ast::AssocItem::Fn(f) => Some(f),
+ _ => None,
+ })?;
+
+ let assoc_list = make::assoc_item_list().clone_for_update();
+ assoc_list.add_item(syntax::ast::AssocItem::Fn(fn_));
+ ted::replace(impl_def.assoc_item_list()?.syntax(), assoc_list.syntax());
+
+ let target = impl_def.syntax().text_range();
+ acc.add(
+ AssistId("generate_mut_trait_impl", AssistKind::Generate),
+ "Generate `IndexMut` impl from this `Index` trait",
+ target,
+ |edit| {
+ edit.insert(target.start(), format!("$0{}\n\n", impl_def.to_string()));
+ },
+ )
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::tests::{check_assist, check_assist_not_applicable};
+
+ use super::*;
+
+ #[test]
+ fn test_generate_mut_trait_impl() {
+ check_assist(
+ generate_mut_trait_impl,
+ r#"
+//- minicore: index
+pub enum Axis { X = 0, Y = 1, Z = 2 }
+
+impl<T> core::ops::Index$0<Axis> for [T; 3] {
+ type Output = T;
+
+ fn index(&self, index: Axis) -> &Self::Output {
+ &self[index as usize]
+ }
+}
+"#,
+ r#"
+pub enum Axis { X = 0, Y = 1, Z = 2 }
+
+$0impl<T> core::ops::IndexMut<Axis> for [T; 3] {
+ fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
+ &self[index as usize]
+ }
+}
+
+impl<T> core::ops::Index<Axis> for [T; 3] {
+ type Output = T;
+
+ fn index(&self, index: Axis) -> &Self::Output {
+ &self[index as usize]
+ }
+}
+"#,
+ );
+
+ check_assist(
+ generate_mut_trait_impl,
+ r#"
+//- minicore: index
+pub enum Axis { X = 0, Y = 1, Z = 2 }
+
+impl<T> core::ops::Index$0<Axis> for [T; 3] where T: Copy {
+ type Output = T;
+
+ fn index(&self, index: Axis) -> &Self::Output {
+ let var_name = &self[index as usize];
+ var_name
+ }
+}
+"#,
+ r#"
+pub enum Axis { X = 0, Y = 1, Z = 2 }
+
+$0impl<T> core::ops::IndexMut<Axis> for [T; 3] where T: Copy {
+ fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
+ let var_name = &self[index as usize];
+ var_name
+ }
+}
+
+impl<T> core::ops::Index<Axis> for [T; 3] where T: Copy {
+ type Output = T;
+
+ fn index(&self, index: Axis) -> &Self::Output {
+ let var_name = &self[index as usize];
+ var_name
+ }
+}
+"#,
+ );
+ }
+
+ #[test]
+ fn test_generate_mut_trait_impl_not_applicable() {
+ check_assist_not_applicable(
+ generate_mut_trait_impl,
+ r#"
+pub trait Index<Idx: ?Sized> {}
+
+impl<T> Index$0<i32> for [T; 3] {}
+"#,
+ );
+ }
+}
diff --git a/crates/ide-assists/src/lib.rs b/crates/ide-assists/src/lib.rs
index d2b2916..e6f0321 100644
--- a/crates/ide-assists/src/lib.rs
+++ b/crates/ide-assists/src/lib.rs
@@ -160,6 +160,7 @@
mod generate_getter_or_setter;
mod generate_impl;
mod generate_is_empty_from_len;
+ mod generate_mut_trait_impl;
mod generate_new;
mod generate_delegate_methods;
mod generate_trait_from_impl;
@@ -274,6 +275,7 @@
generate_function::generate_function,
generate_impl::generate_impl,
generate_impl::generate_trait_impl,
+ generate_mut_trait_impl::generate_mut_trait_impl,
generate_is_empty_from_len::generate_is_empty_from_len,
generate_new::generate_new,
generate_trait_from_impl::generate_trait_from_impl,
diff --git a/crates/ide-assists/src/tests/generated.rs b/crates/ide-assists/src/tests/generated.rs
index 8523632..da5822b 100644
--- a/crates/ide-assists/src/tests/generated.rs
+++ b/crates/ide-assists/src/tests/generated.rs
@@ -1539,6 +1539,42 @@
}
#[test]
+fn doctest_generate_mut_trait_impl() {
+ check_doc_test(
+ "generate_mut_trait_impl",
+ r#####"
+//- minicore: index
+pub enum Axis { X = 0, Y = 1, Z = 2 }
+
+impl<T> core::ops::Index$0<Axis> for [T; 3] {
+ type Output = T;
+
+ fn index(&self, index: Axis) -> &Self::Output {
+ &self[index as usize]
+ }
+}
+"#####,
+ r#####"
+pub enum Axis { X = 0, Y = 1, Z = 2 }
+
+$0impl<T> core::ops::IndexMut<Axis> for [T; 3] {
+ fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
+ &self[index as usize]
+ }
+}
+
+impl<T> core::ops::Index<Axis> for [T; 3] {
+ type Output = T;
+
+ fn index(&self, index: Axis) -> &Self::Output {
+ &self[index as usize]
+ }
+}
+"#####,
+ )
+}
+
+#[test]
fn doctest_generate_new() {
check_doc_test(
"generate_new",
diff --git a/crates/ide-db/src/famous_defs.rs b/crates/ide-db/src/famous_defs.rs
index b63dde2..722517a 100644
--- a/crates/ide-db/src/famous_defs.rs
+++ b/crates/ide-db/src/famous_defs.rs
@@ -54,6 +54,10 @@
self.find_trait("core:convert:Into")
}
+ pub fn core_convert_Index(&self) -> Option<Trait> {
+ self.find_trait("core:ops:Index")
+ }
+
pub fn core_option_Option(&self) -> Option<Enum> {
self.find_enum("core:option:Option")
}
diff --git a/crates/rustc-dependencies/Cargo.toml b/crates/rustc-dependencies/Cargo.toml
index 7ead3d8..a313507 100644
--- a/crates/rustc-dependencies/Cargo.toml
+++ b/crates/rustc-dependencies/Cargo.toml
@@ -11,10 +11,10 @@
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-ra-ap-rustc_lexer = { version = "0.14.0" }
+ra-ap-rustc_lexer = { version = "0.18.0" }
ra-ap-rustc_parse_format = { version = "0.14.0", default-features = false }
-ra-ap-rustc_index = { version = "0.14.0", default-features = false }
-ra-ap-rustc_abi = { version = "0.14.0", default-features = false }
+ra-ap-rustc_index = { version = "0.18.0", default-features = false }
+ra-ap-rustc_abi = { version = "0.18.0", default-features = false }
[features]
in-rust-tree = []
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index 96f685d..31a858b 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -852,6 +852,10 @@
ast_from_text("fn f(&self) { }")
}
+pub fn mut_self_param() -> ast::SelfParam {
+ ast_from_text("fn f(&mut self) { }")
+}
+
pub fn ret_type(ty: ast::Type) -> ast::RetType {
ast_from_text(&format!("fn f() -> {ty} {{ }}"))
}