Skip to content

Commit 9e67f6a

Browse files
authored
Rollup merge of rust-lang#103866 - compiler-errors:fn-ctxt-less-state, r=fee1-dead
Remove some return-type diagnostic booleans from `FnCtxt` These can be calculated on-demand
2 parents bd9e6e0 + 5e016b8 commit 9e67f6a

File tree

8 files changed

+15
-27
lines changed

8 files changed

+15
-27
lines changed

compiler/rustc_hir_typeck/src/_match.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -491,11 +491,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
491491
..
492492
} = self.type_var_origin(expected)? else { return None; };
493493

494-
let sig = *self
495-
.typeck_results
496-
.borrow()
497-
.liberated_fn_sigs()
498-
.get(hir::HirId::make_owner(self.body_id.owner.def_id))?;
494+
let sig = self.body_fn_sig()?;
499495

500496
let substs = sig.output().walk().find_map(|arg| {
501497
if let ty::GenericArgKind::Type(ty) = arg.unpack()

compiler/rustc_hir_typeck/src/check.rs

-5
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ pub(super) fn check_fn<'a, 'tcx>(
3131
fn_id: hir::HirId,
3232
body: &'tcx hir::Body<'tcx>,
3333
can_be_generator: Option<hir::Movability>,
34-
return_type_pre_known: bool,
3534
) -> (FnCtxt<'a, 'tcx>, Option<GeneratorTypes<'tcx>>) {
3635
// Create the function context. This is either derived from scratch or,
3736
// in the case of closures, based on the outer context.
3837
let mut fcx = FnCtxt::new(inherited, param_env, body.value.hir_id);
3938
fcx.ps.set(UnsafetyState::function(fn_sig.unsafety, fn_id));
40-
fcx.return_type_pre_known = return_type_pre_known;
4139

4240
let tcx = fcx.tcx;
4341
let hir = tcx.hir();
@@ -51,9 +49,6 @@ pub(super) fn check_fn<'a, 'tcx>(
5149
decl.output.span(),
5250
param_env,
5351
));
54-
// If we replaced declared_ret_ty with infer vars, then we must be inferring
55-
// an opaque type, so set a flag so we can improve diagnostics.
56-
fcx.return_type_has_opaque = ret_ty != declared_ret_ty;
5752

5853
fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));
5954

compiler/rustc_hir_typeck/src/closure.rs

-3
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8383

8484
debug!(?bound_sig, ?liberated_sig);
8585

86-
let return_type_pre_known = !liberated_sig.output().is_ty_infer();
87-
8886
let generator_types = check_fn(
8987
self,
9088
self.param_env.without_const(),
@@ -93,7 +91,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
9391
expr.hir_id,
9492
body,
9593
gen,
96-
return_type_pre_known,
9794
)
9895
.1;
9996

compiler/rustc_hir_typeck/src/coercion.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
17821782
// may occur at the first return expression we see in the closure
17831783
// (if it conflicts with the declared return type). Skip adding a
17841784
// note in this case, since it would be incorrect.
1785-
&& !fcx.return_type_pre_known
1785+
&& let Some(fn_sig) = fcx.body_fn_sig()
1786+
&& fn_sig.output().is_ty_var()
17861787
{
17871788
err.span_note(
17881789
sp,

compiler/rustc_hir_typeck/src/expr.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
840840
return_expr_ty,
841841
);
842842

843-
if self.return_type_has_opaque {
843+
if let Some(fn_sig) = self.body_fn_sig()
844+
&& fn_sig.output().has_opaque_types()
845+
{
844846
// Point any obligations that were registered due to opaque type
845847
// inference at the return expression.
846848
self.select_obligations_where_possible(false, |errors| {

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

-11
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,6 @@ pub struct FnCtxt<'a, 'tcx> {
118118
pub(super) enclosing_breakables: RefCell<EnclosingBreakables<'tcx>>,
119119

120120
pub(super) inh: &'a Inherited<'tcx>,
121-
122-
/// True if the function or closure's return type is known before
123-
/// entering the function/closure, i.e. if the return type is
124-
/// either given explicitly or inferred from, say, an `Fn*` trait
125-
/// bound. Used for diagnostic purposes only.
126-
pub(super) return_type_pre_known: bool,
127-
128-
/// True if the return type has an Opaque type
129-
pub(super) return_type_has_opaque: bool,
130121
}
131122

132123
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -151,8 +142,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
151142
by_id: Default::default(),
152143
}),
153144
inh,
154-
return_type_pre_known: true,
155-
return_type_has_opaque: false,
156145
}
157146
}
158147

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ use rustc_trait_selection::traits::error_reporting::DefIdOrName;
2222
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
2323

2424
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25+
pub(crate) fn body_fn_sig(&self) -> Option<ty::FnSig<'tcx>> {
26+
self.typeck_results
27+
.borrow()
28+
.liberated_fn_sigs()
29+
.get(self.tcx.hir().get_parent_node(self.body_id))
30+
.copied()
31+
}
32+
2533
pub(in super::super) fn suggest_semicolon_at_end(&self, span: Span, err: &mut Diagnostic) {
2634
err.span_suggestion_short(
2735
span.shrink_to_hi(),

compiler/rustc_hir_typeck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn typeck_with_fallback<'tcx>(
250250
param_env,
251251
fn_sig,
252252
);
253-
check_fn(&inh, param_env, fn_sig, decl, id, body, None, true).0
253+
check_fn(&inh, param_env, fn_sig, decl, id, body, None).0
254254
} else {
255255
let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
256256
let expected_type = body_ty

0 commit comments

Comments
 (0)