Skip to content

Commit 7239d77

Browse files
committed
Point at : when using it instead of ;
When triggering type ascription in such a way that we can infer a statement end was intended, add a suggestion for the change. Always point out the reason for the expectation of a type is due to type ascription.
1 parent 8f1339a commit 7239d77

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2798,7 +2798,22 @@ impl<'a> Parser<'a> {
27982798
lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
27992799
continue
28002800
} else if op == AssocOp::Colon {
2801-
lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Type)?;
2801+
lhs = match self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Type) {
2802+
Ok(lhs) => lhs,
2803+
Err(mut err) => {
2804+
err.span_label(self.span,
2805+
"expecting a type here because of type ascription");
2806+
let cm = self.sess.codemap();
2807+
let cur_pos = cm.lookup_char_pos(self.span.lo);
2808+
let op_pos = cm.lookup_char_pos(cur_op_span.hi);
2809+
if cur_pos.line != op_pos.line {
2810+
err.span_suggestion(cur_op_span,
2811+
"did you mean to end the statement here instead?",
2812+
";".to_string());
2813+
}
2814+
return Err(err);
2815+
}
2816+
};
28022817
continue
28032818
} else if op == AssocOp::DotDot || op == AssocOp::DotDotDot {
28042819
// If we didn’t have to handle `x..`/`x...`, it would be pretty easy to
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 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+
#![feature(type_ascription)]
12+
13+
fn main() {
14+
println!("test"):
15+
0;
16+
}
17+
18+
fn foo() {
19+
println!("test"): 0;
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: expected type, found `0`
2+
--> $DIR/type-ascription-instead-of-statement-end.rs:15:5
3+
|
4+
14 | println!("test"):
5+
| - help: did you mean to end the statement here instead? `;`
6+
15 | 0;
7+
| ^ expecting a type here because of type ascription
8+
9+
error: expected type, found `0`
10+
--> $DIR/type-ascription-instead-of-statement-end.rs:19:23
11+
|
12+
19 | println!("test"): 0;
13+
| ^ expecting a type here because of type ascription
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)