Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 61cabe0

Browse files
committed
the "add missing members" assists: supported bracketed default const values
1 parent 4e2be8e commit 61cabe0

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
@@ -549,6 +549,41 @@ impl m::Foo for () {
549549
)
550550
}
551551

552+
#[test]
553+
fn test_const_substitution_with_defaults_3() {
554+
check_assist(
555+
add_missing_default_members,
556+
r#"
557+
mod m {
558+
pub const VAL: usize = 0;
559+
560+
pub trait Foo<const N: usize = {40 + 2}, const M: usize = {VAL + 1}> {
561+
fn get_n(&self) -> usize { N }
562+
fn get_m(&self) -> usize { M }
563+
}
564+
}
565+
566+
impl m::Foo for () {
567+
$0
568+
}"#,
569+
r#"
570+
mod m {
571+
pub const VAL: usize = 0;
572+
573+
pub trait Foo<const N: usize = {40 + 2}, const M: usize = {VAL + 1}> {
574+
fn get_n(&self) -> usize { N }
575+
fn get_m(&self) -> usize { M }
576+
}
577+
}
578+
579+
impl m::Foo for () {
580+
$0fn get_n(&self) -> usize { {40 + 2} }
581+
582+
fn get_m(&self) -> usize { {m::VAL + 1} }
583+
}"#,
584+
)
585+
}
586+
552587
#[test]
553588
fn test_cursor_after_empty_impl_def() {
554589
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)