From b04d4a88d1ba7f04445e807b6a816930b1e9bbf2 Mon Sep 17 00:00:00 2001
From: roblabla <unfiltered@roblab.la>
Date: Sun, 22 Dec 2019 00:38:23 +0000
Subject: Parse const generics

Fixes #1574
Fixes #2281
---
 crates/ra_syntax/src/ast/generated.rs              | 30 ++++++++++++++++++++++
 crates/ra_syntax/src/grammar.ron                   |  5 ++++
 .../test_data/parser/inline/ok/0147_const_param.rs |  1 +
 .../parser/inline/ok/0147_const_param.txt          | 23 +++++++++++++++++
 4 files changed, 59 insertions(+)
 create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.rs
 create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.txt

(limited to 'crates/ra_syntax')

diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 9dd6bd3ea..b917f77fe 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -551,6 +551,36 @@ impl ConstDef {
     }
 }
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub struct ConstParam {
+    pub(crate) syntax: SyntaxNode,
+}
+impl AstNode for ConstParam {
+    fn can_cast(kind: SyntaxKind) -> bool {
+        match kind {
+            CONST_PARAM => true,
+            _ => false,
+        }
+    }
+    fn cast(syntax: SyntaxNode) -> Option<Self> {
+        if Self::can_cast(syntax.kind()) {
+            Some(Self { syntax })
+        } else {
+            None
+        }
+    }
+    fn syntax(&self) -> &SyntaxNode {
+        &self.syntax
+    }
+}
+impl ast::NameOwner for ConstParam {}
+impl ast::AttrsOwner for ConstParam {}
+impl ast::TypeAscriptionOwner for ConstParam {}
+impl ConstParam {
+    pub fn default_val(&self) -> Option<Expr> {
+        AstChildren::new(&self.syntax).next()
+    }
+}
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub struct ContinueExpr {
     pub(crate) syntax: SyntaxNode,
 }
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index 9ffa9095b..d6802b6fb 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -243,6 +243,7 @@ Grammar(
         "TYPE_PARAM_LIST",
         "LIFETIME_PARAM",
         "TYPE_PARAM",
+        "CONST_PARAM",
         "TYPE_ARG_LIST",
         "LIFETIME_ARG",
         "TYPE_ARG",
@@ -602,6 +603,10 @@ Grammar(
             options: [("default_type", "TypeRef")],
             traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner"],
         ),
+        "ConstParam": (
+            options: [("default_val", "Expr")],
+            traits: ["NameOwner", "AttrsOwner", "TypeAscriptionOwner"],
+        ),
         "LifetimeParam": (
             traits: ["AttrsOwner"],
         ),
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.rs b/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.rs
new file mode 100644
index 000000000..8cdb3b703
--- /dev/null
+++ b/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.rs
@@ -0,0 +1 @@
+struct S<const N: u32>;
diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.txt b/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.txt
new file mode 100644
index 000000000..f81de7bac
--- /dev/null
+++ b/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.txt
@@ -0,0 +1,23 @@
+SOURCE_FILE@[0; 24)
+  STRUCT_DEF@[0; 23)
+    STRUCT_KW@[0; 6) "struct"
+    WHITESPACE@[6; 7) " "
+    NAME@[7; 8)
+      IDENT@[7; 8) "S"
+    TYPE_PARAM_LIST@[8; 22)
+      L_ANGLE@[8; 9) "<"
+      CONST_PARAM@[9; 21)
+        CONST_KW@[9; 14) "const"
+        WHITESPACE@[14; 15) " "
+        NAME@[15; 16)
+          IDENT@[15; 16) "N"
+        COLON@[16; 17) ":"
+        WHITESPACE@[17; 18) " "
+        PATH_TYPE@[18; 21)
+          PATH@[18; 21)
+            PATH_SEGMENT@[18; 21)
+              NAME_REF@[18; 21)
+                IDENT@[18; 21) "u32"
+      R_ANGLE@[21; 22) ">"
+    SEMI@[22; 23) ";"
+  WHITESPACE@[23; 24) "\n"
-- 
cgit v1.2.3