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

Commit 4ebdc6f

Browse files
committed
syntax update: the default value of ConstParam turned from Expr into ConstArg
1 parent 52b4392 commit 4ebdc6f

File tree

14 files changed

+48
-37
lines changed

14 files changed

+48
-37
lines changed

crates/hir-def/src/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ impl GenericParams {
307307
let param = ConstParamData {
308308
name,
309309
ty: Interned::new(ty),
310-
default: ConstRef::from_default_param_value(lower_ctx, const_param),
310+
default: ConstRef::from_const_param(lower_ctx, const_param),
311311
};
312312
let idx = self.type_or_consts.alloc(param.into());
313313
add_param_attrs(idx.into(), ast::GenericParam::ConstParam(const_param));

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

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

396-
pub(crate) fn from_default_param_value(
397-
_: &LowerCtx<'_>,
396+
pub(crate) fn from_const_param(
397+
lower_ctx: &LowerCtx<'_>,
398398
param: ast::ConstParam,
399399
) -> 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));
400+
let default = param.default_val();
401+
match default {
402+
Some(_) => Some(Self::from_const_arg(lower_ctx, default)),
403+
None => None,
403404
}
404-
None
405405
}
406406

407407
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
@@ -723,6 +723,10 @@ where
723723

724724
pub fn known_const_to_string(konst: &Const, db: &dyn HirDatabase) -> Option<String> {
725725
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+
}
726730
if c.interned == ConstScalar::Unknown {
727731
return None;
728732
}

crates/hir/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ use hir_expand::{name::name, MacroCallKind};
6262
use hir_ty::{
6363
all_super_traits, autoderef,
6464
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
65-
diagnostics::BodyValidationDiagnostic, known_const_to_string,
65+
diagnostics::BodyValidationDiagnostic,
66+
known_const_to_string,
6667
layout::{Layout as TyLayout, RustcEnumVariantIdx, TagEncoding},
6768
method_resolution::{self, TyFingerprint},
6869
mir::{self, interpret_mir},

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
@@ -88,7 +88,7 @@ fn const_param(p: &mut Parser<'_>, m: Marker) {
8888

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

9494
m.complete(p, CONST_PARAM);

crates/parser/test_data/parser/inline/err/0022_recover_from_missing_const_default.rast

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ SOURCE_FILE
2020
IDENT "i32"
2121
WHITESPACE " "
2222
EQ "="
23-
WHITESPACE " "
23+
WHITESPACE " "
24+
CONST_ARG
2425
COMMA ","
2526
WHITESPACE " "
2627
CONST_PARAM
@@ -37,8 +38,9 @@ SOURCE_FILE
3738
IDENT "i32"
3839
WHITESPACE " "
3940
EQ "="
41+
CONST_ARG
4042
R_ANGLE ">"
4143
SEMICOLON ";"
4244
WHITESPACE "\n"
43-
error 23: expected a generic const argument
45+
error 24: expected a generic const argument
4446
error 40: expected a generic const argument

crates/parser/test_data/parser/inline/ok/0188_const_param_default_path.rast

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ SOURCE_FILE
2121
WHITESPACE " "
2222
EQ "="
2323
WHITESPACE " "
24-
PATH_EXPR
25-
PATH
24+
CONST_ARG
25+
PATH_EXPR
2626
PATH
27+
PATH
28+
PATH_SEGMENT
29+
NAME_REF
30+
IDENT "i32"
31+
COLON2 "::"
2732
PATH_SEGMENT
2833
NAME_REF
29-
IDENT "i32"
30-
COLON2 "::"
31-
PATH_SEGMENT
32-
NAME_REF
33-
IDENT "MAX"
34+
IDENT "MAX"
3435
R_ANGLE ">"
3536
SEMICOLON ";"
3637
WHITESPACE "\n"

crates/parser/test_data/parser/inline/ok/0199_const_param_default_expression.rast

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ SOURCE_FILE
2121
WHITESPACE " "
2222
EQ "="
2323
WHITESPACE " "
24-
BLOCK_EXPR
25-
STMT_LIST
26-
L_CURLY "{"
27-
WHITESPACE " "
28-
LITERAL
29-
INT_NUMBER "1"
30-
WHITESPACE " "
31-
R_CURLY "}"
24+
CONST_ARG
25+
BLOCK_EXPR
26+
STMT_LIST
27+
L_CURLY "{"
28+
WHITESPACE " "
29+
LITERAL
30+
INT_NUMBER "1"
31+
WHITESPACE " "
32+
R_CURLY "}"
3233
R_ANGLE ">"
3334
SEMICOLON ";"
3435
WHITESPACE "\n"

crates/parser/test_data/parser/inline/ok/0200_const_param_default_literal.rast

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ SOURCE_FILE
2121
WHITESPACE " "
2222
EQ "="
2323
WHITESPACE " "
24-
PREFIX_EXPR
25-
MINUS "-"
26-
LITERAL
27-
INT_NUMBER "1"
24+
CONST_ARG
25+
PREFIX_EXPR
26+
MINUS "-"
27+
LITERAL
28+
INT_NUMBER "1"
2829
R_ANGLE ">"
2930
SEMICOLON ";"
3031
WHITESPACE "\n"

crates/syntax/rust.ungram

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

297297
ConstParam =
298298
Attr* 'const' Name ':' Type
299-
('=' default_val:Expr)?
299+
('=' default_val:ConstArg)?
300300

301301
LifetimeParam =
302302
Attr* Lifetime (':' TypeBoundList?)?

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ impl ConstParam {
709709
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
710710
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
711711
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
712-
pub fn default_val(&self) -> Option<Expr> { support::child(&self.syntax) }
712+
pub fn default_val(&self) -> Option<ConstArg> { support::child(&self.syntax) }
713713
}
714714

715715
#[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
@@ -509,7 +509,7 @@ pub fn expr_literal(text: &str) -> ast::Literal {
509509
ast_from_text(&format!("fn f() {{ let _ = {text}; }}"))
510510
}
511511

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

0 commit comments

Comments
 (0)