Skip to content

Commit eb9da7b

Browse files
committed
Auto merge of rust-lang#111473 - compiler-errors:opaques, r=lcnr
Handle opaques in the new solver (take 2?) Implement a new strategy for handling opaques in the new solver. First, queries now carry both their defining anchor and the opaques that were defined in the inference context at the time of canonicalization. These are both used to pre-populate the inference context used by the canonical query. Second, use the normalizes-to goal to handle opaque types in the new solver. This means that opaques are handled like projection aliases, but with their own rules: * Can only define opaques if they're "defining uses" (i.e. have unique params in all their substs). * Can only define opaques that are from the anchor. * Opaque type definitions are modulo regions. So that means `Opaque<'?0r> = HiddenTy1` and `Opaque<?'1r> = HiddenTy2` equate `HiddenTy1` and `HiddenTy2` instead of defining them as different opaque type keys.
2 parents 0b011b7 + dd98198 commit eb9da7b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+658
-189
lines changed

compiler/rustc_borrowck/src/consumers.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
55
use rustc_hir::def_id::LocalDefId;
66
use rustc_index::{IndexSlice, IndexVec};
7-
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
7+
use rustc_infer::infer::TyCtxtInferExt;
88
use rustc_middle::mir::{Body, Promoted};
9+
use rustc_middle::traits::DefiningAnchor;
910
use rustc_middle::ty::TyCtxt;
1011
use std::rc::Rc;
1112

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_hir::def_id::LocalDefId;
2626
use rustc_index::bit_set::ChunkedBitSet;
2727
use rustc_index::{IndexSlice, IndexVec};
2828
use rustc_infer::infer::{
29-
DefiningAnchor, InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt,
29+
InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt,
3030
};
3131
use rustc_middle::mir::{
3232
traversal, Body, ClearCrossCrate, Local, Location, Mutability, NonDivergingIntrinsic, Operand,
@@ -36,6 +36,7 @@ use rustc_middle::mir::{AggregateKind, BasicBlock, BorrowCheckResult, BorrowKind
3636
use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
3737
use rustc_middle::mir::{ProjectionElem, Promoted, Rvalue, Statement, StatementKind};
3838
use rustc_middle::query::Providers;
39+
use rustc_middle::traits::DefiningAnchor;
3940
use rustc_middle::ty::{self, CapturedPlace, ParamEnv, RegionVid, TyCtxt};
4041
use rustc_session::lint::builtin::UNUSED_MUT;
4142
use rustc_span::{Span, Symbol};

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
22
use rustc_errors::ErrorGuaranteed;
33
use rustc_hir::def_id::LocalDefId;
44
use rustc_hir::OpaqueTyOrigin;
5+
use rustc_infer::infer::InferCtxt;
56
use rustc_infer::infer::TyCtxtInferExt as _;
6-
use rustc_infer::infer::{DefiningAnchor, InferCtxt};
77
use rustc_infer::traits::{Obligation, ObligationCause};
8+
use rustc_middle::traits::DefiningAnchor;
89
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
910
use rustc_middle::ty::visit::TypeVisitableExt;
1011
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable};

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
4848
use rustc_mir_dataflow::move_paths::MoveData;
4949
use rustc_mir_dataflow::ResultsCursor;
5050

51+
use crate::renumber::RegionCtxt;
5152
use crate::session_diagnostics::MoveUnsized;
5253
use crate::{
5354
borrow_set::BorrowSet,
@@ -183,6 +184,15 @@ pub(crate) fn type_check<'mir, 'tcx>(
183184
&mut borrowck_context,
184185
);
185186

187+
// FIXME(-Ztrait-solver=next): A bit dubious that we're only registering
188+
// predefined opaques in the typeck root.
189+
// FIXME(-Ztrait-solver=next): This is also totally wrong for TAITs, since
190+
// the HIR typeck map defining usages back to their definition params,
191+
// they won't actually match up with the usages in this body...
192+
if infcx.tcx.trait_solver_next() && !infcx.tcx.is_typeck_child(body.source.def_id()) {
193+
checker.register_predefined_opaques_in_new_solver();
194+
}
195+
186196
let mut verifier = TypeVerifier::new(&mut checker, promoted);
187197
verifier.visit_body(&body);
188198

@@ -1023,6 +1033,57 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10231033
checker
10241034
}
10251035

1036+
pub(super) fn register_predefined_opaques_in_new_solver(&mut self) {
1037+
// OK to use the identity substitutions for each opaque type key, since
1038+
// we remap opaques from HIR typeck back to their definition params.
1039+
let opaques: Vec<_> = self
1040+
.infcx
1041+
.tcx
1042+
.typeck(self.body.source.def_id().expect_local())
1043+
.concrete_opaque_types
1044+
.iter()
1045+
.map(|(&def_id, &hidden_ty)| {
1046+
let substs = ty::InternalSubsts::identity_for_item(self.infcx.tcx, def_id);
1047+
(ty::OpaqueTypeKey { def_id, substs }, hidden_ty)
1048+
})
1049+
.collect();
1050+
1051+
let renumbered_opaques = self.infcx.tcx.fold_regions(opaques, |_, _| {
1052+
self.infcx.next_nll_region_var(
1053+
NllRegionVariableOrigin::Existential { from_forall: false },
1054+
|| RegionCtxt::Unknown,
1055+
)
1056+
});
1057+
1058+
let param_env = self.param_env;
1059+
let result = self.fully_perform_op(
1060+
Locations::All(self.body.span),
1061+
ConstraintCategory::OpaqueType,
1062+
CustomTypeOp::new(
1063+
|ocx| {
1064+
for (key, hidden_ty) in renumbered_opaques {
1065+
ocx.register_infer_ok_obligations(
1066+
ocx.infcx.register_hidden_type_in_new_solver(
1067+
key,
1068+
param_env,
1069+
hidden_ty.ty,
1070+
)?,
1071+
);
1072+
}
1073+
Ok(())
1074+
},
1075+
"register pre-defined opaques",
1076+
),
1077+
);
1078+
1079+
if result.is_err() {
1080+
self.infcx.tcx.sess.delay_span_bug(
1081+
self.body.span,
1082+
"failed re-defining predefined opaques in mir typeck",
1083+
);
1084+
}
1085+
}
1086+
10261087
fn body(&self) -> &Body<'tcx> {
10271088
self.body
10281089
}

compiler/rustc_const_eval/src/util/compare_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
//! FIXME: Move this to a more general place. The utility of this extends to
44
//! other areas of the compiler as well.
55
6-
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
7-
use rustc_infer::traits::ObligationCause;
6+
use rustc_infer::infer::TyCtxtInferExt;
7+
use rustc_middle::traits::{DefiningAnchor, ObligationCause};
88
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
99
use rustc_trait_selection::traits::ObligationCtxt;
1010

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ use rustc_hir::intravisit::Visitor;
1313
use rustc_hir::{ItemKind, Node, PathSegment};
1414
use rustc_infer::infer::opaque_types::ConstrainOpaqueTypeRegionVisitor;
1515
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
16-
use rustc_infer::infer::{DefiningAnchor, RegionVariableOrigin, TyCtxtInferExt};
16+
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
1717
use rustc_infer::traits::{Obligation, TraitEngineExt as _};
1818
use rustc_lint_defs::builtin::REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS;
1919
use rustc_middle::hir::nested_filter;
2020
use rustc_middle::middle::stability::EvalResult;
21+
use rustc_middle::traits::DefiningAnchor;
2122
use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
2223
use rustc_middle::ty::subst::GenericArgKind;
2324
use rustc_middle::ty::util::{Discr, IntTypeExt};

compiler/rustc_hir_typeck/src/inherited.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_hir as hir;
55
use rustc_hir::def_id::LocalDefId;
66
use rustc_hir::HirIdMap;
7-
use rustc_infer::infer::{DefiningAnchor, InferCtxt, InferOk, TyCtxtInferExt};
7+
use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
8+
use rustc_middle::traits::DefiningAnchor;
89
use rustc_middle::ty::visit::TypeVisitableExt;
910
use rustc_middle::ty::{self, Ty, TyCtxt};
1011
use rustc_span::def_id::LocalDefIdMap;

compiler/rustc_infer/src/infer/combine.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,7 @@ impl<'tcx> InferCtxt<'tcx> {
113113
bug!()
114114
}
115115

116-
(_, ty::Alias(AliasKind::Projection | AliasKind::Inherent, _))
117-
| (ty::Alias(AliasKind::Projection | AliasKind::Inherent, _), _)
118-
if self.tcx.trait_solver_next() =>
119-
{
116+
(_, ty::Alias(..)) | (ty::Alias(..), _) if self.tcx.trait_solver_next() => {
120117
relation.register_type_relate_obligation(a, b);
121118
Ok(a)
122119
}

compiler/rustc_infer/src/infer/equate.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
104104
(&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
105105
| (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
106106
if self.fields.define_opaque_types == DefineOpaqueTypes::Yes
107-
&& def_id.is_local() =>
107+
&& def_id.is_local()
108+
&& !self.tcx().trait_solver_next() =>
108109
{
109110
self.fields.obligations.extend(
110111
infcx

compiler/rustc_infer/src/infer/lattice.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,12 @@ where
108108
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
109109
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
110110
) if a_def_id == b_def_id => infcx.super_combine_tys(this, a, b),
111+
111112
(&ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }), _)
112113
| (_, &ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }))
113-
if this.define_opaque_types() == DefineOpaqueTypes::Yes && def_id.is_local() =>
114+
if this.define_opaque_types() == DefineOpaqueTypes::Yes
115+
&& def_id.is_local()
116+
&& !this.tcx().trait_solver_next() =>
114117
{
115118
this.register_obligations(
116119
infcx

0 commit comments

Comments
 (0)