about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-04-01 15:33:01 +0000
committerGitHub <noreply@github.com>2022-04-01 15:33:01 +0000
commitf8a21e4c70bba61d63472c730f7e0b16324b5806 (patch)
treeabf5e3455908fafc2fb9f85ea0dea6973b72e839
parentb337a49d21b6ab04c0aa06834523de275a033ca9 (diff)
parent049f0a6d2c31e8f5f4787cb7e3e89834cbb50ac4 (diff)
downloadrust-f8a21e4c70bba61d63472c730f7e0b16324b5806.tar.gz
rust-f8a21e4c70bba61d63472c730f7e0b16324b5806.zip
Merge #11870
11870: Recover from missing type annotation r=Veykril a=HKalbasi

We were missing the init expression in case of `let x: = 2`, which breaks type inference of that variable (previously x were `{unknown}`, now it is `i32`).


Co-authored-by: hkalbasi <hamidrezakalbasi@protonmail.com>
-rw-r--r--crates/hir_ty/src/tests/simple.rs14
-rw-r--r--crates/parser/src/grammar/types.rs6
2 files changed, 20 insertions, 0 deletions
diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs
index df7b3df3d5b..baed34ce238 100644
--- a/crates/hir_ty/src/tests/simple.rs
+++ b/crates/hir_ty/src/tests/simple.rs
@@ -2557,6 +2557,20 @@ fn f() {
 }
 
 #[test]
+fn infer_missing_type() {
+    check_types(
+        r#"
+struct S;
+
+fn f() {
+    let s: = S;
+      //^ S
+}
+    "#,
+    );
+}
+
+#[test]
 fn infer_type_alias_variant() {
     check_infer(
         r#"
diff --git a/crates/parser/src/grammar/types.rs b/crates/parser/src/grammar/types.rs
index ff067f5293d..46db487d02c 100644
--- a/crates/parser/src/grammar/types.rs
+++ b/crates/parser/src/grammar/types.rs
@@ -57,6 +57,12 @@ fn type_with_bounds_cond(p: &mut Parser, allow_bounds: bool) {
 pub(super) fn ascription(p: &mut Parser) {
     assert!(p.at(T![:]));
     p.bump(T![:]);
+    if p.at(T![=]) {
+        // recover from `let x: = expr;`, `const X: = expr;` and similars
+        // hopefully no type starts with `=`
+        p.error("missing type");
+        return;
+    }
     type_(p);
 }