Skip to content

Commit 25b82c5

Browse files
committed
Eliminate DefiningAnchor now that is just a single-variant enum
1 parent 7ccdc2c commit 25b82c5

File tree

20 files changed

+101
-132
lines changed

20 files changed

+101
-132
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
@@ -31,7 +31,6 @@ use rustc_infer::infer::{
3131
use rustc_middle::mir::tcx::PlaceTy;
3232
use rustc_middle::mir::*;
3333
use rustc_middle::query::Providers;
34-
use rustc_middle::traits::DefiningAnchor;
3534
use rustc_middle::ty::{self, ParamEnv, RegionVid, TyCtxt};
3635
use rustc_session::lint::builtin::UNUSED_MUT;
3736
use rustc_span::{Span, Symbol};
@@ -125,7 +124,7 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
125124
return tcx.arena.alloc(result);
126125
}
127126

128-
let infcx = tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::bind(tcx, def)).build();
127+
let infcx = tcx.infer_ctxt().with_opaque_type_inference(def).build();
129128
let promoted: &IndexSlice<_, _> = &promoted.borrow();
130129
let opt_closure_req = do_mir_borrowck(&infcx, input_body, promoted, None).0;
131130
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::InferCtxt;
77
use rustc_infer::infer::TyCtxtInferExt as _;
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};
@@ -322,13 +321,13 @@ fn check_opaque_type_well_formed<'tcx>(
322321
parent_def_id = tcx.local_parent(parent_def_id);
323322
}
324323

325-
// FIXME(-Znext-solver): We probably should use `DefiningAnchor::Bind(&[])`
324+
// FIXME(-Znext-solver): We probably should use `&[]` instead of
326325
// and prepopulate this `InferCtxt` with known opaque values, rather than
327-
// using the `Bind` anchor here. For now it's fine.
326+
// allowing opaque types to be defined and checking them after the fact.
328327
let infcx = tcx
329328
.infer_ctxt()
330329
.with_next_trait_solver(next_trait_solver)
331-
.with_opaque_type_inference(DefiningAnchor::bind(tcx, parent_def_id))
330+
.with_opaque_type_inference(parent_def_id)
332331
.build();
333332
let ocx = ObligationCtxt::new(&infcx);
334333
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};
@@ -344,10 +344,7 @@ fn check_opaque_meets_bounds<'tcx>(
344344
};
345345
let param_env = tcx.param_env(defining_use_anchor);
346346

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

353350
let args = match *origin {
@@ -1557,7 +1554,7 @@ pub(super) fn check_coroutine_obligations(
15571554
.ignoring_regions()
15581555
// Bind opaque types to type checking root, as they should have been checked by borrowck,
15591556
// but may show up in some cases, like when (root) obligations are stalled in the new solver.
1560-
.with_opaque_type_inference(DefiningAnchor::bind(tcx, typeck.hir_owner.def_id))
1557+
.with_opaque_type_inference(typeck.hir_owner.def_id)
15611558
.build();
15621559

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

compiler/rustc_hir_typeck/src/inherited.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;
@@ -76,11 +75,7 @@ impl<'tcx> Inherited<'tcx> {
7675
pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
7776
let hir_owner = tcx.local_def_id_to_hir_id(def_id).owner;
7877

79-
let infcx = tcx
80-
.infer_ctxt()
81-
.ignoring_regions()
82-
.with_opaque_type_inference(DefiningAnchor::bind(tcx, def_id))
83-
.build();
78+
let infcx = tcx.infer_ctxt().ignoring_regions().with_opaque_type_inference(def_id).build();
8479
let typeck_results = RefCell::new(ty::TypeckResults::new(hir_owner));
8580

8681
Inherited {

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};
@@ -235,11 +235,7 @@ pub struct InferCtxt<'tcx> {
235235
pub tcx: TyCtxt<'tcx>,
236236

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

244240
/// Whether this inference context should care about region obligations in
245241
/// the root universe. Most notably, this is used during hir typeck as region
@@ -387,8 +383,8 @@ impl<'tcx> ty::InferCtxtLike for InferCtxt<'tcx> {
387383
self.probe_const_var(vid).ok()
388384
}
389385

390-
fn defining_anchor(&self) -> DefiningAnchor<'tcx> {
391-
self.defining_use_anchor
386+
fn defining_opaque_types(&self) -> &'tcx ty::List<LocalDefId> {
387+
self.defining_opaque_types
392388
}
393389
}
394390

@@ -604,7 +600,7 @@ impl fmt::Display for FixupError {
604600
/// Used to configure inference contexts before their creation.
605601
pub struct InferCtxtBuilder<'tcx> {
606602
tcx: TyCtxt<'tcx>,
607-
defining_use_anchor: DefiningAnchor<'tcx>,
603+
defining_opaque_types: &'tcx ty::List<LocalDefId>,
608604
considering_regions: bool,
609605
skip_leak_check: bool,
610606
/// Whether we are in coherence mode.
@@ -619,7 +615,7 @@ impl<'tcx> TyCtxt<'tcx> {
619615
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
620616
InferCtxtBuilder {
621617
tcx: self,
622-
defining_use_anchor: DefiningAnchor::Bind(ty::List::empty()),
618+
defining_opaque_types: ty::List::empty(),
623619
considering_regions: true,
624620
skip_leak_check: false,
625621
intercrate: false,
@@ -635,8 +631,16 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
635631
/// It is only meant to be called in two places, for typeck
636632
/// (via `Inherited::build`) and for the inference context used
637633
/// in mir borrowck.
638-
pub fn with_opaque_type_inference(mut self, defining_use_anchor: DefiningAnchor<'tcx>) -> Self {
639-
self.defining_use_anchor = defining_use_anchor;
634+
pub fn with_opaque_type_inference(mut self, defining_anchor: LocalDefId) -> Self {
635+
self.defining_opaque_types = self.tcx.opaque_types_defined_by(defining_anchor);
636+
self
637+
}
638+
639+
pub fn with_defining_opaque_types(
640+
mut self,
641+
defining_opaque_types: &'tcx ty::List<LocalDefId>,
642+
) -> Self {
643+
self.defining_opaque_types = defining_opaque_types;
640644
self
641645
}
642646

@@ -675,23 +679,23 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
675679
where
676680
T: TypeFoldable<TyCtxt<'tcx>>,
677681
{
678-
let infcx = self.with_opaque_type_inference(canonical.defining_anchor).build();
682+
let infcx = self.with_defining_opaque_types(canonical.defining_opaque_types).build();
679683
let (value, args) = infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
680684
(infcx, value, args)
681685
}
682686

683687
pub fn build(&mut self) -> InferCtxt<'tcx> {
684688
let InferCtxtBuilder {
685689
tcx,
686-
defining_use_anchor,
690+
defining_opaque_types,
687691
considering_regions,
688692
skip_leak_check,
689693
intercrate,
690694
next_trait_solver,
691695
} = *self;
692696
InferCtxt {
693697
tcx,
694-
defining_use_anchor,
698+
defining_opaque_types,
695699
considering_regions,
696700
skip_leak_check,
697701
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;
@@ -109,47 +109,44 @@ impl<'tcx> InferCtxt<'tcx> {
109109
b,
110110
));
111111
}
112-
match self.defining_use_anchor {
113-
DefiningAnchor::Bind(_) => {
114-
// Check that this is `impl Trait` type is
115-
// declared by `parent_def_id` -- i.e., one whose
116-
// value we are inferring. At present, this is
117-
// always true during the first phase of
118-
// type-check, but not always true later on during
119-
// NLL. Once we support named opaque types more fully,
120-
// this same scenario will be able to arise during all phases.
121-
//
122-
// Here is an example using type alias `impl Trait`
123-
// that indicates the distinction we are checking for:
124-
//
125-
// ```rust
126-
// mod a {
127-
// pub type Foo = impl Iterator;
128-
// pub fn make_foo() -> Foo { .. }
129-
// }
130-
//
131-
// mod b {
132-
// fn foo() -> a::Foo { a::make_foo() }
133-
// }
134-
// ```
135-
//
136-
// Here, the return type of `foo` references an
137-
// `Opaque` indeed, but not one whose value is
138-
// presently being inferred. You can get into a
139-
// similar situation with closure return types
140-
// today:
141-
//
142-
// ```rust
143-
// fn foo() -> impl Iterator { .. }
144-
// fn bar() {
145-
// let x = || foo(); // returns the Opaque assoc with `foo`
146-
// }
147-
// ```
148-
if self.opaque_type_origin(def_id).is_none() {
149-
return None;
150-
}
151-
}
112+
// Check that this is `impl Trait` type is
113+
// declared by `parent_def_id` -- i.e., one whose
114+
// value we are inferring. At present, this is
115+
// always true during the first phase of
116+
// type-check, but not always true later on during
117+
// NLL. Once we support named opaque types more fully,
118+
// this same scenario will be able to arise during all phases.
119+
//
120+
// Here is an example using type alias `impl Trait`
121+
// that indicates the distinction we are checking for:
122+
//
123+
// ```rust
124+
// mod a {
125+
// pub type Foo = impl Iterator;
126+
// pub fn make_foo() -> Foo { .. }
127+
// }
128+
//
129+
// mod b {
130+
// fn foo() -> a::Foo { a::make_foo() }
131+
// }
132+
// ```
133+
//
134+
// Here, the return type of `foo` references an
135+
// `Opaque` indeed, but not one whose value is
136+
// presently being inferred. You can get into a
137+
// similar situation with closure return types
138+
// today:
139+
//
140+
// ```rust
141+
// fn foo() -> impl Iterator { .. }
142+
// fn bar() {
143+
// let x = || foo(); // returns the Opaque assoc with `foo`
144+
// }
145+
// ```
146+
if self.opaque_type_origin(def_id).is_none() {
147+
return None;
152148
}
149+
153150
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }) = *b.kind() {
154151
// We could accept this, but there are various ways to handle this situation, and we don't
155152
// want to make a decision on it right now. Likely this case is so super rare anyway, that
@@ -374,13 +371,9 @@ impl<'tcx> InferCtxt<'tcx> {
374371
/// in its defining scope.
375372
#[instrument(skip(self), level = "trace", ret)]
376373
pub fn opaque_type_origin(&self, def_id: LocalDefId) -> Option<OpaqueTyOrigin> {
377-
let defined_opaque_types = match self.defining_use_anchor {
378-
DefiningAnchor::Bind(bind) => bind,
379-
};
380-
381374
let origin = self.tcx.opaque_type_origin(def_id);
382375

383-
defined_opaque_types.contains(&def_id).then_some(origin)
376+
self.defining_opaque_types.contains(&def_id).then_some(origin)
384377
}
385378
}
386379

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)