Rollup merge of #98654 - nnethercote:pest-2.1.3-opt, r=pnkfelix
An optimization for `pest-2.1.3` An easy win I found while looking at a profile of `pest-2.1.3`. It's also a small code cleanup. r? `@pnkfelix`
This commit is contained in:
commit
fe87923b54
1 changed files with 10 additions and 17 deletions
|
@ -58,10 +58,7 @@ pub fn search_for_structural_match_violation<'tcx>(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
ty: Ty<'tcx>,
|
ty: Ty<'tcx>,
|
||||||
) -> Option<NonStructuralMatchTy<'tcx>> {
|
) -> Option<NonStructuralMatchTy<'tcx>> {
|
||||||
// FIXME: we should instead pass in an `infcx` from the outside.
|
ty.visit_with(&mut Search { tcx, span, seen: FxHashSet::default() }).break_value()
|
||||||
tcx.infer_ctxt().enter(|infcx| {
|
|
||||||
ty.visit_with(&mut Search { infcx, span, seen: FxHashSet::default() }).break_value()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This method returns true if and only if `adt_ty` itself has been marked as
|
/// This method returns true if and only if `adt_ty` itself has been marked as
|
||||||
|
@ -114,27 +111,23 @@ fn type_marked_structural<'tcx>(
|
||||||
/// This implements the traversal over the structure of a given type to try to
|
/// This implements the traversal over the structure of a given type to try to
|
||||||
/// find instances of ADTs (specifically structs or enums) that do not implement
|
/// find instances of ADTs (specifically structs or enums) that do not implement
|
||||||
/// the structural-match traits (`StructuralPartialEq` and `StructuralEq`).
|
/// the structural-match traits (`StructuralPartialEq` and `StructuralEq`).
|
||||||
struct Search<'a, 'tcx> {
|
struct Search<'tcx> {
|
||||||
span: Span,
|
span: Span,
|
||||||
|
|
||||||
infcx: InferCtxt<'a, 'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
|
|
||||||
/// Tracks ADTs previously encountered during search, so that
|
/// Tracks ADTs previously encountered during search, so that
|
||||||
/// we will not recur on them again.
|
/// we will not recur on them again.
|
||||||
seen: FxHashSet<hir::def_id::DefId>,
|
seen: FxHashSet<hir::def_id::DefId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'tcx> Search<'a, 'tcx> {
|
impl<'tcx> Search<'tcx> {
|
||||||
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
||||||
self.infcx.tcx
|
|
||||||
}
|
|
||||||
|
|
||||||
fn type_marked_structural(&self, adt_ty: Ty<'tcx>) -> bool {
|
fn type_marked_structural(&self, adt_ty: Ty<'tcx>) -> bool {
|
||||||
adt_ty.is_structural_eq_shallow(self.tcx())
|
adt_ty.is_structural_eq_shallow(self.tcx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
|
impl<'tcx> TypeVisitor<'tcx> for Search<'tcx> {
|
||||||
type BreakTy = NonStructuralMatchTy<'tcx>;
|
type BreakTy = NonStructuralMatchTy<'tcx>;
|
||||||
|
|
||||||
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
fn visit_ty(&mut self, ty: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
||||||
|
@ -193,7 +186,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
|
||||||
return ControlFlow::CONTINUE;
|
return ControlFlow::CONTINUE;
|
||||||
}
|
}
|
||||||
ty::Array(_, n)
|
ty::Array(_, n)
|
||||||
if { n.try_eval_usize(self.tcx(), ty::ParamEnv::reveal_all()) == Some(0) } =>
|
if { n.try_eval_usize(self.tcx, ty::ParamEnv::reveal_all()) == Some(0) } =>
|
||||||
{
|
{
|
||||||
// rust-lang/rust#62336: ignore type of contents
|
// rust-lang/rust#62336: ignore type of contents
|
||||||
// for empty array.
|
// for empty array.
|
||||||
|
@ -214,7 +207,7 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
|
||||||
bug!("unexpected type during structural-match checking: {:?}", ty);
|
bug!("unexpected type during structural-match checking: {:?}", ty);
|
||||||
}
|
}
|
||||||
ty::Error(_) => {
|
ty::Error(_) => {
|
||||||
self.tcx().sess.delay_span_bug(self.span, "ty::Error in structural-match check");
|
self.tcx.sess.delay_span_bug(self.span, "ty::Error in structural-match check");
|
||||||
// We still want to check other types after encountering an error,
|
// We still want to check other types after encountering an error,
|
||||||
// as this may still emit relevant errors.
|
// as this may still emit relevant errors.
|
||||||
return ControlFlow::CONTINUE;
|
return ControlFlow::CONTINUE;
|
||||||
|
@ -244,9 +237,9 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for Search<'a, 'tcx> {
|
||||||
|
|
||||||
// even though we skip super_visit_with, we must recur on
|
// even though we skip super_visit_with, we must recur on
|
||||||
// fields of ADT.
|
// fields of ADT.
|
||||||
let tcx = self.tcx();
|
let tcx = self.tcx;
|
||||||
adt_def.all_fields().map(|field| field.ty(tcx, substs)).try_for_each(|field_ty| {
|
adt_def.all_fields().map(|field| field.ty(tcx, substs)).try_for_each(|field_ty| {
|
||||||
let ty = self.tcx().normalize_erasing_regions(ty::ParamEnv::empty(), field_ty);
|
let ty = self.tcx.normalize_erasing_regions(ty::ParamEnv::empty(), field_ty);
|
||||||
debug!("structural-match ADT: field_ty={:?}, ty={:?}", field_ty, ty);
|
debug!("structural-match ADT: field_ty={:?}, ty={:?}", field_ty, ty);
|
||||||
ty.visit_with(self)
|
ty.visit_with(self)
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue