From 049f0a6d2c31e8f5f4787cb7e3e89834cbb50ac4 Mon Sep 17 00:00:00 2001 From: hkalbasi Date: Fri, 1 Apr 2022 17:20:54 +0430 Subject: [PATCH] recover from missing type annotation --- crates/hir_ty/src/tests/simple.rs | 14 ++++++++++++++ crates/parser/src/grammar/types.rs | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index df7b3df3d5..baed34ce23 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs @@ -2556,6 +2556,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( diff --git a/crates/parser/src/grammar/types.rs b/crates/parser/src/grammar/types.rs index ff067f5293..46db487d02 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); }