Skip to content

Commit f8594f7

Browse files
committed
Use ConstArg instead of Expr for AstId of InTypeConstId
1 parent d9136df commit f8594f7

File tree

13 files changed

+29
-28
lines changed

13 files changed

+29
-28
lines changed

crates/hir-def/src/body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl Body {
156156
(src.file_id, variant.expr(), false)
157157
}
158158
DefWithBodyId::InTypeConstId(c) => {
159-
(c.lookup(db).0.file_id, Some(c.source(db)), false)
159+
(c.lookup(db).0.file_id, c.source(db).expr(), false)
160160
}
161161
}
162162
};

crates/hir-def/src/db.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub trait InternDatabase: SourceDatabase {
6767
#[salsa::interned]
6868
fn intern_in_type_const(
6969
&self,
70-
id: (AstId<ast::Expr>, TypeOwnerId, Box<dyn OpaqueInternableThing>),
70+
id: (AstId<ast::ConstArg>, TypeOwnerId, Box<dyn OpaqueInternableThing>),
7171
) -> InTypeConstId;
7272
}
7373

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

+9-13
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,7 @@ impl TypeRef {
186186
TypeRef::RawPtr(Box::new(inner_ty), mutability)
187187
}
188188
ast::Type::ArrayType(inner) => {
189-
// FIXME: This is a hack. We should probably reuse the machinery of
190-
// `hir_def::body::lower` to lower this into an `Expr` and then evaluate it at the
191-
// `hir_ty` level, which would allow knowing the type of:
192-
// let v: [u8; 2 + 2] = [0u8; 4];
193-
let len = ConstRef::from_expr_opt(ctx, inner.expr());
189+
let len = ConstRef::from_const_arg(ctx, inner.const_arg());
194190
TypeRef::Array(Box::new(TypeRef::from_ast_opt(ctx, inner.ty())), len)
195191
}
196192
ast::Type::SliceType(inner) => {
@@ -383,18 +379,18 @@ impl TypeBound {
383379
pub enum ConstRef {
384380
Scalar(LiteralConstRef),
385381
Path(Name),
386-
Complex(AstId<ast::Expr>),
382+
Complex(AstId<ast::ConstArg>),
387383
}
388384

389385
impl ConstRef {
390-
pub(crate) fn from_expr_opt(lower_ctx: &LowerCtx<'_>, expr: Option<ast::Expr>) -> Self {
391-
match expr {
392-
Some(x) => {
393-
let ast_id = lower_ctx.ast_id(&x);
394-
Self::from_expr(x, ast_id)
386+
pub(crate) fn from_const_arg(lower_ctx: &LowerCtx<'_>, arg: Option<ast::ConstArg>) -> Self {
387+
if let Some(arg) = arg {
388+
let ast_id = lower_ctx.ast_id(&arg);
389+
if let Some(expr) = arg.expr() {
390+
return Self::from_expr(expr, ast_id);
395391
}
396-
None => Self::Scalar(LiteralConstRef::Unknown),
397392
}
393+
Self::Scalar(LiteralConstRef::Unknown)
398394
}
399395

400396
pub fn display<'a>(&'a self, db: &'a dyn ExpandDatabase) -> impl fmt::Display + 'a {
@@ -412,7 +408,7 @@ impl ConstRef {
412408
}
413409

414410
// We special case literals and single identifiers, to speed up things.
415-
fn from_expr(expr: ast::Expr, ast_id: Option<AstId<ast::Expr>>) -> Self {
411+
fn from_expr(expr: ast::Expr, ast_id: Option<AstId<ast::ConstArg>>) -> Self {
416412
fn is_path_ident(p: &ast::PathExpr) -> bool {
417413
let Some(path) = p.path() else {
418414
return false;

crates/hir-def/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -537,11 +537,11 @@ impl Clone for Box<dyn OpaqueInternableThing> {
537537

538538
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
539539
pub struct InTypeConstId(InternId);
540-
type InTypeConstLoc = (AstId<ast::Expr>, TypeOwnerId, Box<dyn OpaqueInternableThing>);
540+
type InTypeConstLoc = (AstId<ast::ConstArg>, TypeOwnerId, Box<dyn OpaqueInternableThing>);
541541
impl_intern!(InTypeConstId, InTypeConstLoc, intern_in_type_const, lookup_intern_in_type_const);
542542

543543
impl InTypeConstId {
544-
pub fn source(&self, db: &dyn db::DefDatabase) -> ast::Expr {
544+
pub fn source(&self, db: &dyn db::DefDatabase) -> ast::ConstArg {
545545
let src = self.lookup(db).0;
546546
let file_id = src.file_id;
547547
let root = &db.parse_or_expand(file_id);

crates/hir-def/src/path/lower.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ pub(super) fn lower_generic_args(
217217
}
218218
}
219219
ast::GenericArg::ConstArg(arg) => {
220-
let arg = ConstRef::from_expr_opt(lower_ctx, arg.expr());
220+
let arg = ConstRef::from_const_arg(lower_ctx, Some(arg));
221221
args.push(GenericArg::Const(arg))
222222
}
223223
}

crates/hir-expand/src/ast_id_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl AstIdMap {
9898
|| ast::Variant::can_cast(kind)
9999
|| ast::RecordField::can_cast(kind)
100100
|| ast::TupleField::can_cast(kind)
101-
|| ast::Expr::can_cast(kind)
101+
|| ast::ConstArg::can_cast(kind)
102102
{
103103
res.alloc(&it);
104104
true

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn collect_used_generics<'gp>(
158158
.and_then(|lt| known_generics.iter().find(find_lifetime(&lt.text()))),
159159
),
160160
ast::Type::ArrayType(ar) => {
161-
if let Some(ast::Expr::PathExpr(p)) = ar.expr() {
161+
if let Some(ast::Expr::PathExpr(p)) = ar.const_arg().and_then(|x| x.expr()) {
162162
if let Some(path) = p.path() {
163163
if let Some(name_ref) = path.as_single_name_ref() {
164164
if let Some(param) = known_generics.iter().find(|gp| {

crates/parser/src/grammar/types.rs

+2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ fn array_or_slice_type(p: &mut Parser<'_>) {
153153
// type T = [(); 92];
154154
T![;] => {
155155
p.bump(T![;]);
156+
let m = p.start();
156157
expressions::expr(p);
158+
m.complete(p, CONST_ARG);
157159
p.expect(T![']']);
158160
ARRAY_TYPE
159161
}

crates/parser/test_data/parser/inline/ok/0017_array_type.rast

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ SOURCE_FILE
1414
R_PAREN ")"
1515
SEMICOLON ";"
1616
WHITESPACE " "
17-
LITERAL
18-
INT_NUMBER "92"
17+
CONST_ARG
18+
LITERAL
19+
INT_NUMBER "92"
1920
R_BRACK "]"
2021
SEMICOLON ";"
2122
WHITESPACE "\n"

crates/parser/test_data/parser/ok/0030_traits.rast

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ SOURCE_FILE
5151
IDENT "i32"
5252
SEMICOLON ";"
5353
WHITESPACE " "
54-
LITERAL
55-
INT_NUMBER "1"
54+
CONST_ARG
55+
LITERAL
56+
INT_NUMBER "1"
5657
R_BRACK "]"
5758
R_PAREN ")"
5859
SEMICOLON ";"

crates/parser/test_data/parser/ok/0043_complex_assignment.rast

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ SOURCE_FILE
2424
IDENT "u8"
2525
SEMICOLON ";"
2626
WHITESPACE " "
27-
LITERAL
28-
INT_NUMBER "1"
27+
CONST_ARG
28+
LITERAL
29+
INT_NUMBER "1"
2930
R_BRACK "]"
3031
WHITESPACE " "
3132
R_CURLY "}"

crates/syntax/rust.ungram

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ RefType =
565565
'&' Lifetime? 'mut'? Type
566566

567567
ArrayType =
568-
'[' Type ';' Expr ']'
568+
'[' Type ';' ConstArg ']'
569569

570570
SliceType =
571571
'[' Type ']'

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ impl ArrayType {
12071207
pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) }
12081208
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
12091209
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
1210-
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
1210+
pub fn const_arg(&self) -> Option<ConstArg> { support::child(&self.syntax) }
12111211
pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) }
12121212
}
12131213

0 commit comments

Comments
 (0)