Skip to content

Commit edaf962

Browse files
committed
Clean up leftovers from eager hidden type merging
1 parent 38f50d1 commit edaf962

File tree

6 files changed

+34
-109
lines changed

6 files changed

+34
-109
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use rustc_index::vec::{Idx, IndexVec};
1818
use rustc_infer::infer::canonical::QueryRegionConstraints;
1919
use rustc_infer::infer::outlives::env::RegionBoundPairs;
2020
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
21-
use rustc_infer::infer::{InferCtxt, LateBoundRegionConversionTime, NllRegionVariableOrigin};
21+
use rustc_infer::infer::{
22+
InferCtxt, InferOk, LateBoundRegionConversionTime, NllRegionVariableOrigin,
23+
};
2224
use rustc_middle::mir::tcx::PlaceTy;
2325
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
2426
use rustc_middle::mir::AssertKind;
@@ -194,39 +196,32 @@ pub(crate) fn type_check<'mir, 'tcx>(
194196
let opaque_type_values = opaque_type_values
195197
.into_iter()
196198
.filter_map(|(opaque_type_key, decl)| {
197-
let def_id = body.source.def_id().expect_local();
198-
let body_id = cx.tcx().hir().local_def_id_to_hir_id(def_id);
199-
let cause = ObligationCause::misc(body.span, body_id);
200-
let hidden = cx
201-
.fully_perform_op(
202-
Locations::All(body.span),
203-
ConstraintCategory::OpaqueType,
204-
CustomTypeOp::new(
205-
|infcx| {
206-
let res = decl
207-
.hidden_type(infcx, &cause, param_env)
208-
.map_err(|e| e.0)?;
209-
infcx.register_member_constraints(
210-
param_env,
211-
opaque_type_key,
212-
res.value.ty,
213-
res.value.span,
214-
);
215-
Ok(res)
216-
},
217-
|| "opaque_type_map".to_string(),
218-
),
219-
)
220-
.unwrap();
221-
let mut hidden_type = infcx.resolve_vars_if_possible(hidden.ty);
199+
cx.fully_perform_op(
200+
Locations::All(body.span),
201+
ConstraintCategory::OpaqueType,
202+
CustomTypeOp::new(
203+
|infcx| {
204+
infcx.register_member_constraints(
205+
param_env,
206+
opaque_type_key,
207+
decl.hidden_type.ty,
208+
decl.hidden_type.span,
209+
);
210+
Ok(InferOk { value: (), obligations: vec![] })
211+
},
212+
|| "opaque_type_map".to_string(),
213+
),
214+
)
215+
.unwrap();
216+
let mut hidden_type = infcx.resolve_vars_if_possible(decl.hidden_type.ty);
222217
trace!(
223218
"finalized opaque type {:?} to {:#?}",
224219
opaque_type_key,
225220
hidden_type.kind()
226221
);
227222
if hidden_type.has_infer_types_or_consts() {
228223
infcx.tcx.sess.delay_span_bug(
229-
hidden.span,
224+
decl.hidden_type.span,
230225
&format!("could not resolve {:#?}", hidden_type.kind()),
231226
);
232227
hidden_type = infcx.tcx.ty_error();
@@ -263,7 +258,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
263258
);
264259
None
265260
} else {
266-
Some((opaque_type_key, (hidden_type, hidden.span, decl.origin)))
261+
Some((opaque_type_key, (hidden_type, decl.hidden_type.span, decl.origin)))
267262
}
268263
})
269264
.collect();

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
146146
})
147147
}
148148

149-
fn take_opaque_types_for_query_response(&self) -> Vec<(OpaqueTypeKey<'tcx>, Vec<Ty<'tcx>>)> {
149+
fn take_opaque_types_for_query_response(&self) -> Vec<(OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
150150
self.inner
151151
.borrow_mut()
152152
.opaque_type_storage
153153
.take_opaque_types()
154154
.into_iter()
155-
.map(|(k, v)| (k, v.hidden_types.into_iter().map(|ht| ht.ty).collect()))
155+
.map(|(k, v)| (k, v.hidden_type.ty))
156156
.collect()
157157
}
158158

@@ -497,14 +497,11 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
497497
let mut obligations = vec![];
498498

499499
// Carry all newly resolved opaque types to the caller's scope
500-
for (key, tys) in &query_response.value.opaque_types {
500+
for &(key, ty) in &query_response.value.opaque_types {
501501
let substs = substitute_value(self.tcx, &result_subst, key.substs);
502502
let opaque = self.tcx.mk_opaque(key.def_id, substs);
503-
for &ty in tys {
504-
let ty = substitute_value(self.tcx, &result_subst, ty);
505-
obligations
506-
.extend(self.handle_opaque_type(opaque, ty, cause, param_env)?.obligations);
507-
}
503+
let ty = substitute_value(self.tcx, &result_subst, ty);
504+
obligations.extend(self.handle_opaque_type(opaque, ty, cause, param_env)?.obligations);
508505
}
509506

510507
Ok(InferOk { value: result_subst, obligations })

compiler/rustc_infer/src/infer/opaque_types.rs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_data_structures::sync::Lrc;
66
use rustc_data_structures::vec_map::VecMap;
77
use rustc_hir as hir;
88
use rustc_middle::traits::ObligationCause;
9-
use rustc_middle::ty::error::TypeError;
109
use rustc_middle::ty::fold::BottomUpFolder;
1110
use rustc_middle::ty::subst::{GenericArgKind, Subst};
1211
use rustc_middle::ty::{self, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeVisitor};
@@ -33,61 +32,12 @@ pub struct OpaqueTypeDecl<'tcx> {
3332
/// The hidden types that have been inferred for this opaque type.
3433
/// There can be multiple, but they are all `lub`ed together at the end
3534
/// to obtain the canonical hidden type.
36-
pub hidden_types: Vec<OpaqueHiddenType<'tcx>>,
35+
pub hidden_type: OpaqueHiddenType<'tcx>,
3736

3837
/// The origin of the opaque type.
3938
pub origin: hir::OpaqueTyOrigin,
4039
}
4140

42-
impl<'tcx> OpaqueTypeDecl<'tcx> {
43-
pub fn hidden_type(
44-
&self,
45-
infcx: &InferCtxt<'_, 'tcx>,
46-
cause: &ObligationCause<'tcx>,
47-
param_env: ty::ParamEnv<'tcx>,
48-
) -> Result<
49-
InferOk<'tcx, OpaqueHiddenType<'tcx>>,
50-
(TypeError<'tcx>, OpaqueHiddenType<'tcx>, OpaqueHiddenType<'tcx>),
51-
> {
52-
let mut value = self.hidden_types[0];
53-
let mut obligations = vec![];
54-
let mut error: Option<(_, _, OpaqueHiddenType<'tcx>)> = None;
55-
for &next in self.hidden_types[1..].iter() {
56-
// FIXME: make use of the spans to get nicer diagnostics!
57-
let res = match infcx.at(cause, param_env).eq(value.ty, next.ty) {
58-
Ok(res) => res,
59-
Err(e) => {
60-
// Try to improve the span. Sometimes we have dummy spans, sometimes we are pointing
61-
// at an if/match instead of at the arm that gave us the type, but later spans point
62-
// to the right thing.
63-
if let Some((_, _, old)) = &mut error {
64-
old.span = old.span.substitute_dummy(next.span);
65-
// Shrink the span if possible
66-
if old.span.contains(next.span) {
67-
old.span = next.span;
68-
}
69-
} else {
70-
let mut next = next;
71-
next.span = next.span.substitute_dummy(cause.span(infcx.tcx));
72-
error = Some((e, value, next));
73-
}
74-
continue;
75-
}
76-
};
77-
obligations.extend(res.obligations);
78-
value.span = value.span.substitute_dummy(next.span);
79-
// Shrink the span if possible
80-
if value.span.contains(next.span) {
81-
value.span = next.span;
82-
}
83-
}
84-
match error {
85-
None => Ok(InferOk { value, obligations }),
86-
Some(e) => Err(e),
87-
}
88-
}
89-
}
90-
9141
#[derive(Copy, Clone, Debug, TypeFoldable)]
9242
pub struct OpaqueHiddenType<'tcx> {
9343
/// The span of this particular definition of the opaque type. So

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<'tcx> OpaqueTypeStorage<'tcx> {
2020
#[instrument(level = "debug")]
2121
pub(crate) fn remove(&mut self, key: OpaqueTypeKey<'tcx>, idx: Option<OpaqueHiddenType<'tcx>>) {
2222
if let Some(idx) = idx {
23-
self.opaque_types.get_mut(&key).unwrap().hidden_types[0] = idx;
23+
self.opaque_types.get_mut(&key).unwrap().hidden_type = idx;
2424
} else {
2525
match self.opaque_types.remove(&key) {
2626
None => bug!("reverted opaque type inference that was never registered: {:?}", key),
@@ -73,17 +73,15 @@ impl<'a, 'tcx> OpaqueTypeTable<'a, 'tcx> {
7373
&mut self,
7474
key: OpaqueTypeKey<'tcx>,
7575
opaque_type: Ty<'tcx>,
76-
ty: OpaqueHiddenType<'tcx>,
76+
hidden_type: OpaqueHiddenType<'tcx>,
7777
origin: OpaqueTyOrigin,
7878
) -> Option<Ty<'tcx>> {
7979
if let Some(decl) = self.storage.opaque_types.get_mut(&key) {
80-
assert_eq!(decl.hidden_types.len(), 1);
81-
let prev = decl.hidden_types[0];
82-
decl.hidden_types = vec![ty];
80+
let prev = std::mem::replace(&mut decl.hidden_type, hidden_type);
8381
self.undo_log.push(UndoLog::OpaqueTypes(key, Some(prev)));
8482
return Some(prev.ty);
8583
}
86-
let decl = OpaqueTypeDecl { opaque_type, hidden_types: vec![ty], origin };
84+
let decl = OpaqueTypeDecl { opaque_type, hidden_type, origin };
8785
self.storage.opaque_types.insert(key, decl);
8886
self.undo_log.push(UndoLog::OpaqueTypes(key, None));
8987
None

compiler/rustc_middle/src/infer/canonical.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub struct QueryResponse<'tcx, R> {
180180
pub certainty: Certainty,
181181
/// List of opaque types for which we figured out a hidden type
182182
/// during the evaluation of the query.
183-
pub opaque_types: Vec<(OpaqueTypeKey<'tcx>, Vec<Ty<'tcx>>)>,
183+
pub opaque_types: Vec<(OpaqueTypeKey<'tcx>, Ty<'tcx>)>,
184184
pub value: R,
185185
}
186186

compiler/rustc_typeck/src/check/mod.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ pub use diverges::Diverges;
9999
pub use expectation::Expectation;
100100
pub use fn_ctxt::*;
101101
pub use inherited::{Inherited, InheritedBuilder};
102-
use rustc_infer::traits::ObligationCause;
103-
use traits::ObligationCauseCode::MiscObligation;
104102

105103
use crate::astconv::AstConv;
106104
use crate::check::gather_locals::GatherLocalsVisitor;
@@ -474,19 +472,6 @@ fn typeck_with_fallback<'tcx>(
474472
fcx.require_type_is_sized(ty, span, code);
475473
}
476474

477-
let opaque_types = fcx.infcx.inner.borrow_mut().opaque_type_storage.opaque_types();
478-
for (_, decl) in opaque_types {
479-
let cause = ObligationCause::new(body.value.span, id, MiscObligation);
480-
if let Err((err, expected, actual)) =
481-
decl.hidden_type(&fcx.infcx, &cause, fcx.param_env)
482-
{
483-
let cause = ObligationCause::new(actual.span, id, MiscObligation);
484-
fcx.report_mismatched_types(&cause, expected.ty, actual.ty, err)
485-
.span_label(expected.span, "type expected due to this")
486-
.emit();
487-
}
488-
}
489-
490475
fcx.select_all_obligations_or_error();
491476

492477
if fn_sig.is_some() {

0 commit comments

Comments
 (0)