Skip to content

Commit 72472c8

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 6d5a939 commit 72472c8

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
@@ -372,23 +372,14 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
372372
self.item_def_id.to_def_id()
373373
}
374374

375-
fn probe_ty_param_bounds(
376-
&self,
377-
span: Span,
378-
def_id: LocalDefId,
379-
assoc_name: Ident,
380-
) -> ty::GenericPredicates<'tcx> {
381-
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name))
375+
fn allow_infer(&self) -> bool {
376+
false
382377
}
383378

384379
fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
385380
None
386381
}
387382

388-
fn allow_infer(&self) -> bool {
389-
false
390-
}
391-
392383
fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
393384
Ty::new_error_with_message(self.tcx(), span, "bad placeholder type")
394385
}
@@ -403,6 +394,15 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
403394
ty::Const::new_error_with_message(self.tcx(), ty, span, "bad placeholder constant")
404395
}
405396

397+
fn probe_ty_param_bounds(
398+
&self,
399+
span: Span,
400+
def_id: LocalDefId,
401+
assoc_name: Ident,
402+
) -> ty::GenericPredicates<'tcx> {
403+
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name))
404+
}
405+
406406
fn lower_assoc_ty(
407407
&self,
408408
span: Span,
@@ -496,17 +496,17 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
496496
ty.ty_adt_def()
497497
}
498498

499-
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
500-
self.tainted_by_errors.set(Some(err));
501-
}
502-
503499
fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) {
504500
// There's no place to record types from signatures?
505501
}
506502

507503
fn infcx(&self) -> Option<&InferCtxt<'tcx>> {
508504
None
509505
}
506+
507+
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
508+
self.tainted_by_errors.set(Some(err));
509+
}
510510
}
511511

512512
/// 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
@@ -221,31 +221,8 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
221221
self.body_id.to_def_id()
222222
}
223223

224-
fn probe_ty_param_bounds(
225-
&self,
226-
_: Span,
227-
def_id: LocalDefId,
228-
_: Ident,
229-
) -> ty::GenericPredicates<'tcx> {
230-
let tcx = self.tcx;
231-
let item_def_id = tcx.hir().ty_param_owner(def_id);
232-
let generics = tcx.generics_of(item_def_id);
233-
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
234-
// HACK(eddyb) should get the original `Span`.
235-
let span = tcx.def_span(def_id);
236-
ty::GenericPredicates {
237-
parent: None,
238-
predicates: tcx.arena.alloc_from_iter(
239-
self.param_env.caller_bounds().iter().filter_map(|predicate| {
240-
match predicate.kind().skip_binder() {
241-
ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => {
242-
Some((predicate, span))
243-
}
244-
_ => None,
245-
}
246-
}),
247-
),
248-
}
224+
fn allow_infer(&self) -> bool {
225+
true
249226
}
250227

251228
fn re_infer(&self, def: Option<&ty::GenericParamDef>, span: Span) -> Option<ty::Region<'tcx>> {
@@ -256,10 +233,6 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
256233
Some(self.next_region_var(v))
257234
}
258235

259-
fn allow_infer(&self) -> bool {
260-
true
261-
}
262-
263236
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
264237
match param {
265238
Some(param) => self.var_for_def(span, param).as_type().unwrap(),
@@ -292,6 +265,33 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
292265
}
293266
}
294267

268+
fn probe_ty_param_bounds(
269+
&self,
270+
_: Span,
271+
def_id: LocalDefId,
272+
_: Ident,
273+
) -> ty::GenericPredicates<'tcx> {
274+
let tcx = self.tcx;
275+
let item_def_id = tcx.hir().ty_param_owner(def_id);
276+
let generics = tcx.generics_of(item_def_id);
277+
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
278+
// HACK(eddyb) should get the original `Span`.
279+
let span = tcx.def_span(def_id);
280+
ty::GenericPredicates {
281+
parent: None,
282+
predicates: tcx.arena.alloc_from_iter(
283+
self.param_env.caller_bounds().iter().filter_map(|predicate| {
284+
match predicate.kind().skip_binder() {
285+
ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => {
286+
Some((predicate, span))
287+
}
288+
_ => None,
289+
}
290+
}),
291+
),
292+
}
293+
}
294+
295295
fn lower_assoc_ty(
296296
&self,
297297
span: Span,
@@ -328,10 +328,6 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
328328
}
329329
}
330330

331-
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
332-
self.infcx.set_tainted_by_errors(e)
333-
}
334-
335331
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span) {
336332
// FIXME: normalization and escaping regions
337333
let ty = if !ty.has_escaping_bound_vars() {
@@ -355,6 +351,10 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
355351
fn infcx(&self) -> Option<&infer::InferCtxt<'tcx>> {
356352
Some(&self.infcx)
357353
}
354+
355+
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
356+
self.infcx.set_tainted_by_errors(e)
357+
}
358358
}
359359

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

0 commit comments

Comments
 (0)