diff options
| author | Patrick Walton <pcwalton@mimiga.net> | 2014-09-05 21:27:47 -0700 |
|---|---|---|
| committer | Patrick Walton <pcwalton@mimiga.net> | 2014-09-18 16:31:58 -0700 |
| commit | 7c00d77e8bd18d2e1873e8e995885b3500a88a0d (patch) | |
| tree | 21d5a718a9a40ed6920463b2348a0788fadebc2c /src/libsyntax | |
| parent | 9c41064308806907067a1bc5f6f9138c29310221 (diff) | |
| download | rust-7c00d77e8bd18d2e1873e8e995885b3500a88a0d.tar.gz rust-7c00d77e8bd18d2e1873e8e995885b3500a88a0d.zip | |
librustc: Implement the syntax in the RFC for unboxed closure sugar.
Part of issue #16640. I am leaving this issue open to handle parsing of higher-rank lifetimes in traits. This change breaks code that used unboxed closures: * Instead of `F:|&: int| -> int`, write `F:Fn(int) -> int`. * Instead of `F:|&mut: int| -> int`, write `F:FnMut(int) -> int`. * Instead of `F:|: int| -> int`, write `F:FnOnce(int) -> int`. [breaking-change]
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 9 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 24 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 76 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 49 |
4 files changed, 77 insertions, 81 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index eac158e664c..e74222fac09 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -213,13 +213,20 @@ pub static DUMMY_NODE_ID: NodeId = -1; #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] pub enum TyParamBound { TraitTyParamBound(TraitRef), - UnboxedFnTyParamBound(UnboxedFnTy), + UnboxedFnTyParamBound(P<UnboxedFnBound>), RegionTyParamBound(Lifetime) } pub type TyParamBounds = OwnedSlice<TyParamBound>; #[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +pub struct UnboxedFnBound { + pub path: Path, + pub decl: P<FnDecl>, + pub ref_id: NodeId, +} + +#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] pub struct TyParam { pub ident: Ident, pub id: NodeId, diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 3beba5bcda4..1441f5beb6a 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -657,16 +657,26 @@ pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> { }) } -pub fn noop_fold_ty_param_bound<T: Folder>(tpb: TyParamBound, fld: &mut T) - -> TyParamBound { +pub fn noop_fold_ty_param_bound<T>(tpb: TyParamBound, fld: &mut T) + -> TyParamBound + where T: Folder { match tpb { TraitTyParamBound(ty) => TraitTyParamBound(fld.fold_trait_ref(ty)), RegionTyParamBound(lifetime) => RegionTyParamBound(fld.fold_lifetime(lifetime)), - UnboxedFnTyParamBound(UnboxedFnTy {decl, kind}) => { - UnboxedFnTyParamBound(UnboxedFnTy { - decl: fld.fold_fn_decl(decl), - kind: kind, - }) + UnboxedFnTyParamBound(bound) => { + match *bound { + UnboxedFnBound { + ref path, + ref decl, + ref_id + } => { + UnboxedFnTyParamBound(P(UnboxedFnBound { + path: fld.fold_path(path.clone()), + decl: fld.fold_fn_decl(decl.clone()), + ref_id: fld.new_id(ref_id), + })) + } + } } } } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index ff4fd41fbd7..8a0027e5c06 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -55,7 +55,8 @@ use ast::{TyTypeof, TyInfer, TypeMethod}; use ast::{TyNil, TyParam, TyParamBound, TyParen, TyPath, TyPtr, TyQPath}; use ast::{TyRptr, TyTup, TyU32, TyUnboxedFn, TyUniq, TyVec, UnUniq}; use ast::{TypeImplItem, TypeTraitItem, Typedef, UnboxedClosureKind}; -use ast::{UnboxedFnTy, UnboxedFnTyParamBound, UnnamedField, UnsafeBlock}; +use ast::{UnboxedFnBound, UnboxedFnTy, UnboxedFnTyParamBound}; +use ast::{UnnamedField, UnsafeBlock}; use ast::{UnsafeFn, ViewItem, ViewItem_, ViewItemExternCrate, ViewItemUse}; use ast::{ViewPath, ViewPathGlob, ViewPathList, ViewPathSimple}; use ast::{Visibility, WhereClause, WherePredicate}; @@ -3666,39 +3667,6 @@ impl<'a> Parser<'a> { }) } - fn parse_unboxed_function_type(&mut self) -> UnboxedFnTy { - let (optional_unboxed_closure_kind, inputs) = - if self.eat(&token::OROR) { - (None, Vec::new()) - } else { - self.expect_or(); - - let optional_unboxed_closure_kind = - self.parse_optional_unboxed_closure_kind(); - - let inputs = self.parse_seq_to_before_or(&token::COMMA, - |p| { - p.parse_arg_general(false) - }); - self.expect_or(); - (optional_unboxed_closure_kind, inputs) - }; - - let (return_style, output) = self.parse_ret_ty(); - UnboxedFnTy { - decl: P(FnDecl { - inputs: inputs, - output: output, - cf: return_style, - variadic: false, - }), - kind: match optional_unboxed_closure_kind { - Some(kind) => kind, - None => FnMutUnboxedClosureKind, - }, - } - } - // Parses a sequence of bounds if a `:` is found, // otherwise returns empty list. fn parse_colon_then_ty_param_bounds(&mut self) @@ -3730,13 +3698,31 @@ impl<'a> Parser<'a> { self.bump(); } token::MOD_SEP | token::IDENT(..) => { - let tref = self.parse_trait_ref(); - result.push(TraitTyParamBound(tref)); - } - token::BINOP(token::OR) | token::OROR => { - let unboxed_function_type = - self.parse_unboxed_function_type(); - result.push(UnboxedFnTyParamBound(unboxed_function_type)); + let path = + self.parse_path(LifetimeAndTypesWithoutColons).path; + if self.token == token::LPAREN { + self.bump(); + let inputs = self.parse_seq_to_end( + &token::RPAREN, + seq_sep_trailing_allowed(token::COMMA), + |p| p.parse_arg_general(false)); + let (return_style, output) = self.parse_ret_ty(); + result.push(UnboxedFnTyParamBound(P(UnboxedFnBound { + path: path, + decl: P(FnDecl { + inputs: inputs, + output: output, + cf: return_style, + variadic: false, + }), + ref_id: ast::DUMMY_NODE_ID, + }))); + } else { + result.push(TraitTyParamBound(ast::TraitRef { + path: path, + ref_id: ast::DUMMY_NODE_ID, + })) + } } _ => break, } @@ -4423,14 +4409,6 @@ impl<'a> Parser<'a> { Some(attrs)) } - /// Parse a::B<String,int> - fn parse_trait_ref(&mut self) -> TraitRef { - ast::TraitRef { - path: self.parse_path(LifetimeAndTypesWithoutColons).path, - ref_id: ast::DUMMY_NODE_ID, - } - } - /// Parse struct Foo { ... } fn parse_item_struct(&mut self, is_virtual: bool) -> ItemInfo { let class_name = self.parse_ident(); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 0ae5303641b..d7dd87a096e 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -2190,16 +2190,13 @@ impl<'a> State<'a> { self.print_lifetime(lt) } UnboxedFnTyParamBound(ref unboxed_function_type) => { - self.print_ty_fn(None, - None, - ast::NormalFn, - ast::Many, - &*unboxed_function_type.decl, - None, - &OwnedSlice::empty(), - None, - None, - Some(unboxed_function_type.kind)) + try!(self.print_path(&unboxed_function_type.path, + false)); + try!(self.popen()); + try!(self.print_fn_args(&*unboxed_function_type.decl, + None)); + try!(self.pclose()); + self.print_fn_output(&*unboxed_function_type.decl) } }) } @@ -2430,6 +2427,23 @@ impl<'a> State<'a> { self.end() } + pub fn print_fn_output(&mut self, decl: &ast::FnDecl) -> IoResult<()> { + match decl.output.node { + ast::TyNil => Ok(()), + _ => { + try!(self.space_if_not_bol()); + try!(self.ibox(indent_unit)); + try!(self.word_space("->")); + if decl.cf == ast::NoReturn { + try!(self.word_nbsp("!")); + } else { + try!(self.print_type(&*decl.output)); + } + self.end() + } + } + } + pub fn print_ty_fn(&mut self, opt_abi: Option<abi::Abi>, opt_sigil: Option<char>, @@ -2510,20 +2524,7 @@ impl<'a> State<'a> { try!(self.maybe_print_comment(decl.output.span.lo)); - match decl.output.node { - ast::TyNil => {} - _ => { - try!(self.space_if_not_bol()); - try!(self.ibox(indent_unit)); - try!(self.word_space("->")); - if decl.cf == ast::NoReturn { - try!(self.word_nbsp("!")); - } else { - try!(self.print_type(&*decl.output)); - } - try!(self.end()); - } - } + try!(self.print_fn_output(decl)); match generics { Some(generics) => try!(self.print_where_clause(generics)), |
