Skip to content

Commit 2bc5a0a

Browse files
committed
Make Clean take &mut DocContext
- Take `FnMut` in `rustc_trait_selection::find_auto_trait_generics` - Take `&mut DocContext` in most of `clean` - Collect the iterator in auto_trait_impls instead of iterating lazily; the lifetimes were really bad. - Changes `fn sess` to properly return a borrow with the lifetime of `'tcx`, not the mutable borrow.
1 parent 6da9e3c commit 2bc5a0a

File tree

14 files changed

+243
-251
lines changed

14 files changed

+243
-251
lines changed

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
7777
ty: Ty<'tcx>,
7878
orig_env: ty::ParamEnv<'tcx>,
7979
trait_did: DefId,
80-
auto_trait_callback: impl Fn(&InferCtxt<'_, 'tcx>, AutoTraitInfo<'tcx>) -> A,
80+
mut auto_trait_callback: impl FnMut(&InferCtxt<'_, 'tcx>, AutoTraitInfo<'tcx>) -> A,
8181
) -> AutoTraitResult<A> {
8282
let tcx = self.tcx;
8383

src/librustdoc/clean/auto_trait.rs

+36-49
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,38 @@ struct RegionDeps<'tcx> {
2121
}
2222

2323
crate struct AutoTraitFinder<'a, 'tcx> {
24-
crate cx: &'a core::DocContext<'tcx>,
25-
crate f: auto_trait::AutoTraitFinder<'tcx>,
24+
crate cx: &'a mut core::DocContext<'tcx>,
2625
}
2726

2827
impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
29-
crate fn new(cx: &'a core::DocContext<'tcx>) -> Self {
30-
let f = auto_trait::AutoTraitFinder::new(cx.tcx);
31-
32-
AutoTraitFinder { cx, f }
28+
crate fn new(cx: &'a mut core::DocContext<'tcx>) -> Self {
29+
AutoTraitFinder { cx }
3330
}
3431

3532
// FIXME(eddyb) figure out a better way to pass information about
3633
// parametrization of `ty` than `param_env_def_id`.
37-
crate fn get_auto_trait_impls(&self, ty: Ty<'tcx>, param_env_def_id: DefId) -> Vec<Item> {
38-
let param_env = self.cx.tcx.param_env(param_env_def_id);
34+
crate fn get_auto_trait_impls(&mut self, ty: Ty<'tcx>, param_env_def_id: DefId) -> Vec<Item> {
35+
let tcx = self.cx.tcx;
36+
let param_env = tcx.param_env(param_env_def_id);
37+
let f = auto_trait::AutoTraitFinder::new(self.cx.tcx);
3938

4039
debug!("get_auto_trait_impls({:?})", ty);
41-
let auto_traits = self.cx.auto_traits.iter().cloned();
40+
let auto_traits: Vec<_> = self.cx.auto_traits.iter().cloned().collect();
4241
auto_traits
42+
.into_iter()
4343
.filter_map(|trait_def_id| {
44-
let trait_ref = ty::TraitRef {
45-
def_id: trait_def_id,
46-
substs: self.cx.tcx.mk_substs_trait(ty, &[]),
47-
};
44+
let trait_ref =
45+
ty::TraitRef { def_id: trait_def_id, substs: tcx.mk_substs_trait(ty, &[]) };
4846
if !self.cx.generated_synthetics.borrow_mut().insert((ty, trait_def_id)) {
4947
debug!("get_auto_trait_impl_for({:?}): already generated, aborting", trait_ref);
5048
return None;
5149
}
5250

5351
let result =
54-
self.f.find_auto_trait_generics(ty, param_env, trait_def_id, |infcx, info| {
52+
f.find_auto_trait_generics(ty, param_env, trait_def_id, |infcx, info| {
5553
let region_data = info.region_data;
5654

57-
let names_map = self
58-
.cx
59-
.tcx
55+
let names_map = tcx
6056
.generics_of(param_env_def_id)
6157
.params
6258
.iter()
@@ -66,7 +62,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
6662
})
6763
.map(|name| (name, Lifetime(name)))
6864
.collect();
69-
let lifetime_predicates = self.handle_lifetimes(&region_data, &names_map);
65+
let lifetime_predicates = Self::handle_lifetimes(&region_data, &names_map);
7066
let new_generics = self.param_env_to_generics(
7167
infcx.tcx,
7268
param_env_def_id,
@@ -105,12 +101,10 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
105101
// Instead, we generate `impl !Send for Foo<T>`, which better
106102
// expresses the fact that `Foo<T>` never implements `Send`,
107103
// regardless of the choice of `T`.
108-
let params = (
109-
self.cx.tcx.generics_of(param_env_def_id),
110-
ty::GenericPredicates::default(),
111-
)
112-
.clean(self.cx)
113-
.params;
104+
let params =
105+
(tcx.generics_of(param_env_def_id), ty::GenericPredicates::default())
106+
.clean(self.cx)
107+
.params;
114108

115109
Generics { params, where_predicates: Vec::new() }
116110
}
@@ -139,12 +133,8 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
139133
.collect()
140134
}
141135

142-
fn get_lifetime(
143-
&self,
144-
region: Region<'_>,
145-
names_map: &FxHashMap<Symbol, Lifetime>,
146-
) -> Lifetime {
147-
self.region_name(region)
136+
fn get_lifetime(region: Region<'_>, names_map: &FxHashMap<Symbol, Lifetime>) -> Lifetime {
137+
region_name(region)
148138
.map(|name| {
149139
names_map.get(&name).unwrap_or_else(|| {
150140
panic!("Missing lifetime with name {:?} for {:?}", name.as_str(), region)
@@ -154,13 +144,6 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
154144
.clone()
155145
}
156146

157-
fn region_name(&self, region: Region<'_>) -> Option<Symbol> {
158-
match region {
159-
&ty::ReEarlyBound(r) => Some(r.name),
160-
_ => None,
161-
}
162-
}
163-
164147
// This method calculates two things: Lifetime constraints of the form 'a: 'b,
165148
// and region constraints of the form ReVar: 'a
166149
//
@@ -172,7 +155,6 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
172155
// to perform the calculations we need on our own, rather than trying to make
173156
// existing inference/solver code do what we want.
174157
fn handle_lifetimes<'cx>(
175-
&self,
176158
regions: &RegionConstraintData<'cx>,
177159
names_map: &FxHashMap<Symbol, Lifetime>,
178160
) -> Vec<WherePredicate> {
@@ -210,9 +192,9 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
210192
&Constraint::RegSubReg(r1, r2) => {
211193
// The constraint is already in the form that we want, so we're done with it
212194
// Desired order is 'larger, smaller', so flip then
213-
if self.region_name(r1) != self.region_name(r2) {
195+
if region_name(r1) != region_name(r2) {
214196
finished
215-
.entry(self.region_name(r2).expect("no region_name found"))
197+
.entry(region_name(r2).expect("no region_name found"))
216198
.or_default()
217199
.push(r1);
218200
}
@@ -245,9 +227,9 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
245227
for larger in deps.larger.iter() {
246228
match (smaller, larger) {
247229
(&RegionTarget::Region(r1), &RegionTarget::Region(r2)) => {
248-
if self.region_name(r1) != self.region_name(r2) {
230+
if region_name(r1) != region_name(r2) {
249231
finished
250-
.entry(self.region_name(r2).expect("no region name found"))
232+
.entry(region_name(r2).expect("no region name found"))
251233
.or_default()
252234
.push(r1) // Larger, smaller
253235
}
@@ -292,7 +274,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
292274
.get(name)
293275
.unwrap_or(&empty)
294276
.iter()
295-
.map(|region| GenericBound::Outlives(self.get_lifetime(region, names_map)))
277+
.map(|region| GenericBound::Outlives(Self::get_lifetime(region, names_map)))
296278
.collect();
297279

298280
if bounds.is_empty() {
@@ -437,7 +419,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
437419
// K', we use the dedicated syntax 'T: Fn() -> K'
438420
// * We explicitly add a '?Sized' bound if we didn't find any 'Sized' predicates for a type
439421
fn param_env_to_generics(
440-
&self,
422+
&mut self,
441423
tcx: TyCtxt<'tcx>,
442424
param_env_def_id: DefId,
443425
param_env: ty::ParamEnv<'tcx>,
@@ -468,10 +450,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
468450
_ => false,
469451
}
470452
})
471-
.map(|p| {
472-
let replaced = p.fold_with(&mut replacer);
473-
(replaced, replaced.clean(self.cx))
474-
});
453+
.map(|p| p.fold_with(&mut replacer));
475454

476455
let mut generic_params =
477456
(tcx.generics_of(param_env_def_id), tcx.explicit_predicates_of(param_env_def_id))
@@ -490,7 +469,8 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
490469

491470
let mut ty_to_fn: FxHashMap<Type, (Option<PolyTrait>, Option<Type>)> = Default::default();
492471

493-
for (orig_p, p) in clean_where_predicates {
472+
for p in clean_where_predicates {
473+
let (orig_p, p) = (p, p.clean(self.cx));
494474
if p.is_none() {
495475
continue;
496476
}
@@ -749,6 +729,13 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
749729
}
750730
}
751731

732+
fn region_name(region: Region<'_>) -> Option<Symbol> {
733+
match region {
734+
&ty::ReEarlyBound(r) => Some(r.name),
735+
_ => None,
736+
}
737+
}
738+
752739
// Replaces all ReVars in a type with ty::Region's, using the provided map
753740
struct RegionReplacer<'a, 'tcx> {
754741
vid_to_region: &'a FxHashMap<ty::RegionVid, ty::Region<'tcx>>,

src/librustdoc/clean/blanket_impl.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@ use rustc_span::DUMMY_SP;
1010
use super::*;
1111

1212
crate struct BlanketImplFinder<'a, 'tcx> {
13-
crate cx: &'a core::DocContext<'tcx>,
13+
crate cx: &'a mut core::DocContext<'tcx>,
1414
}
1515

1616
impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
17-
crate fn new(cx: &'a core::DocContext<'tcx>) -> Self {
18-
BlanketImplFinder { cx }
19-
}
20-
2117
// FIXME(eddyb) figure out a better way to pass information about
2218
// parametrization of `ty` than `param_env_def_id`.
23-
crate fn get_blanket_impls(&self, ty: Ty<'tcx>, param_env_def_id: DefId) -> Vec<Item> {
19+
crate fn get_blanket_impls(&mut self, ty: Ty<'tcx>, param_env_def_id: DefId) -> Vec<Item> {
2420
let param_env = self.cx.tcx.param_env(param_env_def_id);
2521

2622
debug!("get_blanket_impls({:?})", ty);

src/librustdoc/clean/inline.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type Attrs<'hir> = rustc_middle::ty::Attributes<'hir>;
3737
///
3838
/// `parent_module` refers to the parent of the *re-export*, not the original item.
3939
crate fn try_inline(
40-
cx: &DocContext<'_>,
40+
cx: &mut DocContext<'_>,
4141
parent_module: DefId,
4242
res: Res,
4343
name: Symbol,
@@ -129,7 +129,7 @@ crate fn try_inline(
129129
}
130130

131131
crate fn try_inline_glob(
132-
cx: &DocContext<'_>,
132+
cx: &mut DocContext<'_>,
133133
res: Res,
134134
visited: &mut FxHashSet<DefId>,
135135
) -> Option<Vec<clean::Item>> {
@@ -187,7 +187,7 @@ crate fn record_extern_fqn(cx: &DocContext<'_>, did: DefId, kind: clean::TypeKin
187187
}
188188
}
189189

190-
crate fn build_external_trait(cx: &DocContext<'_>, did: DefId) -> clean::Trait {
190+
crate fn build_external_trait(cx: &mut DocContext<'_>, did: DefId) -> clean::Trait {
191191
let trait_items =
192192
cx.tcx.associated_items(did).in_definition_order().map(|item| item.clean(cx)).collect();
193193

@@ -207,14 +207,14 @@ crate fn build_external_trait(cx: &DocContext<'_>, did: DefId) -> clean::Trait {
207207
}
208208
}
209209

210-
fn build_external_function(cx: &DocContext<'_>, did: DefId) -> clean::Function {
210+
fn build_external_function(cx: &mut DocContext<'_>, did: DefId) -> clean::Function {
211211
let sig = cx.tcx.fn_sig(did);
212212

213213
let constness =
214214
if is_min_const_fn(cx.tcx, did) { hir::Constness::Const } else { hir::Constness::NotConst };
215215
let asyncness = cx.tcx.asyncness(did);
216216
let predicates = cx.tcx.predicates_of(did);
217-
let (generics, decl) = clean::enter_impl_trait(cx, || {
217+
let (generics, decl) = clean::enter_impl_trait(cx, |cx| {
218218
((cx.tcx.generics_of(did), predicates).clean(cx), (did, sig).clean(cx))
219219
});
220220
clean::Function {
@@ -224,7 +224,7 @@ fn build_external_function(cx: &DocContext<'_>, did: DefId) -> clean::Function {
224224
}
225225
}
226226

227-
fn build_enum(cx: &DocContext<'_>, did: DefId) -> clean::Enum {
227+
fn build_enum(cx: &mut DocContext<'_>, did: DefId) -> clean::Enum {
228228
let predicates = cx.tcx.explicit_predicates_of(did);
229229

230230
clean::Enum {
@@ -234,7 +234,7 @@ fn build_enum(cx: &DocContext<'_>, did: DefId) -> clean::Enum {
234234
}
235235
}
236236

237-
fn build_struct(cx: &DocContext<'_>, did: DefId) -> clean::Struct {
237+
fn build_struct(cx: &mut DocContext<'_>, did: DefId) -> clean::Struct {
238238
let predicates = cx.tcx.explicit_predicates_of(did);
239239
let variant = cx.tcx.adt_def(did).non_enum_variant();
240240

@@ -246,7 +246,7 @@ fn build_struct(cx: &DocContext<'_>, did: DefId) -> clean::Struct {
246246
}
247247
}
248248

249-
fn build_union(cx: &DocContext<'_>, did: DefId) -> clean::Union {
249+
fn build_union(cx: &mut DocContext<'_>, did: DefId) -> clean::Union {
250250
let predicates = cx.tcx.explicit_predicates_of(did);
251251
let variant = cx.tcx.adt_def(did).non_enum_variant();
252252

@@ -257,7 +257,7 @@ fn build_union(cx: &DocContext<'_>, did: DefId) -> clean::Union {
257257
}
258258
}
259259

260-
fn build_type_alias(cx: &DocContext<'_>, did: DefId) -> clean::Typedef {
260+
fn build_type_alias(cx: &mut DocContext<'_>, did: DefId) -> clean::Typedef {
261261
let predicates = cx.tcx.explicit_predicates_of(did);
262262
let type_ = cx.tcx.type_of(did).clean(cx);
263263

@@ -270,7 +270,7 @@ fn build_type_alias(cx: &DocContext<'_>, did: DefId) -> clean::Typedef {
270270

271271
/// Builds all inherent implementations of an ADT (struct/union/enum) or Trait item/path/reexport.
272272
crate fn build_impls(
273-
cx: &DocContext<'_>,
273+
cx: &mut DocContext<'_>,
274274
parent_module: Option<DefId>,
275275
did: DefId,
276276
attrs: Option<Attrs<'_>>,
@@ -286,7 +286,7 @@ crate fn build_impls(
286286

287287
/// `parent_module` refers to the parent of the re-export, not the original item
288288
fn merge_attrs(
289-
cx: &DocContext<'_>,
289+
cx: &mut DocContext<'_>,
290290
parent_module: Option<DefId>,
291291
old_attrs: Attrs<'_>,
292292
new_attrs: Option<Attrs<'_>>,
@@ -311,7 +311,7 @@ fn merge_attrs(
311311

312312
/// Builds a specific implementation of a type. The `did` could be a type method or trait method.
313313
crate fn build_impl(
314-
cx: &DocContext<'_>,
314+
cx: &mut DocContext<'_>,
315315
parent_module: impl Into<Option<DefId>>,
316316
did: DefId,
317317
attrs: Option<Attrs<'_>>,
@@ -394,7 +394,7 @@ crate fn build_impl(
394394
}
395395
})
396396
.collect::<Vec<_>>(),
397-
clean::enter_impl_trait(cx, || (tcx.generics_of(did), predicates).clean(cx)),
397+
clean::enter_impl_trait(cx, |cx| (tcx.generics_of(did), predicates).clean(cx)),
398398
),
399399
};
400400
let polarity = tcx.impl_polarity(did);
@@ -437,7 +437,11 @@ crate fn build_impl(
437437
ret.push(item);
438438
}
439439

440-
fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>) -> clean::Module {
440+
fn build_module(
441+
cx: &mut DocContext<'_>,
442+
did: DefId,
443+
visited: &mut FxHashSet<DefId>,
444+
) -> clean::Module {
441445
let mut items = Vec::new();
442446

443447
// If we're re-exporting a re-export it may actually re-export something in
@@ -495,7 +499,7 @@ crate fn print_inlined_const(cx: &DocContext<'_>, did: DefId) -> String {
495499
}
496500
}
497501

498-
fn build_const(cx: &DocContext<'_>, did: DefId) -> clean::Constant {
502+
fn build_const(cx: &mut DocContext<'_>, did: DefId) -> clean::Constant {
499503
clean::Constant {
500504
type_: cx.tcx.type_of(did).clean(cx),
501505
expr: print_inlined_const(cx, did),
@@ -506,15 +510,15 @@ fn build_const(cx: &DocContext<'_>, did: DefId) -> clean::Constant {
506510
}
507511
}
508512

509-
fn build_static(cx: &DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
513+
fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
510514
clean::Static {
511515
type_: cx.tcx.type_of(did).clean(cx),
512516
mutability: if mutable { Mutability::Mut } else { Mutability::Not },
513517
expr: None,
514518
}
515519
}
516520

517-
fn build_macro(cx: &DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind {
521+
fn build_macro(cx: &mut DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind {
518522
let imported_from = cx.tcx.original_crate_name(did.krate);
519523
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) {
520524
LoadedMacro::MacroDef(def, _) => {
@@ -603,7 +607,7 @@ fn separate_supertrait_bounds(
603607
(g, ty_bounds)
604608
}
605609

606-
crate fn record_extern_trait(cx: &DocContext<'_>, did: DefId) {
610+
crate fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) {
607611
if did.is_local() {
608612
return;
609613
}

0 commit comments

Comments
 (0)