From 0e771915faf057ec4561224b75ec9b5be93d71c8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Nov 2019 11:42:58 +0300 Subject: Allow non-path default type parameters --- crates/ra_hir/src/generics.rs | 11 ++++------- crates/ra_hir/src/ty/lower.rs | 4 +--- crates/ra_hir/src/ty/tests.rs | 24 ++++++++++++++++++++++++ crates/ra_syntax/src/ast/generated.rs | 7 +++++-- crates/ra_syntax/src/ast/traits.rs | 6 ------ crates/ra_syntax/src/grammar.ron | 5 ++++- 6 files changed, 38 insertions(+), 19 deletions(-) diff --git a/crates/ra_hir/src/generics.rs b/crates/ra_hir/src/generics.rs index 8925ba3a9..78fab1a13 100644 --- a/crates/ra_hir/src/generics.rs +++ b/crates/ra_hir/src/generics.rs @@ -5,12 +5,9 @@ use std::sync::Arc; -use hir_def::{ - path::Path, - type_ref::{TypeBound, TypeRef}, -}; +use hir_def::type_ref::{TypeBound, TypeRef}; use hir_expand::name::{self, AsName}; -use ra_syntax::ast::{self, DefaultTypeParamOwner, NameOwner, TypeBoundsOwner, TypeParamsOwner}; +use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner}; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, @@ -24,7 +21,7 @@ pub struct GenericParam { // FIXME: give generic params proper IDs pub idx: u32, pub name: Name, - pub default: Option, + pub default: Option, } /// Data about the generic parameters of a function, struct, impl, etc. @@ -140,7 +137,7 @@ impl GenericParams { for (idx, type_param) in params.type_params().enumerate() { let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); // FIXME: Use `Path::from_src` - let default = type_param.default_type().and_then(|t| t.path()).and_then(Path::from_ast); + let default = type_param.default_type().map(TypeRef::from_ast); let param = GenericParam { idx: idx as u32 + start, name: name.clone(), default }; self.params.push(param); diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index de3c56097..03db38605 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -611,9 +611,7 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) -> let defaults = generic_params .params_including_parent() .into_iter() - .map(|p| { - p.default.as_ref().map_or(Ty::Unknown, |path| Ty::from_hir_path(db, &resolver, path)) - }) + .map(|p| p.default.as_ref().map_or(Ty::Unknown, |t| Ty::from_hir(db, &resolver, t))) .collect(); Substs(defaults) diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index c1024d03c..abfaffb5e 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -1979,6 +1979,30 @@ fn test() { ); } +#[test] +fn infer_associated_method_generics_with_default_tuple_param() { + let t = type_at( + r#" +//- /main.rs +struct Gen { + val: T +} + +impl Gen { + pub fn make() -> Gen { + loop { } + } +} + +fn test() { + let a = Gen::make(); + a.val<|>; +} +"#, + ); + assert_eq!(t, "()"); +} + #[test] fn infer_associated_method_generics_without_args() { assert_snapshot!( diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 2b381dcdb..de506d7cd 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -3625,8 +3625,11 @@ impl AstNode for TypeParam { impl ast::NameOwner for TypeParam {} impl ast::AttrsOwner for TypeParam {} impl ast::TypeBoundsOwner for TypeParam {} -impl ast::DefaultTypeParamOwner for TypeParam {} -impl TypeParam {} +impl TypeParam { + pub fn default_type(&self) -> Option { + AstChildren::new(&self.syntax).next() + } +} #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TypeParamList { pub(crate) syntax: SyntaxNode, diff --git a/crates/ra_syntax/src/ast/traits.rs b/crates/ra_syntax/src/ast/traits.rs index c2b005886..f99984fe0 100644 --- a/crates/ra_syntax/src/ast/traits.rs +++ b/crates/ra_syntax/src/ast/traits.rs @@ -163,9 +163,3 @@ impl Iterator for CommentIter { self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast)) } } - -pub trait DefaultTypeParamOwner: AstNode { - fn default_type(&self) -> Option { - child_opt(self) - } -} diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 70d85a8e6..88d1dc109 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -587,7 +587,10 @@ Grammar( ("lifetime_params", "LifetimeParam" ), ] ), - "TypeParam": ( traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner", "DefaultTypeParamOwner"] ), + "TypeParam": ( + options: [("default_type", "TypeRef")], + traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner"], + ), "LifetimeParam": ( traits: ["AttrsOwner"], ), -- cgit v1.2.3