aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-08 19:09:25 +0100
committerGitHub <[email protected]>2020-05-08 19:09:25 +0100
commitf9ec7cebef732fbc9d4849d87d325efef5faadea (patch)
tree4a40dbed04c432dee54f8a8d016c89c7411b1e6d /crates
parentf1fa9aa4c4d4fcfe7d6e90ba9cefca90bc7c4998 (diff)
parentd0129c4ddba3b72e7b26e94e9c25546d37dbf166 (diff)
Merge #4377
4377: Implement better handling of divergence r=matklad a=flodiebold Divergence here means that for some reason, the end of a block will not be reached. We tried to model this just using the never type, but that doesn't work fully (e.g. in `let x = { loop {}; "foo" };` x should still have type `&str`); so this introduces a `diverges` flag that the type checker keeps track of, like rustc does. We also add some checking for `break`, but no support for break-with-value or labeled breaks yet. Co-authored-by: Florian Diebold <[email protected]> Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_ty/src/diagnostics.rs28
-rw-r--r--crates/ra_hir_ty/src/infer.rs60
-rw-r--r--crates/ra_hir_ty/src/infer/expr.rs84
-rw-r--r--crates/ra_hir_ty/src/lib.rs4
-rw-r--r--crates/ra_hir_ty/src/tests.rs18
-rw-r--r--crates/ra_hir_ty/src/tests/coercion.rs4
-rw-r--r--crates/ra_hir_ty/src/tests/macros.rs2
-rw-r--r--crates/ra_hir_ty/src/tests/never_type.rs177
-rw-r--r--crates/ra_hir_ty/src/tests/simple.rs10
9 files changed, 362 insertions, 25 deletions
diff --git a/crates/ra_hir_ty/src/diagnostics.rs b/crates/ra_hir_ty/src/diagnostics.rs
index c8fd54861..41ac70272 100644
--- a/crates/ra_hir_ty/src/diagnostics.rs
+++ b/crates/ra_hir_ty/src/diagnostics.rs
@@ -131,3 +131,31 @@ impl AstDiagnostic for MissingOkInTailExpr {
131 ast::Expr::cast(node).unwrap() 131 ast::Expr::cast(node).unwrap()
132 } 132 }
133} 133}
134
135#[derive(Debug)]
136pub struct BreakOutsideOfLoop {
137 pub file: HirFileId,
138 pub expr: AstPtr<ast::Expr>,
139}
140
141impl Diagnostic for BreakOutsideOfLoop {
142 fn message(&self) -> String {
143 "break outside of loop".to_string()
144 }
145 fn source(&self) -> InFile<SyntaxNodePtr> {
146 InFile { file_id: self.file, value: self.expr.clone().into() }
147 }
148 fn as_any(&self) -> &(dyn Any + Send + 'static) {
149 self
150 }
151}
152
153impl AstDiagnostic for BreakOutsideOfLoop {
154 type AST = ast::Expr;
155
156 fn ast(&self, db: &impl AstDatabase) -> Self::AST {
157 let root = db.parse_or_expand(self.file).unwrap();
158 let node = self.source().value.to_node(&root);
159 ast::Expr::cast(node).unwrap()
160 }
161}
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs
index bd4ef69a0..a21ad8d86 100644
--- a/crates/ra_hir_ty/src/infer.rs
+++ b/crates/ra_hir_ty/src/infer.rs
@@ -210,6 +210,13 @@ struct InferenceContext<'a> {
210 /// closures, but currently this is the only field that will change there, 210 /// closures, but currently this is the only field that will change there,
211 /// so it doesn't make sense. 211 /// so it doesn't make sense.
212 return_ty: Ty, 212 return_ty: Ty,
213 diverges: Diverges,
214 breakables: Vec<BreakableContext>,
215}
216
217#[derive(Clone, Debug)]
218struct BreakableContext {
219 pub may_break: bool,
213} 220}
214 221
215impl<'a> InferenceContext<'a> { 222impl<'a> InferenceContext<'a> {
@@ -224,6 +231,8 @@ impl<'a> InferenceContext<'a> {
224 owner, 231 owner,
225 body: db.body(owner), 232 body: db.body(owner),
226 resolver, 233 resolver,
234 diverges: Diverges::Maybe,
235 breakables: Vec::new(),
227 } 236 }
228 } 237 }
229 238
@@ -666,15 +675,57 @@ impl Expectation {
666 } 675 }
667} 676}
668 677
678#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
679enum Diverges {
680 Maybe,
681 Always,
682}
683
684impl Diverges {
685 fn is_always(self) -> bool {
686 self == Diverges::Always
687 }
688}
689
690impl std::ops::BitAnd for Diverges {
691 type Output = Self;
692 fn bitand(self, other: Self) -> Self {
693 std::cmp::min(self, other)
694 }
695}
696
697impl std::ops::BitOr for Diverges {
698 type Output = Self;
699 fn bitor(self, other: Self) -> Self {
700 std::cmp::max(self, other)
701 }
702}
703
704impl std::ops::BitAndAssign for Diverges {
705 fn bitand_assign(&mut self, other: Self) {
706 *self = *self & other;
707 }
708}
709
710impl std::ops::BitOrAssign for Diverges {
711 fn bitor_assign(&mut self, other: Self) {
712 *self = *self | other;
713 }
714}
715
669mod diagnostics { 716mod diagnostics {
670 use hir_def::{expr::ExprId, FunctionId}; 717 use hir_def::{expr::ExprId, FunctionId};
671 use hir_expand::diagnostics::DiagnosticSink; 718 use hir_expand::diagnostics::DiagnosticSink;
672 719
673 use crate::{db::HirDatabase, diagnostics::NoSuchField}; 720 use crate::{
721 db::HirDatabase,
722 diagnostics::{BreakOutsideOfLoop, NoSuchField},
723 };
674 724
675 #[derive(Debug, PartialEq, Eq, Clone)] 725 #[derive(Debug, PartialEq, Eq, Clone)]
676 pub(super) enum InferenceDiagnostic { 726 pub(super) enum InferenceDiagnostic {
677 NoSuchField { expr: ExprId, field: usize }, 727 NoSuchField { expr: ExprId, field: usize },
728 BreakOutsideOfLoop { expr: ExprId },
678 } 729 }
679 730
680 impl InferenceDiagnostic { 731 impl InferenceDiagnostic {
@@ -690,6 +741,13 @@ mod diagnostics {
690 let field = source_map.field_syntax(*expr, *field); 741 let field = source_map.field_syntax(*expr, *field);
691 sink.push(NoSuchField { file: field.file_id, field: field.value }) 742 sink.push(NoSuchField { file: field.file_id, field: field.value })
692 } 743 }
744 InferenceDiagnostic::BreakOutsideOfLoop { expr } => {
745 let (_, source_map) = db.body_with_source_map(owner.into());
746 let ptr = source_map
747 .expr_syntax(*expr)
748 .expect("break outside of loop in synthetic syntax");
749 sink.push(BreakOutsideOfLoop { file: ptr.file_id, expr: ptr.value })
750 }
693 } 751 }
694 } 752 }
695 } 753 }
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs
index 614c352a0..0b67d216a 100644
--- a/crates/ra_hir_ty/src/infer/expr.rs
+++ b/crates/ra_hir_ty/src/infer/expr.rs
@@ -1,7 +1,7 @@
1//! Type inference for expressions. 1//! Type inference for expressions.
2 2
3use std::iter::{repeat, repeat_with}; 3use std::iter::{repeat, repeat_with};
4use std::sync::Arc; 4use std::{mem, sync::Arc};
5 5
6use hir_def::{ 6use hir_def::{
7 builtin_type::Signedness, 7 builtin_type::Signedness,
@@ -21,11 +21,18 @@ use crate::{
21 Ty, TypeCtor, Uncertain, 21 Ty, TypeCtor, Uncertain,
22}; 22};
23 23
24use super::{BindingMode, Expectation, InferenceContext, InferenceDiagnostic, TypeMismatch}; 24use super::{
25 BindingMode, BreakableContext, Diverges, Expectation, InferenceContext, InferenceDiagnostic,
26 TypeMismatch,
27};
25 28
26impl<'a> InferenceContext<'a> { 29impl<'a> InferenceContext<'a> {
27 pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty { 30 pub(super) fn infer_expr(&mut self, tgt_expr: ExprId, expected: &Expectation) -> Ty {
28 let ty = self.infer_expr_inner(tgt_expr, expected); 31 let ty = self.infer_expr_inner(tgt_expr, expected);
32 if ty.is_never() {
33 // Any expression that produces a value of type `!` must have diverged
34 self.diverges = Diverges::Always;
35 }
29 let could_unify = self.unify(&ty, &expected.ty); 36 let could_unify = self.unify(&ty, &expected.ty);
30 if !could_unify { 37 if !could_unify {
31 self.result.type_mismatches.insert( 38 self.result.type_mismatches.insert(
@@ -64,11 +71,18 @@ impl<'a> InferenceContext<'a> {
64 // if let is desugared to match, so this is always simple if 71 // if let is desugared to match, so this is always simple if
65 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); 72 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
66 73
74 let condition_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
75 let mut both_arms_diverge = Diverges::Always;
76
67 let then_ty = self.infer_expr_inner(*then_branch, &expected); 77 let then_ty = self.infer_expr_inner(*then_branch, &expected);
78 both_arms_diverge &= mem::replace(&mut self.diverges, Diverges::Maybe);
68 let else_ty = match else_branch { 79 let else_ty = match else_branch {
69 Some(else_branch) => self.infer_expr_inner(*else_branch, &expected), 80 Some(else_branch) => self.infer_expr_inner(*else_branch, &expected),
70 None => Ty::unit(), 81 None => Ty::unit(),
71 }; 82 };
83 both_arms_diverge &= self.diverges;
84
85 self.diverges = condition_diverges | both_arms_diverge;
72 86
73 self.coerce_merge_branch(&then_ty, &else_ty) 87 self.coerce_merge_branch(&then_ty, &else_ty)
74 } 88 }
@@ -79,24 +93,43 @@ impl<'a> InferenceContext<'a> {
79 Ty::Unknown 93 Ty::Unknown
80 } 94 }
81 Expr::Loop { body } => { 95 Expr::Loop { body } => {
96 self.breakables.push(BreakableContext { may_break: false });
82 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 97 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
98
99 let ctxt = self.breakables.pop().expect("breakable stack broken");
100 if ctxt.may_break {
101 self.diverges = Diverges::Maybe;
102 }
83 // FIXME handle break with value 103 // FIXME handle break with value
84 Ty::simple(TypeCtor::Never) 104 if ctxt.may_break {
105 Ty::unit()
106 } else {
107 Ty::simple(TypeCtor::Never)
108 }
85 } 109 }
86 Expr::While { condition, body } => { 110 Expr::While { condition, body } => {
111 self.breakables.push(BreakableContext { may_break: false });
87 // while let is desugared to a match loop, so this is always simple while 112 // while let is desugared to a match loop, so this is always simple while
88 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool))); 113 self.infer_expr(*condition, &Expectation::has_type(Ty::simple(TypeCtor::Bool)));
89 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 114 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
115 let _ctxt = self.breakables.pop().expect("breakable stack broken");
116 // the body may not run, so it diverging doesn't mean we diverge
117 self.diverges = Diverges::Maybe;
90 Ty::unit() 118 Ty::unit()
91 } 119 }
92 Expr::For { iterable, body, pat } => { 120 Expr::For { iterable, body, pat } => {
93 let iterable_ty = self.infer_expr(*iterable, &Expectation::none()); 121 let iterable_ty = self.infer_expr(*iterable, &Expectation::none());
94 122
123 self.breakables.push(BreakableContext { may_break: false });
95 let pat_ty = 124 let pat_ty =
96 self.resolve_associated_type(iterable_ty, self.resolve_into_iter_item()); 125 self.resolve_associated_type(iterable_ty, self.resolve_into_iter_item());
97 126
98 self.infer_pat(*pat, &pat_ty, BindingMode::default()); 127 self.infer_pat(*pat, &pat_ty, BindingMode::default());
128
99 self.infer_expr(*body, &Expectation::has_type(Ty::unit())); 129 self.infer_expr(*body, &Expectation::has_type(Ty::unit()));
130 let _ctxt = self.breakables.pop().expect("breakable stack broken");
131 // the body may not run, so it diverging doesn't mean we diverge
132 self.diverges = Diverges::Maybe;
100 Ty::unit() 133 Ty::unit()
101 } 134 }
102 Expr::Lambda { body, args, ret_type, arg_types } => { 135 Expr::Lambda { body, args, ret_type, arg_types } => {
@@ -132,10 +165,12 @@ impl<'a> InferenceContext<'a> {
132 // infer the body. 165 // infer the body.
133 self.coerce(&closure_ty, &expected.ty); 166 self.coerce(&closure_ty, &expected.ty);
134 167
135 let prev_ret_ty = std::mem::replace(&mut self.return_ty, ret_ty.clone()); 168 let prev_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
169 let prev_ret_ty = mem::replace(&mut self.return_ty, ret_ty.clone());
136 170
137 self.infer_expr_coerce(*body, &Expectation::has_type(ret_ty)); 171 self.infer_expr_coerce(*body, &Expectation::has_type(ret_ty));
138 172
173 self.diverges = prev_diverges;
139 self.return_ty = prev_ret_ty; 174 self.return_ty = prev_ret_ty;
140 175
141 closure_ty 176 closure_ty
@@ -165,7 +200,11 @@ impl<'a> InferenceContext<'a> {
165 self.table.new_type_var() 200 self.table.new_type_var()
166 }; 201 };
167 202
203 let matchee_diverges = self.diverges;
204 let mut all_arms_diverge = Diverges::Always;
205
168 for arm in arms { 206 for arm in arms {
207 self.diverges = Diverges::Maybe;
169 let _pat_ty = self.infer_pat(arm.pat, &input_ty, BindingMode::default()); 208 let _pat_ty = self.infer_pat(arm.pat, &input_ty, BindingMode::default());
170 if let Some(guard_expr) = arm.guard { 209 if let Some(guard_expr) = arm.guard {
171 self.infer_expr( 210 self.infer_expr(
@@ -175,9 +214,12 @@ impl<'a> InferenceContext<'a> {
175 } 214 }
176 215
177 let arm_ty = self.infer_expr_inner(arm.expr, &expected); 216 let arm_ty = self.infer_expr_inner(arm.expr, &expected);
217 all_arms_diverge &= self.diverges;
178 result_ty = self.coerce_merge_branch(&result_ty, &arm_ty); 218 result_ty = self.coerce_merge_branch(&result_ty, &arm_ty);
179 } 219 }
180 220
221 self.diverges = matchee_diverges | all_arms_diverge;
222
181 result_ty 223 result_ty
182 } 224 }
183 Expr::Path(p) => { 225 Expr::Path(p) => {
@@ -191,6 +233,13 @@ impl<'a> InferenceContext<'a> {
191 // FIXME handle break with value 233 // FIXME handle break with value
192 self.infer_expr(*expr, &Expectation::none()); 234 self.infer_expr(*expr, &Expectation::none());
193 } 235 }
236 if let Some(ctxt) = self.breakables.last_mut() {
237 ctxt.may_break = true;
238 } else {
239 self.push_diagnostic(InferenceDiagnostic::BreakOutsideOfLoop {
240 expr: tgt_expr,
241 });
242 }
194 Ty::simple(TypeCtor::Never) 243 Ty::simple(TypeCtor::Never)
195 } 244 }
196 Expr::Return { expr } => { 245 Expr::Return { expr } => {
@@ -522,7 +571,6 @@ impl<'a> InferenceContext<'a> {
522 tail: Option<ExprId>, 571 tail: Option<ExprId>,
523 expected: &Expectation, 572 expected: &Expectation,
524 ) -> Ty { 573 ) -> Ty {
525 let mut diverges = false;
526 for stmt in statements { 574 for stmt in statements {
527 match stmt { 575 match stmt {
528 Statement::Let { pat, type_ref, initializer } => { 576 Statement::Let { pat, type_ref, initializer } => {
@@ -544,9 +592,7 @@ impl<'a> InferenceContext<'a> {
544 self.infer_pat(*pat, &ty, BindingMode::default()); 592 self.infer_pat(*pat, &ty, BindingMode::default());
545 } 593 }
546 Statement::Expr(expr) => { 594 Statement::Expr(expr) => {
547 if let ty_app!(TypeCtor::Never) = self.infer_expr(*expr, &Expectation::none()) { 595 self.infer_expr(*expr, &Expectation::none());
548 diverges = true;
549 }
550 } 596 }
551 } 597 }
552 } 598 }
@@ -554,14 +600,22 @@ impl<'a> InferenceContext<'a> {
554 let ty = if let Some(expr) = tail { 600 let ty = if let Some(expr) = tail {
555 self.infer_expr_coerce(expr, expected) 601 self.infer_expr_coerce(expr, expected)
556 } else { 602 } else {
557 self.coerce(&Ty::unit(), expected.coercion_target()); 603 // Citing rustc: if there is no explicit tail expression,
558 Ty::unit() 604 // that is typically equivalent to a tail expression
605 // of `()` -- except if the block diverges. In that
606 // case, there is no value supplied from the tail
607 // expression (assuming there are no other breaks,
608 // this implies that the type of the block will be
609 // `!`).
610 if self.diverges.is_always() {
611 // we don't even make an attempt at coercion
612 self.table.new_maybe_never_type_var()
613 } else {
614 self.coerce(&Ty::unit(), expected.coercion_target());
615 Ty::unit()
616 }
559 }; 617 };
560 if diverges { 618 ty
561 Ty::simple(TypeCtor::Never)
562 } else {
563 ty
564 }
565 } 619 }
566 620
567 fn infer_method_call( 621 fn infer_method_call(
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs
index a6f56c661..3e5f38d0d 100644
--- a/crates/ra_hir_ty/src/lib.rs
+++ b/crates/ra_hir_ty/src/lib.rs
@@ -730,6 +730,10 @@ impl Ty {
730 } 730 }
731 } 731 }
732 732
733 pub fn is_never(&self) -> bool {
734 matches!(self, Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }))
735 }
736
733 /// If this is a `dyn Trait` type, this returns the `Trait` part. 737 /// If this is a `dyn Trait` type, this returns the `Trait` part.
734 pub fn dyn_trait_ref(&self) -> Option<&TraitRef> { 738 pub fn dyn_trait_ref(&self) -> Option<&TraitRef> {
735 match self { 739 match self {
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs
index d60732e19..5af88b368 100644
--- a/crates/ra_hir_ty/src/tests.rs
+++ b/crates/ra_hir_ty/src/tests.rs
@@ -518,3 +518,21 @@ fn missing_record_pat_field_no_diagnostic_if_not_exhaustive() {
518 518
519 assert_snapshot!(diagnostics, @""); 519 assert_snapshot!(diagnostics, @"");
520} 520}
521
522#[test]
523fn break_outside_of_loop() {
524 let diagnostics = TestDB::with_files(
525 r"
526 //- /lib.rs
527 fn foo() {
528 break;
529 }
530 ",
531 )
532 .diagnostics()
533 .0;
534
535 assert_snapshot!(diagnostics, @r###""break": break outside of loop
536 "###
537 );
538}
diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs
index e6fb3e123..0c3a833bd 100644
--- a/crates/ra_hir_ty/src/tests/coercion.rs
+++ b/crates/ra_hir_ty/src/tests/coercion.rs
@@ -384,7 +384,7 @@ fn foo() -> u32 {
384} 384}
385"#, true), 385"#, true),
386 @r###" 386 @r###"
387 17..40 '{ ...own; }': ! 387 17..40 '{ ...own; }': u32
388 23..37 'return unknown': ! 388 23..37 'return unknown': !
389 30..37 'unknown': u32 389 30..37 'unknown': u32
390 "### 390 "###
@@ -514,7 +514,7 @@ fn foo() {
514 27..103 '{ ... }': &u32 514 27..103 '{ ... }': &u32
515 37..82 'if tru... }': () 515 37..82 'if tru... }': ()
516 40..44 'true': bool 516 40..44 'true': bool
517 45..82 '{ ... }': ! 517 45..82 '{ ... }': ()
518 59..71 'return &1u32': ! 518 59..71 'return &1u32': !
519 66..71 '&1u32': &u32 519 66..71 '&1u32': &u32
520 67..71 '1u32': u32 520 67..71 '1u32': u32
diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs
index 07398ddcc..4c6099aa2 100644
--- a/crates/ra_hir_ty/src/tests/macros.rs
+++ b/crates/ra_hir_ty/src/tests/macros.rs
@@ -197,7 +197,7 @@ fn spam() {
197 !0..6 '1isize': isize 197 !0..6 '1isize': isize
198 !0..6 '1isize': isize 198 !0..6 '1isize': isize
199 !0..6 '1isize': isize 199 !0..6 '1isize': isize
200 54..457 '{ ...!(); }': ! 200 54..457 '{ ...!(); }': ()
201 88..109 'spam!(...am!())': {unknown} 201 88..109 'spam!(...am!())': {unknown}
202 115..134 'for _ ...!() {}': () 202 115..134 'for _ ...!() {}': ()
203 119..120 '_': {unknown} 203 119..120 '_': {unknown}
diff --git a/crates/ra_hir_ty/src/tests/never_type.rs b/crates/ra_hir_ty/src/tests/never_type.rs
index a77209480..082c47208 100644
--- a/crates/ra_hir_ty/src/tests/never_type.rs
+++ b/crates/ra_hir_ty/src/tests/never_type.rs
@@ -1,4 +1,6 @@
1use super::type_at; 1use insta::assert_snapshot;
2
3use super::{infer_with_mismatches, type_at};
2 4
3#[test] 5#[test]
4fn infer_never1() { 6fn infer_never1() {
@@ -261,3 +263,176 @@ fn test(a: i32) {
261 ); 263 );
262 assert_eq!(t, "f64"); 264 assert_eq!(t, "f64");
263} 265}
266
267#[test]
268fn diverging_expression_1() {
269 let t = infer_with_mismatches(
270 r#"
271//- /main.rs
272fn test1() {
273 let x: u32 = return;
274}
275fn test2() {
276 let x: u32 = { return; };
277}
278fn test3() {
279 let x: u32 = loop {};
280}
281fn test4() {
282 let x: u32 = { loop {} };
283}
284fn test5() {
285 let x: u32 = { if true { loop {}; } else { loop {}; } };
286}
287fn test6() {
288 let x: u32 = { let y: u32 = { loop {}; }; };
289}
290"#,
291 true,
292 );
293 assert_snapshot!(t, @r###"
294 25..53 '{ ...urn; }': ()
295 35..36 'x': u32
296 44..50 'return': !
297 65..98 '{ ...; }; }': ()
298 75..76 'x': u32
299 84..95 '{ return; }': u32
300 86..92 'return': !
301 110..139 '{ ... {}; }': ()
302 120..121 'x': u32
303 129..136 'loop {}': !
304 134..136 '{}': ()
305 151..184 '{ ...} }; }': ()
306 161..162 'x': u32
307 170..181 '{ loop {} }': u32
308 172..179 'loop {}': !
309 177..179 '{}': ()
310 196..260 '{ ...} }; }': ()
311 206..207 'x': u32
312 215..257 '{ if t...}; } }': u32
313 217..255 'if tru... {}; }': u32
314 220..224 'true': bool
315 225..237 '{ loop {}; }': u32
316 227..234 'loop {}': !
317 232..234 '{}': ()
318 243..255 '{ loop {}; }': u32
319 245..252 'loop {}': !
320 250..252 '{}': ()
321 272..324 '{ ...; }; }': ()
322 282..283 'x': u32
323 291..321 '{ let ...; }; }': u32
324 297..298 'y': u32
325 306..318 '{ loop {}; }': u32
326 308..315 'loop {}': !
327 313..315 '{}': ()
328 "###);
329}
330
331#[test]
332fn diverging_expression_2() {
333 let t = infer_with_mismatches(
334 r#"
335//- /main.rs
336fn test1() {
337 // should give type mismatch
338 let x: u32 = { loop {}; "foo" };
339}
340"#,
341 true,
342 );
343 assert_snapshot!(t, @r###"
344 25..98 '{ ..." }; }': ()
345 68..69 'x': u32
346 77..95 '{ loop...foo" }': &str
347 79..86 'loop {}': !
348 84..86 '{}': ()
349 88..93 '"foo"': &str
350 77..95: expected u32, got &str
351 88..93: expected u32, got &str
352 "###);
353}
354
355#[test]
356fn diverging_expression_3_break() {
357 let t = infer_with_mismatches(
358 r#"
359//- /main.rs
360fn test1() {
361 // should give type mismatch
362 let x: u32 = { loop { break; } };
363}
364fn test2() {
365 // should give type mismatch
366 let x: u32 = { for a in b { break; }; };
367 // should give type mismatch as well
368 let x: u32 = { for a in b {}; };
369 // should give type mismatch as well
370 let x: u32 = { for a in b { return; }; };
371}
372fn test3() {
373 // should give type mismatch
374 let x: u32 = { while true { break; }; };
375 // should give type mismatch as well -- there's an implicit break, even if it's never hit
376 let x: u32 = { while true {}; };
377 // should give type mismatch as well
378 let x: u32 = { while true { return; }; };
379}
380"#,
381 true,
382 );
383 assert_snapshot!(t, @r###"
384 25..99 '{ ...} }; }': ()
385 68..69 'x': u32
386 77..96 '{ loop...k; } }': ()
387 79..94 'loop { break; }': ()
388 84..94 '{ break; }': ()
389 86..91 'break': !
390 77..96: expected u32, got ()
391 79..94: expected u32, got ()
392 111..357 '{ ...; }; }': ()
393 154..155 'x': u32
394 163..189 '{ for ...; }; }': ()
395 165..186 'for a ...eak; }': ()
396 169..170 'a': {unknown}
397 174..175 'b': {unknown}
398 176..186 '{ break; }': ()
399 178..183 'break': !
400 240..241 'x': u32
401 249..267 '{ for ... {}; }': ()
402 251..264 'for a in b {}': ()
403 255..256 'a': {unknown}
404 260..261 'b': {unknown}
405 262..264 '{}': ()
406 318..319 'x': u32
407 327..354 '{ for ...; }; }': ()
408 329..351 'for a ...urn; }': ()
409 333..334 'a': {unknown}
410 338..339 'b': {unknown}
411 340..351 '{ return; }': ()
412 342..348 'return': !
413 163..189: expected u32, got ()
414 249..267: expected u32, got ()
415 327..354: expected u32, got ()
416 369..668 '{ ...; }; }': ()
417 412..413 'x': u32
418 421..447 '{ whil...; }; }': ()
419 423..444 'while ...eak; }': ()
420 429..433 'true': bool
421 434..444 '{ break; }': ()
422 436..441 'break': !
423 551..552 'x': u32
424 560..578 '{ whil... {}; }': ()
425 562..575 'while true {}': ()
426 568..572 'true': bool
427 573..575 '{}': ()
428 629..630 'x': u32
429 638..665 '{ whil...; }; }': ()
430 640..662 'while ...urn; }': ()
431 646..650 'true': bool
432 651..662 '{ return; }': ()
433 653..659 'return': !
434 421..447: expected u32, got ()
435 560..578: expected u32, got ()
436 638..665: expected u32, got ()
437 "###);
438}
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs
index e17a17900..3820175f6 100644
--- a/crates/ra_hir_ty/src/tests/simple.rs
+++ b/crates/ra_hir_ty/src/tests/simple.rs
@@ -179,7 +179,7 @@ fn test(a: u32, b: isize, c: !, d: &str) {
179 17..18 'b': isize 179 17..18 'b': isize
180 27..28 'c': ! 180 27..28 'c': !
181 33..34 'd': &str 181 33..34 'd': &str
182 42..121 '{ ...f32; }': ! 182 42..121 '{ ...f32; }': ()
183 48..49 'a': u32 183 48..49 'a': u32
184 55..56 'b': isize 184 55..56 'b': isize
185 62..63 'c': ! 185 62..63 'c': !
@@ -935,7 +935,7 @@ fn foo() {
935 29..33 'true': bool 935 29..33 'true': bool
936 34..51 '{ ... }': i32 936 34..51 '{ ... }': i32
937 44..45 '1': i32 937 44..45 '1': i32
938 57..80 '{ ... }': ! 938 57..80 '{ ... }': i32
939 67..73 'return': ! 939 67..73 'return': !
940 90..93 '_x2': i32 940 90..93 '_x2': i32
941 96..149 'if tru... }': i32 941 96..149 'if tru... }': i32
@@ -951,7 +951,7 @@ fn foo() {
951 186..190 'true': bool 951 186..190 'true': bool
952 194..195 '3': i32 952 194..195 '3': i32
953 205..206 '_': bool 953 205..206 '_': bool
954 210..241 '{ ... }': ! 954 210..241 '{ ... }': i32
955 224..230 'return': ! 955 224..230 'return': !
956 257..260 '_x4': i32 956 257..260 '_x4': i32
957 263..320 'match ... }': i32 957 263..320 'match ... }': i32
@@ -1687,7 +1687,7 @@ fn foo() -> u32 {
1687 17..59 '{ ...; }; }': () 1687 17..59 '{ ...; }; }': ()
1688 27..28 'x': || -> usize 1688 27..28 'x': || -> usize
1689 31..56 '|| -> ...n 1; }': || -> usize 1689 31..56 '|| -> ...n 1; }': || -> usize
1690 43..56 '{ return 1; }': ! 1690 43..56 '{ return 1; }': usize
1691 45..53 'return 1': ! 1691 45..53 'return 1': !
1692 52..53 '1': usize 1692 52..53 '1': usize
1693 "### 1693 "###
@@ -1706,7 +1706,7 @@ fn foo() -> u32 {
1706 17..48 '{ ...; }; }': () 1706 17..48 '{ ...; }; }': ()
1707 27..28 'x': || -> () 1707 27..28 'x': || -> ()
1708 31..45 '|| { return; }': || -> () 1708 31..45 '|| { return; }': || -> ()
1709 34..45 '{ return; }': ! 1709 34..45 '{ return; }': ()
1710 36..42 'return': ! 1710 36..42 'return': !
1711 "### 1711 "###
1712 ); 1712 );