ObjectSum and PolyTraitRef types

This commit is contained in:
David Tolnay 2016-10-08 00:55:17 -07:00
parent 1241783c01
commit 6414da71fb
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 46 additions and 3 deletions

View File

@ -195,12 +195,15 @@ pub enum FunctionRetTy {
#[cfg(feature = "parsing")]
pub mod parsing {
use super::*;
use {TraitBoundModifier, TyParamBound};
use constant::parsing::const_expr;
use generics::parsing::{lifetime, lifetime_def, ty_param_bound, bound_lifetimes};
use ident::parsing::ident;
use std::str;
named!(pub ty -> Ty, alt!(
ty_poly_trait_ref // must be before ty_path
|
ty_vec
|
ty_array
@ -296,7 +299,18 @@ pub mod parsing {
(Ty::Tup(elems))
));
named!(ty_path -> Ty, map!(qpath, |(qself, p)| Ty::Path(qself, p)));
named!(ty_path -> Ty, do_parse!(
qpath: qpath >>
bounds: many0!(preceded!(punct!("+"), ty_param_bound)) >>
({
let path = Ty::Path(qpath.0, qpath.1);
if bounds.is_empty() {
path
} else {
Ty::ObjectSum(Box::new(path), bounds)
}
})
));
named!(pub qpath -> (Option<QSelf>, Path), alt!(
map!(path, |p| (None, p))
@ -329,6 +343,23 @@ pub mod parsing {
)
));
named!(ty_poly_trait_ref -> Ty, do_parse!(
keyword!("for") >>
punct!("<") >>
lifetimes: separated_list!(punct!(","), lifetime_def) >>
punct!(">") >>
trait_ref: path >>
(Ty::PolyTraitRef(vec![
TyParamBound::Trait(
PolyTraitRef {
bound_lifetimes: lifetimes,
trait_ref: trait_ref,
},
TraitBoundModifier::None,
),
]))
));
named!(ty_impl_trait -> Ty, do_parse!(
keyword!("impl") >>
elem: separated_nonempty_list!(punct!("+"), ty_param_bound) >>
@ -490,8 +521,16 @@ mod printing {
segment.to_tokens(tokens);
}
}
Ty::ObjectSum(_, _) => unimplemented!(),
Ty::PolyTraitRef(_) => unimplemented!(),
Ty::ObjectSum(ref ty, ref bounds) => {
ty.to_tokens(tokens);
for bound in bounds {
tokens.append("+");
bound.to_tokens(tokens);
}
}
Ty::PolyTraitRef(ref bounds) => {
tokens.append_separated(bounds, "+");
}
Ty::ImplTrait(ref bounds) => {
tokens.append("impl");
tokens.append_separated(bounds, "+");

View File

@ -1,3 +1,7 @@
const A: usize = unimplemented!();
const A: [T; std::u16::MAX as usize] = unimplemented!();
const A: for<'a> A<&'a B> = unimplemented!();
const A: Box<A + 'a> = unimplemented!();