Skip to content

Commit e07cec6

Browse files
committed
auto merge of #14885 : pcwalton/rust/struct-literal-tightening, r=alexcrichton
`for...in`. Closes #14803. If you used a structure literal after one of these keywords, surround it in parentheses. [breaking-change] r? @nick29581
2 parents d6c1b85 + 654d644 commit e07cec6

22 files changed

+147
-37
lines changed

src/doc/rust.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -3004,7 +3004,7 @@ ten_times(|j| println!("hello, {}", j));
30043004
### While loops
30053005

30063006
~~~~ {.ebnf .gram}
3007-
while_expr : "while" expr '{' block '}' ;
3007+
while_expr : "while" no_struct_literal_expr '{' block '}' ;
30083008
~~~~
30093009

30103010
A `while` loop begins by evaluating the boolean loop conditional expression.
@@ -3071,7 +3071,7 @@ A `continue` expression is only permitted in the body of a loop.
30713071
### For expressions
30723072

30733073
~~~~ {.ebnf .gram}
3074-
for_expr : "for" pat "in" expr '{' block '}' ;
3074+
for_expr : "for" pat "in" no_struct_literal_expr '{' block '}' ;
30753075
~~~~
30763076

30773077
A `for` expression is a syntactic construct for looping over elements
@@ -3105,7 +3105,7 @@ for i in range(0u, 256) {
31053105
### If expressions
31063106

31073107
~~~~ {.ebnf .gram}
3108-
if_expr : "if" expr '{' block '}'
3108+
if_expr : "if" no_struct_literal_expr '{' block '}'
31093109
else_tail ? ;
31103110
31113111
else_tail : "else" [ if_expr
@@ -3126,7 +3126,7 @@ then any `else` block is executed.
31263126
### Match expressions
31273127

31283128
~~~~ {.ebnf .gram}
3129-
match_expr : "match" expr '{' match_arm * '}' ;
3129+
match_expr : "match" no_struct_literal_expr '{' match_arm * '}' ;
31303130
31313131
match_arm : attribute * match_pat "=>" [ expr "," | '{' block '}' ] ;
31323132

src/libsyntax/parse/parser.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub enum restriction {
8888
RESTRICT_STMT_EXPR,
8989
RESTRICT_NO_BAR_OP,
9090
RESTRICT_NO_BAR_OR_DOUBLEBAR_OP,
91+
RESTRICT_NO_STRUCT_LITERAL,
9192
}
9293

9394
type ItemInfo = (Ident, Item_, Option<Vec<Attribute> >);
@@ -2024,8 +2025,9 @@ impl<'a> Parser<'a> {
20242025

20252026
return self.mk_mac_expr(lo, hi, MacInvocTT(pth, tts, EMPTY_CTXT));
20262027
} else if self.token == token::LBRACE {
2027-
// This might be a struct literal.
2028-
if self.looking_at_struct_literal() {
2028+
// This is a struct literal, unless we're prohibited from
2029+
// parsing struct literals here.
2030+
if self.restriction != RESTRICT_NO_STRUCT_LITERAL {
20292031
// It's a struct literal.
20302032
self.bump();
20312033
let mut fields = Vec::new();
@@ -2042,6 +2044,14 @@ impl<'a> Parser<'a> {
20422044
&[token::COMMA], &[token::RBRACE]);
20432045
}
20442046

2047+
if fields.len() == 0 && base.is_none() {
2048+
let last_span = self.last_span;
2049+
self.span_err(last_span,
2050+
"structure literal must either have at \
2051+
least one field or use functional \
2052+
structure update syntax");
2053+
}
2054+
20452055
hi = self.span.hi;
20462056
self.expect(&token::RBRACE);
20472057
ex = ExprStruct(pth, fields, base);
@@ -2548,7 +2558,7 @@ impl<'a> Parser<'a> {
25482558
// parse an 'if' expression ('if' token already eaten)
25492559
pub fn parse_if_expr(&mut self) -> Gc<Expr> {
25502560
let lo = self.last_span.lo;
2551-
let cond = self.parse_expr();
2561+
let cond = self.parse_expr_res(RESTRICT_NO_STRUCT_LITERAL);
25522562
let thn = self.parse_block();
25532563
let mut els: Option<Gc<Expr>> = None;
25542564
let mut hi = thn.span.hi;
@@ -2633,7 +2643,7 @@ impl<'a> Parser<'a> {
26332643
let lo = self.last_span.lo;
26342644
let pat = self.parse_pat();
26352645
self.expect_keyword(keywords::In);
2636-
let expr = self.parse_expr();
2646+
let expr = self.parse_expr_res(RESTRICT_NO_STRUCT_LITERAL);
26372647
let loop_block = self.parse_block();
26382648
let hi = self.span.hi;
26392649

@@ -2642,7 +2652,7 @@ impl<'a> Parser<'a> {
26422652

26432653
pub fn parse_while_expr(&mut self) -> Gc<Expr> {
26442654
let lo = self.last_span.lo;
2645-
let cond = self.parse_expr();
2655+
let cond = self.parse_expr_res(RESTRICT_NO_STRUCT_LITERAL);
26462656
let body = self.parse_block();
26472657
let hi = body.span.hi;
26482658
return self.mk_expr(lo, hi, ExprWhile(cond, body));
@@ -2655,17 +2665,9 @@ impl<'a> Parser<'a> {
26552665
self.mk_expr(lo, hi, ExprLoop(body, opt_ident))
26562666
}
26572667

2658-
// For distinguishing between struct literals and blocks
2659-
fn looking_at_struct_literal(&mut self) -> bool {
2660-
self.token == token::LBRACE &&
2661-
((self.look_ahead(1, |t| token::is_plain_ident(t)) &&
2662-
self.look_ahead(2, |t| *t == token::COLON))
2663-
|| self.look_ahead(1, |t| *t == token::DOTDOT))
2664-
}
2665-
26662668
fn parse_match_expr(&mut self) -> Gc<Expr> {
26672669
let lo = self.last_span.lo;
2668-
let discriminant = self.parse_expr();
2670+
let discriminant = self.parse_expr_res(RESTRICT_NO_STRUCT_LITERAL);
26692671
self.commit_expr_expecting(discriminant, token::LBRACE);
26702672
let mut arms: Vec<Arm> = Vec::new();
26712673
while self.token != token::RBRACE {

src/test/compile-fail/bind-struct-early-modifiers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
fn main() {
1212
struct Foo { x: int }
13-
match Foo { x: 10 } {
13+
match (Foo { x: 10 }) {
1414
Foo { ref x: ref x } => {}, //~ ERROR unexpected `:`
1515
_ => {}
1616
}

src/test/compile-fail/borrowck-match-binding-is-assignment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn main() {
3131
}
3232
}
3333

34-
match S { bar: 1 } {
34+
match (S { bar: 1 }) {
3535
S { bar: x } => {
3636
x += 1; //~ ERROR re-assignment of immutable variable `x`
3737
}

src/test/compile-fail/borrowck-move-error-with-note.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl Drop for S {
3434
}
3535

3636
fn move_in_match() {
37-
match S {f: "foo".to_string(), g: "bar".to_string()} {
37+
match (S {f: "foo".to_string(), g: "bar".to_string()}) {
3838
S { //~ ERROR cannot move out of type `S`, which defines the `Drop` trait
3939
f: _s, //~ NOTE attempting to move value to here
4040
g: _t //~ NOTE and here

src/test/compile-fail/borrowck-move-out-of-struct-with-dtor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl Drop for S {
1414
}
1515

1616
fn move_in_match() {
17-
match S {f:"foo".to_string()} {
17+
match (S {f:"foo".to_string()}) {
1818
S {f:_s} => {}
1919
//~^ ERROR cannot move out of type `S`, which defines the `Drop` trait
2020
}

src/test/compile-fail/match-struct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct S { a: int }
1313
enum E { C(int) }
1414

1515
fn main() {
16-
match S { a: 1 } {
16+
match (S { a: 1 }) {
1717
C(_) => (), //~ ERROR mismatched types: expected `S` but found `E`
1818
_ => ()
1919
}

src/test/compile-fail/non-exhaustive-pattern-witness.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ enum Color {
2222
}
2323

2424
fn struct_with_a_nested_enum_and_vector() {
25-
match Foo { first: true, second: None } {
25+
match (Foo { first: true, second: None }) {
2626
//~^ ERROR non-exhaustive patterns: `Foo{first: false, second: Some([_, _, _, _])}` not covered
2727
Foo { first: true, second: None } => (),
2828
Foo { first: true, second: Some(_) } => (),
@@ -71,4 +71,4 @@ fn main() {
7171
struct_with_a_nested_enum_and_vector();
7272
enum_with_multiple_missing_variants();
7373
enum_struct_variant();
74-
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo {
12+
x: int,
13+
}
14+
15+
impl Foo {
16+
fn hi(&self) -> bool {
17+
true
18+
}
19+
}
20+
21+
fn main() {
22+
for x in Foo {
23+
x: 3 //~ ERROR expected one of `;`, `}`
24+
}.hi() {
25+
println!("yo");
26+
}
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo {
12+
x: int,
13+
}
14+
15+
impl Foo {
16+
fn hi(&self) -> bool {
17+
true
18+
}
19+
}
20+
21+
fn main() {
22+
if Foo {
23+
x: 3 //~ ERROR expected one of `;`, `}`
24+
}.hi() {
25+
println!("yo");
26+
}
27+
}
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo {
12+
x: int,
13+
}
14+
15+
fn main() {
16+
match Foo {
17+
x: 3 //~ ERROR expected `=>`
18+
} {
19+
Foo {
20+
x: x
21+
} => {}
22+
}
23+
}
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo {
12+
x: int,
13+
}
14+
15+
impl Foo {
16+
fn hi(&self) -> bool {
17+
true
18+
}
19+
}
20+
21+
fn main() {
22+
while Foo {
23+
x: 3 //~ ERROR expected one of `;`, `}`
24+
}.hi() {
25+
println!("yo");
26+
}
27+
}
28+

src/test/compile-fail/struct-no-fields-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Foo;
1212

1313
fn f2() {
1414
let _end_stmt = Foo { };
15-
//~^ ERROR: unit-like struct construction is written with no trailing `{ }`
15+
//~^ ERROR: structure literal must either have at least one field
1616
}
1717

1818
fn main() {}

src/test/compile-fail/struct-no-fields-3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Foo;
1212

1313
fn g3() {
1414
let _mid_tuple = (Foo { }, 2);
15-
//~^ ERROR: unit-like struct construction is written with no trailing `{ }`
15+
//~^ ERROR: structure literal must either have at least one field
1616
}
1717

1818
fn main() {}

src/test/compile-fail/struct-no-fields-4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Foo;
1212

1313
fn h4() {
1414
let _end_of_tuple = (3, Foo { });
15-
//~^ ERROR: unit-like struct construction is written with no trailing `{ }`
15+
//~^ ERROR: structure literal must either have at least one field
1616
}
1717

1818
fn main() {}

src/test/compile-fail/struct-no-fields-5.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct Foo;
1212

1313
fn i5() {
1414
let _end_of_block = { Foo { } };
15-
//~^ ERROR: unit-like struct construction is written with no trailing `{ }`
15+
//~^ ERROR: structure literal must either have at least one field
1616
}
1717

1818
fn main() {}

src/test/debuginfo/lexical-scope-in-match.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,15 @@ fn main() {
105105
_ => {}
106106
}
107107

108-
match Struct { x: 237, y: 238 } {
108+
match (Struct { x: 237, y: 238 }) {
109109
Struct { x: shadowed, y: local_to_arm } => {
110110

111111
zzz();
112112
sentinel();
113113
}
114114
}
115115

116-
match Struct { x: 239, y: 240 } {
116+
match (Struct { x: 239, y: 240 }) {
117117
// ignored field
118118
Struct { x: shadowed, .. } => {
119119

@@ -122,7 +122,7 @@ fn main() {
122122
}
123123
}
124124

125-
match Struct { x: 241, y: 242 } {
125+
match (Struct { x: 241, y: 242 }) {
126126
// with literal
127127
Struct { x: shadowed, y: 242 } => {
128128

src/test/run-pass/guards.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn main() {
1616
assert_eq!(a, 2);
1717

1818
let b =
19-
match Pair {x: 10, y: 20} {
19+
match (Pair {x: 10, y: 20}) {
2020
x if x.x < 5 && x.y < 5 => { 1 }
2121
Pair {x: x, y: y} if x == 10 && y == 20 => { 2 }
2222
Pair {x: _x, y: _y} => { 3 }

src/test/run-pass/match-in-macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ enum Foo {
1616

1717
macro_rules! match_inside_expansion(
1818
() => (
19-
match B { b1:29 , bb1: 100} {
19+
match (B { b1:29 , bb1: 100}) {
2020
B { b1:b2 , bb1:bb2 } => b2+bb2
2121
}
2222
)

src/test/run-pass/nested-exhaustive-match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
struct Foo { foo: bool, bar: Option<int>, baz: int }
1212

1313
pub fn main() {
14-
match Foo{foo: true, bar: Some(10), baz: 20} {
14+
match (Foo{foo: true, bar: Some(10), baz: 20}) {
1515
Foo{foo: true, bar: Some(_), ..} => {}
1616
Foo{foo: false, bar: None, ..} => {}
1717
Foo{foo: true, bar: None, ..} => {}

src/test/run-pass/nested-patterns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct D { a: int, d: C }
1414
struct C { c: int }
1515

1616
pub fn main() {
17-
match A {a: 10, b: 20} {
17+
match (A {a: 10, b: 20}) {
1818
x@A {a, b: 20} => { assert!(x.a == 10); assert!(a == 10); }
1919
A {b: _b, ..} => { fail!(); }
2020
}

src/test/run-pass/struct_variant_xc_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern crate struct_variant_xc_aux;
1414
use struct_variant_xc_aux::{StructVariant, Variant};
1515

1616
pub fn main() {
17-
let arg = match StructVariant { arg: 42 } {
17+
let arg = match (StructVariant { arg: 42 }) {
1818
Variant(_) => unreachable!(),
1919
StructVariant { arg } => arg
2020
};

0 commit comments

Comments
 (0)