Support const generics in derive
Co-Authored-By: Gabriel Smith <yodaldevoid@users.noreply.github.com>
This commit is contained in:
parent
0a8d98a270
commit
d7695abb76
3 changed files with 34 additions and 2 deletions
|
@ -38,12 +38,14 @@ pub trait AstBuilder {
|
||||||
bindings: Vec<ast::TypeBinding>)
|
bindings: Vec<ast::TypeBinding>)
|
||||||
-> (ast::QSelf, ast::Path);
|
-> (ast::QSelf, ast::Path);
|
||||||
|
|
||||||
// types
|
// types and consts
|
||||||
fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy;
|
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(&self, span: Span, ty: ast::TyKind) -> P<ast::Ty>;
|
||||||
fn ty_path(&self, path: ast::Path) -> 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 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,
|
fn ty_rptr(&self, span: Span,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
|
@ -394,6 +396,22 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
|
||||||
self.ty_path(self.path_ident(span, ident))
|
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,
|
fn ty_rptr(&self,
|
||||||
span: Span,
|
span: Span,
|
||||||
ty: P<ast::Ty>,
|
ty: P<ast::Ty>,
|
||||||
|
|
|
@ -560,6 +560,7 @@ impl<'a> TraitDef<'a> {
|
||||||
|
|
||||||
cx.typaram(self.span, param.ident, vec![], bounds, None)
|
cx.typaram(self.span, param.ident, vec![], bounds, None)
|
||||||
}
|
}
|
||||||
|
GenericParamKind::Const { .. } => param.clone(),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// and similarly for where clauses
|
// and similarly for where clauses
|
||||||
|
@ -657,6 +658,9 @@ impl<'a> TraitDef<'a> {
|
||||||
GenericParamKind::Type { .. } => {
|
GenericParamKind::Type { .. } => {
|
||||||
GenericArg::Type(cx.ty_ident(self.span, param.ident))
|
GenericArg::Type(cx.ty_ident(self.span, param.ident))
|
||||||
}
|
}
|
||||||
|
GenericParamKind::Const { .. } => {
|
||||||
|
GenericArg::Const(cx.const_ident(self.span, param.ident))
|
||||||
|
}
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
// Create the type of `self`.
|
// Create the type of `self`.
|
||||||
|
|
|
@ -94,7 +94,7 @@ impl<'a> Path<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A type. Supports pointers, Self, and literals
|
/// A type. Supports pointers, Self, and literals.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub enum Ty<'a> {
|
pub enum Ty<'a> {
|
||||||
Self_,
|
Self_,
|
||||||
|
@ -107,6 +107,13 @@ pub enum Ty<'a> {
|
||||||
Tuple(Vec<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> {
|
pub fn borrowed_ptrty<'r>() -> PtrTy<'r> {
|
||||||
Borrowed(None, ast::Mutability::Immutable)
|
Borrowed(None, ast::Mutability::Immutable)
|
||||||
}
|
}
|
||||||
|
@ -180,6 +187,9 @@ impl<'a> Ty<'a> {
|
||||||
GenericParamKind::Type { .. } => {
|
GenericParamKind::Type { .. } => {
|
||||||
GenericArg::Type(cx.ty_ident(span, param.ident))
|
GenericArg::Type(cx.ty_ident(span, param.ident))
|
||||||
}
|
}
|
||||||
|
GenericParamKind::Const { .. } => {
|
||||||
|
GenericArg::Const(cx.const_ident(span, param.ident))
|
||||||
|
}
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
cx.path_all(span, false, vec![self_ty], params, vec![])
|
cx.path_all(span, false, vec![self_ty], params, vec![])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue