Skip to content

Commit 0678622

Browse files
Simplify more FnCtxt normalization
1 parent 52cd342 commit 0678622

File tree

10 files changed

+46
-111
lines changed

10 files changed

+46
-111
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub trait AstConv<'tcx> {
109109
) -> Ty<'tcx>;
110110

111111
/// Normalize an associated type coming from the user.
112-
///
112+
///
113113
/// This should only be used by astconv. Use `FnCtxt::normalize`
114114
/// or `ObligationCtxt::normalize` in downstream crates.
115115
fn normalize_ty(&self, span: Span, ty: Ty<'tcx>) -> Ty<'tcx>;

compiler/rustc_hir_typeck/src/cast.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
752752
match *self.expr_ty.kind() {
753753
ty::FnDef(..) => {
754754
// Attempt a coercion to a fn pointer type.
755-
let f = fcx.normalize(
756-
self.expr_span,
757-
self.expr_ty.fn_sig(fcx.tcx),
758-
);
755+
let f = fcx.normalize(self.expr_span, self.expr_ty.fn_sig(fcx.tcx));
759756
let res = fcx.try_coerce(
760757
self.expr,
761758
self.expr_ty,

compiler/rustc_hir_typeck/src/check.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::coercion::CoerceMany;
22
use crate::gather_locals::GatherLocalsVisitor;
3-
use crate::{FnCtxt, Inherited};
3+
use crate::FnCtxt;
44
use crate::{GeneratorTypes, UnsafetyState};
55
use rustc_hir as hir;
66
use rustc_hir::def::DefKind;
@@ -20,21 +20,16 @@ use std::cell::RefCell;
2020
///
2121
/// * ...
2222
/// * inherited: other fields inherited from the enclosing fn (if any)
23-
#[instrument(skip(inherited, body), level = "debug")]
23+
#[instrument(skip(fcx, body), level = "debug")]
2424
pub(super) fn check_fn<'a, 'tcx>(
25-
inherited: &'a Inherited<'tcx>,
26-
param_env: ty::ParamEnv<'tcx>,
25+
fcx: &mut FnCtxt<'a, 'tcx>,
2726
fn_sig: ty::FnSig<'tcx>,
2827
decl: &'tcx hir::FnDecl<'tcx>,
2928
fn_def_id: LocalDefId,
3029
body: &'tcx hir::Body<'tcx>,
3130
can_be_generator: Option<hir::Movability>,
32-
) -> (FnCtxt<'a, 'tcx>, Option<GeneratorTypes<'tcx>>) {
33-
let fn_id = inherited.tcx.hir().local_def_id_to_hir_id(fn_def_id);
34-
35-
// Create the function context. This is either derived from scratch or,
36-
// in the case of closures, based on the outer context.
37-
let mut fcx = FnCtxt::new(inherited, param_env, body.value.hir_id);
31+
) -> Option<GeneratorTypes<'tcx>> {
32+
let fn_id = fcx.tcx.hir().local_def_id_to_hir_id(fn_def_id);
3833
fcx.ps.set(UnsafetyState::function(fn_sig.unsafety, fn_id));
3934

4035
let tcx = fcx.tcx;
@@ -47,7 +42,7 @@ pub(super) fn check_fn<'a, 'tcx>(
4742
declared_ret_ty,
4843
body.value.hir_id,
4944
decl.output.span(),
50-
param_env,
45+
fcx.param_env,
5146
));
5247

5348
fcx.ret_coercion = Some(RefCell::new(CoerceMany::new(ret_ty)));
@@ -105,7 +100,7 @@ pub(super) fn check_fn<'a, 'tcx>(
105100
fcx.write_ty(param.hir_id, param_ty);
106101
}
107102

108-
inherited.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);
103+
fcx.typeck_results.borrow_mut().liberated_fn_sigs_mut().insert(fn_id, fn_sig);
109104

110105
if let ty::Dynamic(_, _, ty::Dyn) = declared_ret_ty.kind() {
111106
// FIXME: We need to verify that the return type is `Sized` after the return expression has
@@ -174,7 +169,7 @@ pub(super) fn check_fn<'a, 'tcx>(
174169
check_panic_info_fn(tcx, panic_impl_did.expect_local(), fn_sig, decl, declared_ret_ty);
175170
}
176171

177-
(fcx, gen_ty)
172+
gen_ty
178173
}
179174

180175
fn check_panic_info_fn(

compiler/rustc_hir_typeck/src/closure.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7979

8080
debug!(?bound_sig, ?liberated_sig);
8181

82+
let mut fcx = FnCtxt::new(self, self.param_env.without_const(), body.value.hir_id);
8283
let generator_types = check_fn(
83-
self,
84-
self.param_env.without_const(),
84+
&mut fcx,
8585
liberated_sig,
8686
closure.fn_decl,
8787
expr_def_id,
8888
body,
8989
closure.movability,
90-
)
91-
.1;
90+
);
9291

9392
let parent_substs = InternalSubsts::identity_for_item(
9493
self.tcx,
@@ -797,10 +796,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
797796
) -> ClosureSignatures<'tcx> {
798797
let liberated_sig =
799798
self.tcx().liberate_late_bound_regions(expr_def_id.to_def_id(), bound_sig);
800-
let liberated_sig = self.normalize(
801-
body.value.span,
802-
liberated_sig,
803-
);
799+
let liberated_sig = self.normalize(body.value.span, liberated_sig);
804800
ClosureSignatures { bound_sig, liberated_sig }
805801
}
806802
}

compiler/rustc_hir_typeck/src/coercion.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ use rustc_span::{self, BytePos, DesugaringKind, Span};
6262
use rustc_target::spec::abi::Abi;
6363
use rustc_trait_selection::infer::InferCtxtExt as _;
6464
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
65-
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode, ObligationCtxt};
65+
use rustc_trait_selection::traits::{
66+
self, NormalizeExt, ObligationCause, ObligationCauseCode, ObligationCtxt,
67+
};
6668

6769
use smallvec::{smallvec, SmallVec};
6870
use std::ops::Deref;
@@ -832,7 +834,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
832834
833835
let b = self.shallow_resolve(b);
834836
let InferOk { value: b, mut obligations } =
835-
self.normalize_associated_types_in_as_infer_ok(self.cause.span, b);
837+
self.at(&self.cause, self.param_env).normalize(b);
836838
debug!("coerce_from_fn_item(a={:?}, b={:?})", a, b);
837839

838840
match b.kind() {
@@ -854,7 +856,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
854856
}
855857

856858
let InferOk { value: a_sig, obligations: o1 } =
857-
self.normalize_associated_types_in_as_infer_ok(self.cause.span, a_sig);
859+
self.at(&self.cause, self.param_env).normalize(a_sig);
858860
obligations.extend(o1);
859861

860862
let a_fn_pointer = self.tcx.mk_fn_ptr(a_sig);

compiler/rustc_hir_typeck/src/expr.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1748,9 +1748,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17481748
ty::Adt(adt, substs) if adt.is_struct() => variant
17491749
.fields
17501750
.iter()
1751-
.map(|f| {
1752-
self.normalize(expr_span, f.ty(self.tcx, substs))
1753-
})
1751+
.map(|f| self.normalize(expr_span, f.ty(self.tcx, substs)))
17541752
.collect(),
17551753
_ => {
17561754
self.tcx

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+2-40
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_hir_analysis::astconv::{
1616
};
1717
use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
1818
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
19-
use rustc_infer::infer::{InferOk, InferResult};
19+
use rustc_infer::infer::InferResult;
2020
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
2121
use rustc_middle::ty::error::TypeError;
2222
use rustc_middle::ty::fold::TypeFoldable;
@@ -31,9 +31,7 @@ use rustc_span::hygiene::DesugaringKind;
3131
use rustc_span::symbol::{kw, sym, Ident};
3232
use rustc_span::{Span, DUMMY_SP};
3333
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
34-
use rustc_trait_selection::traits::{
35-
self, NormalizeExt, ObligationCause, ObligationCauseCode, ObligationCtxt,
36-
};
34+
use rustc_trait_selection::traits::{self, NormalizeExt, ObligationCauseCode, ObligationCtxt};
3735

3836
use std::collections::hash_map::Entry;
3937
use std::slice;
@@ -377,42 +375,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
377375
)
378376
}
379377

380-
pub(in super::super) fn normalize_associated_types_in_as_infer_ok<T>(
381-
&self,
382-
span: Span,
383-
value: T,
384-
) -> InferOk<'tcx, T>
385-
where
386-
T: TypeFoldable<'tcx>,
387-
{
388-
self.at(&ObligationCause::misc(span, self.body_id), self.param_env).normalize(value)
389-
}
390-
391-
pub(in super::super) fn normalize_op_associated_types_in_as_infer_ok<T>(
392-
&self,
393-
span: Span,
394-
value: T,
395-
opt_input_expr: Option<&hir::Expr<'_>>,
396-
) -> InferOk<'tcx, T>
397-
where
398-
T: TypeFoldable<'tcx>,
399-
{
400-
self.at(
401-
&ObligationCause::new(
402-
span,
403-
self.body_id,
404-
traits::BinOp {
405-
rhs_span: opt_input_expr.map(|expr| expr.span),
406-
is_lit: opt_input_expr
407-
.map_or(false, |expr| matches!(expr.kind, ExprKind::Lit(_))),
408-
output_ty: None,
409-
},
410-
),
411-
self.param_env,
412-
)
413-
.normalize(value)
414-
}
415-
416378
pub fn require_type_meets(
417379
&self,
418380
ty: Ty<'tcx>,

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_span::Span;
2020
use rustc_trait_selection::infer::InferCtxtExt;
2121
use rustc_trait_selection::traits::error_reporting::DefIdOrName;
2222
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
23+
use rustc_trait_selection::traits::NormalizeExt;
2324

2425
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2526
pub(crate) fn body_fn_sig(&self) -> Option<ty::FnSig<'tcx>> {
@@ -245,7 +246,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
245246
// implied by wf, but also because that would possibly result in
246247
// erroneous errors later on.
247248
let infer::InferOk { value: output, obligations: _ } =
248-
self.normalize_associated_types_in_as_infer_ok(expr.span, output);
249+
self.at(&self.misc(expr.span), self.param_env).normalize(output);
249250

250251
if output.is_ty_var() { None } else { Some((def_id_or_name, output, inputs)) }
251252
}

compiler/rustc_hir_typeck/src/lib.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ pub use diverges::Diverges;
4848
pub use expectation::Expectation;
4949
pub use fn_ctxt::*;
5050
pub use inherited::{Inherited, InheritedBuilder};
51-
use rustc_infer::traits::ObligationCause;
52-
use rustc_trait_selection::traits::NormalizeExt;
5351

5452
use crate::check::check_fn;
5553
use crate::coercion::DynamicCoerceMany;
@@ -235,9 +233,10 @@ fn typeck_with_fallback<'tcx>(
235233

236234
let typeck_results = Inherited::build(tcx, def_id).enter(|inh| {
237235
let param_env = tcx.param_env(def_id);
238-
let mut fcx = if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
236+
let mut fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
237+
238+
if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
239239
let fn_sig = if rustc_hir_analysis::collect::get_infer_ret_ty(&decl.output).is_some() {
240-
let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
241240
<dyn AstConv<'_>>::ty_of_fn(&fcx, id, header.unsafety, header.abi, decl, None, None)
242241
} else {
243242
tcx.fn_sig(def_id)
@@ -247,16 +246,10 @@ fn typeck_with_fallback<'tcx>(
247246

248247
// Compute the function signature from point of view of inside the fn.
249248
let fn_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), fn_sig);
250-
// FIXME(compiler-errors): Remove
251-
let fn_sig = inh
252-
.register_infer_ok_obligations(
253-
inh.at(&ObligationCause::misc(body.value.span, body_id.hir_id),
254-
param_env,
255-
)
256-
.normalize(fn_sig));
257-
check_fn(&inh, param_env, fn_sig, decl, def_id, body, None).0
249+
let fn_sig = fcx.normalize(body.value.span, fn_sig);
250+
251+
check_fn(&mut fcx, fn_sig, decl, def_id, body, None);
258252
} else {
259-
let fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
260253
let expected_type = body_ty
261254
.and_then(|ty| match ty.kind {
262255
hir::TyKind::Infer => Some(<dyn AstConv<'_>>::ast_ty_to_ty(&fcx, ty)),
@@ -316,8 +309,6 @@ fn typeck_with_fallback<'tcx>(
316309
fcx.check_expr_coercable_to_type(&body.value, expected_type, None);
317310

318311
fcx.write_ty(id, expected_type);
319-
320-
fcx
321312
};
322313

323314
fcx.type_inference_fallback();

compiler/rustc_hir_typeck/src/method/mod.rs

+16-23
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
2323
use rustc_middle::ty::{self, DefIdTree, GenericParamDefKind, Ty, TypeVisitable};
2424
use rustc_span::symbol::Ident;
2525
use rustc_span::Span;
26-
use rustc_trait_selection::traits;
2726
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
27+
use rustc_trait_selection::traits::{self, NormalizeExt};
2828

2929
use self::probe::{IsSuggestion, ProbeScope};
3030

@@ -465,11 +465,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
465465
let fn_sig = fn_sig.subst(self.tcx, substs);
466466
let fn_sig = self.replace_bound_vars_with_fresh_vars(span, infer::FnCall, fn_sig);
467467

468-
let InferOk { value, obligations: o } = if is_op {
469-
self.normalize_op_associated_types_in_as_infer_ok(span, fn_sig, opt_input_expr)
468+
let cause = if is_op {
469+
ObligationCause::new(
470+
span,
471+
self.body_id,
472+
traits::BinOp {
473+
rhs_span: opt_input_expr.map(|expr| expr.span),
474+
is_lit: opt_input_expr
475+
.map_or(false, |expr| matches!(expr.kind, hir::ExprKind::Lit(_))),
476+
output_ty: None,
477+
},
478+
)
470479
} else {
471-
self.normalize_associated_types_in_as_infer_ok(span, fn_sig)
480+
traits::ObligationCause::misc(span, self.body_id)
472481
};
482+
483+
let InferOk { value, obligations: o } = self.at(&cause, self.param_env).normalize(fn_sig);
473484
let fn_sig = {
474485
obligations.extend(o);
475486
value
@@ -485,32 +496,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
485496
// any late-bound regions appearing in its bounds.
486497
let bounds = self.tcx.predicates_of(def_id).instantiate(self.tcx, substs);
487498

488-
let InferOk { value, obligations: o } = if is_op {
489-
self.normalize_op_associated_types_in_as_infer_ok(span, bounds, opt_input_expr)
490-
} else {
491-
self.normalize_associated_types_in_as_infer_ok(span, bounds)
492-
};
499+
let InferOk { value, obligations: o } = self.at(&cause, self.param_env).normalize(bounds);
493500
let bounds = {
494501
obligations.extend(o);
495502
value
496503
};
497504

498505
assert!(!bounds.has_escaping_bound_vars());
499506

500-
let cause = if is_op {
501-
ObligationCause::new(
502-
span,
503-
self.body_id,
504-
traits::BinOp {
505-
rhs_span: opt_input_expr.map(|expr| expr.span),
506-
is_lit: opt_input_expr
507-
.map_or(false, |expr| matches!(expr.kind, hir::ExprKind::Lit(_))),
508-
output_ty: None,
509-
},
510-
)
511-
} else {
512-
traits::ObligationCause::misc(span, self.body_id)
513-
};
514507
let predicates_cause = cause.clone();
515508
obligations.extend(traits::predicates_for_generics(
516509
move |_, _| predicates_cause.clone(),

0 commit comments

Comments
 (0)