Skip to content

Commit 47b3db1

Browse files
committed
the "add missing members" assists: supported bracketed default const values
1 parent c7f7416 commit 47b3db1

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

crates/hir-ty/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use hir_expand::name;
5252
use la_arena::{Arena, Idx};
5353
use mir::{MirEvalError, VTableMap};
5454
use rustc_hash::FxHashSet;
55+
use syntax::AstNode;
5556
use traits::FnTrait;
5657
use triomphe::Arc;
5758
use utils::Generics;
@@ -723,12 +724,12 @@ where
723724

724725
pub fn known_const_to_string(konst: &Const, db: &dyn HirDatabase) -> Option<String> {
725726
if let ConstValue::Concrete(c) = &konst.interned().value {
726-
if let ConstScalar::UnevaluatedConst(GeneralConstId::InTypeConstId(_), _) = &c.interned {
727-
// FIXME: stringify the block expression
728-
return None;
729-
}
730-
if c.interned == ConstScalar::Unknown {
731-
return None;
727+
match c.interned {
728+
ConstScalar::UnevaluatedConst(GeneralConstId::InTypeConstId(cid), _) => {
729+
return Some(cid.source(db.upcast()).syntax().to_string());
730+
}
731+
ConstScalar::Unknown => return None,
732+
_ => (),
732733
}
733734
}
734735
Some(konst.display(db).to_string())

crates/ide-assists/src/handlers/add_missing_impl_members.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,41 @@ impl m::Foo for () {
532532
)
533533
}
534534

535+
#[test]
536+
fn test_const_substitution_with_defaults_3() {
537+
check_assist(
538+
add_missing_default_members,
539+
r#"
540+
mod m {
541+
pub const VAL: usize = 0;
542+
543+
pub trait Foo<const N: usize = {40 + 2}, const M: usize = {VAL + 1}> {
544+
fn get_n(&self) -> usize { N }
545+
fn get_m(&self) -> usize { M }
546+
}
547+
}
548+
549+
impl m::Foo for () {
550+
$0
551+
}"#,
552+
r#"
553+
mod m {
554+
pub const VAL: usize = 0;
555+
556+
pub trait Foo<const N: usize = {40 + 2}, const M: usize = {VAL + 1}> {
557+
fn get_n(&self) -> usize { N }
558+
fn get_m(&self) -> usize { M }
559+
}
560+
}
561+
562+
impl m::Foo for () {
563+
$0fn get_n(&self) -> usize { {40 + 2} }
564+
565+
fn get_m(&self) -> usize { {m::VAL + 1} }
566+
}"#,
567+
)
568+
}
569+
535570
#[test]
536571
fn test_cursor_after_empty_impl_def() {
537572
check_assist(

crates/ide-db/src/path_transform.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ impl<'a> PathTransform<'a> {
156156
// is a standalone statement or a part of another expresson)
157157
// and sometimes require slight modifications; see
158158
// https://doc.rust-lang.org/reference/statements.html#expression-statements
159+
// (default values in curly brackets can cause the same problem)
159160
const_substs.insert(k, expr.syntax().clone());
160161
}
161162
}

0 commit comments

Comments
 (0)