Skip to content

Commit 0d4017c

Browse files
committed
Don't look at InferCtxt::defining_use_anchor from deep inside the opaque type resolution and pass it down instead
1 parent bcb610d commit 0d4017c

File tree

21 files changed

+107
-56
lines changed

21 files changed

+107
-56
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
8787
self.locations.span(self.type_checker.body)
8888
}
8989

90+
fn defining_use_anchor(&self) -> rustc_infer::infer::DefiningAnchor {
91+
self.type_checker.infcx.defining_use_anchor
92+
}
93+
9094
fn param_env(&self) -> ty::ParamEnv<'tcx> {
9195
self.type_checker.param_env
9296
}

compiler/rustc_hir_typeck/src/check.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub(super) fn check_fn<'a, 'tcx>(
4646
fn_def_id,
4747
decl.output.span(),
4848
fcx.param_env,
49+
fcx.defining_use_anchor,
4950
));
5051

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

compiler/rustc_hir_typeck/src/closure.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
565565
let cause = self.misc(hir_ty.span);
566566
let InferOk { value: (), obligations } = self
567567
.at(&cause, self.param_env)
568-
.define_opaque_types(true)
568+
.define_opaque_types(self.defining_use_anchor)
569569
.eq(*expected_ty, supplied_ty)?;
570570
all_obligations.extend(obligations);
571571
}
@@ -578,7 +578,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
578578
let cause = &self.misc(decl.output.span());
579579
let InferOk { value: (), obligations } = self
580580
.at(cause, self.param_env)
581-
.define_opaque_types(true)
581+
.define_opaque_types(self.defining_use_anchor)
582582
.eq(expected_sigs.liberated_sig.output(), supplied_output_ty)?;
583583
all_obligations.extend(obligations);
584584

@@ -735,6 +735,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
735735
body_def_id,
736736
self.tcx.def_span(expr_def_id),
737737
self.param_env,
738+
self.defining_use_anchor,
738739
);
739740
self.register_predicates(obligations);
740741

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
143143
fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {
144144
debug!("unify(a: {:?}, b: {:?}, use_lub: {})", a, b, self.use_lub);
145145
self.commit_if_ok(|_| {
146-
let at = self.at(&self.cause, self.fcx.param_env).define_opaque_types(true);
146+
let at = self
147+
.at(&self.cause, self.fcx.param_env)
148+
.define_opaque_types(self.defining_use_anchor);
147149
if self.use_lub {
148150
at.lub(b, a)
149151
} else {
@@ -175,7 +177,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
175177
// so this will have the side-effect of making sure we have no ambiguities
176178
// due to `[type error]` and `_` not coercing together.
177179
let _ = self.commit_if_ok(|_| {
178-
self.at(&self.cause, self.param_env).define_opaque_types(true).eq(a, b)
180+
self.at(&self.cause, self.param_env)
181+
.define_opaque_types(self.defining_use_anchor)
182+
.eq(a, b)
179183
});
180184
return success(vec![], self.fcx.tcx.ty_error(guar), vec![]);
181185
}
@@ -1487,7 +1491,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14871491
assert!(expression_ty.is_unit(), "if let hack without unit type");
14881492
fcx.at(cause, fcx.param_env)
14891493
// needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs
1490-
.define_opaque_types(true)
1494+
.define_opaque_types(fcx.defining_use_anchor)
14911495
.eq_exp(label_expression_as_expected, expression_ty, self.merged_ty())
14921496
.map(|infer_ok| {
14931497
fcx.register_infer_ok_obligations(infer_ok);

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
113113
expected: Ty<'tcx>,
114114
actual: Ty<'tcx>,
115115
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
116-
match self.at(cause, self.param_env).define_opaque_types(true).sup(expected, actual) {
116+
match self.at(cause, self.param_env).define_opaque_types(self.defining_use_anchor).sup(expected, actual) {
117117
Ok(InferOk { obligations, value: () }) => {
118118
self.register_predicates(obligations);
119119
None
@@ -143,7 +143,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
143143
expected: Ty<'tcx>,
144144
actual: Ty<'tcx>,
145145
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
146-
match self.at(cause, self.param_env).define_opaque_types(true).eq(expected, actual) {
146+
match self
147+
.at(cause, self.param_env)
148+
.define_opaque_types(self.defining_use_anchor)
149+
.eq(expected, actual)
150+
{
147151
Ok(InferOk { obligations, value: () }) => {
148152
self.register_predicates(obligations);
149153
None

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
738738
if let ty::subst::GenericArgKind::Type(ty) = ty.unpack()
739739
&& let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *ty.kind()
740740
&& let Some(def_id) = def_id.as_local()
741-
&& self.opaque_type_origin(def_id).is_some() {
741+
&& self.opaque_type_origin(def_id, self.defining_use_anchor).is_some() {
742742
return None;
743743
}
744744
}

compiler/rustc_infer/src/infer/at.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ pub struct At<'a, 'tcx> {
3636
pub param_env: ty::ParamEnv<'tcx>,
3737
/// Whether we should define opaque types
3838
/// or just treat them opaquely.
39-
/// Currently only used to prevent predicate
40-
/// matching from matching anything against opaque
41-
/// types.
42-
pub define_opaque_types: bool,
39+
pub define_opaque_types: DefiningAnchor,
4340
}
4441

4542
pub struct Trace<'a, 'tcx> {
@@ -55,7 +52,7 @@ impl<'tcx> InferCtxt<'tcx> {
5552
cause: &'a ObligationCause<'tcx>,
5653
param_env: ty::ParamEnv<'tcx>,
5754
) -> At<'a, 'tcx> {
58-
At { infcx: self, cause, param_env, define_opaque_types: false }
55+
At { infcx: self, cause, param_env, define_opaque_types: DefiningAnchor::Error }
5956
}
6057

6158
/// Forks the inference context, creating a new inference context with the same inference
@@ -93,7 +90,7 @@ pub trait ToTrace<'tcx>: Relate<'tcx> + Copy {
9390
}
9491

9592
impl<'a, 'tcx> At<'a, 'tcx> {
96-
pub fn define_opaque_types(self, define_opaque_types: bool) -> Self {
93+
pub fn define_opaque_types(self, define_opaque_types: DefiningAnchor) -> Self {
9794
Self { define_opaque_types, ..self }
9895
}
9996

compiler/rustc_infer/src/infer/canonical/query_response.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::infer::canonical::{
1414
};
1515
use crate::infer::nll_relate::{TypeRelating, TypeRelatingDelegate};
1616
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
17-
use crate::infer::{InferCtxt, InferOk, InferResult, NllRegionVariableOrigin};
17+
use crate::infer::{DefiningAnchor, InferCtxt, InferOk, InferResult, NllRegionVariableOrigin};
1818
use crate::traits::query::{Fallible, NoSolution};
1919
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
2020
use crate::traits::{PredicateObligations, TraitEngine, TraitEngineExt};
@@ -499,8 +499,12 @@ impl<'tcx> InferCtxt<'tcx> {
499499
let a = substitute_value(self.tcx, &result_subst, a);
500500
let b = substitute_value(self.tcx, &result_subst, b);
501501
debug!(?a, ?b, "constrain opaque type");
502-
obligations
503-
.extend(self.at(cause, param_env).define_opaque_types(true).eq(a, b)?.obligations);
502+
obligations.extend(
503+
self.at(cause, param_env)
504+
.define_opaque_types(self.defining_use_anchor)
505+
.eq(a, b)?
506+
.obligations,
507+
);
504508
}
505509

506510
Ok(InferOk { value: result_subst, obligations })
@@ -677,6 +681,10 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> {
677681
self.param_env
678682
}
679683

684+
fn defining_use_anchor(&self) -> DefiningAnchor {
685+
DefiningAnchor::Bubble
686+
}
687+
680688
fn create_next_universe(&mut self) -> ty::UniverseIndex {
681689
self.infcx.create_next_universe()
682690
}

compiler/rustc_infer/src/infer/combine.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use super::glb::Glb;
2727
use super::lub::Lub;
2828
use super::sub::Sub;
2929
use super::type_variable::TypeVariableValue;
30-
use super::{InferCtxt, MiscVariable, TypeTrace};
30+
use super::{DefiningAnchor, InferCtxt, MiscVariable, TypeTrace};
3131
use crate::traits::{Obligation, PredicateObligations};
3232
use rustc_data_structures::sso::SsoHashMap;
3333
use rustc_hir::def_id::DefId;
@@ -55,10 +55,7 @@ pub struct CombineFields<'infcx, 'tcx> {
5555
pub obligations: PredicateObligations<'tcx>,
5656
/// Whether we should define opaque types
5757
/// or just treat them opaquely.
58-
/// Currently only used to prevent predicate
59-
/// matching from matching anything against opaque
60-
/// types.
61-
pub define_opaque_types: bool,
58+
pub define_opaque_types: DefiningAnchor,
6259
}
6360

6461
#[derive(Copy, Clone, Debug)]

compiler/rustc_infer/src/infer/equate.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
110110
}
111111
(&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
112112
| (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
113-
if self.fields.define_opaque_types && def_id.is_local() =>
113+
if def_id.is_local() =>
114114
{
115115
self.fields.obligations.extend(
116116
infcx
@@ -120,6 +120,7 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
120120
self.a_is_expected(),
121121
&self.fields.trace.cause,
122122
self.param_env(),
123+
self.fields.define_opaque_types,
123124
)?
124125
.obligations,
125126
);

0 commit comments

Comments
 (0)