Skip to content

Commit d7695ab

Browse files
varkoryodaldevoid
andcommitted
Support const generics in derive
Co-Authored-By: Gabriel Smith <[email protected]>
1 parent 0a8d98a commit d7695ab

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/libsyntax/ext/build.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ pub trait AstBuilder {
3838
bindings: Vec<ast::TypeBinding>)
3939
-> (ast::QSelf, ast::Path);
4040

41-
// types
41+
// types and consts
4242
fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
4343

4444
fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty>;
4545
fn ty_path(&self, path: ast::Path) -> P<ast::Ty>;
4646
fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
47+
fn anon_const(&self, span: Span, expr: ast::ExprKind) -> ast::AnonConst;
48+
fn const_ident(&self, span: Span, idents: ast::Ident) -> ast::AnonConst;
4749

4850
fn ty_rptr(&self, span: Span,
4951
ty: P<ast::Ty>,
@@ -394,6 +396,22 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
394396
self.ty_path(self.path_ident(span, ident))
395397
}
396398

399+
fn anon_const(&self, span: Span, expr: ast::ExprKind) -> ast::AnonConst {
400+
ast::AnonConst {
401+
id: ast::DUMMY_NODE_ID,
402+
value: P(ast::Expr {
403+
id: ast::DUMMY_NODE_ID,
404+
node: expr,
405+
span,
406+
attrs: ThinVec::new(),
407+
})
408+
}
409+
}
410+
411+
fn const_ident(&self, span: Span, ident: ast::Ident) -> ast::AnonConst {
412+
self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
413+
}
414+
397415
fn ty_rptr(&self,
398416
span: Span,
399417
ty: P<ast::Ty>,

src/libsyntax_ext/deriving/generic/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ impl<'a> TraitDef<'a> {
560560

561561
cx.typaram(self.span, param.ident, vec![], bounds, None)
562562
}
563+
GenericParamKind::Const { .. } => param.clone(),
563564
}));
564565

565566
// and similarly for where clauses
@@ -657,6 +658,9 @@ impl<'a> TraitDef<'a> {
657658
GenericParamKind::Type { .. } => {
658659
GenericArg::Type(cx.ty_ident(self.span, param.ident))
659660
}
661+
GenericParamKind::Const { .. } => {
662+
GenericArg::Const(cx.const_ident(self.span, param.ident))
663+
}
660664
}).collect();
661665

662666
// Create the type of `self`.

src/libsyntax_ext/deriving/generic/ty.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<'a> Path<'a> {
9494
}
9595
}
9696

97-
/// A type. Supports pointers, Self, and literals
97+
/// A type. Supports pointers, Self, and literals.
9898
#[derive(Clone)]
9999
pub enum Ty<'a> {
100100
Self_,
@@ -107,6 +107,13 @@ pub enum Ty<'a> {
107107
Tuple(Vec<Ty<'a>>),
108108
}
109109

110+
/// A const expression. Supports literals and blocks.
111+
#[derive(Clone, Eq, PartialEq)]
112+
pub enum Const {
113+
Literal,
114+
Block,
115+
}
116+
110117
pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
111118
Borrowed(None, ast::Mutability::Immutable)
112119
}
@@ -180,6 +187,9 @@ impl<'a> Ty<'a> {
180187
GenericParamKind::Type { .. } => {
181188
GenericArg::Type(cx.ty_ident(span, param.ident))
182189
}
190+
GenericParamKind::Const { .. } => {
191+
GenericArg::Const(cx.const_ident(span, param.ident))
192+
}
183193
}).collect();
184194

185195
cx.path_all(span, false, vec![self_ty], params, vec![])

0 commit comments

Comments
 (0)