mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-08 13:02:50 +00:00
syntax update: the default value of ConstParam
turned from Expr
into ConstArg
This commit is contained in:
parent
52b4392724
commit
4ebdc6f052
@ -307,7 +307,7 @@ impl GenericParams {
|
|||||||
let param = ConstParamData {
|
let param = ConstParamData {
|
||||||
name,
|
name,
|
||||||
ty: Interned::new(ty),
|
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());
|
let idx = self.type_or_consts.alloc(param.into());
|
||||||
add_param_attrs(idx.into(), ast::GenericParam::ConstParam(const_param));
|
add_param_attrs(idx.into(), ast::GenericParam::ConstParam(const_param));
|
||||||
|
@ -393,15 +393,15 @@ impl ConstRef {
|
|||||||
Self::Scalar(LiteralConstRef::Unknown)
|
Self::Scalar(LiteralConstRef::Unknown)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn from_default_param_value(
|
pub(crate) fn from_const_param(
|
||||||
_: &LowerCtx<'_>,
|
lower_ctx: &LowerCtx<'_>,
|
||||||
param: ast::ConstParam,
|
param: ast::ConstParam,
|
||||||
) -> Option<Self> {
|
) -> Option<Self> {
|
||||||
if let Some(expr) = param.default_val() {
|
let default = param.default_val();
|
||||||
// FIXME: pass the `ast_id` arg to recognize complex expressions
|
match default {
|
||||||
return Some(Self::from_expr(expr, None));
|
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 {
|
pub fn display<'a>(&'a self, db: &'a dyn ExpandDatabase) -> impl fmt::Display + 'a {
|
||||||
|
@ -723,6 +723,10 @@ where
|
|||||||
|
|
||||||
pub fn known_const_to_string(konst: &Const, db: &dyn HirDatabase) -> Option<String> {
|
pub fn known_const_to_string(konst: &Const, db: &dyn HirDatabase) -> Option<String> {
|
||||||
if let ConstValue::Concrete(c) = &konst.interned().value {
|
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 {
|
if c.interned == ConstScalar::Unknown {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,8 @@ use hir_expand::{name::name, MacroCallKind};
|
|||||||
use hir_ty::{
|
use hir_ty::{
|
||||||
all_super_traits, autoderef,
|
all_super_traits, autoderef,
|
||||||
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
|
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},
|
layout::{Layout as TyLayout, RustcEnumVariantIdx, TagEncoding},
|
||||||
method_resolution::{self, TyFingerprint},
|
method_resolution::{self, TyFingerprint},
|
||||||
mir::{self, interpret_mir},
|
mir::{self, interpret_mir},
|
||||||
|
@ -810,7 +810,7 @@ impl FunctionBody {
|
|||||||
(true, konst.body(), Some(sema.to_def(&konst)?.ty(sema.db)))
|
(true, konst.body(), Some(sema.to_def(&konst)?.ty(sema.db)))
|
||||||
},
|
},
|
||||||
ast::ConstParam(cp) => {
|
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) => {
|
ast::ConstBlockPat(cbp) => {
|
||||||
let expr = cbp.block_expr().map(ast::Expr::BlockExpr);
|
let expr = cbp.block_expr().map(ast::Expr::BlockExpr);
|
||||||
|
@ -160,9 +160,10 @@ impl<'a> PathTransform<'a> {
|
|||||||
}
|
}
|
||||||
(Either::Left(k), None) => {
|
(Either::Left(k), None) => {
|
||||||
if let Some(default) = k.default(db) {
|
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());
|
const_substs.insert(k, default.syntax().clone_for_update());
|
||||||
// FIXME: transform the default value
|
// FIXME: transform the default value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => (), // ignore mismatching params
|
_ => (), // ignore mismatching params
|
||||||
|
@ -88,7 +88,7 @@ fn const_param(p: &mut Parser<'_>, m: Marker) {
|
|||||||
|
|
||||||
// test const_param_default_path
|
// test const_param_default_path
|
||||||
// struct A<const N: i32 = i32::MAX>;
|
// struct A<const N: i32 = i32::MAX>;
|
||||||
generic_args::const_arg_expr(p);
|
generic_args::const_arg(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
m.complete(p, CONST_PARAM);
|
m.complete(p, CONST_PARAM);
|
||||||
|
@ -20,7 +20,8 @@ SOURCE_FILE
|
|||||||
IDENT "i32"
|
IDENT "i32"
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
EQ "="
|
EQ "="
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
|
CONST_ARG
|
||||||
COMMA ","
|
COMMA ","
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
CONST_PARAM
|
CONST_PARAM
|
||||||
@ -37,8 +38,9 @@ SOURCE_FILE
|
|||||||
IDENT "i32"
|
IDENT "i32"
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
EQ "="
|
EQ "="
|
||||||
|
CONST_ARG
|
||||||
R_ANGLE ">"
|
R_ANGLE ">"
|
||||||
SEMICOLON ";"
|
SEMICOLON ";"
|
||||||
WHITESPACE "\n"
|
WHITESPACE "\n"
|
||||||
error 23: expected a generic const argument
|
error 24: expected a generic const argument
|
||||||
error 40: expected a generic const argument
|
error 40: expected a generic const argument
|
||||||
|
@ -21,16 +21,17 @@ SOURCE_FILE
|
|||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
EQ "="
|
EQ "="
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
PATH_EXPR
|
CONST_ARG
|
||||||
PATH
|
PATH_EXPR
|
||||||
PATH
|
PATH
|
||||||
|
PATH
|
||||||
|
PATH_SEGMENT
|
||||||
|
NAME_REF
|
||||||
|
IDENT "i32"
|
||||||
|
COLON2 "::"
|
||||||
PATH_SEGMENT
|
PATH_SEGMENT
|
||||||
NAME_REF
|
NAME_REF
|
||||||
IDENT "i32"
|
IDENT "MAX"
|
||||||
COLON2 "::"
|
|
||||||
PATH_SEGMENT
|
|
||||||
NAME_REF
|
|
||||||
IDENT "MAX"
|
|
||||||
R_ANGLE ">"
|
R_ANGLE ">"
|
||||||
SEMICOLON ";"
|
SEMICOLON ";"
|
||||||
WHITESPACE "\n"
|
WHITESPACE "\n"
|
||||||
|
@ -21,14 +21,15 @@ SOURCE_FILE
|
|||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
EQ "="
|
EQ "="
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
BLOCK_EXPR
|
CONST_ARG
|
||||||
STMT_LIST
|
BLOCK_EXPR
|
||||||
L_CURLY "{"
|
STMT_LIST
|
||||||
WHITESPACE " "
|
L_CURLY "{"
|
||||||
LITERAL
|
WHITESPACE " "
|
||||||
INT_NUMBER "1"
|
LITERAL
|
||||||
WHITESPACE " "
|
INT_NUMBER "1"
|
||||||
R_CURLY "}"
|
WHITESPACE " "
|
||||||
|
R_CURLY "}"
|
||||||
R_ANGLE ">"
|
R_ANGLE ">"
|
||||||
SEMICOLON ";"
|
SEMICOLON ";"
|
||||||
WHITESPACE "\n"
|
WHITESPACE "\n"
|
||||||
|
@ -21,10 +21,11 @@ SOURCE_FILE
|
|||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
EQ "="
|
EQ "="
|
||||||
WHITESPACE " "
|
WHITESPACE " "
|
||||||
PREFIX_EXPR
|
CONST_ARG
|
||||||
MINUS "-"
|
PREFIX_EXPR
|
||||||
LITERAL
|
MINUS "-"
|
||||||
INT_NUMBER "1"
|
LITERAL
|
||||||
|
INT_NUMBER "1"
|
||||||
R_ANGLE ">"
|
R_ANGLE ">"
|
||||||
SEMICOLON ";"
|
SEMICOLON ";"
|
||||||
WHITESPACE "\n"
|
WHITESPACE "\n"
|
||||||
|
@ -296,7 +296,7 @@ TypeParam =
|
|||||||
|
|
||||||
ConstParam =
|
ConstParam =
|
||||||
Attr* 'const' Name ':' Type
|
Attr* 'const' Name ':' Type
|
||||||
('=' default_val:Expr)?
|
('=' default_val:ConstArg)?
|
||||||
|
|
||||||
LifetimeParam =
|
LifetimeParam =
|
||||||
Attr* Lifetime (':' TypeBoundList?)?
|
Attr* Lifetime (':' TypeBoundList?)?
|
||||||
|
@ -709,7 +709,7 @@ impl ConstParam {
|
|||||||
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
|
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
|
||||||
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
|
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
|
||||||
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
|
@ -509,7 +509,7 @@ pub fn expr_literal(text: &str) -> ast::Literal {
|
|||||||
ast_from_text(&format!("fn f() {{ let _ = {text}; }}"))
|
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}> {{}}"))
|
ast_from_text(&format!("trait Foo<const N: usize = {text}> {{}}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user