]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_def/src/type_ref.rs
parameters.split_last()
[rust.git] / crates / hir_def / src / type_ref.rs
index dcc43c1fada2248b9b367a2b6129df8d199f4393..ee8ef6caa306a1dc72e8db72441741b94a09aa01 100644 (file)
@@ -1,9 +1,12 @@
 //! HIR for references to types. Paths in these are not yet resolved. They can
 //! be directly created from an ast::TypeRef, without further queries.
 
-use hir_expand::{name::Name, AstId, InFile};
+use hir_expand::{
+    name::{AsName, Name},
+    AstId, InFile,
+};
 use std::convert::TryInto;
-use syntax::{ast, AstNode};
+use syntax::ast::{self, HasName};
 
 use crate::{body::LowerCtx, intern::Interned, path::Path};
 
@@ -89,7 +92,7 @@ pub enum TypeRef {
     Array(Box<TypeRef>, ConstScalar),
     Slice(Box<TypeRef>),
     /// A fn pointer. Last element of the vector is the return type.
-    Fn(Vec<(Option<String>, TypeRef)>, bool /*varargs*/),
+    Fn(Vec<(Option<Name>, TypeRef)>, bool /*varargs*/),
     // For
     ImplTrait(Vec<Interned<TypeBound>>),
     DynTrait(Vec<Interned<TypeBound>>),
@@ -189,13 +192,13 @@ pub fn from_ast(ctx: &LowerCtx, node: ast::Type) -> Self {
                     }
 
                     pl.params()
-                        .map(|p| (p.pat(), p.ty()))
                         .map(|it| {
-                            let type_ref = TypeRef::from_ast_opt(ctx, it.1);
-                            let name = if it.0.is_some() {
-                                Some(it.0.unwrap().syntax().text().to_string())
-                            } else {
-                                None
+                            let type_ref = TypeRef::from_ast_opt(ctx, it.ty());
+                            let name = match it.pat() {
+                                Some(ast::Pat::IdentPat(it)) => Some(
+                                    it.name().map(|nr| nr.as_name()).unwrap_or_else(Name::missing),
+                                ),
+                                _ => None,
                             };
                             (name, type_ref)
                         })
@@ -241,7 +244,9 @@ pub fn walk(&self, f: &mut impl FnMut(&TypeRef)) {
         fn go(type_ref: &TypeRef, f: &mut impl FnMut(&TypeRef)) {
             f(type_ref);
             match type_ref {
-                TypeRef::Fn(types, _) => types.iter().for_each(|t| go(&t.1, f)),
+                TypeRef::Fn(params, _) => {
+                    params.iter().for_each(|(_, param_type)| go(&param_type, f))
+                }
                 TypeRef::Tuple(types) => types.iter().for_each(|t| go(t, f)),
                 TypeRef::RawPtr(type_ref, _)
                 | TypeRef::Reference(type_ref, ..)