1
Fork 0

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
This commit is contained in:
León Orell Valerian Liehr 2024-03-22 05:47:11 +01:00
parent 6d5a93949a
commit 72472c831c
No known key found for this signature in database
GPG key ID: D17A07215F68E713
3 changed files with 72 additions and 72 deletions

View file

@ -372,23 +372,14 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
self.item_def_id.to_def_id() self.item_def_id.to_def_id()
} }
fn probe_ty_param_bounds( fn allow_infer(&self) -> bool {
&self, false
span: Span,
def_id: LocalDefId,
assoc_name: Ident,
) -> ty::GenericPredicates<'tcx> {
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name))
} }
fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> { fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
None None
} }
fn allow_infer(&self) -> bool {
false
}
fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> { fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
Ty::new_error_with_message(self.tcx(), span, "bad placeholder type") Ty::new_error_with_message(self.tcx(), span, "bad placeholder type")
} }
@ -403,6 +394,15 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
ty::Const::new_error_with_message(self.tcx(), ty, span, "bad placeholder constant") ty::Const::new_error_with_message(self.tcx(), ty, span, "bad placeholder constant")
} }
fn probe_ty_param_bounds(
&self,
span: Span,
def_id: LocalDefId,
assoc_name: Ident,
) -> ty::GenericPredicates<'tcx> {
self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_name))
}
fn lower_assoc_ty( fn lower_assoc_ty(
&self, &self,
span: Span, span: Span,
@ -496,10 +496,6 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
ty.ty_adt_def() ty.ty_adt_def()
} }
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
self.tainted_by_errors.set(Some(err));
}
fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) { fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) {
// There's no place to record types from signatures? // There's no place to record types from signatures?
} }
@ -507,6 +503,10 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
fn infcx(&self) -> Option<&InferCtxt<'tcx>> { fn infcx(&self) -> Option<&InferCtxt<'tcx>> {
None None
} }
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
self.tainted_by_errors.set(Some(err));
}
} }
/// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present. /// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present.

View file

@ -89,6 +89,24 @@ pub trait HirTyLowerer<'tcx> {
/// Returns the [`DefId`] of the overarching item whose constituents get lowered. /// Returns the [`DefId`] of the overarching item whose constituents get lowered.
fn item_def_id(&self) -> DefId; fn item_def_id(&self) -> DefId;
/// Returns `true` if the current context allows the use of inference variables.
fn allow_infer(&self) -> bool;
/// Returns the region to use when a lifetime is omitted (and not elided).
fn re_infer(&self, param: Option<&ty::GenericParamDef>, span: Span)
-> Option<ty::Region<'tcx>>;
/// Returns the type to use when a type is omitted.
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;
/// Returns the const to use when a const is omitted.
fn ct_infer(
&self,
ty: Ty<'tcx>,
param: Option<&ty::GenericParamDef>,
span: Span,
) -> Const<'tcx>;
/// Probe bounds in scope where the bounded type coincides with the given type parameter. /// Probe bounds in scope where the bounded type coincides with the given type parameter.
/// ///
/// Rephrased, this returns bounds of the form `T: Trait`, where `T` is a type parameter /// Rephrased, this returns bounds of the form `T: Trait`, where `T` is a type parameter
@ -110,24 +128,6 @@ pub trait HirTyLowerer<'tcx> {
assoc_name: Ident, assoc_name: Ident,
) -> ty::GenericPredicates<'tcx>; ) -> ty::GenericPredicates<'tcx>;
/// Returns the region to use when a lifetime is omitted (and not elided).
fn re_infer(&self, param: Option<&ty::GenericParamDef>, span: Span)
-> Option<ty::Region<'tcx>>;
/// Returns the type to use when a type is omitted.
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx>;
/// Returns `true` if the current context allows the use of inference variables.
fn allow_infer(&self) -> bool;
/// Returns the const to use when a const is omitted.
fn ct_infer(
&self,
ty: Ty<'tcx>,
param: Option<&ty::GenericParamDef>,
span: Span,
) -> Const<'tcx>;
/// Lower an associated type to a projection. /// Lower an associated type to a projection.
/// ///
/// This method has to be defined by the concrete lowering context because /// This method has to be defined by the concrete lowering context because
@ -156,15 +156,18 @@ pub trait HirTyLowerer<'tcx> {
/// or to an enum variant depending on the result of this function. /// or to an enum variant depending on the result of this function.
fn probe_adt(&self, span: Span, ty: Ty<'tcx>) -> Option<ty::AdtDef<'tcx>>; fn probe_adt(&self, span: Span, ty: Ty<'tcx>) -> Option<ty::AdtDef<'tcx>>;
/// Record the lowered type of a HIR node in this context.
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
/// The inference context of the lowering context if applicable.
fn infcx(&self) -> Option<&InferCtxt<'tcx>>;
/// Taint the context with errors. /// Taint the context with errors.
/// ///
/// Invoke this when you encounter an error from some prior pass like name resolution. /// Invoke this when you encounter an error from some prior pass like name resolution.
/// This is used to help suppress derived errors typeck might otherwise report. /// This is used to help suppress derived errors typeck might otherwise report.
fn set_tainted_by_errors(&self, e: ErrorGuaranteed); fn set_tainted_by_errors(&self, e: ErrorGuaranteed);
/// Record the lowered type of a HIR node in this context.
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
/// Convenience method for coercing the lowering context into a trait object type. /// Convenience method for coercing the lowering context into a trait object type.
/// ///
/// Most lowering routines are defined on the trait object type directly /// Most lowering routines are defined on the trait object type directly
@ -175,9 +178,6 @@ pub trait HirTyLowerer<'tcx> {
{ {
self self
} }
/// The inference context of the lowering context if applicable.
fn infcx(&self) -> Option<&InferCtxt<'tcx>>;
} }
/// New-typed boolean indicating whether explicit late-bound lifetimes /// New-typed boolean indicating whether explicit late-bound lifetimes

View file

@ -221,31 +221,8 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
self.body_id.to_def_id() self.body_id.to_def_id()
} }
fn probe_ty_param_bounds( fn allow_infer(&self) -> bool {
&self, true
_: Span,
def_id: LocalDefId,
_: Ident,
) -> ty::GenericPredicates<'tcx> {
let tcx = self.tcx;
let item_def_id = tcx.hir().ty_param_owner(def_id);
let generics = tcx.generics_of(item_def_id);
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
// HACK(eddyb) should get the original `Span`.
let span = tcx.def_span(def_id);
ty::GenericPredicates {
parent: None,
predicates: tcx.arena.alloc_from_iter(
self.param_env.caller_bounds().iter().filter_map(|predicate| {
match predicate.kind().skip_binder() {
ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => {
Some((predicate, span))
}
_ => None,
}
}),
),
}
} }
fn re_infer(&self, def: Option<&ty::GenericParamDef>, span: Span) -> Option<ty::Region<'tcx>> { 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> {
Some(self.next_region_var(v)) Some(self.next_region_var(v))
} }
fn allow_infer(&self) -> bool {
true
}
fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> { fn ty_infer(&self, param: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
match param { match param {
Some(param) => self.var_for_def(span, param).as_type().unwrap(), Some(param) => self.var_for_def(span, param).as_type().unwrap(),
@ -292,6 +265,33 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
} }
} }
fn probe_ty_param_bounds(
&self,
_: Span,
def_id: LocalDefId,
_: Ident,
) -> ty::GenericPredicates<'tcx> {
let tcx = self.tcx;
let item_def_id = tcx.hir().ty_param_owner(def_id);
let generics = tcx.generics_of(item_def_id);
let index = generics.param_def_id_to_index[&def_id.to_def_id()];
// HACK(eddyb) should get the original `Span`.
let span = tcx.def_span(def_id);
ty::GenericPredicates {
parent: None,
predicates: tcx.arena.alloc_from_iter(
self.param_env.caller_bounds().iter().filter_map(|predicate| {
match predicate.kind().skip_binder() {
ty::ClauseKind::Trait(data) if data.self_ty().is_param(index) => {
Some((predicate, span))
}
_ => None,
}
}),
),
}
}
fn lower_assoc_ty( fn lower_assoc_ty(
&self, &self,
span: Span, span: Span,
@ -328,10 +328,6 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
} }
} }
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
self.infcx.set_tainted_by_errors(e)
}
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span) { fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span) {
// FIXME: normalization and escaping regions // FIXME: normalization and escaping regions
let ty = if !ty.has_escaping_bound_vars() { let ty = if !ty.has_escaping_bound_vars() {
@ -355,6 +351,10 @@ impl<'a, 'tcx> HirTyLowerer<'tcx> for FnCtxt<'a, 'tcx> {
fn infcx(&self) -> Option<&infer::InferCtxt<'tcx>> { fn infcx(&self) -> Option<&infer::InferCtxt<'tcx>> {
Some(&self.infcx) Some(&self.infcx)
} }
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
self.infcx.set_tainted_by_errors(e)
}
} }
/// The `ty` representation of a user-provided type. Depending on the use-site /// The `ty` representation of a user-provided type. Depending on the use-site