From 61c07a9a23d8967564bdddb5137efadeb48df271 Mon Sep 17 00:00:00 2001 From: Ellen Date: Fri, 14 Jan 2022 13:44:52 +0000 Subject: [PATCH] reviews ish --- compiler/rustc_hir/src/hir.rs | 7 +++++++ compiler/rustc_middle/src/ty/generics.rs | 7 +++++++ compiler/rustc_typeck/src/collect/type_of.rs | 11 +++++------ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_hir/src/hir.rs b/compiler/rustc_hir/src/hir.rs index 7a348a2c94aed..76f4df6ec2da8 100644 --- a/compiler/rustc_hir/src/hir.rs +++ b/compiler/rustc_hir/src/hir.rs @@ -314,6 +314,13 @@ impl GenericArg<'_> { GenericArg::Infer(_) => ast::ParamKindOrd::Infer, } } + + pub fn is_ty_or_const(&self) -> bool { + match self { + GenericArg::Lifetime(_) => false, + GenericArg::Type(_) | GenericArg::Const(_) | GenericArg::Infer(_) => true, + } + } } #[derive(Debug, HashStable_Generic)] diff --git a/compiler/rustc_middle/src/ty/generics.rs b/compiler/rustc_middle/src/ty/generics.rs index 1c3a01e2cfadf..0bd96f8f865fc 100644 --- a/compiler/rustc_middle/src/ty/generics.rs +++ b/compiler/rustc_middle/src/ty/generics.rs @@ -31,6 +31,13 @@ impl GenericParamDefKind { GenericParamDefKind::Const { .. } => ast::ParamKindOrd::Const, } } + + pub fn is_ty_or_const(&self) -> bool { + match self { + GenericParamDefKind::Lifetime => false, + GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => true, + } + } } #[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)] diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index effe31e4ee70c..5ff2a74754117 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -80,7 +80,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< .and_then(|args| { args.args .iter() - .filter(|arg| !matches!(arg, GenericArg::Lifetime(_))) + .filter(|arg| arg.is_ty_or_const()) .position(|arg| arg.id() == hir_id) }) .unwrap_or_else(|| { @@ -113,7 +113,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< .and_then(|args| { args.args .iter() - .filter(|arg| !matches!(arg, GenericArg::Lifetime(_))) + .filter(|arg| arg.is_ty_or_const()) .position(|arg| arg.id() == hir_id) }) .unwrap_or_else(|| { @@ -169,7 +169,7 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< .filter_map(|seg| seg.args.map(|args| (args.args, seg))) .find_map(|(args, seg)| { args.iter() - .filter(|arg| !matches!(arg, GenericArg::Lifetime(_))) + .filter(|arg| arg.is_ty_or_const()) .position(|arg| arg.id() == hir_id) .map(|index| (index, seg)) }); @@ -232,12 +232,11 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option< }; debug!(?parent_node); - debug!(?generics); - debug!(?arg_idx); + debug!(?generics, ?arg_idx); generics .params .iter() - .filter(|param| !matches!(param.kind, ty::GenericParamDefKind::Lifetime { .. })) + .filter(|param| param.kind.is_ty_or_const()) .nth(match generics.has_self && generics.parent.is_none() { true => arg_idx + 1, false => arg_idx,