]> git.lizzy.rs Git - rust.git/commitdiff
Remove some return-type booleans from FnCtxt
authorMichael Goulet <michael@errs.io>
Wed, 2 Nov 2022 02:02:33 +0000 (02:02 +0000)
committerMichael Goulet <michael@errs.io>
Wed, 2 Nov 2022 02:03:09 +0000 (02:03 +0000)
compiler/rustc_hir_typeck/src/_match.rs
compiler/rustc_hir_typeck/src/check.rs
compiler/rustc_hir_typeck/src/closure.rs
compiler/rustc_hir_typeck/src/coercion.rs
compiler/rustc_hir_typeck/src/expr.rs
compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
compiler/rustc_hir_typeck/src/lib.rs

index 2b15d4dcd084849def308fecfec0c8f668ebfd2d..8d39fa81165ea6bdec7d832b39d3313f31f09b46 100644 (file)
@@ -491,11 +491,7 @@ pub(crate) fn opt_suggest_box_span(
                     ..
                 } = self.type_var_origin(expected)? else { return None; };
 
-                let sig = *self
-                    .typeck_results
-                    .borrow()
-                    .liberated_fn_sigs()
-                    .get(hir::HirId::make_owner(self.body_id.owner.def_id))?;
+                let sig = self.body_fn_sig()?;
 
                 let substs = sig.output().walk().find_map(|arg| {
                     if let ty::GenericArgKind::Type(ty) = arg.unpack()
index b706d786b52518ef5db07778c06ae3b6e2e3296e..80147d9009113f329cefc5ca4ada2d660210adad 100644 (file)
@@ -31,13 +31,11 @@ pub(super) fn check_fn<'a, 'tcx>(
     fn_id: hir::HirId,
     body: &'tcx hir::Body<'tcx>,
     can_be_generator: Option<hir::Movability>,
-    return_type_pre_known: bool,
 ) -> (FnCtxt<'a, 'tcx>, Option<GeneratorTypes<'tcx>>) {
     // Create the function context. This is either derived from scratch or,
     // in the case of closures, based on the outer context.
     let mut fcx = FnCtxt::new(inherited, param_env, body.value.hir_id);
     fcx.ps.set(UnsafetyState::function(fn_sig.unsafety, fn_id));
-    fcx.return_type_pre_known = return_type_pre_known;
 
     let tcx = fcx.tcx;
     let hir = tcx.hir();
@@ -51,9 +49,6 @@ pub(super) fn check_fn<'a, 'tcx>(
             decl.output.span(),
             param_env,
         ));
-    // If we replaced declared_ret_ty with infer vars, then we must be inferring
-    // an opaque type, so set a flag so we can improve diagnostics.
-    fcx.return_type_has_opaque = ret_ty != declared_ret_ty;
 
     fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));
 
index a5a45f75e0e240c25a0ec8fb1bcad01d5b6dfa14..1c2a7dfd994f9a5533c421a84591863527315bfe 100644 (file)
@@ -82,8 +82,6 @@ fn check_closure(
 
         debug!(?bound_sig, ?liberated_sig);
 
-        let return_type_pre_known = !liberated_sig.output().is_ty_infer();
-
         let generator_types = check_fn(
             self,
             self.param_env.without_const(),
@@ -92,7 +90,6 @@ fn check_closure(
             expr.hir_id,
             body,
             gen,
-            return_type_pre_known,
         )
         .1;
 
index 86597a703e833810dafaa5f4a25062d7bbd67a6a..e8bf299b0378ea2241f04cd11c3fb29700a925df 100644 (file)
@@ -1782,7 +1782,8 @@ fn report_return_mismatched_types<'a>(
             // may occur at the first return expression we see in the closure
             // (if it conflicts with the declared return type). Skip adding a
             // note in this case, since it would be incorrect.
-            && !fcx.return_type_pre_known
+            && let Some(fn_sig) = fcx.body_fn_sig()
+            && fn_sig.output().is_ty_var()
         {
             err.span_note(
                 sp,
index 9fde62a81a1a666704152916dd50dcd36a8a2c94..14d36d37776072add01aa1bc2ee430dddc17cb3c 100644 (file)
@@ -840,7 +840,9 @@ pub(super) fn check_return_expr(
             return_expr_ty,
         );
 
-        if self.return_type_has_opaque {
+        if let Some(fn_sig) = self.body_fn_sig()
+            && fn_sig.output().has_opaque_types()
+        {
             // Point any obligations that were registered due to opaque type
             // inference at the return expression.
             self.select_obligations_where_possible(false, |errors| {
index 0c600daf4459e8e6886f6042f927eb4b2e78a549..3956db7eebea15101b0dbc857ac07edf4f958896 100644 (file)
@@ -118,15 +118,6 @@ pub struct FnCtxt<'a, 'tcx> {
     pub(super) enclosing_breakables: RefCell<EnclosingBreakables<'tcx>>,
 
     pub(super) inh: &'a Inherited<'tcx>,
-
-    /// True if the function or closure's return type is known before
-    /// entering the function/closure, i.e. if the return type is
-    /// either given explicitly or inferred from, say, an `Fn*` trait
-    /// bound. Used for diagnostic purposes only.
-    pub(super) return_type_pre_known: bool,
-
-    /// True if the return type has an Opaque type
-    pub(super) return_type_has_opaque: bool,
 }
 
 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -151,8 +142,6 @@ pub fn new(
                 by_id: Default::default(),
             }),
             inh,
-            return_type_pre_known: true,
-            return_type_has_opaque: false,
         }
     }
 
index 4db9c56f98fee3440daa27e6aafa5e4002c283bf..e3b3fb499b16ab888026d0f72ba480bc5003e7f4 100644 (file)
 use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
 
 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
+    pub(crate) fn body_fn_sig(&self) -> Option<ty::FnSig<'tcx>> {
+        self.typeck_results
+            .borrow()
+            .liberated_fn_sigs()
+            .get(self.tcx.hir().get_parent_node(self.body_id))
+            .copied()
+    }
+
     pub(in super::super) fn suggest_semicolon_at_end(&self, span: Span, err: &mut Diagnostic) {
         err.span_suggestion_short(
             span.shrink_to_hi(),
index 959c54866453dd8520f2ef00eaf979ce3886d264..d1762598a5206760b70093fe16ab53d181d83039 100644 (file)
@@ -250,7 +250,7 @@ fn typeck_with_fallback<'tcx>(
                 param_env,
                 fn_sig,
             );
-            check_fn(&inh, param_env, fn_sig, decl, id, body, None, true).0
+            check_fn(&inh, param_env, fn_sig, decl, id, body, None).0
         } else {
             let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
             let expected_type = body_ty