the "add missing members" assists: supported bracketed default const values

This commit is contained in:
ponyii 2023-07-12 18:31:40 +04:00
parent 4e2be8e959
commit 61cabe029f
3 changed files with 43 additions and 6 deletions

View file

@ -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())

View file

@ -549,6 +549,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(

View file

@ -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());
}
}