Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/expr_store/lower.rs')
-rw-r--r--crates/hir-def/src/expr_store/lower.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/crates/hir-def/src/expr_store/lower.rs b/crates/hir-def/src/expr_store/lower.rs
index e7f2247c5b..d49c283bee 100644
--- a/crates/hir-def/src/expr_store/lower.rs
+++ b/crates/hir-def/src/expr_store/lower.rs
@@ -788,6 +788,7 @@ impl ExprCollector<'_> {
node: ast::GenericArgList,
impl_trait_lower_fn: &mut impl FnMut(ThinVec<TypeBound>) -> TypeRef,
) -> Option<GenericArgs> {
+ // This needs to be kept in sync with `hir_generic_arg_to_ast()`.
let mut args = Vec::new();
let mut bindings = Vec::new();
for generic_arg in node.generic_args() {
@@ -797,6 +798,7 @@ impl ExprCollector<'_> {
args.push(GenericArg::Type(type_ref));
}
ast::GenericArg::AssocTypeArg(assoc_type_arg) => {
+ // This needs to be kept in sync with `hir_assoc_type_binding_to_ast()`.
if assoc_type_arg.param_list().is_some() {
// We currently ignore associated return type bounds.
continue;
@@ -3228,3 +3230,33 @@ enum ArgumentType {
Format(FormatTrait),
Usize,
}
+
+/// This function find the AST fragment that corresponds to an `AssociatedTypeBinding` in the HIR.
+pub fn hir_assoc_type_binding_to_ast(
+ segment_args: &ast::GenericArgList,
+ binding_idx: u32,
+) -> Option<ast::AssocTypeArg> {
+ segment_args
+ .generic_args()
+ .filter_map(|arg| match arg {
+ ast::GenericArg::AssocTypeArg(it) => Some(it),
+ _ => None,
+ })
+ .filter(|binding| binding.param_list().is_none() && binding.name_ref().is_some())
+ .nth(binding_idx as usize)
+}
+
+/// This function find the AST generic argument from the one in the HIR. Does not support the `Self` argument.
+pub fn hir_generic_arg_to_ast(
+ args: &ast::GenericArgList,
+ arg_idx: u32,
+ has_self_arg: bool,
+) -> Option<ast::GenericArg> {
+ args.generic_args()
+ .filter(|arg| match arg {
+ ast::GenericArg::AssocTypeArg(_) => false,
+ ast::GenericArg::LifetimeArg(arg) => arg.lifetime().is_some(),
+ ast::GenericArg::ConstArg(_) | ast::GenericArg::TypeArg(_) => true,
+ })
+ .nth(arg_idx as usize - has_self_arg as usize)
+}