Skip to content

Commit 506f934

Browse files
committed
Rename and clarify that Bubble is now just Ingore as it doesn't actually register hidden types anymore
1 parent 8c66d6d commit 506f934

File tree

6 files changed

+45
-42
lines changed

6 files changed

+45
-42
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,13 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
272272
// This logic duplicates most of `check_opaque_meets_bounds`.
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);
275-
// HACK This bubble is required for this tests to pass:
276-
// type-alias-impl-trait/issue-67844-nested-opaque.rs
277-
let infcx =
278-
self.tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).build();
275+
let infcx = self
276+
.tcx
277+
.infer_ctxt()
278+
// HACK This bubble is required for this tests to pass:
279+
// type-alias-impl-trait/issue-67844-nested-opaque.rs
280+
.with_opaque_type_inference(DefiningAnchor::Ignore)
281+
.build();
279282
let ocx = ObligationCtxt::new(&infcx);
280283
// Require the hidden type to be well-formed with only the generics of the opaque type.
281284
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
@@ -316,10 +319,6 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
316319
// version.
317320
let errors = ocx.select_all_or_error();
318321

319-
// This is still required for many(half of the tests in ui/type-alias-impl-trait)
320-
// tests to pass
321-
let _ = infcx.take_opaque_types();
322-
323322
if errors.is_empty() {
324323
definition_ty
325324
} else {

compiler/rustc_const_eval/src/util/compare_types.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@ pub fn is_subtype<'tcx>(
4141
return true;
4242
}
4343

44-
let mut builder =
45-
tcx.infer_ctxt().ignoring_regions().with_opaque_type_inference(DefiningAnchor::Bubble);
44+
let mut builder = tcx
45+
.infer_ctxt()
46+
.ignoring_regions()
47+
// With `Reveal::All`, opaque types get normalized away, with `Reveal::UserFacing`
48+
// we would get unification errors because we're unable to look into opaque types,
49+
// even if they're constrained in our current function.
50+
//
51+
// It seems very unlikely that this hides any bugs.
52+
.with_opaque_type_inference(DefiningAnchor::Ignore);
4653
let infcx = builder.build();
4754
let ocx = ObligationCtxt::new(&infcx);
4855
let cause = ObligationCause::dummy();
@@ -53,11 +60,5 @@ pub fn is_subtype<'tcx>(
5360
Err(_) => return false,
5461
};
5562
let errors = ocx.select_all_or_error();
56-
// With `Reveal::All`, opaque types get normalized away, with `Reveal::UserFacing`
57-
// we would get unification errors because we're unable to look into opaque types,
58-
// even if they're constrained in our current function.
59-
//
60-
// It seems very unlikely that this hides any bugs.
61-
let _ = infcx.take_opaque_types();
6263
errors.is_empty()
6364
}

compiler/rustc_infer/src/infer/mod.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,12 @@ impl<'tcx> InferCtxtInner<'tcx> {
244244

245245
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
246246
pub enum DefiningAnchor {
247-
/// `DefId` of the item.
247+
/// `DefId` of the item for which we check whether any opaque types may
248+
/// have their hidden types registered.
248249
Bind(LocalDefId),
249-
/// When opaque types are not resolved, we `Bubble` up, meaning
250-
/// return the opaque/hidden type pair from query, for caller of query to handle it.
251-
Bubble,
250+
/// Used to treat all opaque types as in their defining scope.
251+
/// Used for coherence, codegen and sanity checks.
252+
Ignore,
252253
/// Used to catch type mismatch errors when handling opaque types.
253254
Error,
254255
}
@@ -259,7 +260,7 @@ pub struct InferCtxt<'tcx> {
259260
/// The `DefId` of the item in whose context we are performing inference or typeck.
260261
/// It is used to check whether an opaque type use is a defining use.
261262
///
262-
/// If it is `DefiningAnchor::Bubble`, we can't resolve opaque types here and need to bubble up
263+
/// If it is `DefiningAnchor::Ignore`, we can't resolve opaque types here and need to bubble up
263264
/// the obligation. This frequently happens for
264265
/// short lived InferCtxt within queries. The opaque type obligations are forwarded
265266
/// to the outside until the end up in an `InferCtxt` for typeck or borrowck.
@@ -1340,7 +1341,11 @@ impl<'tcx> InferCtxt<'tcx> {
13401341

13411342
#[instrument(level = "debug", skip(self), ret)]
13421343
pub fn take_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx> {
1343-
debug_assert_ne!(self.defining_use_anchor, DefiningAnchor::Error);
1344+
debug_assert!(
1345+
matches!(self.defining_use_anchor, DefiningAnchor::Bind(_)),
1346+
"{:?}",
1347+
self.defining_use_anchor
1348+
);
13441349
std::mem::take(&mut self.inner.borrow_mut().opaque_type_storage.opaque_types)
13451350
}
13461351

compiler/rustc_infer/src/infer/opaque_types.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'tcx> InferCtxt<'tcx> {
144144
// ```
145145
self.opaque_type_origin(def_id)?
146146
}
147-
DefiningAnchor::Bubble => self.opaque_type_origin_unchecked(def_id),
147+
DefiningAnchor::Ignore => self.opaque_type_origin_unchecked(def_id),
148148
DefiningAnchor::Error => return None,
149149
};
150150
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }) = *b.kind() {
@@ -374,7 +374,7 @@ impl<'tcx> InferCtxt<'tcx> {
374374
pub fn opaque_type_origin(&self, def_id: LocalDefId) -> Option<OpaqueTyOrigin> {
375375
let opaque_hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
376376
let parent_def_id = match self.defining_use_anchor {
377-
DefiningAnchor::Bubble | DefiningAnchor::Error => return None,
377+
DefiningAnchor::Ignore | DefiningAnchor::Error => return None,
378378
DefiningAnchor::Bind(bind) => bind,
379379
};
380380

@@ -539,14 +539,16 @@ impl<'tcx> InferCtxt<'tcx> {
539539
let span = cause.span;
540540

541541
let mut obligations = vec![];
542-
let prev = self.inner.borrow_mut().opaque_types().register(
543-
OpaqueTypeKey { def_id, substs },
544-
OpaqueHiddenType { ty: hidden_ty, span },
545-
origin,
546-
);
547-
if let Some(prev) = prev {
548-
obligations =
549-
self.at(&cause, param_env).eq_exp(a_is_expected, prev, hidden_ty)?.obligations;
542+
if !matches!(self.defining_use_anchor, DefiningAnchor::Ignore) {
543+
let prev = self.inner.borrow_mut().opaque_types().register(
544+
OpaqueTypeKey { def_id, substs },
545+
OpaqueHiddenType { ty: hidden_ty, span },
546+
origin,
547+
);
548+
if let Some(prev) = prev {
549+
obligations =
550+
self.at(&cause, param_env).eq_exp(a_is_expected, prev, hidden_ty)?.obligations;
551+
}
550552
}
551553

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

compiler/rustc_trait_selection/src/traits/coherence.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn overlapping_impls(
9696
}
9797

9898
let infcx =
99-
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).intercrate().build();
99+
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Ignore).intercrate().build();
100100
let selcx = &mut SelectionContext::new(&infcx);
101101
let overlaps =
102102
overlap(selcx, skip_leak_check, impl1_def_id, impl2_def_id, overlap_mode).is_some();
@@ -108,7 +108,7 @@ pub fn overlapping_impls(
108108
// this time tracking intercrate ambiguity causes for better
109109
// diagnostics. (These take time and can lead to false errors.)
110110
let infcx =
111-
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).intercrate().build();
111+
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Ignore).intercrate().build();
112112
let selcx = &mut SelectionContext::new(&infcx);
113113
selcx.enable_tracking_intercrate_ambiguity_causes();
114114
Some(overlap(selcx, skip_leak_check, impl1_def_id, impl2_def_id, overlap_mode).unwrap())

compiler/rustc_traits/src/codegen.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ pub fn codegen_select_candidate<'tcx>(
3232
let infcx = tcx
3333
.infer_ctxt()
3434
.ignoring_regions()
35-
.with_opaque_type_inference(DefiningAnchor::Bubble)
35+
// Opaque types may have gotten their hidden types constrained, but we can ignore them safely
36+
// as they will get constrained elsewhere, too.
37+
// (ouz-a) This is required for `type-alias-impl-trait/assoc-projection-ice.rs` to pass
38+
.with_opaque_type_inference(DefiningAnchor::Ignore)
3639
.build();
37-
//~^ HACK `Bubble` is required for
38-
// this test to pass: type-alias-impl-trait/assoc-projection-ice.rs
3940
let mut selcx = SelectionContext::new(&infcx);
4041

4142
let obligation_cause = ObligationCause::dummy();
@@ -79,10 +80,5 @@ pub fn codegen_select_candidate<'tcx>(
7980
let impl_source = infcx.resolve_vars_if_possible(impl_source);
8081
let impl_source = infcx.tcx.erase_regions(impl_source);
8182

82-
// Opaque types may have gotten their hidden types constrained, but we can ignore them safely
83-
// as they will get constrained elsewhere, too.
84-
// (ouz-a) This is required for `type-alias-impl-trait/assoc-projection-ice.rs` to pass
85-
let _ = infcx.take_opaque_types();
86-
8783
Ok(&*tcx.arena.alloc(impl_source))
8884
}

0 commit comments

Comments
 (0)