about summary refs log tree commit diff
diff options
context:
space:
mode:
authorponyii <ponyii@protonmail.com>2023-07-12 18:31:40 +0400
committerponyii <ponyii@protonmail.com>2023-08-08 21:57:55 +0400
commit61cabe029fcd74aae6a2811768bc7f46f22354fe (patch)
tree9ae0819e8aa69c9f0d86ca7c9ba8817518eee7dd
parent4e2be8e959179f8b7f9614e1d147b878bbd2f071 (diff)
downloadrust-61cabe029fcd74aae6a2811768bc7f46f22354fe.tar.gz
rust-61cabe029fcd74aae6a2811768bc7f46f22354fe.zip
the "add missing members" assists: supported bracketed default const values
-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 14346c27941..d0bb16c7808 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 d9faf3a7f6c..c0e5429a22c 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 efe13f06040..4bd4f1e845f 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());
                     }
                 }