Skip to content

Commit cf001dc

Browse files
committed
Remove failed and review comments
1 parent 09978bd commit cf001dc

File tree

2 files changed

+69
-99
lines changed

2 files changed

+69
-99
lines changed

compiler/rustc_trait_selection/src/traits/project.rs

+56-86
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,11 @@ impl<'a, 'b, 'tcx> AssocTypeNormalizer<'a, 'b, 'tcx> {
315315
fn fold<T: TypeFoldable<'tcx>>(&mut self, value: T) -> T {
316316
let value = self.selcx.infcx().resolve_vars_if_possible(value);
317317

318-
if value.has_escaping_bound_vars() {
319-
bug!("Normalizing without wrapping in a `Binder`");
320-
}
318+
assert!(
319+
!value.has_escaping_bound_vars(),
320+
"Normalizing {:?} without wrapping in a `Binder`",
321+
value
322+
);
321323

322324
if !value.has_projections() { value } else { value.fold_with(self) }
323325
}
@@ -427,40 +429,36 @@ impl<'a, 'b, 'tcx> TypeFolder<'tcx> for AssocTypeNormalizer<'a, 'b, 'tcx> {
427429
// give up and fall back to pretending like we never tried!
428430

429431
let infcx = self.selcx.infcx();
430-
let replaced =
432+
let (data, mapped_regions, mapped_types, mapped_consts) =
431433
BoundVarReplacer::replace_bound_vars(infcx, &mut self.universes, data);
432-
if let Some((data, mapped_regions, mapped_types, mapped_consts)) = replaced {
433-
let normalized_ty = opt_normalize_projection_type(
434-
self.selcx,
435-
self.param_env,
436-
data,
437-
self.cause.clone(),
438-
self.depth,
439-
&mut self.obligations,
440-
)
441-
.ok()
442-
.flatten()
443-
.unwrap_or_else(|| ty);
444-
445-
let normalized_ty = PlaceholderReplacer::replace_placeholders(
446-
infcx,
447-
mapped_regions,
448-
mapped_types,
449-
mapped_consts,
450-
&self.universes,
451-
normalized_ty,
452-
);
453-
debug!(
454-
?self.depth,
455-
?ty,
456-
?normalized_ty,
457-
obligations.len = ?self.obligations.len(),
458-
"AssocTypeNormalizer: normalized type"
459-
);
460-
normalized_ty
461-
} else {
462-
ty
463-
}
434+
let normalized_ty = opt_normalize_projection_type(
435+
self.selcx,
436+
self.param_env,
437+
data,
438+
self.cause.clone(),
439+
self.depth,
440+
&mut self.obligations,
441+
)
442+
.ok()
443+
.flatten()
444+
.unwrap_or_else(|| ty);
445+
446+
let normalized_ty = PlaceholderReplacer::replace_placeholders(
447+
infcx,
448+
mapped_regions,
449+
mapped_types,
450+
mapped_consts,
451+
&self.universes,
452+
normalized_ty,
453+
);
454+
debug!(
455+
?self.depth,
456+
?ty,
457+
?normalized_ty,
458+
obligations.len = ?self.obligations.len(),
459+
"AssocTypeNormalizer: normalized type"
460+
);
461+
normalized_ty
464462
}
465463

466464
_ => ty,
@@ -491,14 +489,6 @@ pub struct BoundVarReplacer<'me, 'tcx> {
491489
// The `UniverseIndex` of the binding levels above us. These are optional, since we are lazy:
492490
// we don't actually create a universe until we see a bound var we have to replace.
493491
universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
494-
// FIXME: So, this is a less-than-ideal solution to a problem we want to solve eventually. Ideally, we
495-
// shouldn't need to worry about bound vars for which we haven't passed (`self.current_index`)
496-
// or that we don't explicitly know about (`self.universe_indices`). This is true for
497-
// `AssocTypeNormalizer` but not `QueryNormalizer` currently. When we can always know about
498-
// any binding levels above us, we can remove this. (The alternative would be
499-
// `outer_exclusive_binder`, but that only exists on `Ty`. Otherwise, we would have to visit
500-
// through the `T`, which we specifically want to avoid not being lazy.)
501-
failed: bool,
502492
}
503493

504494
impl<'me, 'tcx> BoundVarReplacer<'me, 'tcx> {
@@ -508,12 +498,12 @@ impl<'me, 'tcx> BoundVarReplacer<'me, 'tcx> {
508498
infcx: &'me InferCtxt<'me, 'tcx>,
509499
universe_indices: &'me mut Vec<Option<ty::UniverseIndex>>,
510500
value: T,
511-
) -> Option<(
501+
) -> (
512502
T,
513503
BTreeMap<ty::PlaceholderRegion, ty::BoundRegion>,
514504
BTreeMap<ty::PlaceholderType, ty::BoundTy>,
515505
BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar>,
516-
)> {
506+
) {
517507
let mapped_regions: BTreeMap<ty::PlaceholderRegion, ty::BoundRegion> = BTreeMap::new();
518508
let mapped_types: BTreeMap<ty::PlaceholderType, ty::BoundTy> = BTreeMap::new();
519509
let mapped_consts: BTreeMap<ty::PlaceholderConst<'tcx>, ty::BoundVar> = BTreeMap::new();
@@ -525,14 +515,24 @@ impl<'me, 'tcx> BoundVarReplacer<'me, 'tcx> {
525515
mapped_consts,
526516
current_index: ty::INNERMOST,
527517
universe_indices,
528-
failed: false,
529518
};
530519

531520
let value = value.super_fold_with(&mut replacer);
532521

533-
(!replacer.failed).then(|| {
534-
(value, replacer.mapped_regions, replacer.mapped_types, replacer.mapped_consts)
535-
})
522+
(value, replacer.mapped_regions, replacer.mapped_types, replacer.mapped_consts)
523+
}
524+
525+
fn universe_for(&mut self, debruijn: ty::DebruijnIndex) -> ty::UniverseIndex {
526+
let infcx = self.infcx;
527+
let index =
528+
self.universe_indices.len() - debruijn.as_usize() + self.current_index.as_usize() - 1;
529+
let universe = self.universe_indices[index].unwrap_or_else(|| {
530+
for i in self.universe_indices.iter_mut().take(index + 1) {
531+
*i = i.or_else(|| Some(infcx.create_next_universe()))
532+
}
533+
self.universe_indices[index].unwrap()
534+
});
535+
universe
536536
}
537537
}
538538

@@ -557,20 +557,10 @@ impl TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
557557
if debruijn.as_usize() + 1
558558
> self.current_index.as_usize() + self.universe_indices.len() =>
559559
{
560-
self.failed = true;
561-
r
560+
bug!("Bound vars outside of `self.universe_indices`");
562561
}
563562
ty::ReLateBound(debruijn, br) if debruijn >= self.current_index => {
564-
let infcx = self.infcx;
565-
let index = self.universe_indices.len() - debruijn.as_usize()
566-
+ self.current_index.as_usize()
567-
- 1;
568-
let universe = self.universe_indices[index].unwrap_or_else(|| {
569-
for i in self.universe_indices.iter_mut().take(index + 1) {
570-
*i = i.or_else(|| Some(infcx.create_next_universe()))
571-
}
572-
self.universe_indices[index].unwrap()
573-
});
563+
let universe = self.universe_for(debruijn);
574564
let p = ty::PlaceholderRegion { universe, name: br.kind };
575565
self.mapped_regions.insert(p.clone(), br);
576566
self.infcx.tcx.mk_region(ty::RePlaceholder(p))
@@ -585,20 +575,10 @@ impl TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
585575
if debruijn.as_usize() + 1
586576
> self.current_index.as_usize() + self.universe_indices.len() =>
587577
{
588-
self.failed = true;
589-
t
578+
bug!("Bound vars outside of `self.universe_indices`");
590579
}
591580
ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
592-
let infcx = self.infcx;
593-
let index = self.universe_indices.len() - debruijn.as_usize()
594-
+ self.current_index.as_usize()
595-
- 1;
596-
let universe = self.universe_indices[index].unwrap_or_else(|| {
597-
for i in self.universe_indices.iter_mut().take(index + 1) {
598-
*i = i.or_else(|| Some(infcx.create_next_universe()))
599-
}
600-
self.universe_indices[index].unwrap()
601-
});
581+
let universe = self.universe_for(debruijn);
602582
let p = ty::PlaceholderType { universe, name: bound_ty.var };
603583
self.mapped_types.insert(p.clone(), bound_ty);
604584
self.infcx.tcx.mk_ty(ty::Placeholder(p))
@@ -614,22 +594,12 @@ impl TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
614594
if debruijn.as_usize() + 1
615595
> self.current_index.as_usize() + self.universe_indices.len() =>
616596
{
617-
self.failed = true;
618-
ct
597+
bug!("Bound vars outside of `self.universe_indices`");
619598
}
620599
ty::Const { val: ty::ConstKind::Bound(debruijn, bound_const), ty }
621600
if debruijn >= self.current_index =>
622601
{
623-
let infcx = self.infcx;
624-
let index = self.universe_indices.len() - debruijn.as_usize()
625-
+ self.current_index.as_usize()
626-
- 1;
627-
let universe = self.universe_indices[index].unwrap_or_else(|| {
628-
for i in self.universe_indices.iter_mut().take(index + 1) {
629-
*i = i.or_else(|| Some(infcx.create_next_universe()))
630-
}
631-
self.universe_indices[index].unwrap()
632-
});
602+
let universe = self.universe_for(debruijn);
633603
let p = ty::PlaceholderConst {
634604
universe,
635605
name: ty::BoundConst { var: bound_const, ty },

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -226,21 +226,21 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for QueryNormalizer<'cx, 'tcx> {
226226
// to return `ty` because there are bound vars that we aren't yet handling in a more
227227
// complete way.
228228

229+
// `BoundVarReplacer` can't handle escaping bound vars. Ideally, we want this before even calling
230+
// `QueryNormalizer`, but some const-generics tests pass escaping bound vars.
231+
// Also, use `ty` so we get that sweet `outer_exclusive_binder` optimization
232+
assert!(!ty.has_vars_bound_at_or_above(ty::DebruijnIndex::from_usize(
233+
self.universes.len()
234+
)));
235+
229236
let tcx = self.infcx.tcx;
230237
let infcx = self.infcx;
231-
let replaced = crate::traits::project::BoundVarReplacer::replace_bound_vars(
232-
infcx,
233-
&mut self.universes,
234-
data,
235-
);
236-
let (data, mapped_regions, mapped_types, mapped_consts) = match replaced {
237-
Some(r) => r,
238-
None => {
239-
bug!("{:?} {:?}", data, self.universes);
240-
//self.error = true;
241-
//return ty.super_fold_with(self);
242-
}
243-
};
238+
let (data, mapped_regions, mapped_types, mapped_consts) =
239+
crate::traits::project::BoundVarReplacer::replace_bound_vars(
240+
infcx,
241+
&mut self.universes,
242+
data,
243+
);
244244
let data = data.super_fold_with(self);
245245

246246
let mut orig_values = OriginalQueryValues::default();

0 commit comments

Comments
 (0)