aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/generics.rs11
-rw-r--r--crates/ra_hir/src/ty/lower.rs4
-rw-r--r--crates/ra_hir/src/ty/tests.rs24
-rw-r--r--crates/ra_syntax/src/ast/generated.rs7
-rw-r--r--crates/ra_syntax/src/ast/traits.rs6
-rw-r--r--crates/ra_syntax/src/grammar.ron5
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 @@
5 5
6use std::sync::Arc; 6use std::sync::Arc;
7 7
8use hir_def::{ 8use hir_def::type_ref::{TypeBound, TypeRef};
9 path::Path,
10 type_ref::{TypeBound, TypeRef},
11};
12use hir_expand::name::{self, AsName}; 9use hir_expand::name::{self, AsName};
13use ra_syntax::ast::{self, DefaultTypeParamOwner, NameOwner, TypeBoundsOwner, TypeParamsOwner}; 10use ra_syntax::ast::{self, NameOwner, TypeBoundsOwner, TypeParamsOwner};
14 11
15use crate::{ 12use crate::{
16 db::{AstDatabase, DefDatabase, HirDatabase}, 13 db::{AstDatabase, DefDatabase, HirDatabase},
@@ -24,7 +21,7 @@ pub struct GenericParam {
24 // FIXME: give generic params proper IDs 21 // FIXME: give generic params proper IDs
25 pub idx: u32, 22 pub idx: u32,
26 pub name: Name, 23 pub name: Name,
27 pub default: Option<Path>, 24 pub default: Option<TypeRef>,
28} 25}
29 26
30/// Data about the generic parameters of a function, struct, impl, etc. 27/// Data about the generic parameters of a function, struct, impl, etc.
@@ -140,7 +137,7 @@ impl GenericParams {
140 for (idx, type_param) in params.type_params().enumerate() { 137 for (idx, type_param) in params.type_params().enumerate() {
141 let name = type_param.name().map_or_else(Name::missing, |it| it.as_name()); 138 let name = type_param.name().map_or_else(Name::missing, |it| it.as_name());
142 // FIXME: Use `Path::from_src` 139 // FIXME: Use `Path::from_src`
143 let default = type_param.default_type().and_then(|t| t.path()).and_then(Path::from_ast); 140 let default = type_param.default_type().map(TypeRef::from_ast);
144 141
145 let param = GenericParam { idx: idx as u32 + start, name: name.clone(), default }; 142 let param = GenericParam { idx: idx as u32 + start, name: name.clone(), default };
146 self.params.push(param); 143 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) ->
611 let defaults = generic_params 611 let defaults = generic_params
612 .params_including_parent() 612 .params_including_parent()
613 .into_iter() 613 .into_iter()
614 .map(|p| { 614 .map(|p| p.default.as_ref().map_or(Ty::Unknown, |t| Ty::from_hir(db, &resolver, t)))
615 p.default.as_ref().map_or(Ty::Unknown, |path| Ty::from_hir_path(db, &resolver, path))
616 })
617 .collect(); 615 .collect();
618 616
619 Substs(defaults) 617 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
@@ -1980,6 +1980,30 @@ fn test() {
1980} 1980}
1981 1981
1982#[test] 1982#[test]
1983fn infer_associated_method_generics_with_default_tuple_param() {
1984 let t = type_at(
1985 r#"
1986//- /main.rs
1987struct Gen<T=()> {
1988 val: T
1989}
1990
1991impl<T> Gen<T> {
1992 pub fn make() -> Gen<T> {
1993 loop { }
1994 }
1995}
1996
1997fn test() {
1998 let a = Gen::make();
1999 a.val<|>;
2000}
2001"#,
2002 );
2003 assert_eq!(t, "()");
2004}
2005
2006#[test]
1983fn infer_associated_method_generics_without_args() { 2007fn infer_associated_method_generics_without_args() {
1984 assert_snapshot!( 2008 assert_snapshot!(
1985 infer(r#" 2009 infer(r#"
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 {
3625impl ast::NameOwner for TypeParam {} 3625impl ast::NameOwner for TypeParam {}
3626impl ast::AttrsOwner for TypeParam {} 3626impl ast::AttrsOwner for TypeParam {}
3627impl ast::TypeBoundsOwner for TypeParam {} 3627impl ast::TypeBoundsOwner for TypeParam {}
3628impl ast::DefaultTypeParamOwner for TypeParam {} 3628impl TypeParam {
3629impl TypeParam {} 3629 pub fn default_type(&self) -> Option<TypeRef> {
3630 AstChildren::new(&self.syntax).next()
3631 }
3632}
3630#[derive(Debug, Clone, PartialEq, Eq, Hash)] 3633#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3631pub struct TypeParamList { 3634pub struct TypeParamList {
3632 pub(crate) syntax: SyntaxNode, 3635 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 {
163 self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast)) 163 self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast))
164 } 164 }
165} 165}
166
167pub trait DefaultTypeParamOwner: AstNode {
168 fn default_type(&self) -> Option<ast::PathType> {
169 child_opt(self)
170 }
171}
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(
587 ("lifetime_params", "LifetimeParam" ), 587 ("lifetime_params", "LifetimeParam" ),
588 ] 588 ]
589 ), 589 ),
590 "TypeParam": ( traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner", "DefaultTypeParamOwner"] ), 590 "TypeParam": (
591 options: [("default_type", "TypeRef")],
592 traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner"],
593 ),
591 "LifetimeParam": ( 594 "LifetimeParam": (
592 traits: ["AttrsOwner"], 595 traits: ["AttrsOwner"],
593 ), 596 ),