Skip to content

Commit be8e5ba

Browse files
committed
Auto merge of rust-lang#109917 - compiler-errors:remove-relation-methods, r=oli-obk
Remove `intercrate` and `mark_ambiguous` from `TypeRelation` Fixes rust-lang#109863 Pulls this logic into `super_combine_tys`, which has access to `InferCtxt` and takes a `ObligationEmittingRelation` -- both of which simplify the logic here. r? `@oli-obk` `@aliemjay`
2 parents 7c96e40 + a368316 commit be8e5ba

File tree

12 files changed

+22
-133
lines changed

12 files changed

+22
-133
lines changed

compiler/rustc_hir_analysis/src/check/dropck.rs

-8
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,6 @@ impl<'tcx> TypeRelation<'tcx> for SimpleEqRelation<'tcx> {
253253
self.tcx
254254
}
255255

256-
fn intercrate(&self) -> bool {
257-
false
258-
}
259-
260256
fn param_env(&self) -> ty::ParamEnv<'tcx> {
261257
self.param_env
262258
}
@@ -269,10 +265,6 @@ impl<'tcx> TypeRelation<'tcx> for SimpleEqRelation<'tcx> {
269265
true
270266
}
271267

272-
fn mark_ambiguous(&mut self) {
273-
bug!()
274-
}
275-
276268
fn relate_with_variance<T: Relate<'tcx>>(
277269
&mut self,
278270
_: ty::Variance,

compiler/rustc_infer/src/infer/combine.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ impl<'tcx> InferCtxt<'tcx> {
137137
Err(TypeError::Sorts(ty::relate::expected_found(relation, a, b)))
138138
}
139139

140+
// During coherence, opaque types should be treated as *possibly*
141+
// equal to each other, even if their generic params differ, as
142+
// they could resolve to the same hidden type, even for different
143+
// generic params.
144+
(
145+
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, .. }),
146+
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }),
147+
) if self.intercrate && a_def_id == b_def_id => {
148+
relation.register_predicates([ty::Binder::dummy(ty::PredicateKind::Ambiguous)]);
149+
Ok(a)
150+
}
151+
140152
_ => ty::relate::super_relate_tys(relation, a, b),
141153
}
142154
}
@@ -505,10 +517,6 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
505517
Obligation::new(self.infcx.tcx, self.trace.cause.clone(), self.param_env, to_pred)
506518
}))
507519
}
508-
509-
pub fn mark_ambiguous(&mut self) {
510-
self.register_predicates([ty::Binder::dummy(ty::PredicateKind::Ambiguous)]);
511-
}
512520
}
513521

514522
struct Generalizer<'cx, 'tcx> {
@@ -581,10 +589,6 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
581589
self.infcx.tcx
582590
}
583591

584-
fn intercrate(&self) -> bool {
585-
self.infcx.intercrate
586-
}
587-
588592
fn param_env(&self) -> ty::ParamEnv<'tcx> {
589593
self.param_env
590594
}
@@ -597,10 +601,6 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
597601
true
598602
}
599603

600-
fn mark_ambiguous(&mut self) {
601-
span_bug!(self.cause.span, "opaque types are handled in `tys`");
602-
}
603-
604604
fn binders<T>(
605605
&mut self,
606606
a: ty::Binder<'tcx, T>,

compiler/rustc_infer/src/infer/equate.rs

-8
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
3535
self.fields.tcx()
3636
}
3737

38-
fn intercrate(&self) -> bool {
39-
self.fields.infcx.intercrate
40-
}
41-
4238
fn param_env(&self) -> ty::ParamEnv<'tcx> {
4339
self.fields.param_env
4440
}
@@ -47,10 +43,6 @@ impl<'tcx> TypeRelation<'tcx> for Equate<'_, '_, 'tcx> {
4743
self.a_is_expected
4844
}
4945

50-
fn mark_ambiguous(&mut self) {
51-
self.fields.mark_ambiguous();
52-
}
53-
5446
fn relate_item_substs(
5547
&mut self,
5648
_item_def_id: DefId,

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

-9
Original file line numberDiff line numberDiff line change
@@ -2697,11 +2697,6 @@ impl<'tcx> TypeRelation<'tcx> for SameTypeModuloInfer<'_, 'tcx> {
26972697
self.0.tcx
26982698
}
26992699

2700-
fn intercrate(&self) -> bool {
2701-
assert!(!self.0.intercrate);
2702-
false
2703-
}
2704-
27052700
fn param_env(&self) -> ty::ParamEnv<'tcx> {
27062701
// Unused, only for consts which we treat as always equal
27072702
ty::ParamEnv::empty()
@@ -2715,10 +2710,6 @@ impl<'tcx> TypeRelation<'tcx> for SameTypeModuloInfer<'_, 'tcx> {
27152710
true
27162711
}
27172712

2718-
fn mark_ambiguous(&mut self) {
2719-
bug!()
2720-
}
2721-
27222713
fn relate_with_variance<T: relate::Relate<'tcx>>(
27232714
&mut self,
27242715
_variance: ty::Variance,

compiler/rustc_infer/src/infer/glb.rs

-9
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
2929
"Glb"
3030
}
3131

32-
fn intercrate(&self) -> bool {
33-
assert!(!self.fields.infcx.intercrate);
34-
false
35-
}
36-
3732
fn tcx(&self) -> TyCtxt<'tcx> {
3833
self.fields.tcx()
3934
}
@@ -46,10 +41,6 @@ impl<'tcx> TypeRelation<'tcx> for Glb<'_, '_, 'tcx> {
4641
self.a_is_expected
4742
}
4843

49-
fn mark_ambiguous(&mut self) {
50-
bug!("mark_ambiguous used outside of coherence");
51-
}
52-
5344
fn relate_with_variance<T: Relate<'tcx>>(
5445
&mut self,
5546
variance: ty::Variance,

compiler/rustc_infer/src/infer/lub.rs

-9
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
2929
"Lub"
3030
}
3131

32-
fn intercrate(&self) -> bool {
33-
assert!(!self.fields.infcx.intercrate);
34-
false
35-
}
36-
3732
fn tcx(&self) -> TyCtxt<'tcx> {
3833
self.fields.tcx()
3934
}
@@ -46,10 +41,6 @@ impl<'tcx> TypeRelation<'tcx> for Lub<'_, '_, 'tcx> {
4641
self.a_is_expected
4742
}
4843

49-
fn mark_ambiguous(&mut self) {
50-
bug!("mark_ambiguous used outside of coherence");
51-
}
52-
5344
fn relate_with_variance<T: Relate<'tcx>>(
5445
&mut self,
5546
variance: ty::Variance,

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

-24
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,6 @@ where
443443
self.infcx.tcx
444444
}
445445

446-
fn intercrate(&self) -> bool {
447-
self.infcx.intercrate
448-
}
449-
450446
fn param_env(&self) -> ty::ParamEnv<'tcx> {
451447
self.delegate.param_env()
452448
}
@@ -459,17 +455,6 @@ where
459455
true
460456
}
461457

462-
fn mark_ambiguous(&mut self) {
463-
let cause = ObligationCause::dummy_with_span(self.delegate.span());
464-
let param_env = self.delegate.param_env();
465-
self.delegate.register_obligations(vec![Obligation::new(
466-
self.tcx(),
467-
cause,
468-
param_env,
469-
ty::Binder::dummy(ty::PredicateKind::Ambiguous),
470-
)]);
471-
}
472-
473458
#[instrument(skip(self, info), level = "trace", ret)]
474459
fn relate_with_variance<T: Relate<'tcx>>(
475460
&mut self,
@@ -834,11 +819,6 @@ where
834819
self.infcx.tcx
835820
}
836821

837-
fn intercrate(&self) -> bool {
838-
assert!(!self.infcx.intercrate);
839-
false
840-
}
841-
842822
fn param_env(&self) -> ty::ParamEnv<'tcx> {
843823
self.delegate.param_env()
844824
}
@@ -851,10 +831,6 @@ where
851831
true
852832
}
853833

854-
fn mark_ambiguous(&mut self) {
855-
bug!()
856-
}
857-
858834
fn relate_with_variance<T: Relate<'tcx>>(
859835
&mut self,
860836
variance: ty::Variance,

compiler/rustc_infer/src/infer/outlives/test_type_match.rs

-8
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@ impl<'tcx> TypeRelation<'tcx> for Match<'tcx> {
137137
"Match"
138138
}
139139

140-
fn intercrate(&self) -> bool {
141-
false
142-
}
143-
144140
fn tcx(&self) -> TyCtxt<'tcx> {
145141
self.tcx
146142
}
@@ -151,10 +147,6 @@ impl<'tcx> TypeRelation<'tcx> for Match<'tcx> {
151147
true
152148
} // irrelevant
153149

154-
fn mark_ambiguous(&mut self) {
155-
bug!()
156-
}
157-
158150
#[instrument(level = "trace", skip(self))]
159151
fn relate_with_variance<T: Relate<'tcx>>(
160152
&mut self,

compiler/rustc_infer/src/infer/sub.rs

-8
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
3535
"Sub"
3636
}
3737

38-
fn intercrate(&self) -> bool {
39-
self.fields.infcx.intercrate
40-
}
41-
4238
fn tcx(&self) -> TyCtxt<'tcx> {
4339
self.fields.infcx.tcx
4440
}
@@ -51,10 +47,6 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
5147
self.a_is_expected
5248
}
5349

54-
fn mark_ambiguous(&mut self) {
55-
self.fields.mark_ambiguous()
56-
}
57-
5850
fn with_cause<F, R>(&mut self, cause: Cause, f: F) -> R
5951
where
6052
F: FnOnce(&mut Self) -> R,

compiler/rustc_middle/src/ty/_match.rs

-8
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,13 @@ impl<'tcx> TypeRelation<'tcx> for Match<'tcx> {
3737
self.tcx
3838
}
3939

40-
fn intercrate(&self) -> bool {
41-
false
42-
}
43-
4440
fn param_env(&self) -> ty::ParamEnv<'tcx> {
4541
self.param_env
4642
}
4743
fn a_is_expected(&self) -> bool {
4844
true
4945
} // irrelevant
5046

51-
fn mark_ambiguous(&mut self) {
52-
bug!()
53-
}
54-
5547
fn relate_with_variance<T: Relate<'tcx>>(
5648
&mut self,
5749
_: ty::Variance,

compiler/rustc_middle/src/ty/relate.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ pub enum Cause {
2222
pub trait TypeRelation<'tcx>: Sized {
2323
fn tcx(&self) -> TyCtxt<'tcx>;
2424

25-
fn intercrate(&self) -> bool;
26-
2725
fn param_env(&self) -> ty::ParamEnv<'tcx>;
2826

2927
/// Returns a static string we can use for printouts.
@@ -33,9 +31,6 @@ pub trait TypeRelation<'tcx>: Sized {
3331
/// relation. Just affects error messages.
3432
fn a_is_expected(&self) -> bool;
3533

36-
/// Used during coherence. If called, must emit an always-ambiguous obligation.
37-
fn mark_ambiguous(&mut self);
38-
3934
fn with_cause<F, R>(&mut self, _cause: Cause, f: F) -> R
4035
where
4136
F: FnOnce(&mut Self) -> R,
@@ -559,23 +554,16 @@ pub fn super_relate_tys<'tcx, R: TypeRelation<'tcx>>(
559554
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, substs: a_substs, .. }),
560555
&ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, substs: b_substs, .. }),
561556
) if a_def_id == b_def_id => {
562-
if relation.intercrate() {
563-
// During coherence, opaque types should be treated as equal to each other, even if their generic params
564-
// differ, as they could resolve to the same hidden type, even for different generic params.
565-
relation.mark_ambiguous();
566-
Ok(a)
567-
} else {
568-
let opt_variances = tcx.variances_of(a_def_id);
569-
let substs = relate_substs_with_variances(
570-
relation,
571-
a_def_id,
572-
opt_variances,
573-
a_substs,
574-
b_substs,
575-
false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle
576-
)?;
577-
Ok(tcx.mk_opaque(a_def_id, substs))
578-
}
557+
let opt_variances = tcx.variances_of(a_def_id);
558+
let substs = relate_substs_with_variances(
559+
relation,
560+
a_def_id,
561+
opt_variances,
562+
a_substs,
563+
b_substs,
564+
false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle
565+
)?;
566+
Ok(tcx.mk_opaque(a_def_id, substs))
579567
}
580568

581569
_ => Err(TypeError::Sorts(expected_found(relation, a, b))),

compiler/rustc_trait_selection/src/traits/error_reporting/method_chain.rs

-8
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> {
2121
self.infcx.tcx
2222
}
2323

24-
fn intercrate(&self) -> bool {
25-
false
26-
}
27-
2824
fn param_env(&self) -> ty::ParamEnv<'tcx> {
2925
self.param_env
3026
}
@@ -33,10 +29,6 @@ impl<'a, 'tcx> TypeRelation<'tcx> for CollectAllMismatches<'a, 'tcx> {
3329
true
3430
}
3531

36-
fn mark_ambiguous(&mut self) {
37-
bug!()
38-
}
39-
4032
fn relate_with_variance<T: Relate<'tcx>>(
4133
&mut self,
4234
_: ty::Variance,

0 commit comments

Comments
 (0)