Skip to content

Commit de4f805

Browse files
Add new typing mode
1 parent e964cca commit de4f805

File tree

21 files changed

+81
-26
lines changed

21 files changed

+81
-26
lines changed

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,7 @@ impl<'tcx> InferCtxt<'tcx> {
968968
// and post-borrowck analysis mode. We may need to modify its uses
969969
// to support PostBorrowckAnalysis in the old solver as well.
970970
TypingMode::Coherence
971+
| TypingMode::CheckObjectOverlap
971972
| TypingMode::PostBorrowckAnalysis { .. }
972973
| TypingMode::PostAnalysis => false,
973974
}
@@ -1260,6 +1261,7 @@ impl<'tcx> InferCtxt<'tcx> {
12601261
TypingMode::non_body_analysis()
12611262
}
12621263
mode @ (ty::TypingMode::Coherence
1264+
| TypingMode::CheckObjectOverlap
12631265
| ty::TypingMode::PostBorrowckAnalysis { .. }
12641266
| ty::TypingMode::PostAnalysis) => mode,
12651267
};

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ impl<'tcx> InferCtxt<'tcx> {
8787
let process = |a: Ty<'tcx>, b: Ty<'tcx>| match *a.kind() {
8888
ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) if def_id.is_local() => {
8989
let def_id = def_id.expect_local();
90-
if let ty::TypingMode::Coherence = self.typing_mode() {
90+
if let ty::TypingMode::Coherence | ty::TypingMode::CheckObjectOverlap =
91+
self.typing_mode()
92+
{
9193
// See comment on `insert_hidden_type` for why this is sufficient in coherence
9294
return Some(self.register_hidden_type(
9395
OpaqueTypeKey { def_id, args },
@@ -226,7 +228,7 @@ impl<'tcx> InferCtxt<'tcx> {
226228
// these are the same span, but not in cases like `-> (impl
227229
// Foo, impl Bar)`.
228230
match self.typing_mode() {
229-
ty::TypingMode::Coherence => {
231+
ty::TypingMode::Coherence | ty::TypingMode::CheckObjectOverlap => {
230232
// During intercrate we do not define opaque types but instead always
231233
// force ambiguity unless the hidden type is known to not implement
232234
// our trait.

compiler/rustc_infer/src/infer/relate/generalize.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,10 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
519519
//
520520
// cc trait-system-refactor-initiative#108
521521
if self.infcx.next_trait_solver()
522-
&& !matches!(self.infcx.typing_mode(), TypingMode::Coherence)
522+
&& !matches!(
523+
self.infcx.typing_mode(),
524+
TypingMode::Coherence | TypingMode::CheckObjectOverlap
525+
)
523526
&& self.in_alias
524527
{
525528
inner.type_variables().equate(vid, new_var_id);
@@ -650,7 +653,10 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for Generalizer<'_, 'tcx> {
650653
// See the comment for type inference variables
651654
// for more details.
652655
if self.infcx.next_trait_solver()
653-
&& !matches!(self.infcx.typing_mode(), TypingMode::Coherence)
656+
&& !matches!(
657+
self.infcx.typing_mode(),
658+
TypingMode::Coherence | TypingMode::CheckObjectOverlap
659+
)
654660
&& self.in_alias
655661
{
656662
variable_table.union(vid, new_var_id);

compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ where
380380

381381
let mut candidates = vec![];
382382

383-
if let TypingMode::Coherence = self.typing_mode() {
383+
if let TypingMode::Coherence | TypingMode::CheckObjectOverlap = self.typing_mode() {
384384
if let Ok(candidate) = self.consider_coherence_unknowable_candidate(goal) {
385385
return vec![candidate];
386386
}
@@ -831,7 +831,7 @@ where
831831
candidates: &mut Vec<Candidate<I>>,
832832
) {
833833
match self.typing_mode() {
834-
TypingMode::Coherence => return,
834+
TypingMode::Coherence | TypingMode::CheckObjectOverlap => return,
835835
TypingMode::Analysis { .. }
836836
| TypingMode::Borrowck { .. }
837837
| TypingMode::PostBorrowckAnalysis { .. }

compiler/rustc_next_trait_solver/src/solve/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ where
339339
fn opaque_type_is_rigid(&self, def_id: I::DefId) -> bool {
340340
match self.typing_mode() {
341341
// Opaques are never rigid outside of analysis mode.
342-
TypingMode::Coherence | TypingMode::PostAnalysis => false,
342+
TypingMode::Coherence | TypingMode::CheckObjectOverlap | TypingMode::PostAnalysis => {
343+
false
344+
}
343345
// During analysis, opaques are rigid unless they may be defined by
344346
// the current body.
345347
TypingMode::Analysis { defining_opaque_types_and_generators: non_rigid_opaques }

compiler/rustc_next_trait_solver/src/solve/normalizes_to/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ where
240240
// would be relevant if any of the nested goals refer to the `term`.
241241
// This is not the case here and we only prefer adding an ambiguous
242242
// nested goal for consistency.
243-
ty::TypingMode::Coherence => {
243+
ty::TypingMode::Coherence | ty::TypingMode::CheckObjectOverlap => {
244244
ecx.add_goal(GoalSource::Misc, goal.with(cx, PredicateKind::Ambiguous));
245245
return ecx
246246
.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);
@@ -280,7 +280,7 @@ where
280280
// would be relevant if any of the nested goals refer to the `term`.
281281
// This is not the case here and we only prefer adding an ambiguous
282282
// nested goal for consistency.
283-
ty::TypingMode::Coherence => {
283+
ty::TypingMode::Coherence | ty::TypingMode::CheckObjectOverlap => {
284284
ecx.add_goal(GoalSource::Misc, goal.with(cx, PredicateKind::Ambiguous));
285285
return ecx
286286
.evaluate_added_goals_and_make_canonical_response(Certainty::Yes);

compiler/rustc_next_trait_solver/src/solve/normalizes_to/opaque_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ where
2323
let expected = goal.predicate.term.as_type().expect("no such thing as an opaque const");
2424

2525
match self.typing_mode() {
26-
TypingMode::Coherence => {
26+
TypingMode::Coherence | TypingMode::CheckObjectOverlap => {
2727
// An impossible opaque type bound is the only way this goal will fail
2828
// e.g. assigning `impl Copy := NotCopy`
2929
self.add_item_bounds_for_hidden_type(

compiler/rustc_next_trait_solver/src/solve/search_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ where
5858
// example where this would matter. We likely should change these cycles to `NoSolution`
5959
// even in coherence once this is a bit more settled.
6060
PathKind::Inductive => match input.typing_mode {
61-
TypingMode::Coherence => {
61+
TypingMode::Coherence | TypingMode::CheckObjectOverlap => {
6262
response_no_constraints(cx, input, Certainty::overflow(false))
6363
}
6464
TypingMode::Analysis { .. }

compiler/rustc_next_trait_solver/src/solve/trait_goals.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ where
7070
// In intercrate mode, this is ambiguous. But outside of intercrate,
7171
// it's not a real impl.
7272
(ty::ImplPolarity::Reservation, _) => match ecx.typing_mode() {
73-
TypingMode::Coherence => Certainty::AMBIGUOUS,
73+
TypingMode::Coherence | TypingMode::CheckObjectOverlap => Certainty::AMBIGUOUS,
7474
TypingMode::Analysis { .. }
7575
| TypingMode::Borrowck { .. }
7676
| TypingMode::PostBorrowckAnalysis { .. }
@@ -1257,7 +1257,7 @@ where
12571257
&mut self,
12581258
mut candidates: Vec<Candidate<I>>,
12591259
) -> Result<(CanonicalResponse<I>, Option<TraitGoalProvenVia>), NoSolution> {
1260-
if let TypingMode::Coherence = self.typing_mode() {
1260+
if let TypingMode::Coherence | TypingMode::CheckObjectOverlap = self.typing_mode() {
12611261
let all_candidates: Vec<_> = candidates.into_iter().map(|c| c.result).collect();
12621262
return if let Some(response) = self.try_merge_responses(&all_candidates) {
12631263
Ok((response, Some(TraitGoalProvenVia::Misc)))
@@ -1360,6 +1360,7 @@ where
13601360
}
13611361
}
13621362
TypingMode::Coherence
1363+
| TypingMode::CheckObjectOverlap
13631364
| TypingMode::PostAnalysis
13641365
| TypingMode::Borrowck { defining_opaque_types: _ }
13651366
| TypingMode::PostBorrowckAnalysis { defined_opaque_types: _ } => {}

compiler/rustc_trait_selection/src/solve/delegate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ impl<'tcx> rustc_next_trait_solver::delegate::SolverDelegate for SolverDelegate<
178178
// get a result which isn't correct for all monomorphizations.
179179
match self.typing_mode() {
180180
TypingMode::Coherence
181+
| TypingMode::CheckObjectOverlap
181182
| TypingMode::Analysis { .. }
182183
| TypingMode::Borrowck { .. }
183184
| TypingMode::PostBorrowckAnalysis { .. } => false,

compiler/rustc_trait_selection/src/solve/fulfill.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ where
229229
defining_opaque_types_and_generators
230230
}
231231
TypingMode::Coherence
232+
| TypingMode::CheckObjectOverlap
232233
| TypingMode::Borrowck { defining_opaque_types: _ }
233234
| TypingMode::PostBorrowckAnalysis { defined_opaque_types: _ }
234235
| TypingMode::PostAnalysis => return Default::default(),

compiler/rustc_trait_selection/src/traits/effects.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn evaluate_host_effect_obligation<'tcx>(
2424
selcx: &mut SelectionContext<'_, 'tcx>,
2525
obligation: &HostEffectObligation<'tcx>,
2626
) -> Result<ThinVec<PredicateObligation<'tcx>>, EvaluationFailure> {
27-
if matches!(selcx.infcx.typing_mode(), TypingMode::Coherence) {
27+
if matches!(selcx.infcx.typing_mode(), TypingMode::Coherence | TypingMode::CheckObjectOverlap) {
2828
span_bug!(
2929
obligation.cause.span,
3030
"should not select host obligation in old solver in intercrate mode"

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,11 @@ impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> {
793793
stalled_on: &mut Vec<TyOrConstInferVar>,
794794
) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
795795
let infcx = self.selcx.infcx;
796-
if obligation.predicate.is_global() && !matches!(infcx.typing_mode(), TypingMode::Coherence)
796+
if obligation.predicate.is_global()
797+
&& !matches!(
798+
infcx.typing_mode(),
799+
TypingMode::Coherence | TypingMode::CheckObjectOverlap
800+
)
797801
{
798802
// no type variables present, can use evaluation for better caching.
799803
// FIXME: consider caching errors too.
@@ -848,7 +852,11 @@ impl<'a, 'tcx> FulfillProcessor<'a, 'tcx> {
848852
) -> ProcessResult<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>> {
849853
let tcx = self.selcx.tcx();
850854
let infcx = self.selcx.infcx;
851-
if obligation.predicate.is_global() && !matches!(infcx.typing_mode(), TypingMode::Coherence)
855+
if obligation.predicate.is_global()
856+
&& !matches!(
857+
infcx.typing_mode(),
858+
TypingMode::Coherence | TypingMode::CheckObjectOverlap
859+
)
852860
{
853861
// no type variables present, can use evaluation for better caching.
854862
// FIXME: consider caching errors too.

compiler/rustc_trait_selection/src/traits/normalize.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub(super) fn needs_normalization<'tcx, T: TypeVisitable<TyCtxt<'tcx>>>(
135135
match infcx.typing_mode() {
136136
// FIXME(#132279): We likely want to reveal opaques during post borrowck analysis
137137
TypingMode::Coherence
138+
| TypingMode::CheckObjectOverlap
138139
| TypingMode::Analysis { .. }
139140
| TypingMode::Borrowck { .. }
140141
| TypingMode::PostBorrowckAnalysis { .. } => flags.remove(ty::TypeFlags::HAS_TY_OPAQUE),
@@ -389,6 +390,7 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
389390
match self.selcx.infcx.typing_mode() {
390391
// FIXME(#132279): We likely want to reveal opaques during post borrowck analysis
391392
TypingMode::Coherence
393+
| TypingMode::CheckObjectOverlap
392394
| TypingMode::Analysis { .. }
393395
| TypingMode::Borrowck { .. }
394396
| TypingMode::PostBorrowckAnalysis { .. } => ty.super_fold_with(self),

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
938938
// get a result which isn't correct for all monomorphizations.
939939
match selcx.infcx.typing_mode() {
940940
TypingMode::Coherence
941+
| TypingMode::CheckObjectOverlap
941942
| TypingMode::Analysis { .. }
942943
| TypingMode::Borrowck { .. }
943944
| TypingMode::PostBorrowckAnalysis { .. } => {

compiler/rustc_trait_selection/src/traits/query/normalize.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ impl<'a, 'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for QueryNormalizer<'a, 'tcx> {
215215
// Only normalize `impl Trait` outside of type inference, usually in codegen.
216216
match self.infcx.typing_mode() {
217217
TypingMode::Coherence
218+
| TypingMode::CheckObjectOverlap
218219
| TypingMode::Analysis { .. }
219220
| TypingMode::Borrowck { .. }
220221
| TypingMode::PostBorrowckAnalysis { .. } => ty.try_super_fold_with(self)?,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
779779
//
780780
// Note that this is only sound as projection candidates of opaque types
781781
// are always applicable for auto traits.
782-
} else if let TypingMode::Coherence = self.infcx.typing_mode() {
782+
} else if let TypingMode::Coherence | TypingMode::CheckObjectOverlap =
783+
self.infcx.typing_mode()
784+
{
783785
// We do not emit auto trait candidates for opaque types in coherence.
784786
// Doing so can result in weird dependency cycles.
785787
candidates.ambiguous = true;

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,8 +1004,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
10041004
previous_stack: TraitObligationStackList<'o, 'tcx>,
10051005
mut obligation: PolyTraitObligation<'tcx>,
10061006
) -> Result<EvaluationResult, OverflowError> {
1007-
if !matches!(self.infcx.typing_mode(), TypingMode::Coherence)
1008-
&& obligation.is_global()
1007+
if !matches!(
1008+
self.infcx.typing_mode(),
1009+
TypingMode::Coherence | TypingMode::CheckObjectOverlap
1010+
) && obligation.is_global()
10091011
&& obligation.param_env.caller_bounds().iter().all(|bound| bound.has_param())
10101012
{
10111013
// If a param env has no global bounds, global obligations do not
@@ -1449,7 +1451,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
14491451
fn is_knowable<'o>(&mut self, stack: &TraitObligationStack<'o, 'tcx>) -> Result<(), Conflict> {
14501452
let obligation = &stack.obligation;
14511453
match self.infcx.typing_mode() {
1452-
TypingMode::Coherence => {}
1454+
TypingMode::Coherence | TypingMode::CheckObjectOverlap => {}
14531455
TypingMode::Analysis { .. }
14541456
| TypingMode::Borrowck { .. }
14551457
| TypingMode::PostBorrowckAnalysis { .. }
@@ -1488,7 +1490,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
14881490
// the master cache. Since coherence executes pretty quickly,
14891491
// it's not worth going to more trouble to increase the
14901492
// hit-rate, I don't think.
1491-
TypingMode::Coherence => false,
1493+
TypingMode::Coherence | TypingMode::CheckObjectOverlap => false,
14921494
// Avoid using the global cache when we're defining opaque types
14931495
// as their hidden type may impact the result of candidate selection.
14941496
//
@@ -2509,7 +2511,10 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
25092511
nested_obligations.extend(obligations);
25102512

25112513
if impl_trait_header.polarity == ty::ImplPolarity::Reservation
2512-
&& !matches!(self.infcx.typing_mode(), TypingMode::Coherence)
2514+
&& !matches!(
2515+
self.infcx.typing_mode(),
2516+
TypingMode::Coherence | TypingMode::CheckObjectOverlap
2517+
)
25132518
{
25142519
debug!("reservation impls only apply in intercrate mode");
25152520
return Err(());

compiler/rustc_ty_utils/src/instance.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ fn resolve_associated_item<'tcx>(
156156
// get a result which isn't correct for all monomorphizations.
157157
match typing_env.typing_mode {
158158
ty::TypingMode::Coherence
159+
| ty::TypingMode::CheckObjectOverlap
159160
| ty::TypingMode::Analysis { .. }
160161
| ty::TypingMode::Borrowck { .. }
161162
| ty::TypingMode::PostBorrowckAnalysis { .. } => false,

compiler/rustc_type_ir/src/infer_ctxt.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,27 @@ pub enum TypingMode<I: Interner> {
6767
/// let x: <() as Assoc>::Output = true;
6868
/// }
6969
/// ```
70-
Analysis { defining_opaque_types_and_generators: I::LocalDefIds },
70+
Analysis {
71+
defining_opaque_types_and_generators: I::LocalDefIds,
72+
},
7173
/// The behavior during MIR borrowck is identical to `TypingMode::Analysis`
7274
/// except that the initial value for opaque types is the type computed during
7375
/// HIR typeck with unique unconstrained region inference variables.
7476
///
7577
/// This is currently only used with by the new solver as it results in new
7678
/// non-universal defining uses of opaque types, which is a breaking change.
7779
/// See tests/ui/impl-trait/non-defining-use/as-projection-term.rs.
78-
Borrowck { defining_opaque_types: I::LocalDefIds },
80+
Borrowck {
81+
defining_opaque_types: I::LocalDefIds,
82+
},
7983
/// Any analysis after borrowck for a given body should be able to use all the
8084
/// hidden types defined by borrowck, without being able to define any new ones.
8185
///
8286
/// This is currently only used by the new solver, but should be implemented in
8387
/// the old solver as well.
84-
PostBorrowckAnalysis { defined_opaque_types: I::LocalDefIds },
88+
PostBorrowckAnalysis {
89+
defined_opaque_types: I::LocalDefIds,
90+
},
8591
/// After analysis, mostly during codegen and MIR optimizations, we're able to
8692
/// reveal all opaque types. As the concrete type should *never* be observable
8793
/// directly by the user, this should not be used by checks which may expose
@@ -91,6 +97,11 @@ pub enum TypingMode<I: Interner> {
9197
/// always run in `PostAnalysis` mode, even when used during analysis. This exposes
9298
/// some information about the underlying type to users, but not the type itself.
9399
PostAnalysis,
100+
101+
// Hack modes:
102+
103+
// TODUwU:
104+
CheckObjectOverlap,
94105
}
95106

96107
impl<I: Interner> TypingMode<I> {

compiler/rustc_type_ir/src/relate/combine.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ where
132132
// equal to any other type. This is an
133133
// extremely heavy hammer, but can be relaxed in a forwards-compatible
134134
// way later.
135-
TypingMode::Coherence => {
135+
TypingMode::Coherence | TypingMode::CheckObjectOverlap => {
136136
relation.register_predicates([ty::Binder::dummy(ty::PredicateKind::Ambiguous)]);
137137
Ok(a)
138138
}
@@ -143,6 +143,15 @@ where
143143
}
144144
}
145145

146+
(ty::Dynamic(a_data, _, a_kind), ty::Dynamic(b_data, _, b_kind))
147+
if a_data.principal_def_id() == b_data.principal_def_id()
148+
&& a_kind == b_kind
149+
&& matches!(infcx.typing_mode(), TypingMode::CheckObjectOverlap) =>
150+
{
151+
relation.register_predicates([ty::Binder::dummy(ty::PredicateKind::Ambiguous)]);
152+
Ok(a)
153+
}
154+
146155
_ => structurally_relate_tys(relation, a, b),
147156
}
148157
}

0 commit comments

Comments
 (0)