Skip to content

Commit 3440429

Browse files
committed
const type with colon also get type infer
1 parent 2da8820 commit 3440429

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

compiler/rustc_parse/src/parser/item.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1176,14 +1176,13 @@ impl<'a> Parser<'a> {
11761176
m: Option<Mutability>,
11771177
) -> PResult<'a, (Ident, P<Ty>, Option<P<ast::Expr>>)> {
11781178
let id = if m.is_none() { self.parse_ident_or_underscore() } else { self.parse_ident() }?;
1179-
11801179
// Parse the type of a `const` or `static mut?` item.
11811180
// That is, the `":" $ty` fragment.
1182-
let ty = if self.eat(&token::Colon) {
1183-
self.parse_ty()?
1184-
} else {
1181+
let _ = self.eat(&token::Colon);
1182+
let ty = self.parse_ty().unwrap_or_else(|e| {
1183+
e.cancel();
11851184
self.recover_missing_const_type(id, m)
1186-
};
1185+
});
11871186

11881187
let expr = if self.eat(&token::Eq) { Some(self.parse_expr()?) } else { None };
11891188
self.expect_semi()?;

src/test/ui/parser/removed-syntax-static-fn.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ struct S;
33
impl S {
44
static fn f() {}
55
//~^ ERROR expected identifier, found keyword `fn`
6-
//~| ERROR expected one of `:`, `;`, or `=`
7-
//~| ERROR missing type for `static` item
6+
//~| ERROR expected one of `!`, `+`, `->`, `::`, `;`, or `=`, found `{`
87
}
98

109
fn main() {}

src/test/ui/parser/removed-syntax-static-fn.stderr

+4-10
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,16 @@ error: expected identifier, found keyword `fn`
44
LL | static fn f() {}
55
| ^^ expected identifier, found keyword
66

7-
error: expected one of `:`, `;`, or `=`, found `f`
8-
--> $DIR/removed-syntax-static-fn.rs:4:15
7+
error: expected one of `!`, `+`, `->`, `::`, `;`, or `=`, found `{`
8+
--> $DIR/removed-syntax-static-fn.rs:4:19
99
|
1010
LL | impl S {
1111
| - while parsing this item list starting here
1212
LL | static fn f() {}
13-
| ^ expected one of `:`, `;`, or `=`
13+
| ^ expected one of `!`, `+`, `->`, `::`, `;`, or `=`
1414
...
1515
LL | }
1616
| - the item list ends here
1717

18-
error: missing type for `static` item
19-
--> $DIR/removed-syntax-static-fn.rs:4:12
20-
|
21-
LL | static fn f() {}
22-
| ^^ help: provide a type for the item: `r#fn: <type>`
23-
24-
error: aborting due to 3 previous errors
18+
error: aborting due to 2 previous errors
2519

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
const a: = 123;
3+
//~^ ERROR missing type for `const` item
4+
const b = 345;
5+
//~^ ERROR missing type for `const` item
6+
7+
const c: = "hello";
8+
//~^ ERROR missing type for `const` item
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: missing type for `const` item
2+
--> $DIR/const-without-type-but-colon.rs:2:11
3+
|
4+
LL | const a: = 123;
5+
| ^ help: provide a type for the constant: `a: i32`
6+
7+
error: missing type for `const` item
8+
--> $DIR/const-without-type-but-colon.rs:4:11
9+
|
10+
LL | const b = 345;
11+
| ^ help: provide a type for the constant: `b: i32`
12+
13+
error: missing type for `const` item
14+
--> $DIR/const-without-type-but-colon.rs:7:11
15+
|
16+
LL | const c: = "hello";
17+
| ^ help: provide a type for the constant: `c: &str`
18+
19+
error: aborting due to 3 previous errors
20+

0 commit comments

Comments
 (0)