Skip to content

Commit 2665241

Browse files
committed
syntax update: the default value of ConstParam turned from Expr into ConstArg
1 parent df7e701 commit 2665241

File tree

9 files changed

+19
-17
lines changed

9 files changed

+19
-17
lines changed

crates/hir-def/src/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl GenericParams {
248248
let param = ConstParamData {
249249
name,
250250
ty: Interned::new(ty),
251-
default: ConstRef::from_default_param_value(lower_ctx, const_param),
251+
default: ConstRef::from_const_param(lower_ctx, const_param),
252252
};
253253
self.type_or_consts.alloc(param.into());
254254
}

crates/hir-def/src/hir/type_ref.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,15 +393,12 @@ impl ConstRef {
393393
Self::Scalar(LiteralConstRef::Unknown)
394394
}
395395

396-
pub(crate) fn from_default_param_value(
397-
_: &LowerCtx<'_>,
398-
param: ast::ConstParam,
399-
) -> Option<Self> {
400-
if let Some(expr) = param.default_val() {
401-
// FIXME: pass the `ast_id` arg to recognize complex expressions
402-
return Some(Self::from_expr(expr, None));
396+
pub(crate) fn from_const_param(lower_ctx: &LowerCtx<'_>, param: ast::ConstParam) -> Option<Self> {
397+
let default = param.default_val();
398+
match default {
399+
Some(_) => Some(Self::from_const_arg(lower_ctx, default)),
400+
None => None
403401
}
404-
None
405402
}
406403

407404
pub fn display<'a>(&'a self, db: &'a dyn ExpandDatabase) -> impl fmt::Display + 'a {

crates/hir-ty/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,10 @@ where
716716

717717
pub fn known_const_to_string(konst: &Const, db: &dyn HirDatabase) -> Option<String> {
718718
if let ConstValue::Concrete(c) = &konst.interned().value {
719+
if let ConstScalar::UnevaluatedConst(_, _) = &c.interned {
720+
// FIXME: stringify the block expression
721+
return None;
722+
}
719723
if c.interned == ConstScalar::Unknown {
720724
return None;
721725
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ impl FunctionBody {
810810
(true, konst.body(), Some(sema.to_def(&konst)?.ty(sema.db)))
811811
},
812812
ast::ConstParam(cp) => {
813-
(true, cp.default_val(), Some(sema.to_def(&cp)?.ty(sema.db)))
813+
(true, cp.default_val()?.expr(), Some(sema.to_def(&cp)?.ty(sema.db)))
814814
},
815815
ast::ConstBlockPat(cbp) => {
816816
let expr = cbp.block_expr().map(ast::Expr::BlockExpr);

crates/ide-db/src/path_transform.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,10 @@ impl<'a> PathTransform<'a> {
160160
}
161161
(Either::Left(k), None) => {
162162
if let Some(default) = k.default(db) {
163-
let default = ast::make::expr_const_value(&default);
164-
const_substs.insert(k, default.syntax().clone_for_update());
165-
// FIXME: transform the default value
163+
if let Some(default) = ast::make::expr_const_value(&default).expr() {
164+
const_substs.insert(k, default.syntax().clone_for_update());
165+
// FIXME: transform the default value
166+
}
166167
}
167168
}
168169
_ => (), // ignore mismatching params

crates/parser/src/grammar/generic_params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn const_param(p: &mut Parser<'_>, m: Marker) {
8989

9090
// test const_param_default_path
9191
// struct A<const N: i32 = i32::MAX>;
92-
generic_args::const_arg_expr(p);
92+
generic_args::const_arg(p);
9393
}
9494

9595
m.complete(p, CONST_PARAM);

crates/syntax/rust.ungram

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ TypeParam =
290290

291291
ConstParam =
292292
Attr* 'const' Name ':' Type
293-
('=' default_val:Expr)?
293+
('=' default_val:ConstArg)?
294294

295295
LifetimeParam =
296296
Attr* Lifetime (':' TypeBoundList?)?

crates/syntax/src/ast/generated/nodes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ impl ConstParam {
695695
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
696696
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
697697
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
698-
pub fn default_val(&self) -> Option<Expr> { support::child(&self.syntax) }
698+
pub fn default_val(&self) -> Option<ConstArg> { support::child(&self.syntax) }
699699
}
700700

701701
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

crates/syntax/src/ast/make.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ pub fn expr_literal(text: &str) -> ast::Literal {
507507
ast_from_text(&format!("fn f() {{ let _ = {text}; }}"))
508508
}
509509

510-
pub fn expr_const_value(text: &str) -> ast::Expr {
510+
pub fn expr_const_value(text: &str) -> ast::ConstArg {
511511
ast_from_text(&format!("trait Foo<const N: usize = {text}> {{}}"))
512512
}
513513

0 commit comments

Comments
 (0)