]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_def/src/item_tree/lower.rs
Make some functions non-generic
[rust.git] / crates / hir_def / src / item_tree / lower.rs
index cfda7cb321cdb1a3afa21be139f5c840830b28a5..2926f32b63018fba53eadceb80725eb889e258d5 100644 (file)
@@ -4,13 +4,13 @@
 
 use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, name::known, HirFileId};
 use syntax::{
-    ast::{self, ModuleItemOwner},
+    ast::{self, HasModuleItem},
     SyntaxNode, WalkEvent,
 };
 
 use crate::{
     generics::{GenericParams, TypeParamData, TypeParamProvenance},
-    type_ref::{LifetimeRef, TraitRef},
+    type_ref::{LifetimeRef, TraitBoundModifier, TraitRef},
 };
 
 use super::*;
@@ -40,7 +40,7 @@ pub(super) fn new(db: &'a dyn DefDatabase, hygiene: Hygiene, file: HirFileId) ->
         }
     }
 
-    pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree {
+    pub(super) fn lower_module_items(mut self, item_owner: &dyn HasModuleItem) -> ItemTree {
         self.tree.top_level =
             item_owner.items().flat_map(|item| self.lower_mod_item(&item, false)).collect();
         self.tree
@@ -51,6 +51,15 @@ pub(super) fn lower_macro_stmts(mut self, stmts: ast::MacroStmts) -> ItemTree {
             .statements()
             .filter_map(|stmt| match stmt {
                 ast::Stmt::Item(item) => Some(item),
+                // Macro calls can be both items and expressions. The syntax library always treats
+                // them as expressions here, so we undo that.
+                ast::Stmt::ExprStmt(es) => match es.expr()? {
+                    ast::Expr::MacroCall(call) => {
+                        cov_mark::hit!(macro_call_in_macro_stmts_is_added_to_item_tree);
+                        Some(call.into())
+                    }
+                    _ => None,
+                },
                 _ => None,
             })
             .flat_map(|item| self.lower_mod_item(&item, false))
@@ -255,8 +264,7 @@ fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field {
         let name = Name::new_tuple_field(idx);
         let visibility = self.lower_visibility(field);
         let type_ref = self.lower_type_ref_opt(field.ty());
-        let res = Field { name, type_ref, visibility };
-        res
+        Field { name, type_ref, visibility }
     }
 
     fn lower_union(&mut self, union: &ast::Union) -> Option<FileItemTreeId<Union>> {
@@ -360,7 +368,7 @@ fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>>
         let (ret_type, async_ret_type) = if func.async_token().is_some() {
             let async_ret_type = ret_type.clone();
             let future_impl = desugar_future_path(ret_type);
-            let ty_bound = Interned::new(TypeBound::Path(future_impl));
+            let ty_bound = Interned::new(TypeBound::Path(future_impl, TraitBoundModifier::None));
             (TypeRef::ImplTrait(vec![ty_bound]), Some(async_ret_type))
         } else {
             (ret_type, None)
@@ -393,7 +401,7 @@ fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>>
         let mut res = Function {
             name,
             visibility,
-            generic_params: Interned::new(GenericParams::default()),
+            explicit_generic_params: Interned::new(GenericParams::default()),
             abi,
             params,
             ret_type: Interned::new(ret_type),
@@ -401,7 +409,8 @@ fn lower_function(&mut self, func: &ast::Fn) -> Option<FileItemTreeId<Function>>
             ast_id,
             flags,
         };
-        res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func);
+        res.explicit_generic_params =
+            self.lower_generic_params(GenericsOwner::Function(&res), func);
 
         Some(id(self.data().functions.alloc(res)))
     }
@@ -439,7 +448,12 @@ fn lower_static(&mut self, static_: &ast::Static) -> Option<FileItemTreeId<Stati
     }
 
     fn lower_const(&mut self, konst: &ast::Const) -> FileItemTreeId<Const> {
-        let name = konst.name().map(|it| it.as_name());
+        let mut name = konst.name().map(|it| it.as_name());
+        if name.as_ref().map_or(false, |n| n.to_string().starts_with("_DERIVE_")) {
+            // FIXME: this is a hack to treat consts generated by synstructure as unnamed
+            // remove this some time in the future
+            name = None;
+        }
         let type_ref = self.lower_type_ref_opt(konst.ty());
         let visibility = self.lower_visibility(konst);
         let ast_id = self.source_ast_id_map.ast_id(konst);
@@ -560,8 +574,8 @@ fn lower_extern_crate(
     fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option<FileItemTreeId<MacroCall>> {
         let path = Interned::new(ModPath::from_src(self.db, m.path()?, &self.hygiene)?);
         let ast_id = self.source_ast_id_map.ast_id(m);
-        let fragment = hir_expand::to_fragment_kind(m);
-        let res = MacroCall { path, ast_id, fragment };
+        let expand_to = hir_expand::ExpandTo::from_call_site(m);
+        let res = MacroCall { path, ast_id, expand_to };
         Some(id(self.data().macro_calls.alloc(res)))
     }
 
@@ -631,7 +645,7 @@ fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> FileItemTreeId<Ext
     fn lower_generic_params_and_inner_items(
         &mut self,
         owner: GenericsOwner<'_>,
-        node: &impl ast::GenericParamsOwner,
+        node: &dyn ast::HasGenericParams,
     ) -> Interned<GenericParams> {
         // Generics are part of item headers and may contain inner items we need to collect.
         if let Some(params) = node.generic_param_list() {
@@ -647,45 +661,35 @@ fn lower_generic_params_and_inner_items(
     fn lower_generic_params(
         &mut self,
         owner: GenericsOwner<'_>,
-        node: &impl ast::GenericParamsOwner,
+        node: &dyn ast::HasGenericParams,
     ) -> Interned<GenericParams> {
-        let mut sm = &mut Default::default();
         let mut generics = GenericParams::default();
         match owner {
-            GenericsOwner::Function(func) => {
-                generics.fill(&self.body_ctx, sm, node);
-                // lower `impl Trait` in arguments
-                for id in func.params.clone() {
-                    if let Param::Normal(ty) = &self.data().params[id] {
-                        generics.fill_implicit_impl_trait_args(ty);
-                    }
-                }
-            }
-            GenericsOwner::Struct
+            GenericsOwner::Function(_)
+            | GenericsOwner::Struct
             | GenericsOwner::Enum
             | GenericsOwner::Union
             | GenericsOwner::TypeAlias => {
-                generics.fill(&self.body_ctx, sm, node);
+                generics.fill(&self.body_ctx, node);
             }
             GenericsOwner::Trait(trait_def) => {
                 // traits get the Self type as an implicit first type parameter
-                let self_param_id = generics.types.alloc(TypeParamData {
+                generics.types.alloc(TypeParamData {
                     name: Some(name![Self]),
                     default: None,
                     provenance: TypeParamProvenance::TraitSelf,
                 });
-                sm.type_params.insert(self_param_id, Either::Left(trait_def.clone()));
                 // add super traits as bounds on Self
                 // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar
                 let self_param = TypeRef::Path(name![Self].into());
                 generics.fill_bounds(&self.body_ctx, trait_def, Either::Left(self_param));
-                generics.fill(&self.body_ctx, &mut sm, node);
+                generics.fill(&self.body_ctx, node);
             }
             GenericsOwner::Impl => {
                 // Note that we don't add `Self` here: in `impl`s, `Self` is not a
                 // type-parameter, but rather is a type-alias for impl's target
                 // type, so this is handled by the resolver.
-                generics.fill(&self.body_ctx, &mut sm, node);
+                generics.fill(&self.body_ctx, node);
             }
         }
 
@@ -693,7 +697,7 @@ fn lower_generic_params(
         Interned::new(generics)
     }
 
-    fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec<Interned<TypeBound>> {
+    fn lower_type_bounds(&mut self, node: &dyn ast::HasTypeBounds) -> Vec<Interned<TypeBound>> {
         match node.type_bound_list() {
             Some(bound_list) => bound_list
                 .bounds()
@@ -703,7 +707,7 @@ fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec<Interne
         }
     }
 
-    fn lower_visibility(&mut self, item: &impl ast::VisibilityOwner) -> RawVisibilityId {
+    fn lower_visibility(&mut self, item: &dyn ast::HasVisibility) -> RawVisibilityId {
         let vis = match self.forced_visibility {
             Some(vis) => return vis,
             None => RawVisibility::from_ast_with_hygiene(self.db, item.visibility(), &self.hygiene),
@@ -786,44 +790,44 @@ enum GenericsOwner<'a> {
 
 /// Returns `true` if the given intrinsic is unsafe to call, or false otherwise.
 fn is_intrinsic_fn_unsafe(name: &Name) -> bool {
-    // Should be kept in sync with https://github.com/rust-lang/rust/blob/c6e4db620a7d2f569f11dcab627430921ea8aacf/compiler/rustc_typeck/src/check/intrinsic.rs#L68
+    // Should be kept in sync with https://github.com/rust-lang/rust/blob/0cd0709f19d316c4796fa71c5f52c8612a5f3771/compiler/rustc_typeck/src/check/intrinsic.rs#L72-L105
     ![
         known::abort,
-        known::min_align_of,
-        known::needs_drop,
-        known::caller_location,
-        known::size_of_val,
-        known::min_align_of_val,
         known::add_with_overflow,
-        known::sub_with_overflow,
-        known::mul_with_overflow,
-        known::wrapping_add,
-        known::wrapping_sub,
-        known::wrapping_mul,
-        known::saturating_add,
-        known::saturating_sub,
-        known::rotate_left,
-        known::rotate_right,
-        known::ctpop,
+        known::bitreverse,
+        known::bswap,
+        known::caller_location,
         known::ctlz,
+        known::ctpop,
         known::cttz,
-        known::bswap,
-        known::bitreverse,
         known::discriminant_value,
-        known::type_id,
+        known::forget,
         known::likely,
-        known::unlikely,
-        known::ptr_guaranteed_eq,
-        known::ptr_guaranteed_ne,
+        known::maxnumf32,
+        known::maxnumf64,
+        known::min_align_of,
         known::minnumf32,
         known::minnumf64,
-        known::maxnumf32,
+        known::mul_with_overflow,
+        known::needs_drop,
+        known::ptr_guaranteed_eq,
+        known::ptr_guaranteed_ne,
+        known::rotate_left,
+        known::rotate_right,
         known::rustc_peek,
-        known::maxnumf64,
+        known::saturating_add,
+        known::saturating_sub,
+        known::size_of,
+        known::sub_with_overflow,
+        known::type_id,
         known::type_name,
+        known::unlikely,
         known::variant_count,
+        known::wrapping_add,
+        known::wrapping_mul,
+        known::wrapping_sub,
     ]
-    .contains(&name)
+    .contains(name)
 }
 
 fn lower_abi(abi: ast::Abi) -> Interned<str> {
@@ -855,7 +859,7 @@ fn lower_use_tree(&mut self, tree: ast::UseTree) -> Option<UseTree> {
                 // E.g. `use something::{inner}` (prefix is `None`, path is `something`)
                 // or `use something::{path::{inner::{innerer}}}` (prefix is `something::path`, path is `inner`)
                 Some(path) => {
-                    match ModPath::from_src(self.db, path, &self.hygiene) {
+                    match ModPath::from_src(self.db, path, self.hygiene) {
                         Some(it) => Some(it),
                         None => return None, // FIXME: report errors somewhere
                     }
@@ -874,7 +878,7 @@ fn lower_use_tree(&mut self, tree: ast::UseTree) -> Option<UseTree> {
         } else {
             let is_glob = tree.star_token().is_some();
             let path = match tree.path() {
-                Some(path) => Some(ModPath::from_src(self.db, path, &self.hygiene)?),
+                Some(path) => Some(ModPath::from_src(self.db, path, self.hygiene)?),
                 None => None,
             };
             let alias = tree.rename().map(|a| {