Skip to content

Commit 2f2350e

Browse files
committed
Eliminate DefiningAnchor now that is just a single-variant enum
1 parent dd72bf9 commit 2f2350e

File tree

20 files changed

+109
-140
lines changed

20 files changed

+109
-140
lines changed

compiler/rustc_borrowck/src/consumers.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_hir::def_id::LocalDefId;
44
use rustc_index::{IndexSlice, IndexVec};
55
use rustc_infer::infer::TyCtxtInferExt;
66
use rustc_middle::mir::{Body, Promoted};
7-
use rustc_middle::traits::DefiningAnchor;
87
use rustc_middle::ty::TyCtxt;
98
use std::rc::Rc;
109

@@ -106,7 +105,7 @@ pub fn get_body_with_borrowck_facts(
106105
options: ConsumerOptions,
107106
) -> BodyWithBorrowckFacts<'_> {
108107
let (input_body, promoted) = tcx.mir_promoted(def);
109-
let infcx = tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::bind(tcx, def)).build();
108+
let infcx = tcx.infer_ctxt().with_opaque_type_inference(def).build();
110109
let input_body: &Body<'_> = &input_body.borrow();
111110
let promoted: &IndexSlice<_, _> = &promoted.borrow();
112111
*super::do_mir_borrowck(&infcx, input_body, promoted, Some(options)).1.unwrap()

compiler/rustc_borrowck/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use rustc_infer::infer::{
3232
use rustc_middle::mir::tcx::PlaceTy;
3333
use rustc_middle::mir::*;
3434
use rustc_middle::query::Providers;
35-
use rustc_middle::traits::DefiningAnchor;
3635
use rustc_middle::ty::{self, ParamEnv, RegionVid, TyCtxt};
3736
use rustc_session::lint::builtin::UNUSED_MUT;
3837
use rustc_span::{Span, Symbol};
@@ -126,7 +125,7 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
126125
return tcx.arena.alloc(result);
127126
}
128127

129-
let infcx = tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::bind(tcx, def)).build();
128+
let infcx = tcx.infer_ctxt().with_opaque_type_inference(def).build();
130129
let promoted: &IndexSlice<_, _> = &promoted.borrow();
131130
let opt_closure_req = do_mir_borrowck(&infcx, input_body, promoted, None).0;
132131
debug!("mir_borrowck done");

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_infer::infer::TyCtxtInferExt as _;
77
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
88
use rustc_infer::traits::{Obligation, ObligationCause};
99
use rustc_macros::extension;
10-
use rustc_middle::traits::DefiningAnchor;
1110
use rustc_middle::ty::visit::TypeVisitableExt;
1211
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable};
1312
use rustc_middle::ty::{GenericArgKind, GenericArgs};
@@ -333,13 +332,13 @@ fn check_opaque_type_well_formed<'tcx>(
333332
parent_def_id = tcx.local_parent(parent_def_id);
334333
}
335334

336-
// FIXME(-Znext-solver): We probably should use `DefiningAnchor::Bind(&[])`
335+
// FIXME(-Znext-solver): We probably should use `&[]` instead of
337336
// and prepopulate this `InferCtxt` with known opaque values, rather than
338-
// using the `Bind` anchor here. For now it's fine.
337+
// allowing opaque types to be defined and checking them after the fact.
339338
let infcx = tcx
340339
.infer_ctxt()
341340
.with_next_trait_solver(next_trait_solver)
342-
.with_opaque_type_inference(DefiningAnchor::bind(tcx, parent_def_id))
341+
.with_opaque_type_inference(parent_def_id)
343342
.build();
344343
let ocx = ObligationCtxt::new(&infcx);
345344
let identity_args = GenericArgs::identity_for_item(tcx, def_id);

compiler/rustc_hir_analysis/src/check/check.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
1313
use rustc_infer::traits::{Obligation, TraitEngineExt as _};
1414
use rustc_lint_defs::builtin::REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS;
1515
use rustc_middle::middle::stability::EvalResult;
16-
use rustc_middle::traits::{DefiningAnchor, ObligationCauseCode};
16+
use rustc_middle::traits::ObligationCauseCode;
1717
use rustc_middle::ty::fold::BottomUpFolder;
1818
use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
1919
use rustc_middle::ty::util::{Discr, InspectCoroutineFields, IntTypeExt};
@@ -345,10 +345,7 @@ fn check_opaque_meets_bounds<'tcx>(
345345
};
346346
let param_env = tcx.param_env(defining_use_anchor);
347347

348-
let infcx = tcx
349-
.infer_ctxt()
350-
.with_opaque_type_inference(DefiningAnchor::bind(tcx, defining_use_anchor))
351-
.build();
348+
let infcx = tcx.infer_ctxt().with_opaque_type_inference(defining_use_anchor).build();
352349
let ocx = ObligationCtxt::new(&infcx);
353350

354351
let args = match *origin {
@@ -1567,7 +1564,7 @@ pub(super) fn check_coroutine_obligations(
15671564
.ignoring_regions()
15681565
// Bind opaque types to type checking root, as they should have been checked by borrowck,
15691566
// but may show up in some cases, like when (root) obligations are stalled in the new solver.
1570-
.with_opaque_type_inference(DefiningAnchor::bind(tcx, typeck.hir_owner.def_id))
1567+
.with_opaque_type_inference(typeck.hir_owner.def_id)
15711568
.build();
15721569

15731570
let mut fulfillment_cx = <dyn TraitEngine<'_>>::new(&infcx);

compiler/rustc_hir_typeck/src/typeck_root_ctxt.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_hir as hir;
55
use rustc_hir::def_id::LocalDefId;
66
use rustc_hir::HirIdMap;
77
use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
8-
use rustc_middle::traits::DefiningAnchor;
98
use rustc_middle::ty::visit::TypeVisitableExt;
109
use rustc_middle::ty::{self, Ty, TyCtxt};
1110
use rustc_span::def_id::LocalDefIdMap;
@@ -78,11 +77,7 @@ impl<'tcx> TypeckRootCtxt<'tcx> {
7877
pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
7978
let hir_owner = tcx.local_def_id_to_hir_id(def_id).owner;
8079

81-
let infcx = tcx
82-
.infer_ctxt()
83-
.ignoring_regions()
84-
.with_opaque_type_inference(DefiningAnchor::bind(tcx, def_id))
85-
.build();
80+
let infcx = tcx.infer_ctxt().ignoring_regions().with_opaque_type_inference(def_id).build();
8681
let typeck_results = RefCell::new(ty::TypeckResults::new(hir_owner));
8782

8883
TypeckRootCtxt {

compiler/rustc_infer/src/infer/at.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'tcx> InferCtxt<'tcx> {
7575
pub fn fork_with_intercrate(&self, intercrate: bool) -> Self {
7676
Self {
7777
tcx: self.tcx,
78-
defining_use_anchor: self.defining_use_anchor,
78+
defining_opaque_types: self.defining_opaque_types,
7979
considering_regions: self.considering_regions,
8080
skip_leak_check: self.skip_leak_check,
8181
inner: self.inner.clone(),

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<'tcx> InferCtxt<'tcx> {
4545
let param_env = self.tcx.canonical_param_env_cache.get_or_insert(
4646
self.tcx,
4747
param_env,
48-
self.defining_use_anchor,
48+
self.defining_opaque_types,
4949
query_state,
5050
|tcx, param_env, query_state| {
5151
// FIXME(#118965): We don't canonicalize the static lifetimes that appear in the
@@ -541,7 +541,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
541541
max_universe: ty::UniverseIndex::ROOT,
542542
variables: List::empty(),
543543
value: (),
544-
defining_anchor: infcx.map(|i| i.defining_use_anchor).unwrap_or_default(),
544+
defining_opaque_types: infcx.map(|i| i.defining_opaque_types).unwrap_or_default(),
545545
};
546546
Canonicalizer::canonicalize_with_base(
547547
base,
@@ -615,7 +615,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
615615
max_universe,
616616
variables: canonical_variables,
617617
value: (base.value, out_value),
618-
defining_anchor: base.defining_anchor,
618+
defining_opaque_types: base.defining_opaque_types,
619619
}
620620
}
621621

compiler/rustc_infer/src/infer/mod.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKin
3434
use rustc_middle::infer::unify_key::{ConstVidKey, EffectVidKey};
3535
use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult};
3636
use rustc_middle::mir::ConstraintCategory;
37-
use rustc_middle::traits::{select, DefiningAnchor};
37+
use rustc_middle::traits::select;
3838
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3939
use rustc_middle::ty::fold::BoundVarReplacerDelegate;
4040
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
@@ -244,11 +244,7 @@ pub struct InferCtxt<'tcx> {
244244
pub tcx: TyCtxt<'tcx>,
245245

246246
/// The `DefIds` of the opaque types that may have their hidden types constrained.
247-
///
248-
/// Its default value is `DefiningAnchor::Bind(&[])`, which means no opaque types may be defined.
249-
/// This way it is easier to catch errors that
250-
/// might come up during inference or typeck.
251-
pub defining_use_anchor: DefiningAnchor<'tcx>,
247+
pub defining_opaque_types: &'tcx ty::List<LocalDefId>,
252248

253249
/// Whether this inference context should care about region obligations in
254250
/// the root universe. Most notably, this is used during hir typeck as region
@@ -396,8 +392,8 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
396392
self.probe_const_var(vid).ok()
397393
}
398394

399-
fn defining_anchor(&self) -> DefiningAnchor<'tcx> {
400-
self.defining_use_anchor
395+
fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> {
396+
self.defining_opaque_types
401397
}
402398
}
403399

@@ -613,7 +609,7 @@ impl fmt::Display for FixupError {
613609
/// Used to configure inference contexts before their creation.
614610
pub struct InferCtxtBuilder<'tcx> {
615611
tcx: TyCtxt<'tcx>,
616-
defining_use_anchor: DefiningAnchor<'tcx>,
612+
defining_opaque_types: &'tcx ty::List<LocalDefId>,
617613
considering_regions: bool,
618614
skip_leak_check: bool,
619615
/// Whether we are in coherence mode.
@@ -628,7 +624,7 @@ impl<'tcx> TyCtxt<'tcx> {
628624
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
629625
InferCtxtBuilder {
630626
tcx: self,
631-
defining_use_anchor: DefiningAnchor::Bind(ty::List::empty()),
627+
defining_opaque_types: ty::List::empty(),
632628
considering_regions: true,
633629
skip_leak_check: false,
634630
intercrate: false,
@@ -644,8 +640,16 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
644640
/// It is only meant to be called in two places, for typeck
645641
/// (via `Inherited::build`) and for the inference context used
646642
/// in mir borrowck.
647-
pub fn with_opaque_type_inference(mut self, defining_use_anchor: DefiningAnchor<'tcx>) -> Self {
648-
self.defining_use_anchor = defining_use_anchor;
643+
pub fn with_opaque_type_inference(mut self, defining_anchor: LocalDefId) -> Self {
644+
self.defining_opaque_types = self.tcx.opaque_types_defined_by(defining_anchor);
645+
self
646+
}
647+
648+
pub fn with_defining_opaque_types(
649+
mut self,
650+
defining_opaque_types: &'tcx ty::List<LocalDefId>,
651+
) -> Self {
652+
self.defining_opaque_types = defining_opaque_types;
649653
self
650654
}
651655

@@ -684,23 +688,23 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
684688
where
685689
T: TypeFoldable<TyCtxt<'tcx>>,
686690
{
687-
let infcx = self.with_opaque_type_inference(canonical.defining_anchor).build();
691+
let infcx = self.with_defining_opaque_types(canonical.defining_opaque_types).build();
688692
let (value, args) = infcx.instantiate_canonical(span, canonical);
689693
(infcx, value, args)
690694
}
691695

692696
pub fn build(&mut self) -> InferCtxt<'tcx> {
693697
let InferCtxtBuilder {
694698
tcx,
695-
defining_use_anchor,
699+
defining_opaque_types,
696700
considering_regions,
697701
skip_leak_check,
698702
intercrate,
699703
next_trait_solver,
700704
} = *self;
701705
InferCtxt {
702706
tcx,
703-
defining_use_anchor,
707+
defining_opaque_types,
704708
considering_regions,
705709
skip_leak_check,
706710
inner: RefCell::new(InferCtxtInner::new()),

compiler/rustc_infer/src/infer/opaque_types/mod.rs

+39-46
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use hir::OpaqueTyOrigin;
88
use rustc_data_structures::fx::FxIndexMap;
99
use rustc_data_structures::sync::Lrc;
1010
use rustc_hir as hir;
11-
use rustc_middle::traits::{DefiningAnchor, ObligationCause};
11+
use rustc_middle::traits::ObligationCause;
1212
use rustc_middle::ty::error::{ExpectedFound, TypeError};
1313
use rustc_middle::ty::fold::BottomUpFolder;
1414
use rustc_middle::ty::GenericArgKind;
@@ -106,47 +106,44 @@ impl<'tcx> InferCtxt<'tcx> {
106106
b,
107107
));
108108
}
109-
match self.defining_use_anchor {
110-
DefiningAnchor::Bind(_) => {
111-
// Check that this is `impl Trait` type is
112-
// declared by `parent_def_id` -- i.e., one whose
113-
// value we are inferring. At present, this is
114-
// always true during the first phase of
115-
// type-check, but not always true later on during
116-
// NLL. Once we support named opaque types more fully,
117-
// this same scenario will be able to arise during all phases.
118-
//
119-
// Here is an example using type alias `impl Trait`
120-
// that indicates the distinction we are checking for:
121-
//
122-
// ```rust
123-
// mod a {
124-
// pub type Foo = impl Iterator;
125-
// pub fn make_foo() -> Foo { .. }
126-
// }
127-
//
128-
// mod b {
129-
// fn foo() -> a::Foo { a::make_foo() }
130-
// }
131-
// ```
132-
//
133-
// Here, the return type of `foo` references an
134-
// `Opaque` indeed, but not one whose value is
135-
// presently being inferred. You can get into a
136-
// similar situation with closure return types
137-
// today:
138-
//
139-
// ```rust
140-
// fn foo() -> impl Iterator { .. }
141-
// fn bar() {
142-
// let x = || foo(); // returns the Opaque assoc with `foo`
143-
// }
144-
// ```
145-
if self.opaque_type_origin(def_id).is_none() {
146-
return None;
147-
}
148-
}
109+
// Check that this is `impl Trait` type is
110+
// declared by `parent_def_id` -- i.e., one whose
111+
// value we are inferring. At present, this is
112+
// always true during the first phase of
113+
// type-check, but not always true later on during
114+
// NLL. Once we support named opaque types more fully,
115+
// this same scenario will be able to arise during all phases.
116+
//
117+
// Here is an example using type alias `impl Trait`
118+
// that indicates the distinction we are checking for:
119+
//
120+
// ```rust
121+
// mod a {
122+
// pub type Foo = impl Iterator;
123+
// pub fn make_foo() -> Foo { .. }
124+
// }
125+
//
126+
// mod b {
127+
// fn foo() -> a::Foo { a::make_foo() }
128+
// }
129+
// ```
130+
//
131+
// Here, the return type of `foo` references an
132+
// `Opaque` indeed, but not one whose value is
133+
// presently being inferred. You can get into a
134+
// similar situation with closure return types
135+
// today:
136+
//
137+
// ```rust
138+
// fn foo() -> impl Iterator { .. }
139+
// fn bar() {
140+
// let x = || foo(); // returns the Opaque assoc with `foo`
141+
// }
142+
// ```
143+
if self.opaque_type_origin(def_id).is_none() {
144+
return None;
149145
}
146+
150147
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }) = *b.kind() {
151148
// We could accept this, but there are various ways to handle this situation, and we don't
152149
// want to make a decision on it right now. Likely this case is so super rare anyway, that
@@ -371,13 +368,9 @@ impl<'tcx> InferCtxt<'tcx> {
371368
/// in its defining scope.
372369
#[instrument(skip(self), level = "trace", ret)]
373370
pub fn opaque_type_origin(&self, def_id: LocalDefId) -> Option<OpaqueTyOrigin> {
374-
let defined_opaque_types = match self.defining_use_anchor {
375-
DefiningAnchor::Bind(bind) => bind,
376-
};
377-
378371
let origin = self.tcx.opaque_type_origin(def_id);
379372

380-
defined_opaque_types.contains(&def_id).then_some(origin)
373+
self.defining_opaque_types.contains(&def_id).then_some(origin)
381374
}
382375
}
383376

compiler/rustc_middle/src/infer/canonical.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
2424
use rustc_data_structures::fx::FxHashMap;
2525
use rustc_data_structures::sync::Lock;
26+
use rustc_hir::def_id::LocalDefId;
2627
use rustc_macros::HashStable;
2728
use rustc_type_ir::Canonical as IrCanonical;
2829
use rustc_type_ir::CanonicalVarInfo as IrCanonicalVarInfo;
@@ -33,7 +34,6 @@ use std::ops::Index;
3334

3435
use crate::infer::MemberConstraint;
3536
use crate::mir::ConstraintCategory;
36-
use crate::traits::DefiningAnchor;
3737
use crate::ty::GenericArg;
3838
use crate::ty::{self, BoundVar, List, Region, Ty, TyCtxt, TypeFlags, TypeVisitableExt};
3939

@@ -312,7 +312,7 @@ impl<'tcx> CanonicalParamEnvCache<'tcx> {
312312
&self,
313313
tcx: TyCtxt<'tcx>,
314314
key: ty::ParamEnv<'tcx>,
315-
defining_anchor: DefiningAnchor<'tcx>,
315+
defining_opaque_types: &'tcx ty::List<LocalDefId>,
316316
state: &mut OriginalQueryValues<'tcx>,
317317
canonicalize_op: fn(
318318
TyCtxt<'tcx>,
@@ -327,7 +327,7 @@ impl<'tcx> CanonicalParamEnvCache<'tcx> {
327327
max_universe: ty::UniverseIndex::ROOT,
328328
variables: List::empty(),
329329
value: key,
330-
defining_anchor,
330+
defining_opaque_types,
331331
};
332332
}
333333

@@ -343,7 +343,7 @@ impl<'tcx> CanonicalParamEnvCache<'tcx> {
343343
}
344344
Entry::Vacant(e) => {
345345
let mut canonical = canonicalize_op(tcx, key, state);
346-
canonical.defining_anchor = defining_anchor;
346+
canonical.defining_opaque_types = defining_opaque_types;
347347
let OriginalQueryValues { var_values, universe_map } = state;
348348
assert_eq!(universe_map.len(), 1);
349349
e.insert((canonical, tcx.arena.alloc_slice(var_values)));

0 commit comments

Comments
 (0)