Emit specific error for struct literal in conditions

This commit is contained in:
Esteban Küber 2019-04-14 17:09:03 -07:00
parent a2bbf7deba
commit 2f36b54f0f
14 changed files with 174 additions and 219 deletions

View file

@ -897,8 +897,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
self.cannot_borrow_path_as_mutable(error_span, &descr, Origin::Ast) self.cannot_borrow_path_as_mutable(error_span, &descr, Origin::Ast)
} }
BorrowViolation(euv::ClosureInvocation) => { BorrowViolation(euv::ClosureInvocation) => {
span_bug!(err.span, span_bug!(err.span, "err_mutbl with a closure invocation");
"err_mutbl with a closure invocation");
} }
}; };
@ -1096,7 +1095,6 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
BorrowViolation(euv::MatchDiscriminant) => { BorrowViolation(euv::MatchDiscriminant) => {
"cannot borrow data mutably" "cannot borrow data mutably"
} }
BorrowViolation(euv::ClosureInvocation) => { BorrowViolation(euv::ClosureInvocation) => {
is_closure = true; is_closure = true;
"closure invocation" "closure invocation"

View file

@ -2855,11 +2855,13 @@ impl<'a> Parser<'a> {
let (delim, tts) = self.expect_delimited_token_tree()?; let (delim, tts) = self.expect_delimited_token_tree()?;
hi = self.prev_span; hi = self.prev_span;
ex = ExprKind::Mac(respan(lo.to(hi), Mac_ { path, tts, delim })); ex = ExprKind::Mac(respan(lo.to(hi), Mac_ { path, tts, delim }));
} else if self.check(&token::OpenDelim(token::Brace)) && } else if self.check(&token::OpenDelim(token::Brace)) {
!self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL) { if let Some(expr) = self.should_parse_struct_expr(lo, path.clone(), attrs.clone()) {
// This is a struct literal, unless we're prohibited return expr;
// from parsing struct literals here. } else {
return self.parse_struct_expr(lo, path, attrs); hi = path.span;
ex = ExprKind::Path(None, path);
}
} else { } else {
hi = path.span; hi = path.span;
ex = ExprKind::Path(None, path); ex = ExprKind::Path(None, path);
@ -2902,6 +2904,51 @@ impl<'a> Parser<'a> {
self.maybe_recover_from_bad_qpath(expr, true) self.maybe_recover_from_bad_qpath(expr, true)
} }
fn should_parse_struct_expr(
&mut self,
lo: Span,
path: ast::Path,
attrs: ThinVec<Attribute>,
) -> Option<PResult<'a, P<Expr>>> {
let could_be_struct = self.look_ahead(1, |t| t.is_ident()) && (
self.look_ahead(2, |t| *t == token::Colon)
|| self.look_ahead(2, |t| *t == token::Comma)
// We could also check for `token::CloseDelim(token::Brace)`, but that would
// have false positives in the case of `if x == y { z } { a }`.
);
let mut bad_struct = false;
let mut parse_struct = !self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL);
if self.restrictions.contains(Restrictions::NO_STRUCT_LITERAL) && could_be_struct {
// This is a struct literal, but we don't can't accept them here
bad_struct = true;
parse_struct = true;
}
if parse_struct {
match self.parse_struct_expr(lo, path, attrs) {
Err(err) => return Some(Err(err)),
Ok(expr) => {
if bad_struct {
let mut err = self.diagnostic().struct_span_err(
expr.span,
"struct literals are not allowed here",
);
err.multipart_suggestion(
"surround the struct literal with parenthesis",
vec![
(lo.shrink_to_lo(), "(".to_string()),
(expr.span.shrink_to_hi(), ")".to_string()),
],
Applicability::MachineApplicable,
);
err.emit();
}
return Some(Ok(expr));
}
}
}
None
}
fn parse_struct_expr(&mut self, lo: Span, pth: ast::Path, mut attrs: ThinVec<Attribute>) fn parse_struct_expr(&mut self, lo: Span, pth: ast::Path, mut attrs: ThinVec<Attribute>)
-> PResult<'a, P<Expr>> { -> PResult<'a, P<Expr>> {
let struct_sp = lo.to(self.prev_span); let struct_sp = lo.to(self.prev_span);

View file

@ -10,8 +10,7 @@ fn bar() {
struct T {} struct T {}
if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); } if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
//~^ ERROR E0423 //~^ ERROR struct literals are not allowed here
//~| expected type, found `1`
if T {} == T {} { println!("Ok"); } if T {} == T {} { println!("Ok"); }
//~^ ERROR E0423 //~^ ERROR E0423
//~| ERROR expected expression, found `==` //~| ERROR expected expression, found `==`
@ -19,6 +18,5 @@ fn bar() {
fn foo() { fn foo() {
for _ in std::ops::Range { start: 0, end: 10 } {} for _ in std::ops::Range { start: 0, end: 10 } {}
//~^ ERROR E0423 //~^ ERROR struct literals are not allowed here
//~| ERROR expected type, found `0`
} }

View file

@ -1,36 +1,28 @@
error: expected type, found `1` error: struct literals are not allowed here
--> $DIR/E0423.rs:12:39 --> $DIR/E0423.rs:12:32
| |
LL | if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); } LL | if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
| ^ expecting a type here because of type ascription | ^^^^^^^^^^^^^^^^
help: surround the struct literal with parenthesis
| |
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>` LL | if let S { x: _x, y: 2 } = (S { x: 1, y: 2 }) { println!("Ok"); }
note: this expression expects an ascribed type after the colon | ^ ^
--> $DIR/E0423.rs:12:36
|
LL | if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
| ^
= help: this might be indicative of a syntax error elsewhere
error: expected expression, found `==` error: expected expression, found `==`
--> $DIR/E0423.rs:15:13 --> $DIR/E0423.rs:14:13
| |
LL | if T {} == T {} { println!("Ok"); } LL | if T {} == T {} { println!("Ok"); }
| ^^ expected expression | ^^ expected expression
error: expected type, found `0` error: struct literals are not allowed here
--> $DIR/E0423.rs:21:39 --> $DIR/E0423.rs:20:14
| |
LL | for _ in std::ops::Range { start: 0, end: 10 } {} LL | for _ in std::ops::Range { start: 0, end: 10 } {}
| ^ expecting a type here because of type ascription | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: surround the struct literal with parenthesis
| |
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>` LL | for _ in (std::ops::Range { start: 0, end: 10 }) {}
note: this expression expects an ascribed type after the colon | ^ ^
--> $DIR/E0423.rs:21:32
|
LL | for _ in std::ops::Range { start: 0, end: 10 } {}
| ^^^^^
= help: this might be indicative of a syntax error elsewhere
error[E0423]: expected function, found struct `Foo` error[E0423]: expected function, found struct `Foo`
--> $DIR/E0423.rs:4:13 --> $DIR/E0423.rs:4:13
@ -41,30 +33,14 @@ LL | let f = Foo();
| did you mean `Foo { /* fields */ }`? | did you mean `Foo { /* fields */ }`?
| help: a function with a similar name exists: `foo` | help: a function with a similar name exists: `foo`
error[E0423]: expected value, found struct `S`
--> $DIR/E0423.rs:12:32
|
LL | if let S { x: _x, y: 2 } = S { x: 1, y: 2 } { println!("Ok"); }
| ^---------------
| |
| help: surround the struct literal with parenthesis: `(S { x: 1, y: 2 })`
error[E0423]: expected value, found struct `T` error[E0423]: expected value, found struct `T`
--> $DIR/E0423.rs:15:8 --> $DIR/E0423.rs:14:8
| |
LL | if T {} == T {} { println!("Ok"); } LL | if T {} == T {} { println!("Ok"); }
| ^--- | ^---
| | | |
| help: surround the struct literal with parenthesis: `(T {})` | help: surround the struct literal with parenthesis: `(T {})`
error[E0423]: expected value, found struct `std::ops::Range` error: aborting due to 5 previous errors
--> $DIR/E0423.rs:21:14
|
LL | for _ in std::ops::Range { start: 0, end: 10 } {}
| ^^^^^^^^^^^^^^^----------------------
| |
| help: surround the struct literal with parenthesis: `(std::ops::Range { start: 0, end: 10 })`
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0423`. For more information about this error, try `rustc --explain E0423`.

View file

@ -9,9 +9,9 @@ impl Foo {
} }
fn main() { fn main() {
for x in Foo { //~ ERROR expected value, found struct `Foo` for x in Foo { //~ ERROR struct literals are not allowed here
x: 3 //~ ERROR expected type, found `3` x: 3 //~^ ERROR `bool` is not an iterator
}.hi() { //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `{` }.hi() {
println!("yo"); println!("yo");
} }
} }

View file

@ -1,29 +1,30 @@
error: expected type, found `3` error: struct literals are not allowed here
--> $DIR/struct-literal-in-for.rs:13:12
|
LL | x: 3
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/struct-literal-in-for.rs:13:9
|
LL | x: 3
| ^
= help: this might be indicative of a syntax error elsewhere
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `{`
--> $DIR/struct-literal-in-for.rs:14:12
|
LL | }.hi() {
| ^ expected one of `.`, `;`, `?`, `}`, or an operator here
error[E0423]: expected value, found struct `Foo`
--> $DIR/struct-literal-in-for.rs:12:14 --> $DIR/struct-literal-in-for.rs:12:14
| |
LL | for x in Foo { LL | for x in Foo {
| ^^^ did you mean `(Foo { /* fields */ })`? | ______________^
LL | | x: 3
LL | | }.hi() {
| |_____^
help: surround the struct literal with parenthesis
|
LL | for x in (Foo {
LL | x: 3
LL | }).hi() {
|
error: aborting due to 3 previous errors error[E0277]: `bool` is not an iterator
--> $DIR/struct-literal-in-for.rs:12:14
|
LL | for x in Foo {
| ______________^
LL | | x: 3
LL | | }.hi() {
| |__________^ `bool` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `bool`
= note: required by `std::iter::IntoIterator::into_iter`
For more information about this error, try `rustc --explain E0423`. error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.

View file

@ -9,9 +9,9 @@ impl Foo {
} }
fn main() { fn main() {
if Foo { //~ ERROR expected value, found struct `Foo` if Foo { //~ ERROR struct literals are not allowed here
x: 3 //~ ERROR expected type, found `3` x: 3
}.hi() { //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `{` }.hi() {
println!("yo"); println!("yo");
} }
} }

View file

@ -1,29 +1,17 @@
error: expected type, found `3` error: struct literals are not allowed here
--> $DIR/struct-literal-in-if.rs:13:12
|
LL | x: 3
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/struct-literal-in-if.rs:13:9
|
LL | x: 3
| ^
= help: this might be indicative of a syntax error elsewhere
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `{`
--> $DIR/struct-literal-in-if.rs:14:12
|
LL | }.hi() {
| ^ expected one of `.`, `;`, `?`, `}`, or an operator here
error[E0423]: expected value, found struct `Foo`
--> $DIR/struct-literal-in-if.rs:12:8 --> $DIR/struct-literal-in-if.rs:12:8
| |
LL | if Foo { LL | if Foo {
| ^^^ did you mean `(Foo { /* fields */ })`? | ________^
LL | | x: 3
LL | | }.hi() {
| |_____^
help: surround the struct literal with parenthesis
|
LL | if (Foo {
LL | x: 3
LL | }).hi() {
|
error: aborting due to 3 previous errors error: aborting due to previous error
For more information about this error, try `rustc --explain E0423`.

View file

@ -3,11 +3,11 @@ struct Foo {
} }
fn main() { fn main() {
match Foo { //~ ERROR expected value, found struct `Foo` match Foo { //~ ERROR struct literals are not allowed here
x: 3 //~ ERROR expected one of `=>`, `@`, `if`, or `|`, found `:` x: 3
} { } {
Foo { //~ ERROR mismatched types Foo {
x: x //~ ERROR cannot find value `x` in this scope x: x
} => {} //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `=>` } => {}
} }
} }

View file

@ -1,42 +1,16 @@
error: expected one of `=>`, `@`, `if`, or `|`, found `:` error: struct literals are not allowed here
--> $DIR/struct-literal-in-match-discriminant.rs:7:10
|
LL | x: 3
| ^ expected one of `=>`, `@`, `if`, or `|` here
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `=>`
--> $DIR/struct-literal-in-match-discriminant.rs:11:11
|
LL | } => {}
| ^^ expected one of `.`, `;`, `?`, `}`, or an operator here
error[E0423]: expected value, found struct `Foo`
--> $DIR/struct-literal-in-match-discriminant.rs:6:11 --> $DIR/struct-literal-in-match-discriminant.rs:6:11
| |
LL | match Foo { LL | match Foo {
| ^^^ did you mean `(Foo { /* fields */ })`? | ___________^
LL | | x: 3
error[E0425]: cannot find value `x` in this scope LL | | } {
--> $DIR/struct-literal-in-match-discriminant.rs:10:16 | |_____^
help: surround the struct literal with parenthesis
| |
LL | x: x LL | match (Foo {
| ^ not found in this scope LL | x: 3
LL | }) {
error[E0308]: mismatched types
--> $DIR/struct-literal-in-match-discriminant.rs:9:9
| |
LL | fn main() {
| - expected `()` because of default return type
...
LL | / Foo {
LL | | x: x
LL | | } => {}
| |_________^ expected (), found struct `Foo`
|
= note: expected type `()`
found type `Foo`
error: aborting due to 5 previous errors error: aborting due to previous error
Some errors have detailed explanations: E0308, E0423, E0425.
For more information about an error, try `rustc --explain E0308`.

View file

@ -9,10 +9,9 @@ impl Foo {
} }
fn main() { fn main() {
while Foo { //~ ERROR expected value, found struct `Foo` while Foo { //~ ERROR struct literals are not allowed here
x: 3 //~ ERROR expected type, found `3` x: 3
}.hi() { //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `{` }.hi() {
//~| ERROR no method named `hi` found for type `()` in the current scope
println!("yo"); println!("yo");
} }
} }

View file

@ -1,36 +1,17 @@
error: expected type, found `3` error: struct literals are not allowed here
--> $DIR/struct-literal-in-while.rs:13:12
|
LL | x: 3
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/struct-literal-in-while.rs:13:9
|
LL | x: 3
| ^
= help: this might be indicative of a syntax error elsewhere
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `{`
--> $DIR/struct-literal-in-while.rs:14:12
|
LL | }.hi() {
| ^ expected one of `.`, `;`, `?`, `}`, or an operator here
error[E0423]: expected value, found struct `Foo`
--> $DIR/struct-literal-in-while.rs:12:11 --> $DIR/struct-literal-in-while.rs:12:11
| |
LL | while Foo { LL | while Foo {
| ^^^ did you mean `(Foo { /* fields */ })`? | ___________^
LL | | x: 3
error[E0599]: no method named `hi` found for type `()` in the current scope LL | | }.hi() {
--> $DIR/struct-literal-in-while.rs:14:7 | |_____^
help: surround the struct literal with parenthesis
|
LL | while (Foo {
LL | x: 3
LL | }).hi() {
| |
LL | }.hi() {
| ^^
error: aborting due to 4 previous errors error: aborting due to previous error
Some errors have detailed explanations: E0423, E0599.
For more information about an error, try `rustc --explain E0423`.

View file

@ -9,10 +9,9 @@ impl Foo {
} }
fn main() { fn main() {
while || Foo { //~ ERROR expected value, found struct `Foo` while || Foo { //~ ERROR struct literals are not allowed here
x: 3 //~ ERROR expected type, found `3` x: 3 //~^ ERROR mismatched types
}.hi() { //~ ERROR expected one of `.`, `;`, `?`, `}`, or an operator, found `{` }.hi() {
//~| ERROR no method named `hi` found for type `()` in the current scope
println!("yo"); println!("yo");
} }
} }

View file

@ -1,36 +1,30 @@
error: expected type, found `3` error: struct literals are not allowed here
--> $DIR/struct-literal-restrictions-in-lamda.rs:13:12
|
LL | x: 3
| ^ expecting a type here because of type ascription
|
= note: type ascription is a nightly-only feature that lets you annotate an expression with a type: `<expr>: <type>`
note: this expression expects an ascribed type after the colon
--> $DIR/struct-literal-restrictions-in-lamda.rs:13:9
|
LL | x: 3
| ^
= help: this might be indicative of a syntax error elsewhere
error: expected one of `.`, `;`, `?`, `}`, or an operator, found `{`
--> $DIR/struct-literal-restrictions-in-lamda.rs:14:12
|
LL | }.hi() {
| ^ expected one of `.`, `;`, `?`, `}`, or an operator here
error[E0423]: expected value, found struct `Foo`
--> $DIR/struct-literal-restrictions-in-lamda.rs:12:14 --> $DIR/struct-literal-restrictions-in-lamda.rs:12:14
| |
LL | while || Foo { LL | while || Foo {
| ^^^ did you mean `(Foo { /* fields */ })`? | ______________^
LL | | x: 3
error[E0599]: no method named `hi` found for type `()` in the current scope LL | | }.hi() {
--> $DIR/struct-literal-restrictions-in-lamda.rs:14:7 | |_____^
help: surround the struct literal with parenthesis
|
LL | while || (Foo {
LL | x: 3
LL | }).hi() {
| |
LL | }.hi() {
| ^^
error: aborting due to 4 previous errors error[E0308]: mismatched types
--> $DIR/struct-literal-restrictions-in-lamda.rs:12:11
|
LL | while || Foo {
| ___________^
LL | | x: 3
LL | | }.hi() {
| |__________^ expected bool, found closure
|
= note: expected type `bool`
found type `[closure@$DIR/struct-literal-restrictions-in-lamda.rs:12:11: 14:11]`
Some errors have detailed explanations: E0423, E0599. error: aborting due to 2 previous errors
For more information about an error, try `rustc --explain E0423`.
For more information about this error, try `rustc --explain E0308`.