Skip to content

Commit ed620cf

Browse files
committed
Auto merge of rust-lang#105612 - oli-obk:bind_rustdoc, r=GuillaumeGomez
use ty::Binder in rustdoc instead of `skip_binder` r? `@GuillaumeGomez` this is a preliminary cleanup required to be able to normalize correctly/conveniently in rustdoc
2 parents 109cccb + 3075451 commit ed620cf

File tree

7 files changed

+174
-102
lines changed

7 files changed

+174
-102
lines changed

compiler/rustc_middle/src/ty/sty.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,12 @@ where
980980
/// contain any bound vars that would be bound by the
981981
/// binder. This is commonly used to 'inject' a value T into a
982982
/// different binding level.
983+
#[track_caller]
983984
pub fn dummy(value: T) -> Binder<'tcx, T> {
984-
assert!(!value.has_escaping_bound_vars());
985+
assert!(
986+
!value.has_escaping_bound_vars(),
987+
"`{value:?}` has escaping bound vars, so it cannot be wrapped in a dummy binder."
988+
);
985989
Binder(value, ty::List::empty())
986990
}
987991

@@ -1128,6 +1132,13 @@ impl<'tcx, T> Binder<'tcx, Option<T>> {
11281132
}
11291133
}
11301134

1135+
impl<'tcx, T: IntoIterator> Binder<'tcx, T> {
1136+
pub fn iter(self) -> impl Iterator<Item = ty::Binder<'tcx, T::Item>> {
1137+
let bound_vars = self.1;
1138+
self.0.into_iter().map(|v| Binder(v, bound_vars))
1139+
}
1140+
}
1141+
11311142
/// Represents the projection of an associated type. In explicit UFCS
11321143
/// form this would be written `<T as Trait<..>>::N`.
11331144
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]

src/librustdoc/clean/auto_trait.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ where
4444
discard_positive_impl: bool,
4545
) -> Option<Item> {
4646
let tcx = self.cx.tcx;
47-
let trait_ref = tcx.mk_trait_ref(trait_def_id, [ty]);
47+
let trait_ref = ty::Binder::dummy(tcx.mk_trait_ref(trait_def_id, [ty]));
4848
if !self.cx.generated_synthetics.insert((ty, trait_def_id)) {
4949
debug!("get_auto_trait_impl_for({:?}): already generated, aborting", trait_ref);
5050
return None;
@@ -124,7 +124,7 @@ where
124124
unsafety: hir::Unsafety::Normal,
125125
generics: new_generics,
126126
trait_: Some(clean_trait_ref_with_bindings(self.cx, trait_ref, ThinVec::new())),
127-
for_: clean_middle_ty(ty, self.cx, None),
127+
for_: clean_middle_ty(ty::Binder::dummy(ty), self.cx, None),
128128
items: Vec::new(),
129129
polarity,
130130
kind: ImplKind::Auto,

src/librustdoc/clean/blanket_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
105105
// the post-inference `trait_ref`, as it's more accurate.
106106
trait_: Some(clean_trait_ref_with_bindings(
107107
cx,
108-
trait_ref.0,
108+
ty::Binder::dummy(trait_ref.0),
109109
ThinVec::new(),
110110
)),
111-
for_: clean_middle_ty(ty.0, cx, None),
111+
for_: clean_middle_ty(ty::Binder::dummy(ty.0), cx, None),
112112
items: cx
113113
.tcx
114114
.associated_items(impl_def_id)
@@ -117,7 +117,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
117117
.collect::<Vec<_>>(),
118118
polarity: ty::ImplPolarity::Positive,
119119
kind: ImplKind::Blanket(Box::new(clean_middle_ty(
120-
trait_ref.0.self_ty(),
120+
ty::Binder::dummy(trait_ref.0.self_ty()),
121121
cx,
122122
None,
123123
))),

src/librustdoc/clean/inline.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
293293

294294
fn build_type_alias(cx: &mut DocContext<'_>, did: DefId) -> Box<clean::Typedef> {
295295
let predicates = cx.tcx.explicit_predicates_of(did);
296-
let type_ = clean_middle_ty(cx.tcx.type_of(did), cx, Some(did));
296+
let type_ = clean_middle_ty(ty::Binder::dummy(cx.tcx.type_of(did)), cx, Some(did));
297297

298298
Box::new(clean::Typedef {
299299
type_,
@@ -405,7 +405,7 @@ pub(crate) fn build_impl(
405405

406406
let for_ = match &impl_item {
407407
Some(impl_) => clean_ty(impl_.self_ty, cx),
408-
None => clean_middle_ty(tcx.type_of(did), cx, Some(did)),
408+
None => clean_middle_ty(ty::Binder::dummy(tcx.type_of(did)), cx, Some(did)),
409409
};
410410

411411
// Only inline impl if the implementing type is
@@ -496,7 +496,8 @@ pub(crate) fn build_impl(
496496
),
497497
};
498498
let polarity = tcx.impl_polarity(did);
499-
let trait_ = associated_trait.map(|t| clean_trait_ref_with_bindings(cx, t, ThinVec::new()));
499+
let trait_ = associated_trait
500+
.map(|t| clean_trait_ref_with_bindings(cx, ty::Binder::dummy(t), ThinVec::new()));
500501
if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
501502
super::build_deref_target_impls(cx, &trait_items, ret);
502503
}
@@ -640,14 +641,14 @@ pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
640641

641642
fn build_const(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
642643
clean::Constant {
643-
type_: clean_middle_ty(cx.tcx.type_of(def_id), cx, Some(def_id)),
644+
type_: clean_middle_ty(ty::Binder::dummy(cx.tcx.type_of(def_id)), cx, Some(def_id)),
644645
kind: clean::ConstantKind::Extern { def_id },
645646
}
646647
}
647648

648649
fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
649650
clean::Static {
650-
type_: clean_middle_ty(cx.tcx.type_of(did), cx, Some(did)),
651+
type_: clean_middle_ty(ty::Binder::dummy(cx.tcx.type_of(did)), cx, Some(did)),
651652
mutability: if mutable { Mutability::Mut } else { Mutability::Not },
652653
expr: None,
653654
}

0 commit comments

Comments
 (0)