syntax update: the default value of ConstParam turned from Expr into ConstArg

This commit is contained in:
ponyii 2023-07-10 17:56:07 +04:00
parent 52b4392724
commit 4ebdc6f052
14 changed files with 48 additions and 37 deletions

View file

@ -307,7 +307,7 @@ impl GenericParams {
let param = ConstParamData {
name,
ty: Interned::new(ty),
default: ConstRef::from_default_param_value(lower_ctx, const_param),
default: ConstRef::from_const_param(lower_ctx, const_param),
};
let idx = self.type_or_consts.alloc(param.into());
add_param_attrs(idx.into(), ast::GenericParam::ConstParam(const_param));

View file

@ -393,15 +393,15 @@ impl ConstRef {
Self::Scalar(LiteralConstRef::Unknown)
}
pub(crate) fn from_default_param_value(
_: &LowerCtx<'_>,
pub(crate) fn from_const_param(
lower_ctx: &LowerCtx<'_>,
param: ast::ConstParam,
) -> Option<Self> {
if let Some(expr) = param.default_val() {
// FIXME: pass the `ast_id` arg to recognize complex expressions
return Some(Self::from_expr(expr, None));
let default = param.default_val();
match default {
Some(_) => Some(Self::from_const_arg(lower_ctx, default)),
None => None,
}
None
}
pub fn display<'a>(&'a self, db: &'a dyn ExpandDatabase) -> impl fmt::Display + 'a {

View file

@ -723,6 +723,10 @@ 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;
}

View file

@ -62,7 +62,8 @@ use hir_expand::{name::name, MacroCallKind};
use hir_ty::{
all_super_traits, autoderef,
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
diagnostics::BodyValidationDiagnostic, known_const_to_string,
diagnostics::BodyValidationDiagnostic,
known_const_to_string,
layout::{Layout as TyLayout, RustcEnumVariantIdx, TagEncoding},
method_resolution::{self, TyFingerprint},
mir::{self, interpret_mir},

View file

@ -810,7 +810,7 @@ impl FunctionBody {
(true, konst.body(), Some(sema.to_def(&konst)?.ty(sema.db)))
},
ast::ConstParam(cp) => {
(true, cp.default_val(), Some(sema.to_def(&cp)?.ty(sema.db)))
(true, cp.default_val()?.expr(), Some(sema.to_def(&cp)?.ty(sema.db)))
},
ast::ConstBlockPat(cbp) => {
let expr = cbp.block_expr().map(ast::Expr::BlockExpr);

View file

@ -160,11 +160,12 @@ impl<'a> PathTransform<'a> {
}
(Either::Left(k), None) => {
if let Some(default) = k.default(db) {
let default = ast::make::expr_const_value(&default);
if let Some(default) = ast::make::expr_const_value(&default).expr() {
const_substs.insert(k, default.syntax().clone_for_update());
// FIXME: transform the default value
}
}
}
_ => (), // ignore mismatching params
});
let lifetime_substs: FxHashMap<_, _> = self

View file

@ -88,7 +88,7 @@ fn const_param(p: &mut Parser<'_>, m: Marker) {
// test const_param_default_path
// struct A<const N: i32 = i32::MAX>;
generic_args::const_arg_expr(p);
generic_args::const_arg(p);
}
m.complete(p, CONST_PARAM);

View file

@ -21,6 +21,7 @@ SOURCE_FILE
WHITESPACE " "
EQ "="
WHITESPACE " "
CONST_ARG
COMMA ","
WHITESPACE " "
CONST_PARAM
@ -37,8 +38,9 @@ SOURCE_FILE
IDENT "i32"
WHITESPACE " "
EQ "="
CONST_ARG
R_ANGLE ">"
SEMICOLON ";"
WHITESPACE "\n"
error 23: expected a generic const argument
error 24: expected a generic const argument
error 40: expected a generic const argument

View file

@ -21,6 +21,7 @@ SOURCE_FILE
WHITESPACE " "
EQ "="
WHITESPACE " "
CONST_ARG
PATH_EXPR
PATH
PATH

View file

@ -21,6 +21,7 @@ SOURCE_FILE
WHITESPACE " "
EQ "="
WHITESPACE " "
CONST_ARG
BLOCK_EXPR
STMT_LIST
L_CURLY "{"

View file

@ -21,6 +21,7 @@ SOURCE_FILE
WHITESPACE " "
EQ "="
WHITESPACE " "
CONST_ARG
PREFIX_EXPR
MINUS "-"
LITERAL

View file

@ -296,7 +296,7 @@ TypeParam =
ConstParam =
Attr* 'const' Name ':' Type
('=' default_val:Expr)?
('=' default_val:ConstArg)?
LifetimeParam =
Attr* Lifetime (':' TypeBoundList?)?

View file

@ -709,7 +709,7 @@ impl ConstParam {
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
pub fn default_val(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn default_val(&self) -> Option<ConstArg> { support::child(&self.syntax) }
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]

View file

@ -509,7 +509,7 @@ pub fn expr_literal(text: &str) -> ast::Literal {
ast_from_text(&format!("fn f() {{ let _ = {text}; }}"))
}
pub fn expr_const_value(text: &str) -> ast::Expr {
pub fn expr_const_value(text: &str) -> ast::ConstArg {
ast_from_text(&format!("trait Foo<const N: usize = {text}> {{}}"))
}