Skip to content

Commit 7d171b6

Browse files
committed
Parse mGCA's type-level const items
1 parent 149ee72 commit 7d171b6

4 files changed

Lines changed: 34 additions & 12 deletions

File tree

src/ast/item.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,20 @@ pub(crate) enum ItemKind<'src> {
3939
#[derive(Debug)]
4040
pub(crate) struct ConstItem<'src> {
4141
pub(crate) defaultness: Defaultness,
42+
pub(crate) tyness: Tyness,
4243
pub(crate) binder: Ident<'src>,
4344
pub(crate) generics: Generics<'src>,
4445
pub(crate) ty: Ty<'src>,
4546
pub(crate) body: Option<Expr<'src>>,
4647
}
4748

49+
// FIXME: horrible name
50+
#[derive(Debug)]
51+
pub(crate) enum Tyness {
52+
Ty,
53+
Not,
54+
}
55+
4856
#[derive(Debug)]
4957
pub(crate) struct ConstBlockItem<'src> {
5058
pub(crate) body: BlockExpr<'src>,

src/fmter/item.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,13 @@ impl Fmt for (ast::ItemKind<'_>, Vec<ast::Attr<'_, ast::attr::Inner>>) {
6060

6161
impl Fmt for ast::ConstItem<'_> {
6262
fn fmt(self, cx: &mut Cx<'_>) {
63-
let Self { defaultness, binder, generics, ty, body } = self;
63+
let Self { defaultness, tyness, binder, generics, ty, body } = self;
6464

6565
defaultness.trailing_space().fmt(cx);
66+
match tyness {
67+
ast::Tyness::Ty => fmt!(cx, "type "),
68+
ast::Tyness::Not => {}
69+
}
6670
fmt!(cx, "const {binder}");
6771
if !generics.params.is_empty() {
6872
generics.params.fmt(cx);

src/parser/item.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ impl<'src> Parser<'_, '_, 'src> {
9090
| TokenKind::Macro
9191
| TokenKind::Struct
9292
| TokenKind::Trait
93-
| TokenKind::Type
9493
| TokenKind::Use => return true,
9594
TokenKind::CommonIdent => match self.source(self.token.span) {
9695
weak::Union::STR => return weak::Union.qualifies(self),
@@ -146,13 +145,22 @@ impl<'src> Parser<'_, '_, 'src> {
146145
// FIXME: Provide more targeted diagnostics if the qualifiers don't make sense.
147146
match qualifiers.as_slice() {
148147
[] => {}
149-
[Qualifier::Const] => {
150-
return if self.consume(TokenKind::OpenCurlyBracket) {
151-
// FEATURE: `const_block_items` <https://github.com/rust-lang/rust/issues/149226>
152-
self.fin_parse_const_block_item()
153-
} else {
154-
self.fin_parse_const_item(defaultness)
148+
[Qualifier::Type] => return self.fin_parse_ty_alias_item(defaultness),
149+
// FEATURE: `const_block_items` <https://github.com/rust-lang/rust/issues/149226>
150+
[Qualifier::Const] if self.consume(TokenKind::OpenCurlyBracket) => {
151+
return self.fin_parse_const_block_item();
152+
}
153+
[qualifiers @ .., Qualifier::Const] => {
154+
// FEATURE: `min_generic_const_args` <https://github.com/rust-lang/rust/issues/132980>
155+
let (tyness, qualifiers) = match qualifiers {
156+
[Qualifier::Type, qualifiers @ ..] => (ast::Tyness::Ty, qualifiers),
157+
_ => (ast::Tyness::Not, qualifiers),
155158
};
159+
if !qualifiers.is_empty() {
160+
return self.fatal(Error::InvalidItemPrefix(start.until(self.token.span)));
161+
}
162+
163+
return self.fin_parse_const_item(defaultness, tyness);
156164
}
157165
// `crate` can't be a qualifier itself because it may also begin paths & it's not worth the look-ahead.
158166
[Qualifier::Extern(None)] if self.consume(TokenKind::Crate) => {
@@ -265,10 +273,6 @@ impl<'src> Parser<'_, '_, 'src> {
265273
self.advance();
266274
return self.fin_parse_struct_item();
267275
}
268-
TokenKind::Type => {
269-
self.advance();
270-
return self.fin_parse_ty_alias_item(defaultness);
271-
}
272276
TokenKind::Use => {
273277
self.advance();
274278
return self.fin_parse_use_item();
@@ -333,6 +337,7 @@ impl<'src> Parser<'_, '_, 'src> {
333337
TokenKind::Mod => Qualifier::Mod,
334338
TokenKind::Static => Qualifier::Static,
335339
TokenKind::Trait => Qualifier::Trait,
340+
TokenKind::Type => Qualifier::Type,
336341
TokenKind::Unsafe if self.peek(1).kind != TokenKind::OpenCurlyBracket => {
337342
Qualifier::Unsafe
338343
}
@@ -359,6 +364,7 @@ impl<'src> Parser<'_, '_, 'src> {
359364
fn fin_parse_const_item(
360365
&mut self,
361366
defaultness: ast::Defaultness,
367+
tyness: ast::Tyness,
362368
) -> Result<ast::ItemKind<'src>> {
363369
let (binder, _) = self.parse_common_ident_or(TokenKind::Underscore)?;
364370
// FEATURE: `generic_const_items` <https://github.com/rust-lang/rust/issues/113521>
@@ -371,6 +377,7 @@ impl<'src> Parser<'_, '_, 'src> {
371377

372378
Ok(ast::ItemKind::Const(Box::new(ast::ConstItem {
373379
defaultness,
380+
tyness,
374381
binder,
375382
generics: ast::Generics { params, preds },
376383
ty,
@@ -1101,6 +1108,7 @@ enum Qualifier<'src> {
11011108
Safe,
11021109
Static,
11031110
Trait,
1111+
Type,
11041112
Unsafe,
11051113
}
11061114

src/parser/path.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ impl<'src> Parser<'_, '_, 'src> {
219219
self.advance();
220220
Ok(ast::ExprKind::Path(Box::new(ast::ExtPath::ident(ident))).into())
221221
}
222+
// FEATURE: `min_generic_const_args` <https://github.com/rust-lang/rust/issues/132980>
222223
TokenKind::Const => {
223224
self.advance();
224225
let block = self.parse_block_expr()?;
@@ -237,6 +238,7 @@ impl<'src> Parser<'_, '_, 'src> {
237238
fn begins_const_arg(&self) -> bool {
238239
// NOTE: To be kept in sync with `Self::parse_const_arg`.
239240

241+
// Re. `const`, FEATURE: `min_generic_const_args` <https://github.com/rust-lang/rust/issues/132980>
240242
matches!(self.token.kind, TokenKind::OpenCurlyBracket | TokenKind::Const)
241243
|| self.begins_negatable_lit()
242244
}

0 commit comments

Comments
 (0)