Use ConstArg instead of Expr for AstId of InTypeConstId

This commit is contained in:
hkalbasi 2023-06-08 23:47:45 +03:30
parent d9136df9e5
commit f8594f78bb
13 changed files with 29 additions and 28 deletions

View File

@ -156,7 +156,7 @@ impl Body {
(src.file_id, variant.expr(), false)
}
DefWithBodyId::InTypeConstId(c) => {
(c.lookup(db).0.file_id, Some(c.source(db)), false)
(c.lookup(db).0.file_id, c.source(db).expr(), false)
}
}
};

View File

@ -67,7 +67,7 @@ pub trait InternDatabase: SourceDatabase {
#[salsa::interned]
fn intern_in_type_const(
&self,
id: (AstId<ast::Expr>, TypeOwnerId, Box<dyn OpaqueInternableThing>),
id: (AstId<ast::ConstArg>, TypeOwnerId, Box<dyn OpaqueInternableThing>),
) -> InTypeConstId;
}

View File

@ -186,11 +186,7 @@ impl TypeRef {
TypeRef::RawPtr(Box::new(inner_ty), mutability)
}
ast::Type::ArrayType(inner) => {
// FIXME: This is a hack. We should probably reuse the machinery of
// `hir_def::body::lower` to lower this into an `Expr` and then evaluate it at the
// `hir_ty` level, which would allow knowing the type of:
// let v: [u8; 2 + 2] = [0u8; 4];
let len = ConstRef::from_expr_opt(ctx, inner.expr());
let len = ConstRef::from_const_arg(ctx, inner.const_arg());
TypeRef::Array(Box::new(TypeRef::from_ast_opt(ctx, inner.ty())), len)
}
ast::Type::SliceType(inner) => {
@ -383,18 +379,18 @@ impl TypeBound {
pub enum ConstRef {
Scalar(LiteralConstRef),
Path(Name),
Complex(AstId<ast::Expr>),
Complex(AstId<ast::ConstArg>),
}
impl ConstRef {
pub(crate) fn from_expr_opt(lower_ctx: &LowerCtx<'_>, expr: Option<ast::Expr>) -> Self {
match expr {
Some(x) => {
let ast_id = lower_ctx.ast_id(&x);
Self::from_expr(x, ast_id)
pub(crate) fn from_const_arg(lower_ctx: &LowerCtx<'_>, arg: Option<ast::ConstArg>) -> Self {
if let Some(arg) = arg {
let ast_id = lower_ctx.ast_id(&arg);
if let Some(expr) = arg.expr() {
return Self::from_expr(expr, ast_id);
}
None => Self::Scalar(LiteralConstRef::Unknown),
}
Self::Scalar(LiteralConstRef::Unknown)
}
pub fn display<'a>(&'a self, db: &'a dyn ExpandDatabase) -> impl fmt::Display + 'a {
@ -412,7 +408,7 @@ impl ConstRef {
}
// We special case literals and single identifiers, to speed up things.
fn from_expr(expr: ast::Expr, ast_id: Option<AstId<ast::Expr>>) -> Self {
fn from_expr(expr: ast::Expr, ast_id: Option<AstId<ast::ConstArg>>) -> Self {
fn is_path_ident(p: &ast::PathExpr) -> bool {
let Some(path) = p.path() else {
return false;

View File

@ -537,11 +537,11 @@ impl Clone for Box<dyn OpaqueInternableThing> {
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct InTypeConstId(InternId);
type InTypeConstLoc = (AstId<ast::Expr>, TypeOwnerId, Box<dyn OpaqueInternableThing>);
type InTypeConstLoc = (AstId<ast::ConstArg>, TypeOwnerId, Box<dyn OpaqueInternableThing>);
impl_intern!(InTypeConstId, InTypeConstLoc, intern_in_type_const, lookup_intern_in_type_const);
impl InTypeConstId {
pub fn source(&self, db: &dyn db::DefDatabase) -> ast::Expr {
pub fn source(&self, db: &dyn db::DefDatabase) -> ast::ConstArg {
let src = self.lookup(db).0;
let file_id = src.file_id;
let root = &db.parse_or_expand(file_id);

View File

@ -217,7 +217,7 @@ pub(super) fn lower_generic_args(
}
}
ast::GenericArg::ConstArg(arg) => {
let arg = ConstRef::from_expr_opt(lower_ctx, arg.expr());
let arg = ConstRef::from_const_arg(lower_ctx, Some(arg));
args.push(GenericArg::Const(arg))
}
}

View File

@ -98,7 +98,7 @@ impl AstIdMap {
|| ast::Variant::can_cast(kind)
|| ast::RecordField::can_cast(kind)
|| ast::TupleField::can_cast(kind)
|| ast::Expr::can_cast(kind)
|| ast::ConstArg::can_cast(kind)
{
res.alloc(&it);
true

View File

@ -158,7 +158,7 @@ fn collect_used_generics<'gp>(
.and_then(|lt| known_generics.iter().find(find_lifetime(&lt.text()))),
),
ast::Type::ArrayType(ar) => {
if let Some(ast::Expr::PathExpr(p)) = ar.expr() {
if let Some(ast::Expr::PathExpr(p)) = ar.const_arg().and_then(|x| x.expr()) {
if let Some(path) = p.path() {
if let Some(name_ref) = path.as_single_name_ref() {
if let Some(param) = known_generics.iter().find(|gp| {

View File

@ -153,7 +153,9 @@ fn array_or_slice_type(p: &mut Parser<'_>) {
// type T = [(); 92];
T![;] => {
p.bump(T![;]);
let m = p.start();
expressions::expr(p);
m.complete(p, CONST_ARG);
p.expect(T![']']);
ARRAY_TYPE
}

View File

@ -14,8 +14,9 @@ SOURCE_FILE
R_PAREN ")"
SEMICOLON ";"
WHITESPACE " "
LITERAL
INT_NUMBER "92"
CONST_ARG
LITERAL
INT_NUMBER "92"
R_BRACK "]"
SEMICOLON ";"
WHITESPACE "\n"

View File

@ -51,8 +51,9 @@ SOURCE_FILE
IDENT "i32"
SEMICOLON ";"
WHITESPACE " "
LITERAL
INT_NUMBER "1"
CONST_ARG
LITERAL
INT_NUMBER "1"
R_BRACK "]"
R_PAREN ")"
SEMICOLON ";"

View File

@ -24,8 +24,9 @@ SOURCE_FILE
IDENT "u8"
SEMICOLON ";"
WHITESPACE " "
LITERAL
INT_NUMBER "1"
CONST_ARG
LITERAL
INT_NUMBER "1"
R_BRACK "]"
WHITESPACE " "
R_CURLY "}"

View File

@ -565,7 +565,7 @@ RefType =
'&' Lifetime? 'mut'? Type
ArrayType =
'[' Type ';' Expr ']'
'[' Type ';' ConstArg ']'
SliceType =
'[' Type ']'

View File

@ -1207,7 +1207,7 @@ impl ArrayType {
pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) }
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn const_arg(&self) -> Option<ConstArg> { support::child(&self.syntax) }
pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) }
}