Unnamed repository; edit this file 'description' to name the repository.
the "add missing members" assists: supported bracketed default const values
ponyii 2023-08-09
parent 4e2be8e · commit 61cabe0
-rw-r--r--crates/hir-ty/src/lib.rs13
-rw-r--r--crates/ide-assists/src/handlers/add_missing_impl_members.rs35
-rw-r--r--crates/ide-db/src/path_transform.rs1
3 files changed, 43 insertions, 6 deletions
diff --git a/crates/hir-ty/src/lib.rs b/crates/hir-ty/src/lib.rs
index 14346c2794..d0bb16c780 100644
--- a/crates/hir-ty/src/lib.rs
+++ b/crates/hir-ty/src/lib.rs
@@ -52,6 +52,7 @@ use hir_expand::name;
use la_arena::{Arena, Idx};
use mir::{MirEvalError, VTableMap};
use rustc_hash::FxHashSet;
+use syntax::AstNode;
use traits::FnTrait;
use triomphe::Arc;
use utils::Generics;
@@ -723,12 +724,12 @@ where
pub fn known_const_to_string(konst: &Const, db: &dyn HirDatabase) -> Option<String> {
if let ConstValue::Concrete(c) = &konst.interned().value {
- if let ConstScalar::UnevaluatedConst(GeneralConstId::InTypeConstId(_), _) = &c.interned {
- // FIXME: stringify the block expression
- return None;
- }
- if c.interned == ConstScalar::Unknown {
- return None;
+ match c.interned {
+ ConstScalar::UnevaluatedConst(GeneralConstId::InTypeConstId(cid), _) => {
+ return Some(cid.source(db.upcast()).syntax().to_string());
+ }
+ ConstScalar::Unknown => return None,
+ _ => (),
}
}
Some(konst.display(db).to_string())
diff --git a/crates/ide-assists/src/handlers/add_missing_impl_members.rs b/crates/ide-assists/src/handlers/add_missing_impl_members.rs
index d9faf3a7f6..c0e5429a22 100644
--- a/crates/ide-assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ide-assists/src/handlers/add_missing_impl_members.rs
@@ -550,6 +550,41 @@ impl m::Foo for () {
}
#[test]
+ fn test_const_substitution_with_defaults_3() {
+ check_assist(
+ add_missing_default_members,
+ r#"
+mod m {
+ pub const VAL: usize = 0;
+
+ pub trait Foo<const N: usize = {40 + 2}, const M: usize = {VAL + 1}> {
+ fn get_n(&self) -> usize { N }
+ fn get_m(&self) -> usize { M }
+ }
+}
+
+impl m::Foo for () {
+ $0
+}"#,
+ r#"
+mod m {
+ pub const VAL: usize = 0;
+
+ pub trait Foo<const N: usize = {40 + 2}, const M: usize = {VAL + 1}> {
+ fn get_n(&self) -> usize { N }
+ fn get_m(&self) -> usize { M }
+ }
+}
+
+impl m::Foo for () {
+ $0fn get_n(&self) -> usize { {40 + 2} }
+
+ fn get_m(&self) -> usize { {m::VAL + 1} }
+}"#,
+ )
+ }
+
+ #[test]
fn test_cursor_after_empty_impl_def() {
check_assist(
add_missing_impl_members,
diff --git a/crates/ide-db/src/path_transform.rs b/crates/ide-db/src/path_transform.rs
index efe13f0604..4bd4f1e845 100644
--- a/crates/ide-db/src/path_transform.rs
+++ b/crates/ide-db/src/path_transform.rs
@@ -156,6 +156,7 @@ impl<'a> PathTransform<'a> {
// is a standalone statement or a part of another expresson)
// and sometimes require slight modifications; see
// https://doc.rust-lang.org/reference/statements.html#expression-statements
+ // (default values in curly brackets can cause the same problem)
const_substs.insert(k, expr.syntax().clone());
}
}