Support const generics in derive

Co-Authored-By: Gabriel Smith <yodaldevoid@users.noreply.github.com>
This commit is contained in:
varkor 2019-02-05 16:50:00 +01:00
parent 0a8d98a270
commit d7695abb76
3 changed files with 34 additions and 2 deletions

View File

@ -38,12 +38,14 @@ pub trait AstBuilder {
bindings: Vec<ast::TypeBinding>)
-> (ast::QSelf, ast::Path);
// types
// types and consts
fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
fn ty(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty>;
fn ty_path(&self, path: ast::Path) -> P<ast::Ty>;
fn ty_ident(&self, span: Span, idents: ast::Ident) -> P<ast::Ty>;
fn anon_const(&self, span: Span, expr: ast::ExprKind) -> ast::AnonConst;
fn const_ident(&self, span: Span, idents: ast::Ident) -> ast::AnonConst;
fn ty_rptr(&self, span: Span,
ty: P<ast::Ty>,
@ -394,6 +396,22 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
self.ty_path(self.path_ident(span, ident))
}
fn anon_const(&self, span: Span, expr: ast::ExprKind) -> ast::AnonConst {
ast::AnonConst {
id: ast::DUMMY_NODE_ID,
value: P(ast::Expr {
id: ast::DUMMY_NODE_ID,
node: expr,
span,
attrs: ThinVec::new(),
})
}
}
fn const_ident(&self, span: Span, ident: ast::Ident) -> ast::AnonConst {
self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
}
fn ty_rptr(&self,
span: Span,
ty: P<ast::Ty>,

View File

@ -560,6 +560,7 @@ impl<'a> TraitDef<'a> {
cx.typaram(self.span, param.ident, vec![], bounds, None)
}
GenericParamKind::Const { .. } => param.clone(),
}));
// and similarly for where clauses
@ -657,6 +658,9 @@ impl<'a> TraitDef<'a> {
GenericParamKind::Type { .. } => {
GenericArg::Type(cx.ty_ident(self.span, param.ident))
}
GenericParamKind::Const { .. } => {
GenericArg::Const(cx.const_ident(self.span, param.ident))
}
}).collect();
// Create the type of `self`.

View File

@ -94,7 +94,7 @@ impl<'a> Path<'a> {
}
}
/// A type. Supports pointers, Self, and literals
/// A type. Supports pointers, Self, and literals.
#[derive(Clone)]
pub enum Ty<'a> {
Self_,
@ -107,6 +107,13 @@ pub enum Ty<'a> {
Tuple(Vec<Ty<'a>>),
}
/// A const expression. Supports literals and blocks.
#[derive(Clone, Eq, PartialEq)]
pub enum Const {
Literal,
Block,
}
pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
Borrowed(None, ast::Mutability::Immutable)
}
@ -180,6 +187,9 @@ impl<'a> Ty<'a> {
GenericParamKind::Type { .. } => {
GenericArg::Type(cx.ty_ident(span, param.ident))
}
GenericParamKind::Const { .. } => {
GenericArg::Const(cx.const_ident(span, param.ident))
}
}).collect();
cx.path_all(span, false, vec![self_ty], params, vec![])