Parse const generics

Fixes #1574
Fixes #2281
This commit is contained in:
roblabla 2019-12-22 00:38:23 +00:00
parent 90f3b31efc
commit b04d4a88d1
6 changed files with 71 additions and 0 deletions

View file

@ -25,6 +25,7 @@ fn type_param_list(p: &mut Parser) {
match p.current() { match p.current() {
LIFETIME => lifetime_param(p, m), LIFETIME => lifetime_param(p, m),
IDENT => type_param(p, m), IDENT => type_param(p, m),
CONST_KW => type_const_param(p, m),
_ => { _ => {
m.abandon(p); m.abandon(p);
p.err_and_bump("expected type parameter") p.err_and_bump("expected type parameter")
@ -62,6 +63,16 @@ fn type_param(p: &mut Parser, m: Marker) {
m.complete(p, TYPE_PARAM); m.complete(p, TYPE_PARAM);
} }
// test const_param
// struct S<const N: u32>;
fn type_const_param(p: &mut Parser, m: Marker) {
assert!(p.at(CONST_KW));
p.bump(T![const]);
name(p);
types::ascription(p);
m.complete(p, CONST_PARAM);
}
// test type_param_bounds // test type_param_bounds
// struct S<T: 'a + ?Sized + (Copy)>; // struct S<T: 'a + ?Sized + (Copy)>;
pub(super) fn bounds(p: &mut Parser) { pub(super) fn bounds(p: &mut Parser) {

View file

@ -227,6 +227,7 @@ pub enum SyntaxKind {
TYPE_PARAM_LIST, TYPE_PARAM_LIST,
LIFETIME_PARAM, LIFETIME_PARAM,
TYPE_PARAM, TYPE_PARAM,
CONST_PARAM,
TYPE_ARG_LIST, TYPE_ARG_LIST,
LIFETIME_ARG, LIFETIME_ARG,
TYPE_ARG, TYPE_ARG,

View file

@ -551,6 +551,36 @@ impl ConstDef {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[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 struct ContinueExpr {
pub(crate) syntax: SyntaxNode, pub(crate) syntax: SyntaxNode,
} }

View file

@ -243,6 +243,7 @@ Grammar(
"TYPE_PARAM_LIST", "TYPE_PARAM_LIST",
"LIFETIME_PARAM", "LIFETIME_PARAM",
"TYPE_PARAM", "TYPE_PARAM",
"CONST_PARAM",
"TYPE_ARG_LIST", "TYPE_ARG_LIST",
"LIFETIME_ARG", "LIFETIME_ARG",
"TYPE_ARG", "TYPE_ARG",
@ -602,6 +603,10 @@ Grammar(
options: [("default_type", "TypeRef")], options: [("default_type", "TypeRef")],
traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner"], traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner"],
), ),
"ConstParam": (
options: [("default_val", "Expr")],
traits: ["NameOwner", "AttrsOwner", "TypeAscriptionOwner"],
),
"LifetimeParam": ( "LifetimeParam": (
traits: ["AttrsOwner"], traits: ["AttrsOwner"],
), ),

View file

@ -0,0 +1 @@
struct S<const N: u32>;

View file

@ -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"