about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorliudingming <liudingming@bytedance.com>2021-08-09 20:25:57 +0800
committerNiko Matsakis <niko@alum.mit.edu>2021-08-23 19:16:16 +0000
commite8910440a24ecb2ddb08b4dbe170488ddf61fc87 (patch)
tree7b574ec36f695dd6346584fcb7850cafbbc98794 /compiler
parentb6e334d87349502766be70d649e6fe4a73573482 (diff)
select obligations after `check_casts`
Otherwise, we can get into a situation where you have
a subtype obligation `#1 <: #2` pending, #1 is constrained
by `check_casts`, but #2` is unaffected.

Co-authored-by: Niko Matsakis <niko@alum.mit.edu>
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_typeck/src/check/fallback.rs6
-rw-r--r--compiler/rustc_typeck/src/check/mod.rs3
-rw-r--r--compiler/rustc_typeck/src/expr_use_visitor.rs10
3 files changed, 15 insertions, 4 deletions
diff --git a/compiler/rustc_typeck/src/check/fallback.rs b/compiler/rustc_typeck/src/check/fallback.rs
index 69a8970ae09..8f6cdc7bb12 100644
--- a/compiler/rustc_typeck/src/check/fallback.rs
+++ b/compiler/rustc_typeck/src/check/fallback.rs
@@ -3,7 +3,9 @@ use rustc_infer::infer::type_variable::Diverging;
 use rustc_middle::ty::{self, Ty};
 
 impl<'tcx> FnCtxt<'_, 'tcx> {
-    pub(super) fn type_inference_fallback(&self) {
+    /// Performs type inference fallback, returning true if any fallback
+    /// occurs.
+    pub(super) fn type_inference_fallback(&self) -> bool {
         // All type checking constraints were added, try to fallback unsolved variables.
         self.select_obligations_where_possible(false, |_| {});
         let mut fallback_has_occurred = false;
@@ -50,6 +52,8 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
 
         // See if we can make any more progress.
         self.select_obligations_where_possible(fallback_has_occurred, |_| {});
+
+        fallback_has_occurred
     }
 
     // Tries to apply a fallback to `ty` if it is an unsolved variable.
diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs
index ad7e96e2833..ff6cb35a752 100644
--- a/compiler/rustc_typeck/src/check/mod.rs
+++ b/compiler/rustc_typeck/src/check/mod.rs
@@ -446,11 +446,12 @@ fn typeck_with_fallback<'tcx>(
             fcx
         };
 
-        fcx.type_inference_fallback();
+        let fallback_has_occurred = fcx.type_inference_fallback();
 
         // Even though coercion casts provide type hints, we check casts after fallback for
         // backwards compatibility. This makes fallback a stronger type hint than a cast coercion.
         fcx.check_casts();
+        fcx.select_obligations_where_possible(fallback_has_occurred, |_| {});
 
         // Closure and generator analysis may run after fallback
         // because they don't constrain other type variables.
diff --git a/compiler/rustc_typeck/src/expr_use_visitor.rs b/compiler/rustc_typeck/src/expr_use_visitor.rs
index b0c95939bb7..8d401b7f444 100644
--- a/compiler/rustc_typeck/src/expr_use_visitor.rs
+++ b/compiler/rustc_typeck/src/expr_use_visitor.rs
@@ -243,7 +243,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
                 let ExprUseVisitor { ref mc, body_owner: _, delegate: _ } = *self;
                 let mut needs_to_be_read = false;
                 for arm in arms.iter() {
-                    return_if_err!(mc.cat_pattern(discr_place.clone(), &arm.pat, |place, pat| {
+                    match mc.cat_pattern(discr_place.clone(), &arm.pat, |place, pat| {
                         match &pat.kind {
                             PatKind::Binding(.., opt_sub_pat) => {
                                 // If the opt_sub_pat is None, than the binding does not count as
@@ -290,7 +290,13 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
                                 // examined
                             }
                         }
-                    }));
+                    }) {
+                        Ok(_) => (),
+                        Err(_) => {
+                            // If typeck failed, assume borrow is needed.
+                            needs_to_be_read = true;
+                        }
+                    }
                 }
 
                 if needs_to_be_read {