Skip to content

Commit 19a366e

Browse files
committed
Arrange methods on HirTyLowerer more logically
This makes it easier to read the trait definition for newcomers: Sorted from least “complex” to most “complex” followed by trivial “plumbing” and grouped by area. * Move `allow_infer` above all `*_infer` methods * It's the least complex method of those * Allows the `*_infer` to be placed right next to each other * Move `probe_ty_param_bounds` further down right next to `lower_assoc_ty` and `probe_adt` * It's more complex than the `infer` methods, it should come “later” * Now all required lowering functions are grouped together * Move the “plumbing” function `set_tainted_by_errors` further down below any actual lowering methods. * Provided method should come last
1 parent 0bb04c2 commit 19a366e

File tree

3 files changed

+72
-72
lines changed

3 files changed

+72
-72
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -369,23 +369,14 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
369369
self.item_def_id.to_def_id()
370370
}
371371

372-
fn probe_ty_param_bounds(
373-
&self,
374-
span: Span,
375-
def_id: LocalDefId,
376-
assoc_name: Ident,
377-
) -> ty::GenericPredicates<'tcx> {
378-
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name))
372+
fn allow_infer(&self) -> bool {
373+
false
379374
}
380375

381376
fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
382377
None
383378
}
384379

385-
fn allow_infer(&self) -> bool {
386-
false
387-
}
388-
389380
fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
390381
Ty::new_error_with_message(self.tcx(), span, "bad placeholder type")
391382
}
@@ -400,6 +391,15 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
400391
ty::Const::new_error_with_message(self.tcx(), ty, span, "bad placeholder constant")
401392
}
402393

394+
fn probe_ty_param_bounds(
395+
&self,
396+
span: Span,
397+
def_id: LocalDefId,
398+
assoc_name: Ident,
399+
) -> ty::GenericPredicates<'tcx> {
400+
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name))
401+
}
402+
403403
fn lower_assoc_ty(
404404
&self,
405405
span: Span,
@@ -493,17 +493,17 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
493493
ty.ty_adt_def()
494494
}
495495

496-
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
497-
self.tainted_by_errors.set(Some(err));
498-
}
499-
500496
fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) {
501497
// There's no place to record types from signatures?
502498
}
503499

504500
fn infcx(&self) -> Option<&InferCtxt<'tcx>> {
505501
None
506502
}
503+
504+
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
505+
self.tainted_by_errors.set(Some(err));
506+
}
507507
}
508508

509509
/// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present.

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ pub trait HirTyLowerer<'tcx> {
8989
/// Returns the [`DefId`] of the overarching item whose constituents get lowered.
9090
fn item_def_id(&self) -> DefId;
9191

92+
/// Returns `true` if the current context allows the use of inference variables.
93+
fn allow_infer(&self) -> bool;
94+
95+
/// Returns the region to use when a lifetime is omitted (and not elided).
96+
fn re_infer(&self, param: Option<&ty::GenericParamDef>, span: Span)
97+
-> Option<ty::Region<'tcx>>;
98+
99+
/// Returns the type to use when a type is omitted.
100+
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;
101+
102+
/// Returns the const to use when a const is omitted.
103+
fn ct_infer(
104+
&self,
105+
ty: Ty<'tcx>,
106+
param: Option<&ty::GenericParamDef>,
107+
span: Span,
108+
) -> Const<'tcx>;
109+
92110
/// Probe bounds in scope where the bounded type coincides with the given type parameter.
93111
///
94112
/// Rephrased, this returns bounds of the form `T: Trait`, where `T` is a type parameter
@@ -110,24 +128,6 @@ pub trait HirTyLowerer<'tcx> {
110128
assoc_name: Ident,
111129
) -> ty::GenericPredicates<'tcx>;
112130

113-
/// Returns the region to use when a lifetime is omitted (and not elided).
114-
fn re_infer(&self, param: Option<&ty::GenericParamDef>, span: Span)
115-
-> Option<ty::Region<'tcx>>;
116-
117-
/// Returns the type to use when a type is omitted.
118-
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;
119-
120-
/// Returns `true` if the current context allows the use of inference variables.
121-
fn allow_infer(&self) -> bool;
122-
123-
/// Returns the const to use when a const is omitted.
124-
fn ct_infer(
125-
&self,
126-
ty: Ty<'tcx>,
127-
param: Option<&ty::GenericParamDef>,
128-
span: Span,
129-
) -> Const<'tcx>;
130-
131131
/// Lower an associated type to a projection.
132132
///
133133
/// This method has to be defined by the concrete lowering context because
@@ -156,15 +156,18 @@ pub trait HirTyLowerer<'tcx> {
156156
/// or to an enum variant depending on the result of this function.
157157
fn probe_adt(&self, span: Span, ty: Ty<'tcx>) -> Option<ty::AdtDef<'tcx>>;
158158

159+
/// Record the lowered type of a HIR node in this context.
160+
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
161+
162+
/// The inference context of the lowering context if applicable.
163+
fn infcx(&self) -> Option<&InferCtxt<'tcx>>;
164+
159165
/// Taint the context with errors.
160166
///
161167
/// Invoke this when you encounter an error from some prior pass like name resolution.
162168
/// This is used to help suppress derived errors typeck might otherwise report.
163169
fn set_tainted_by_errors(&self, e: ErrorGuaranteed);
164170

165-
/// Record the lowered type of a HIR node in this context.
166-
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
167-
168171
/// Convenience method for coercing the lowering context into a trait object type.
169172
///
170173
/// Most lowering routines are defined on the trait object type directly
@@ -175,9 +178,6 @@ pub trait HirTyLowerer<'tcx> {
175178
{
176179
self
177180
}
178-
179-
/// The inference context of the lowering context if applicable.
180-
fn infcx(&self) -> Option<&InferCtxt<'tcx>>;
181181
}
182182

183183
/// New-typed boolean indicating whether explicit late-bound lifetimes

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+33-33
Original file line numberDiff line numberDiff line change
@@ -211,31 +211,8 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
211211
self.body_id.to_def_id()
212212
}
213213

214-
fn probe_ty_param_bounds(
215-
&self,
216-
_: Span,
217-
def_id: LocalDefId,
218-
_: Ident,
219-
) -> ty::GenericPredicates<'tcx> {
220-
let tcx = self.tcx;
221-
let item_def_id = tcx.hir().ty_param_owner(def_id);
222-
let generics = tcx.generics_of(item_def_id);
223-
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
224-
// HACK(eddyb) should get the original `Span`.
225-
let span = tcx.def_span(def_id);
226-
ty::GenericPredicates {
227-
parent: None,
228-
predicates: tcx.arena.alloc_from_iter(
229-
self.param_env.caller_bounds().iter().filter_map(|predicate| {
230-
match predicate.kind().skip_binder() {
231-
ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => {
232-
Some((predicate, span))
233-
}
234-
_ => None,
235-
}
236-
}),
237-
),
238-
}
214+
fn allow_infer(&self) -> bool {
215+
true
239216
}
240217

241218
fn re_infer(&self, def: Option<&ty::GenericParamDef>, span: Span) -> Option<ty::Region<'tcx>> {
@@ -246,10 +223,6 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
246223
Some(self.next_region_var(v))
247224
}
248225

249-
fn allow_infer(&self) -> bool {
250-
true
251-
}
252-
253226
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
254227
match param {
255228
Some(param) => self.var_for_def(span, param).as_type().unwrap(),
@@ -282,6 +255,33 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
282255
}
283256
}
284257

258+
fn probe_ty_param_bounds(
259+
&self,
260+
_: Span,
261+
def_id: LocalDefId,
262+
_: Ident,
263+
) -> ty::GenericPredicates<'tcx> {
264+
let tcx = self.tcx;
265+
let item_def_id = tcx.hir().ty_param_owner(def_id);
266+
let generics = tcx.generics_of(item_def_id);
267+
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
268+
// HACK(eddyb) should get the original `Span`.
269+
let span = tcx.def_span(def_id);
270+
ty::GenericPredicates {
271+
parent: None,
272+
predicates: tcx.arena.alloc_from_iter(
273+
self.param_env.caller_bounds().iter().filter_map(|predicate| {
274+
match predicate.kind().skip_binder() {
275+
ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => {
276+
Some((predicate, span))
277+
}
278+
_ => None,
279+
}
280+
}),
281+
),
282+
}
283+
}
284+
285285
fn lower_assoc_ty(
286286
&self,
287287
span: Span,
@@ -318,10 +318,6 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
318318
}
319319
}
320320

321-
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
322-
self.infcx.set_tainted_by_errors(e)
323-
}
324-
325321
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span) {
326322
// FIXME: normalization and escaping regions
327323
let ty = if !ty.has_escaping_bound_vars() {
@@ -345,6 +341,10 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
345341
fn infcx(&self) -> Option<&infer::InferCtxt<'tcx>> {
346342
Some(&self.infcx)
347343
}
344+
345+
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
346+
self.infcx.set_tainted_by_errors(e)
347+
}
348348
}
349349

350350
/// The `ty` representation of a user-provided type. Depending on the use-site

0 commit comments

Comments
 (0)