about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhkalbasi <hamidrezakalbasi@protonmail.com>2023-06-08 23:47:45 +0330
committerhkalbasi <hamidrezakalbasi@protonmail.com>2023-06-11 00:39:28 +0330
commitf8594f78bbb424e240120f02f423a81568b7d037 (patch)
treee5274f1f09af7c8d76b9d37cee2ecaa47f576f1a
parentd9136df9e56f8a6cb306e0f1df938edc9b864e33 (diff)
downloadrust-f8594f78bbb424e240120f02f423a81568b7d037.tar.gz
rust-f8594f78bbb424e240120f02f423a81568b7d037.zip
Use `ConstArg` instead of `Expr` for `AstId` of `InTypeConstId`
-rw-r--r--crates/hir-def/src/body.rs2
-rw-r--r--crates/hir-def/src/db.rs2
-rw-r--r--crates/hir-def/src/hir/type_ref.rs22
-rw-r--r--crates/hir-def/src/lib.rs4
-rw-r--r--crates/hir-def/src/path/lower.rs2
-rw-r--r--crates/hir-expand/src/ast_id_map.rs2
-rw-r--r--crates/ide-assists/src/handlers/extract_type_alias.rs2
-rw-r--r--crates/parser/src/grammar/types.rs2
-rw-r--r--crates/parser/test_data/parser/inline/ok/0017_array_type.rast5
-rw-r--r--crates/parser/test_data/parser/ok/0030_traits.rast5
-rw-r--r--crates/parser/test_data/parser/ok/0043_complex_assignment.rast5
-rw-r--r--crates/syntax/rust.ungram2
-rw-r--r--crates/syntax/src/ast/generated/nodes.rs2
13 files changed, 29 insertions, 28 deletions
diff --git a/crates/hir-def/src/body.rs b/crates/hir-def/src/body.rs
index 28a45008191..3ed7dfefc0f 100644
--- a/crates/hir-def/src/body.rs
+++ b/crates/hir-def/src/body.rs
@@ -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)
                 }
             }
         };
diff --git a/crates/hir-def/src/db.rs b/crates/hir-def/src/db.rs
index 28e76fb5a82..b1a2d2028a8 100644
--- a/crates/hir-def/src/db.rs
+++ b/crates/hir-def/src/db.rs
@@ -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;
 }
 
diff --git a/crates/hir-def/src/hir/type_ref.rs b/crates/hir-def/src/hir/type_ref.rs
index b4451c0426e..fa1f4933a26 100644
--- a/crates/hir-def/src/hir/type_ref.rs
+++ b/crates/hir-def/src/hir/type_ref.rs
@@ -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;
diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs
index e90572ec97d..06b5dde1fc4 100644
--- a/crates/hir-def/src/lib.rs
+++ b/crates/hir-def/src/lib.rs
@@ -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);
diff --git a/crates/hir-def/src/path/lower.rs b/crates/hir-def/src/path/lower.rs
index c8f6526ee86..1cb17ff0d26 100644
--- a/crates/hir-def/src/path/lower.rs
+++ b/crates/hir-def/src/path/lower.rs
@@ -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))
             }
         }
diff --git a/crates/hir-expand/src/ast_id_map.rs b/crates/hir-expand/src/ast_id_map.rs
index 023a9c86777..02efaace437 100644
--- a/crates/hir-expand/src/ast_id_map.rs
+++ b/crates/hir-expand/src/ast_id_map.rs
@@ -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
diff --git a/crates/ide-assists/src/handlers/extract_type_alias.rs b/crates/ide-assists/src/handlers/extract_type_alias.rs
index 71b45d88cb8..b6e7d6209c9 100644
--- a/crates/ide-assists/src/handlers/extract_type_alias.rs
+++ b/crates/ide-assists/src/handlers/extract_type_alias.rs
@@ -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| {
diff --git a/crates/parser/src/grammar/types.rs b/crates/parser/src/grammar/types.rs
index 93ef4835027..96a6cdeaaff 100644
--- a/crates/parser/src/grammar/types.rs
+++ b/crates/parser/src/grammar/types.rs
@@ -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
         }
diff --git a/crates/parser/test_data/parser/inline/ok/0017_array_type.rast b/crates/parser/test_data/parser/inline/ok/0017_array_type.rast
index 2a5c644d467..0d50144b730 100644
--- a/crates/parser/test_data/parser/inline/ok/0017_array_type.rast
+++ b/crates/parser/test_data/parser/inline/ok/0017_array_type.rast
@@ -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"
diff --git a/crates/parser/test_data/parser/ok/0030_traits.rast b/crates/parser/test_data/parser/ok/0030_traits.rast
index 44423581e6a..3965ae9596b 100644
--- a/crates/parser/test_data/parser/ok/0030_traits.rast
+++ b/crates/parser/test_data/parser/ok/0030_traits.rast
@@ -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 ";"
diff --git a/crates/parser/test_data/parser/ok/0043_complex_assignment.rast b/crates/parser/test_data/parser/ok/0043_complex_assignment.rast
index 3b02c3f96ae..f3c85b45b6b 100644
--- a/crates/parser/test_data/parser/ok/0043_complex_assignment.rast
+++ b/crates/parser/test_data/parser/ok/0043_complex_assignment.rast
@@ -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 "}"
diff --git a/crates/syntax/rust.ungram b/crates/syntax/rust.ungram
index 4c9027dec68..b096c997448 100644
--- a/crates/syntax/rust.ungram
+++ b/crates/syntax/rust.ungram
@@ -565,7 +565,7 @@ RefType =
   '&' Lifetime? 'mut'? Type
 
 ArrayType =
-  '[' Type ';' Expr ']'
+  '[' Type ';' ConstArg ']'
 
 SliceType =
   '[' Type ']'
diff --git a/crates/syntax/src/ast/generated/nodes.rs b/crates/syntax/src/ast/generated/nodes.rs
index 61f6a04c98d..e520801ea2e 100644
--- a/crates/syntax/src/ast/generated/nodes.rs
+++ b/crates/syntax/src/ast/generated/nodes.rs
@@ -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![']']) }
 }