Skip to content

Commit f4c7596

Browse files
committed
Auto merge of #108311 - oli-obk:invert_defines, r=lcnr
Make hidden type registration opt-in, so that each site can be reviewed on its own and we have the right defaults for trait solvers r? `@lcnr` pulled out of #107891 as it is the uncontroversial part
2 parents 3200982 + 88a7b68 commit f4c7596

File tree

15 files changed

+51
-26
lines changed

15 files changed

+51
-26
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
273273
// FIXME(oli-obk): Also do region checks here and then consider removing `check_opaque_meets_bounds` entirely.
274274
let param_env = self.tcx.param_env(def_id);
275275
// HACK This bubble is required for this tests to pass:
276-
// type-alias-impl-trait/issue-67844-nested-opaque.rs
276+
// nested-return-type2-tait2.rs
277+
// nested-return-type2-tait3.rs
277278
let infcx =
278279
self.tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).build();
279280
let ocx = ObligationCtxt::new(&infcx);

compiler/rustc_hir_typeck/src/closure.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
561561
) {
562562
// Check that E' = S'.
563563
let cause = self.misc(hir_ty.span);
564-
let InferOk { value: (), obligations } =
565-
self.at(&cause, self.param_env).eq(*expected_ty, supplied_ty)?;
564+
let InferOk { value: (), obligations } = self
565+
.at(&cause, self.param_env)
566+
.define_opaque_types(true)
567+
.eq(*expected_ty, supplied_ty)?;
566568
all_obligations.extend(obligations);
567569
}
568570

@@ -574,6 +576,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
574576
let cause = &self.misc(decl.output.span());
575577
let InferOk { value: (), obligations } = self
576578
.at(cause, self.param_env)
579+
.define_opaque_types(true)
577580
.eq(expected_sigs.liberated_sig.output(), supplied_output_ty)?;
578581
all_obligations.extend(obligations);
579582

compiler/rustc_hir_typeck/src/coercion.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ 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);
146147
if self.use_lub {
147-
self.at(&self.cause, self.fcx.param_env).lub(b, a)
148+
at.lub(b, a)
148149
} else {
149-
self.at(&self.cause, self.fcx.param_env)
150-
.sup(b, a)
150+
at.sup(b, a)
151151
.map(|InferOk { value: (), obligations }| InferOk { value: a, obligations })
152152
}
153153
})
@@ -174,7 +174,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
174174
// Best-effort try to unify these types -- we're already on the error path,
175175
// so this will have the side-effect of making sure we have no ambiguities
176176
// due to `[type error]` and `_` not coercing together.
177-
let _ = self.commit_if_ok(|_| self.at(&self.cause, self.param_env).eq(a, b));
177+
let _ = self.commit_if_ok(|_| {
178+
self.at(&self.cause, self.param_env).define_opaque_types(true).eq(a, b)
179+
});
178180
return success(vec![], self.fcx.tcx.ty_error(), vec![]);
179181
}
180182

@@ -1484,6 +1486,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14841486
// Another example is `break` with no argument expression.
14851487
assert!(expression_ty.is_unit(), "if let hack without unit type");
14861488
fcx.at(cause, fcx.param_env)
1489+
// needed for tests/ui/type-alias-impl-trait/issue-65679-inst-opaque-ty-from-val-twice.rs
1490+
.define_opaque_types(true)
14871491
.eq_exp(label_expression_as_expected, expression_ty, self.merged_ty())
14881492
.map(|infer_ok| {
14891493
fcx.register_infer_ok_obligations(infer_ok);

compiler/rustc_hir_typeck/src/demand.rs

+2-2
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).sup(expected, actual) {
116+
match self.at(cause, self.param_env).define_opaque_types(true).sup(expected, actual) {
117117
Ok(InferOk { obligations, value: () }) => {
118118
self.register_predicates(obligations);
119119
None
@@ -143,7 +143,7 @@ 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).eq(expected, actual) {
146+
match self.at(cause, self.param_env).define_opaque_types(true).eq(expected, actual) {
147147
Ok(InferOk { obligations, value: () }) => {
148148
self.register_predicates(obligations);
149149
None

compiler/rustc_hir_typeck/src/method/probe.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14361436
TraitCandidate(trait_ref) => self.probe(|_| {
14371437
let _ = self
14381438
.at(&ObligationCause::dummy(), self.param_env)
1439-
.define_opaque_types(false)
14401439
.sup(candidate.xform_self_ty, self_ty);
14411440
match self.select_trait_candidate(trait_ref) {
14421441
Ok(Some(traits::ImplSource::UserDefined(ref impl_data))) => {
@@ -1466,7 +1465,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14661465
// First check that the self type can be related.
14671466
let sub_obligations = match self
14681467
.at(&ObligationCause::dummy(), self.param_env)
1469-
.define_opaque_types(false)
14701468
.sup(probe.xform_self_ty, self_ty)
14711469
{
14721470
Ok(InferOk { obligations, value: () }) => obligations,
@@ -1683,7 +1681,6 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16831681
if let ProbeResult::Match = result
16841682
&& self
16851683
.at(&ObligationCause::dummy(), self.param_env)
1686-
.define_opaque_types(false)
16871684
.sup(return_ty, xform_ret_ty)
16881685
.is_err()
16891686
{

compiler/rustc_infer/src/infer/at.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'tcx> InferCtxt<'tcx> {
5555
cause: &'a ObligationCause<'tcx>,
5656
param_env: ty::ParamEnv<'tcx>,
5757
) -> At<'a, 'tcx> {
58-
At { infcx: self, cause, param_env, define_opaque_types: true }
58+
At { infcx: self, cause, param_env, define_opaque_types: false }
5959
}
6060

6161
/// Forks the inference context, creating a new inference context with the same inference

compiler/rustc_infer/src/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ pub struct InferCtxt<'tcx> {
264264
/// short lived InferCtxt within queries. The opaque type obligations are forwarded
265265
/// to the outside until the end up in an `InferCtxt` for typeck or borrowck.
266266
///
267-
/// It is default value is `DefiningAnchor::Error`, this way it is easier to catch errors that
267+
/// Its default value is `DefiningAnchor::Error`, this way it is easier to catch errors that
268268
/// might come up during inference or typeck.
269269
pub defining_use_anchor: DefiningAnchor,
270270

compiler/rustc_infer/src/infer/opaque_types.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,11 @@ impl<'tcx> InferCtxt<'tcx> {
545545
origin,
546546
);
547547
if let Some(prev) = prev {
548-
obligations =
549-
self.at(&cause, param_env).eq_exp(a_is_expected, prev, hidden_ty)?.obligations;
548+
obligations = self
549+
.at(&cause, param_env)
550+
.define_opaque_types(true)
551+
.eq_exp(a_is_expected, prev, hidden_ty)?
552+
.obligations;
550553
}
551554

552555
let item_bounds = tcx.bound_explicit_item_bounds(def_id.to_def_id());

compiler/rustc_trait_selection/src/solve/infcx_ext.rs

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
5454
rhs: T,
5555
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
5656
self.at(&ObligationCause::dummy(), param_env)
57-
.define_opaque_types(false)
5857
.eq(lhs, rhs)
5958
.map(|InferOk { value: (), obligations }| {
6059
obligations.into_iter().map(|o| o.into()).collect()

compiler/rustc_trait_selection/src/traits/coherence.rs

+1
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ fn equate_impl_headers<'cx, 'tcx>(
217217
selcx
218218
.infcx
219219
.at(&ObligationCause::dummy(), ty::ParamEnv::empty())
220+
.define_opaque_types(true)
220221
.eq_impl_headers(impl1_header, impl2_header)
221222
.map(|infer_ok| infer_ok.obligations)
222223
.ok()

compiler/rustc_trait_selection/src/traits/engine.rs

+4
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
128128
{
129129
self.infcx
130130
.at(cause, param_env)
131+
.define_opaque_types(true)
131132
.eq_exp(a_is_expected, a, b)
132133
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
133134
}
@@ -141,6 +142,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
141142
) -> Result<(), TypeError<'tcx>> {
142143
self.infcx
143144
.at(cause, param_env)
145+
.define_opaque_types(true)
144146
.eq(expected, actual)
145147
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
146148
}
@@ -155,6 +157,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
155157
) -> Result<(), TypeError<'tcx>> {
156158
self.infcx
157159
.at(cause, param_env)
160+
.define_opaque_types(true)
158161
.sup(expected, actual)
159162
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
160163
}
@@ -169,6 +172,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
169172
) -> Result<(), TypeError<'tcx>> {
170173
self.infcx
171174
.at(cause, param_env)
175+
.define_opaque_types(true)
172176
.sup(expected, actual)
173177
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
174178
}

compiler/rustc_trait_selection/src/traits/project.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,12 @@ fn project_and_unify_type<'cx, 'tcx>(
286286
);
287287
obligations.extend(new);
288288

289-
match infcx.at(&obligation.cause, obligation.param_env).eq(normalized, actual) {
289+
match infcx
290+
.at(&obligation.cause, obligation.param_env)
291+
// This is needed to support nested opaque types like `impl Fn() -> impl Trait`
292+
.define_opaque_types(true)
293+
.eq(normalized, actual)
294+
{
290295
Ok(InferOk { obligations: inferred_obligations, value: () }) => {
291296
obligations.extend(inferred_obligations);
292297
ProjectAndUnifyResult::Holds(obligations)

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+2
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
821821

822822
self.infcx
823823
.at(&obligation.cause, obligation.param_env)
824+
// needed for tests/ui/type-alias-impl-trait/assoc-projection-ice.rs
825+
.define_opaque_types(true)
824826
.sup(obligation_trait_ref, expected_trait_ref)
825827
.map(|InferOk { mut obligations, .. }| {
826828
obligations.extend(nested);

compiler/rustc_trait_selection/src/traits/select/mod.rs

-8
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
17521752
});
17531753
self.infcx
17541754
.at(&obligation.cause, obligation.param_env)
1755-
.define_opaque_types(false)
17561755
.sup(ty::Binder::dummy(placeholder_trait_ref), trait_bound)
17571756
.map(|InferOk { obligations: _, value: () }| {
17581757
// This method is called within a probe, so we can't have
@@ -1815,7 +1814,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
18151814
let is_match = self
18161815
.infcx
18171816
.at(&obligation.cause, obligation.param_env)
1818-
.define_opaque_types(false)
18191817
.sup(obligation.predicate, infer_projection)
18201818
.map_or(false, |InferOk { obligations, value: () }| {
18211819
self.evaluate_predicates_recursively(
@@ -2507,7 +2505,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
25072505
let InferOk { obligations, .. } = self
25082506
.infcx
25092507
.at(&cause, obligation.param_env)
2510-
.define_opaque_types(false)
25112508
.eq(placeholder_obligation_trait_ref, impl_trait_ref)
25122509
.map_err(|e| {
25132510
debug!("match_impl: failed eq_trait_refs due to `{}`", e.to_string(self.tcx()))
@@ -2558,11 +2555,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
25582555
) -> Result<Vec<PredicateObligation<'tcx>>, ()> {
25592556
self.infcx
25602557
.at(&obligation.cause, obligation.param_env)
2561-
// We don't want predicates for opaque types to just match all other types,
2562-
// if there is an obligation on the opaque type, then that obligation must be met
2563-
// opaquely. Otherwise we'd match any obligation to the opaque type and then error
2564-
// out later.
2565-
.define_opaque_types(false)
25662558
.sup(obligation.predicate.to_poly_trait_ref(), poly_trait_ref)
25672559
.map(|InferOk { obligations, .. }| obligations)
25682560
.map_err(|_| ())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::fmt::Debug;
2+
3+
// check-pass
4+
5+
fn bar() -> impl Debug {}
6+
7+
fn baz(b: bool) -> Option<impl Debug> {
8+
match b {
9+
true => baz(false),
10+
false => Some(bar()),
11+
}
12+
}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)