aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-20 08:22:53 +0100
committerGitHub <[email protected]>2020-05-20 08:22:53 +0100
commitc0bcaea46652ade4259559f08368179d54d4fdd1 (patch)
treed6f3f19a010aa8c4de653df0d2638d8fd3aa8e78
parentefac093093f1bd598957822e9e859b33c5f13e1f (diff)
parent45021cae551826727c32c7499c68ca48d046890f (diff)
Merge #4505
4505: Infer return type of loops with value breaks r=flodiebold a=ruabmbua Creates a type variable to represent the return value of the loop. Uses `coerce_merge_branch` on each break with the previous value, to determine the actual return value of the loop. Resolves: https://github.com/rust-analyzer/rust-analyzer/issues/4492 , https://github.com/rust-analyzer/rust-analyzer/issues/4512 Co-authored-by: Roland Ruckerbauer <[email protected]>
-rw-r--r--crates/ra_hir_ty/src/_match.rs39
-rw-r--r--crates/ra_hir_ty/src/infer.rs1
-rw-r--r--crates/ra_hir_ty/src/infer/expr.rs33
-rw-r--r--crates/ra_hir_ty/src/tests/simple.rs63
4 files changed, 106 insertions, 30 deletions
diff --git a/crates/ra_hir_ty/src/_match.rs b/crates/ra_hir_ty/src/_match.rs
index 149f65042..3e6e1e333 100644
--- a/crates/ra_hir_ty/src/_match.rs
+++ b/crates/ra_hir_ty/src/_match.rs
@@ -1946,6 +1946,23 @@ mod tests {
1946 1946
1947 check_no_diagnostic(content); 1947 check_no_diagnostic(content);
1948 } 1948 }
1949
1950 #[test]
1951 fn expr_diverges_missing_arm() {
1952 let content = r"
1953 enum Either {
1954 A,
1955 B,
1956 }
1957 fn test_fn() {
1958 match loop {} {
1959 Either::A => (),
1960 }
1961 }
1962 ";
1963
1964 check_no_diagnostic(content);
1965 }
1949} 1966}
1950 1967
1951#[cfg(test)] 1968#[cfg(test)]
@@ -1998,26 +2015,6 @@ mod false_negatives {
1998 } 2015 }
1999 2016
2000 #[test] 2017 #[test]
2001 fn expr_diverges_missing_arm() {
2002 let content = r"
2003 enum Either {
2004 A,
2005 B,
2006 }
2007 fn test_fn() {
2008 match loop {} {
2009 Either::A => (),
2010 }
2011 }
2012 ";
2013
2014 // This is a false negative.
2015 // Even though the match expression diverges, rustc fails
2016 // to compile here since `Either::B` is missing.
2017 check_no_diagnostic(content);
2018 }
2019
2020 #[test]
2021 fn expr_loop_missing_arm() { 2018 fn expr_loop_missing_arm() {
2022 let content = r" 2019 let content = r"
2023 enum Either { 2020 enum Either {
@@ -2035,7 +2032,7 @@ mod false_negatives {
2035 // We currently infer the type of `loop { break Foo::A }` to `!`, which 2032 // We currently infer the type of `loop { break Foo::A }` to `!`, which
2036 // causes us to skip the diagnostic since `Either::A` doesn't type check 2033 // causes us to skip the diagnostic since `Either::A` doesn't type check
2037 // with `!`. 2034 // with `!`.
2038 check_no_diagnostic(content); 2035 check_diagnostic(content);
2039 } 2036 }
2040 2037
2041 #[test] 2038 #[test]
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs
index 2876cb141..957d6e0b5 100644
--- a/crates/ra_hir_ty/src/infer.rs
+++ b/crates/ra_hir_ty/src/infer.rs
@@ -218,6 +218,7 @@ struct InferenceContext<'a> {
218#[derive(Clone, Debug)] 218#[derive(Clone, Debug)]
219struct BreakableContext { 219struct BreakableContext {
220 pub may_break: bool, 220 pub may_break: bool,
221 pub break_ty: Ty,
221} 222}
222 223
223impl<'a> InferenceContext<'a> { 224impl<'a> InferenceContext<'a> {
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs
index 0b67d216a..b28724f0e 100644
--- a/crates/ra_hir_ty/src/infer/expr.rs
+++ b/crates/ra_hir_ty/src/infer/expr.rs
@@ -93,22 +93,25 @@ impl<'a> InferenceContext<'a> {
93 Ty::Unknown 93 Ty::Unknown
94 } 94 }
95 Expr::Loop { body } => { 95 Expr::Loop { body } => {
96 self.breakables.push(BreakableContext { may_break: false }); 96 self.breakables.push(BreakableContext {
97 may_break: false,
98 break_ty: self.table.new_type_var(),
99 });
97 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 100 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
98 101
99 let ctxt = self.breakables.pop().expect("breakable stack broken"); 102 let ctxt = self.breakables.pop().expect("breakable stack broken");
100 if ctxt.may_break { 103 if ctxt.may_break {
101 self.diverges = Diverges::Maybe; 104 self.diverges = Diverges::Maybe;
102 } 105 }
103 // FIXME handle break with value 106
104 if ctxt.may_break { 107 if ctxt.may_break {
105 Ty::unit() 108 ctxt.break_ty
106 } else { 109 } else {
107 Ty::simple(TypeCtor::Never) 110 Ty::simple(TypeCtor::Never)
108 } 111 }
109 } 112 }
110 Expr::While { condition, body } => { 113 Expr::While { condition, body } => {
111 self.breakables.push(BreakableContext { may_break: false }); 114 self.breakables.push(BreakableContext { may_break: false, break_ty: Ty::Unknown });
112 // while let is desugared to a match loop, so this is always simple while 115 // while let is desugared to a match loop, so this is always simple while
113 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); 116 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
114 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 117 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
@@ -120,7 +123,7 @@ impl<'a> InferenceContext<'a> {
120 Expr::For { iterable, body, pat } => { 123 Expr::For { iterable, body, pat } => {
121 let iterable_ty = self.infer_expr(*iterable, &Expectation::none()); 124 let iterable_ty = self.infer_expr(*iterable, &Expectation::none());
122 125
123 self.breakables.push(BreakableContext { may_break: false }); 126 self.breakables.push(BreakableContext { may_break: false, break_ty: Ty::Unknown });
124 let pat_ty = 127 let pat_ty =
125 self.resolve_associated_type(iterable_ty, self.resolve_into_iter_item()); 128 self.resolve_associated_type(iterable_ty, self.resolve_into_iter_item());
126 129
@@ -229,17 +232,29 @@ impl<'a> InferenceContext<'a> {
229 } 232 }
230 Expr::Continue => Ty::simple(TypeCtor::Never), 233 Expr::Continue => Ty::simple(TypeCtor::Never),
231 Expr::Break { expr } => { 234 Expr::Break { expr } => {
232 if let Some(expr) = expr { 235 let val_ty = if let Some(expr) = expr {
233 // FIXME handle break with value 236 self.infer_expr(*expr, &Expectation::none())
234 self.infer_expr(*expr, &Expectation::none()); 237 } else {
235 } 238 Ty::unit()
239 };
240
241 let last_ty = if let Some(ctxt) = self.breakables.last() {
242 ctxt.break_ty.clone()
243 } else {
244 Ty::Unknown
245 };
246
247 let merged_type = self.coerce_merge_branch(&last_ty, &val_ty);
248
236 if let Some(ctxt) = self.breakables.last_mut() { 249 if let Some(ctxt) = self.breakables.last_mut() {
250 ctxt.break_ty = merged_type;
237 ctxt.may_break = true; 251 ctxt.may_break = true;
238 } else { 252 } else {
239 self.push_diagnostic(InferenceDiagnostic::BreakOutsideOfLoop { 253 self.push_diagnostic(InferenceDiagnostic::BreakOutsideOfLoop {
240 expr: tgt_expr, 254 expr: tgt_expr,
241 }); 255 });
242 } 256 }
257
243 Ty::simple(TypeCtor::Never) 258 Ty::simple(TypeCtor::Never)
244 } 259 }
245 Expr::Return { expr } => { 260 Expr::Return { expr } => {
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs
index 72122c070..fd2208af2 100644
--- a/crates/ra_hir_ty/src/tests/simple.rs
+++ b/crates/ra_hir_ty/src/tests/simple.rs
@@ -1860,3 +1860,66 @@ fn test() {
1860 "### 1860 "###
1861 ); 1861 );
1862} 1862}
1863
1864#[test]
1865fn infer_loop_break_with_val() {
1866 assert_snapshot!(
1867 infer(r#"
1868enum Option<T> { Some(T), None }
1869use Option::*;
1870
1871fn test() {
1872 let x = loop {
1873 if false {
1874 break None;
1875 }
1876
1877 break Some(true);
1878 };
1879}
1880"#),
1881 @r###"
1882 60..169 '{ ... }; }': ()
1883 70..71 'x': Option<bool>
1884 74..166 'loop {... }': Option<bool>
1885 79..166 '{ ... }': ()
1886 89..133 'if fal... }': ()
1887 92..97 'false': bool
1888 98..133 '{ ... }': ()
1889 112..122 'break None': !
1890 118..122 'None': Option<bool>
1891 143..159 'break ...(true)': !
1892 149..153 'Some': Some<bool>(bool) -> Option<bool>
1893 149..159 'Some(true)': Option<bool>
1894 154..158 'true': bool
1895 "###
1896 );
1897}
1898
1899#[test]
1900fn infer_loop_break_without_val() {
1901 assert_snapshot!(
1902 infer(r#"
1903enum Option<T> { Some(T), None }
1904use Option::*;
1905
1906fn test() {
1907 let x = loop {
1908 if false {
1909 break;
1910 }
1911 };
1912}
1913"#),
1914 @r###"
1915 60..137 '{ ... }; }': ()
1916 70..71 'x': ()
1917 74..134 'loop {... }': ()
1918 79..134 '{ ... }': ()
1919 89..128 'if fal... }': ()
1920 92..97 'false': bool
1921 98..128 '{ ... }': ()
1922 112..117 'break': !
1923 "###
1924 );
1925}