Rollup merge of #103866 - compiler-errors:fn-ctxt-less-state, r=fee1-dead

Remove some return-type diagnostic booleans from `FnCtxt`

These can be calculated on-demand
This commit is contained in:
Dylan DPC 2022-11-05 11:31:29 +05:30 committed by GitHub
commit 9e67f6a68d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 27 deletions

View File

@ -491,11 +491,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.. ..
} = self.type_var_origin(expected)? else { return None; }; } = self.type_var_origin(expected)? else { return None; };
let sig = *self let sig = self.body_fn_sig()?;
.typeck_results
.borrow()
.liberated_fn_sigs()
.get(hir::HirId::make_owner(self.body_id.owner.def_id))?;
let substs = sig.output().walk().find_map(|arg| { let substs = sig.output().walk().find_map(|arg| {
if let ty::GenericArgKind::Type(ty) = arg.unpack() if let ty::GenericArgKind::Type(ty) = arg.unpack()

View File

@ -31,13 +31,11 @@ pub(super) fn check_fn<'a, 'tcx>(
fn_id: hir::HirId, fn_id: hir::HirId,
body: &'tcx hir::Body<'tcx>, body: &'tcx hir::Body<'tcx>,
can_be_generator: Option<hir::Movability>, can_be_generator: Option<hir::Movability>,
return_type_pre_known: bool,
) -> (FnCtxt<'a, 'tcx>, Option<GeneratorTypes<'tcx>>) { ) -> (FnCtxt<'a, 'tcx>, Option<GeneratorTypes<'tcx>>) {
// Create the function context. This is either derived from scratch or, // Create the function context. This is either derived from scratch or,
// in the case of closures, based on the outer context. // in the case of closures, based on the outer context.
let mut fcx = FnCtxt::new(inherited, param_env, body.value.hir_id); let mut fcx = FnCtxt::new(inherited, param_env, body.value.hir_id);
fcx.ps.set(UnsafetyState::function(fn_sig.unsafety, fn_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 tcx = fcx.tcx;
let hir = tcx.hir(); let hir = tcx.hir();
@ -51,9 +49,6 @@ pub(super) fn check_fn<'a, 'tcx>(
decl.output.span(), decl.output.span(),
param_env, 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))); fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));

View File

@ -83,8 +83,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
debug!(?bound_sig, ?liberated_sig); debug!(?bound_sig, ?liberated_sig);
let return_type_pre_known = !liberated_sig.output().is_ty_infer();
let generator_types = check_fn( let generator_types = check_fn(
self, self,
self.param_env.without_const(), self.param_env.without_const(),
@ -93,7 +91,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expr.hir_id, expr.hir_id,
body, body,
gen, gen,
return_type_pre_known,
) )
.1; .1;

View File

@ -1782,7 +1782,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
// may occur at the first return expression we see in the closure // may occur at the first return expression we see in the closure
// (if it conflicts with the declared return type). Skip adding a // (if it conflicts with the declared return type). Skip adding a
// note in this case, since it would be incorrect. // 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( err.span_note(
sp, sp,

View File

@ -840,7 +840,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return_expr_ty, 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 // Point any obligations that were registered due to opaque type
// inference at the return expression. // inference at the return expression.
self.select_obligations_where_possible(false, |errors| { self.select_obligations_where_possible(false, |errors| {

View File

@ -118,15 +118,6 @@ pub struct FnCtxt<'a, 'tcx> {
pub(super) enclosing_breakables: RefCell<EnclosingBreakables<'tcx>>, pub(super) enclosing_breakables: RefCell<EnclosingBreakables<'tcx>>,
pub(super) inh: &'a Inherited<'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> { impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@ -151,8 +142,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
by_id: Default::default(), by_id: Default::default(),
}), }),
inh, inh,
return_type_pre_known: true,
return_type_has_opaque: false,
} }
} }

View File

@ -22,6 +22,14 @@ use rustc_trait_selection::traits::error_reporting::DefIdOrName;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
impl<'a, 'tcx> FnCtxt<'a, 'tcx> { 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) { pub(in super::super) fn suggest_semicolon_at_end(&self, span: Span, err: &mut Diagnostic) {
err.span_suggestion_short( err.span_suggestion_short(
span.shrink_to_hi(), span.shrink_to_hi(),

View File

@ -250,7 +250,7 @@ fn typeck_with_fallback<'tcx>(
param_env, param_env,
fn_sig, 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 { } else {
let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id); let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
let expected_type = body_ty let expected_type = body_ty