Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Higher Kinded Types (WIP) #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,14 @@ pub struct TyParam {
pub bounds: TyParamBounds,
pub unbound: Option<TyParamBound>,
pub default: Option<P<Ty>>,
pub span: Span
pub span: Span,
pub kind: TyKind
}

#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)]
pub enum TyKind {
Star,
Arrow(Box<TyKind>, Box<TyKind>)
}

/// Represents lifetimes and type parameters attached to a declaration
Expand Down
9 changes: 6 additions & 3 deletions src/libsyntax/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ pub trait AstBuilder {
id: ast::Ident,
bounds: OwnedSlice<ast::TyParamBound>,
unbound: Option<ast::TyParamBound>,
default: Option<P<ast::Ty>>) -> ast::TyParam;
default: Option<P<ast::Ty>>,
kind: ast::TyKind) -> ast::TyParam;

fn trait_ref(&self, path: ast::Path) -> ast::TraitRef;
fn typarambound(&self, path: ast::Path) -> ast::TyParamBound;
Expand Down Expand Up @@ -409,14 +410,16 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
id: ast::Ident,
bounds: OwnedSlice<ast::TyParamBound>,
unbound: Option<ast::TyParamBound>,
default: Option<P<ast::Ty>>) -> ast::TyParam {
default: Option<P<ast::Ty>>,
kind: ast::TyKind) -> ast::TyParam {
ast::TyParam {
ident: id,
id: ast::DUMMY_NODE_ID,
bounds: bounds,
unbound: unbound,
default: default,
span: span
span: span,
kind: kind
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/ext/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The compiler code necessary for `#[deriving(Decodable)]`. See
encodable.rs for more.
*/

use ast::{MetaItem, Item, Expr, MutMutable, Ident};
use ast::{MetaItem, Item, Expr, MutMutable, Ident, Star};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::build::AstBuilder;
Expand All @@ -37,10 +37,10 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
additional_bounds: Vec::new(),
generics: LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec!(("__D", None, vec!(Path::new_(
bounds: vec!(("__D", Star, None, vec!(Path::new_(
vec!("serialize", "Decoder"), None,
vec!(box Literal(Path::new_local("__E"))), true))),
("__E", None, vec!()))
("__E", Star, None, vec!()))
},
methods: vec!(
MethodDef {
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/ext/deriving/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
//! }
//! ```

use ast::{MetaItem, Item, Expr, ExprRet, MutMutable, LitNil};
use ast::{MetaItem, Item, Expr, ExprRet, MutMutable, LitNil, Star};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::build::AstBuilder;
Expand All @@ -102,10 +102,10 @@ pub fn expand_deriving_encodable(cx: &mut ExtCtxt,
additional_bounds: Vec::new(),
generics: LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec!(("__S", None, vec!(Path::new_(
bounds: vec!(("__S", Star, None, vec!(Path::new_(
vec!("serialize", "Encoder"), None,
vec!(box Literal(Path::new_local("__E"))), true))),
("__E", None, vec!()))
("__E", Star, None, vec!()))
},
methods: vec!(
MethodDef {
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ impl<'a> TraitDef<'a> {
ty_param.ident,
OwnedSlice::from_vec(bounds),
ty_param.unbound.clone(),
None)
None,
ty_param.kind.clone())
}));
let trait_generics = Generics {
lifetimes: lifetimes,
Expand Down
11 changes: 6 additions & 5 deletions src/libsyntax/ext/deriving/generic/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ impl<'a> Ty<'a> {

fn mk_ty_param(cx: &ExtCtxt, span: Span, name: &str,
bounds: &[Path], unbound: Option<ast::TyParamBound>,
self_ident: Ident, self_generics: &Generics) -> ast::TyParam {
self_ident: Ident, self_generics: &Generics, kind: ast::TyKind) -> ast::TyParam {
let bounds =
bounds.iter().map(|b| {
let path = b.to_path(cx, span, self_ident, self_generics);
cx.typarambound(path)
}).collect();
cx.typaram(span, cx.ident_of(name), bounds, unbound, None)
cx.typaram(span, cx.ident_of(name), bounds, unbound, None, kind)
}

fn mk_generics(lifetimes: Vec<ast::LifetimeDef>, ty_params: Vec<ast::TyParam>)
Expand All @@ -220,7 +220,7 @@ fn mk_generics(lifetimes: Vec<ast::LifetimeDef>, ty_params: Vec<ast::TyParam>)
#[deriving(Clone)]
pub struct LifetimeBounds<'a> {
pub lifetimes: Vec<(&'a str, Vec<&'a str>)>,
pub bounds: Vec<(&'a str, Option<ast::TyParamBound>, Vec<Path<'a>>)>,
pub bounds: Vec<(&'a str, ast::TyKind, Option<ast::TyParamBound>, Vec<Path<'a>>)>,
}

impl<'a> LifetimeBounds<'a> {
Expand All @@ -243,14 +243,15 @@ impl<'a> LifetimeBounds<'a> {
}).collect();
let ty_params = self.bounds.iter().map(|t| {
match t {
&(ref name, ref unbound, ref bounds) => {
&(ref name, ref kind, ref unbound, ref bounds) => {
mk_ty_param(cx,
span,
*name,
bounds.as_slice(),
unbound.clone(),
self_ty,
self_generics)
self_generics,
kind.clone())
}
}
}).collect();
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/deriving/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ast::{MetaItem, Item, Expr, MutMutable};
use ast::{MetaItem, Item, Expr, MutMutable, Star};
use codemap::Span;
use ext::base::ExtCtxt;
use ext::build::AstBuilder;
Expand All @@ -28,7 +28,7 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt,
vec!(box Literal(Path::new_local("__S"))), true),
LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec!(("__S", None,
bounds: vec!(("__S", Star, None,
vec!(Path::new(vec!("std", "hash", "Writer"))))),
},
Path::new_local("__S"))
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ pub fn expand_deriving_rand(cx: &mut ExtCtxt,
generics: LifetimeBounds {
lifetimes: Vec::new(),
bounds: vec!(("R",
ast::Star,
None,
vec!( Path::new(vec!("std", "rand", "Rng")) )))
vec!( Path::new(vec!("std", "rand", "Rng")) ))),
},
explicit_self: None,
args: vec!(
Expand Down
5 changes: 3 additions & 2 deletions src/libsyntax/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,14 +682,15 @@ pub fn noop_fold_ty_param_bound<T>(tpb: TyParamBound, fld: &mut T)
}

pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam {
let TyParam {id, ident, bounds, unbound, default, span} = tp;
let TyParam {id, ident, bounds, unbound, default, span, kind} = tp;
TyParam {
id: fld.new_id(id),
ident: ident,
bounds: fld.fold_bounds(bounds),
unbound: unbound.map(|x| fld.fold_ty_param_bound(x)),
default: default.map(|x| fld.fold_ty(x)),
span: span
span: span,
kind: kind.clone()
}
}

Expand Down
20 changes: 17 additions & 3 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use ast::{TokenTree, TraitItem, TraitRef, TTDelim, TTSeq, TTTok};
use ast::{TTNonterminal, TupleVariantKind, Ty, Ty_, TyBot, TyBox};
use ast::{TypeField, TyFixedLengthVec, TyClosure, TyProc, TyBareFn};
use ast::{TyTypeof, TyInfer, TypeMethod};
use ast::{TyNil, TyParam, TyParamBound, TyParen, TyPath, TyPtr, TyQPath};
use ast::{TyNil, TyParam, TyParamBound, TyKind, TyParen, TyPath, TyPtr, TyQPath};
use ast::{TyRptr, TyTup, TyU32, TyUnboxedFn, TyUniq, TyVec, UnUniq};
use ast::{TypeImplItem, TypeTraitItem, Typedef, UnboxedClosureKind};
use ast::{UnboxedFnBound, UnboxedFnTy, UnboxedFnTyParamBound};
Expand Down Expand Up @@ -3839,7 +3839,7 @@ impl<'a> Parser<'a> {
}
}

/// Matches typaram = (unbound`?`)? IDENT optbounds ( EQ ty )?
/// Matches typaram = (unbound`?`)? IDENT optkind optbounds ( EQ ty )?
fn parse_ty_param(&mut self) -> TyParam {
// This is a bit hacky. Currently we are only interested in a single
// unbound, and it may only be `Sized`. To avoid backtracking and other
Expand All @@ -3856,6 +3856,8 @@ impl<'a> Parser<'a> {
ident = self.parse_ident();
}

let kind = self.parse_ty_kind();

let bounds = self.parse_colon_then_ty_param_bounds();

let default = if self.token == token::EQ {
Expand All @@ -3871,9 +3873,22 @@ impl<'a> Parser<'a> {
unbound: unbound,
default: default,
span: span,
kind: kind
}
}

fn parse_ty_kind(&mut self) -> ast::TyKind {
if self.eat(&token::LT) {
println!("Inside parse_ty_kind!")
let placeholders = self.parse_seq_to_gt(Some(token::COMMA), |p| {
p.eat(&token::UNDERSCORE)
});
placeholders
.iter()
.skip(1)
.fold(ast::Star, |accum, _| ast::Arrow(box ast::Star, box accum))
} else { ast::Star }
}
/// Parse a set of optional generic type parameter declarations. Where
/// clauses are not parsed here, and must be added later via
/// `parse_where_clause()`.
Expand Down Expand Up @@ -5781,4 +5796,3 @@ impl<'a> Parser<'a> {
}
}
}