From c225824bded695c8bced713f5a4c62fe327277bc Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 18 Mar 2015 09:22:38 -0400 Subject: Require braces when a closure has an explicit return type. This is a [breaking-change]: instead of a closure like `|| -> i32 22`, prefer `|| -> i32 { 22 }`. Fixes #23420. --- src/libsyntax/parse/mod.rs | 5 +++-- src/libsyntax/parse/parser.rs | 36 ++++++++++++++++++++++-------------- src/libsyntax/print/pprust.rs | 7 ++++++- 3 files changed, 31 insertions(+), 17 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 82ba873e54b..968d2fd7e2a 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -404,7 +404,7 @@ pub fn char_lit(lit: &str) -> (char, isize) { .map(|x| (x, len as isize)) } - let unicode_escape = || -> Option<(char, isize)> + let unicode_escape = || -> Option<(char, isize)> { if lit.as_bytes()[2] == b'{' { let idx = lit.find('}').expect(msg2); let subslice = &lit[3..idx]; @@ -413,7 +413,8 @@ pub fn char_lit(lit: &str) -> (char, isize) { .map(|x| (x, subslice.chars().count() as isize + 4)) } else { esc(6, lit) - }; + } + }; // Unicode escapes return match lit.as_bytes()[1] as char { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index bf2b2c0afe6..d76355f9af0 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2101,10 +2101,7 @@ impl<'a> Parser<'a> { } }, token::OpenDelim(token::Brace) => { - self.bump(); - let blk = self.parse_block_tail(lo, DefaultBlock); - return self.mk_expr(blk.span.lo, blk.span.hi, - ExprBlock(blk)); + return self.parse_block_expr(lo, DefaultBlock); }, token::BinOp(token::Or) | token::OrOr => { return self.parse_lambda_expr(CaptureByRef); @@ -3000,19 +2997,30 @@ impl<'a> Parser<'a> { { let lo = self.span.lo; let decl = self.parse_fn_block_decl(); - let body = self.parse_expr(); - let fakeblock = P(ast::Block { - id: ast::DUMMY_NODE_ID, - stmts: vec![], - span: body.span, - expr: Some(body), - rules: DefaultBlock, - }); + let body = match decl.output { + DefaultReturn(_) => { + // If no explicit return type is given, parse any + // expr and wrap it up in a dummy block: + let body_expr = self.parse_expr(); + P(ast::Block { + id: ast::DUMMY_NODE_ID, + stmts: vec![], + span: body_expr.span, + expr: Some(body_expr), + rules: DefaultBlock, + }) + } + _ => { + // If an explicit return type is given, require a + // block to appear (RFC 968). + self.parse_block() + } + }; self.mk_expr( lo, - fakeblock.span.hi, - ExprClosure(capture_clause, decl, fakeblock)) + body.span.hi, + ExprClosure(capture_clause, decl, body)) } pub fn parse_else_expr(&mut self) -> P { diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 07303ba51ff..b58c121c5fd 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1777,7 +1777,12 @@ impl<'a> State<'a> { try!(self.print_fn_block_args(&**decl)); try!(space(&mut self.s)); - if !body.stmts.is_empty() || !body.expr.is_some() { + let default_return = match decl.output { + ast::DefaultReturn(..) => true, + _ => false + }; + + if !default_return || !body.stmts.is_empty() || body.expr.is_none() { try!(self.print_block_unclosed(&**body)); } else { // we extract the block, so as not to create another set of boxes -- cgit 1.4.1-3-g733a5 From 835c9bbbf049de2b6b37b3f2cf14b44de21c2ec5 Mon Sep 17 00:00:00 2001 From: mdinger Date: Wed, 18 Mar 2015 19:36:23 -0400 Subject: Update ast.rs Typo --- src/libsyntax/ast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 5c275715352..5a32045db4a 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1155,7 +1155,7 @@ pub enum Lit_ { LitByte(u8), /// A character literal (`'a'`) LitChar(char), - /// An integer liteal (`1u8`) + /// An integer literal (`1u8`) LitInt(u64, LitIntType), /// A float literal (`1f64` or `1E10f64`) LitFloat(InternedString, FloatTy), -- cgit 1.4.1-3-g733a5 From d943d9b801e7621c08aab912b5ade24a80865191 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 18 Mar 2015 18:06:10 +0530 Subject: Address huon's comments --- src/libsyntax/ast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 5c275715352..936fbd62786 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -215,8 +215,8 @@ pub struct Lifetime { pub name: Name } -#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] /// A lifetime definition, eg `'a: 'b+'c+'d` +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct LifetimeDef { pub lifetime: Lifetime, pub bounds: Vec -- cgit 1.4.1-3-g733a5 From c20c652a9211d538dcb8b264b0b9c7479684b4d9 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Thu, 19 Mar 2015 00:56:37 +0530 Subject: Space and punctuation fixes --- src/libsyntax/ast.rs | 55 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 14 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 936fbd62786..f2fc453cd32 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -590,6 +590,7 @@ pub enum Pat_ { /// A PatIdent may either be a new bound variable, /// or a nullary enum (in which case the third field /// is None). + /// /// In the nullary enum case, the parser can't determine /// which it is. The resolver determines this, and /// records this pattern's NodeId in an auxiliary @@ -786,18 +787,22 @@ pub enum Expr_ { /// An array (`[a, b, c, d]`) ExprVec(Vec>), /// A function call + /// /// The first field resolves to the function itself, /// and the second field is the list of arguments ExprCall(P, Vec>), /// A method call (`x.foo::(a, b, c, d)`) - /// The `SpannedIdent` is the identifier for the method name + /// + /// The `SpannedIdent` is the identifier for the method name. /// The vector of `Ty`s are the ascripted type parameters for the method - /// (within the angle brackets) + /// (within the angle brackets). + /// /// The first element of the vector of `Expr`s is the expression that evaluates /// to the object on which the method is being called on (the receiver), /// and the remaining elements are the rest of the arguments. + /// /// Thus, `x.foo::(a, b, c, d)` is represented as - /// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])` + /// `ExprMethodCall(foo, [Bar, Baz], [x, a, b, c, d])`. ExprMethodCall(SpannedIdent, Vec>, Vec>), /// A tuple (`(a, b, c ,d)`) ExprTup(Vec>), @@ -810,32 +815,41 @@ pub enum Expr_ { /// A cast (`foo as f64`) ExprCast(P, P), /// An `if` block, with an optional else block + /// /// `if expr { block } else { expr }` ExprIf(P, P, Option>), /// An `if let` expression with an optional else block + /// /// `if let pat = expr { block } else { expr }` - /// This is desugared to a `match` expression + /// + /// This is desugared to a `match` expression. ExprIfLet(P, P, P, Option>), // FIXME #6993: change to Option ... or not, if these are hygienic. /// A while loop, with an optional label + /// /// `'label: while expr { block }` ExprWhile(P, P, Option), // FIXME #6993: change to Option ... or not, if these are hygienic. /// A while-let loop, with an optional label + /// /// `'label: while let pat = expr { block }` - /// This is desugared to a combination of `loop` and `match` expressions + /// + /// This is desugared to a combination of `loop` and `match` expressions. ExprWhileLet(P, P, P, Option), // FIXME #6993: change to Option ... or not, if these are hygienic. /// A for loop, with an optional label + /// /// `'label: for pat in expr { block }` - /// This is desugared to a combination of `loop` and `match` expressions + /// + /// This is desugared to a combination of `loop` and `match` expressions. ExprForLoop(P, P, P, Option), /// Conditionless loop (can be exited with break, continue, or return) + /// /// `'label: loop { block }` // FIXME #6993: change to Option ... or not, if these are hygienic. ExprLoop(P, Option), /// A `match` block, with a source that indicates whether or not it is - /// the result of a desugaring, and if so, which kind + /// the result of a desugaring, and if so, which kind. ExprMatch(P, Vec, MatchSource), /// A closure (for example, `move |a, b, c| {a + b + c}`) ExprClosure(CaptureClause, P, P), @@ -845,12 +859,14 @@ pub enum Expr_ { /// An assignment (`a = foo()`) ExprAssign(P, P), /// An assignment with an operator - /// For example, `a += 1` + /// + /// For example, `a += 1`. ExprAssignOp(BinOp, P, P), /// Access of a named struct field (`obj.foo`) ExprField(P, SpannedIdent), /// Access of an unnamed field of a struct or tuple-struct - /// For example, `foo.0` + /// + /// For example, `foo.0`. ExprTupField(P, Spanned), /// An indexing operation (`foo[2]`) ExprIndex(P, P), @@ -858,7 +874,9 @@ pub enum Expr_ { ExprRange(Option>, Option>), /// Variable reference, possibly containing `::` and/or type - /// parameters, e.g. foo::bar::. Optionally "qualified", + /// parameters, e.g. foo::bar::. + /// + /// Optionally "qualified", /// e.g. ` as SomeTrait>::SomeType`. ExprPath(Option, Path), @@ -878,13 +896,15 @@ pub enum Expr_ { ExprMac(Mac), /// A struct literal expression. + /// /// For example, `Foo {x: 1, y: 2}`, or - /// `Foo {x: 1, .. base}`, where `base` is the `Option` + /// `Foo {x: 1, .. base}`, where `base` is the `Option`. ExprStruct(Path, Vec, Option>), /// A vector literal constructed from one repeated element. + /// /// For example, `[1u8; 5]`. The first expression is the element - /// to be repeated; the second is the number of times to repeat it + /// to be repeated; the second is the number of times to repeat it. ExprRepeat(P, P), /// No-op: used solely so we can pretty-print faithfully @@ -1092,6 +1112,7 @@ pub type Mac = Spanned; /// Represents a macro invocation. The Path indicates which macro /// is being invoked, and the vector of token-trees contains the source /// of the macro invocation. +/// /// There's only one flavor, now, so this could presumably be simplified. #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum Mac_ { @@ -1105,6 +1126,7 @@ pub enum StrStyle { /// A regular string, like `"foo"` CookedStr, /// A raw string, like `r##"foo"##` + /// /// The uint is the number of `#` symbols used RawStr(usize) } @@ -1459,7 +1481,7 @@ impl Arg { } } -/// represents the header (not the body) of a function declaration +/// Represents the header (not the body) of a function declaration #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub struct FnDecl { pub inputs: Vec, @@ -1505,7 +1527,9 @@ pub enum FunctionRetTy { /// Functions with return type `!`that always /// raise an error or exit (i.e. never return to the caller) NoReturn(Span), - /// Return type is not specified. Functions default to `()` and + /// Return type is not specified. + /// + /// Functions default to `()` and /// closures default to inference. Span points to where return /// type would be inserted. DefaultReturn(Span), @@ -1645,6 +1669,7 @@ pub struct Attribute_ { } /// TraitRef's appear in impls. +/// /// resolve maps each TraitRef's ref_id to its defining trait; that's all /// that the ref_id is for. The impl_id maps to the "self type" of this impl. /// If this impl is an ItemImpl, the impl_id is redundant (it could be the @@ -1745,6 +1770,7 @@ pub struct Item { #[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum Item_ { /// An`extern crate` item, with optional original crate name, + /// /// e.g. `extern crate foo` or `extern crate "foo-bar" as foo` ItemExternCrate(Option<(InternedString, StrStyle)>), /// A `use` or `pub use` item @@ -1773,6 +1799,7 @@ pub enum Item_ { Vec>), // Default trait implementations + /// // `impl Trait for .. {}` ItemDefaultImpl(Unsafety, TraitRef), /// An implementation, eg `impl Trait for Foo { .. }` -- cgit 1.4.1-3-g733a5