aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2020-04-10 21:05:46 +0100
committerFlorian Diebold <[email protected]>2020-04-13 14:07:39 +0100
commitdb32a2e4211f9444ef4f10b633e400d27ed2662e (patch)
tree00095f637d1775f0cce68d7c0bd6a01bae63e5de /crates
parentc388130f5ffbcbe7d3131213a24d12d02f769b87 (diff)
Implement inline associated type bounds
Like `Iterator<Item: SomeTrait>`. This is an unstable feature, but it's used in the standard library e.g. in the definition of Flatten, so we can't get away with not implementing it :)
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_def/src/data.rs8
-rw-r--r--crates/ra_hir_def/src/path.rs21
-rw-r--r--crates/ra_hir_def/src/path/lower.rs21
-rw-r--r--crates/ra_hir_def/src/type_ref.rs12
-rw-r--r--crates/ra_hir_ty/src/lower.rs28
-rw-r--r--crates/ra_hir_ty/src/tests/traits.rs47
6 files changed, 120 insertions, 17 deletions
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs
index 56a20c5bd..b3c91fea2 100644
--- a/crates/ra_hir_def/src/data.rs
+++ b/crates/ra_hir_def/src/data.rs
@@ -15,7 +15,7 @@ use ra_syntax::ast::{
15use crate::{ 15use crate::{
16 attr::Attrs, 16 attr::Attrs,
17 db::DefDatabase, 17 db::DefDatabase,
18 path::{path, GenericArgs, Path}, 18 path::{path, AssociatedTypeBinding, GenericArgs, Path},
19 src::HasSource, 19 src::HasSource,
20 type_ref::{Mutability, TypeBound, TypeRef}, 20 type_ref::{Mutability, TypeBound, TypeRef},
21 visibility::RawVisibility, 21 visibility::RawVisibility,
@@ -95,7 +95,11 @@ fn desugar_future_path(orig: TypeRef) -> Path {
95 let path = path![std::future::Future]; 95 let path = path![std::future::Future];
96 let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect(); 96 let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect();
97 let mut last = GenericArgs::empty(); 97 let mut last = GenericArgs::empty();
98 last.bindings.push((name![Output], orig)); 98 last.bindings.push(AssociatedTypeBinding {
99 name: name![Output],
100 type_ref: Some(orig),
101 bounds: Vec::new(),
102 });
99 generic_args.push(Some(Arc::new(last))); 103 generic_args.push(Some(Arc::new(last)));
100 104
101 Path::from_known_path(path, generic_args) 105 Path::from_known_path(path, generic_args)
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs
index 91c7b3e09..162b3c8c7 100644
--- a/crates/ra_hir_def/src/path.rs
+++ b/crates/ra_hir_def/src/path.rs
@@ -14,7 +14,10 @@ use hir_expand::{
14use ra_db::CrateId; 14use ra_db::CrateId;
15use ra_syntax::ast; 15use ra_syntax::ast;
16 16
17use crate::{type_ref::TypeRef, InFile}; 17use crate::{
18 type_ref::{TypeBound, TypeRef},
19 InFile,
20};
18 21
19#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] 22#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
20pub struct ModPath { 23pub struct ModPath {
@@ -111,7 +114,21 @@ pub struct GenericArgs {
111 /// is left out. 114 /// is left out.
112 pub has_self_type: bool, 115 pub has_self_type: bool,
113 /// Associated type bindings like in `Iterator<Item = T>`. 116 /// Associated type bindings like in `Iterator<Item = T>`.
114 pub bindings: Vec<(Name, TypeRef)>, 117 pub bindings: Vec<AssociatedTypeBinding>,
118}
119
120/// An associated type binding like in `Iterator<Item = T>`.
121#[derive(Debug, Clone, PartialEq, Eq, Hash)]
122pub struct AssociatedTypeBinding {
123 /// The name of the associated type.
124 pub name: Name,
125 /// The type bound to this associated type (in `Item = T`, this would be the
126 /// `T`). This can be `None` if there are bounds instead.
127 pub type_ref: Option<TypeRef>,
128 /// Bounds for the associated type, like in `Iterator<Item:
129 /// SomeOtherTrait>`. (This is the unstable `associated_type_bounds`
130 /// feature.)
131 pub bounds: Vec<TypeBound>,
115} 132}
116 133
117/// A single generic argument. 134/// A single generic argument.
diff --git a/crates/ra_hir_def/src/path/lower.rs b/crates/ra_hir_def/src/path/lower.rs
index 0f806d6fb..9ec2e0dcd 100644
--- a/crates/ra_hir_def/src/path/lower.rs
+++ b/crates/ra_hir_def/src/path/lower.rs
@@ -9,11 +9,12 @@ use hir_expand::{
9 hygiene::Hygiene, 9 hygiene::Hygiene,
10 name::{name, AsName}, 10 name::{name, AsName},
11}; 11};
12use ra_syntax::ast::{self, AstNode, TypeAscriptionOwner}; 12use ra_syntax::ast::{self, AstNode, TypeAscriptionOwner, TypeBoundsOwner};
13 13
14use super::AssociatedTypeBinding;
14use crate::{ 15use crate::{
15 path::{GenericArg, GenericArgs, ModPath, Path, PathKind}, 16 path::{GenericArg, GenericArgs, ModPath, Path, PathKind},
16 type_ref::TypeRef, 17 type_ref::{TypeBound, TypeRef},
17}; 18};
18 19
19pub(super) use lower_use::lower_use_tree; 20pub(super) use lower_use::lower_use_tree;
@@ -136,10 +137,16 @@ pub(super) fn lower_generic_args(node: ast::TypeArgList) -> Option<GenericArgs>
136 // lifetimes ignored for now 137 // lifetimes ignored for now
137 let mut bindings = Vec::new(); 138 let mut bindings = Vec::new();
138 for assoc_type_arg in node.assoc_type_args() { 139 for assoc_type_arg in node.assoc_type_args() {
140 let assoc_type_arg: ast::AssocTypeArg = assoc_type_arg;
139 if let Some(name_ref) = assoc_type_arg.name_ref() { 141 if let Some(name_ref) = assoc_type_arg.name_ref() {
140 let name = name_ref.as_name(); 142 let name = name_ref.as_name();
141 let type_ref = TypeRef::from_ast_opt(assoc_type_arg.type_ref()); 143 let type_ref = assoc_type_arg.type_ref().map(TypeRef::from_ast);
142 bindings.push((name, type_ref)); 144 let bounds = if let Some(l) = assoc_type_arg.type_bound_list() {
145 l.bounds().map(TypeBound::from_ast).collect()
146 } else {
147 Vec::new()
148 };
149 bindings.push(AssociatedTypeBinding { name, type_ref, bounds });
143 } 150 }
144 } 151 }
145 if args.is_empty() && bindings.is_empty() { 152 if args.is_empty() && bindings.is_empty() {
@@ -168,7 +175,11 @@ fn lower_generic_args_from_fn_path(
168 } 175 }
169 if let Some(ret_type) = ret_type { 176 if let Some(ret_type) = ret_type {
170 let type_ref = TypeRef::from_ast_opt(ret_type.type_ref()); 177 let type_ref = TypeRef::from_ast_opt(ret_type.type_ref());
171 bindings.push((name![Output], type_ref)) 178 bindings.push(AssociatedTypeBinding {
179 name: name![Output],
180 type_ref: Some(type_ref),
181 bounds: Vec::new(),
182 });
172 } 183 }
173 if args.is_empty() && bindings.is_empty() { 184 if args.is_empty() && bindings.is_empty() {
174 None 185 None
diff --git a/crates/ra_hir_def/src/type_ref.rs b/crates/ra_hir_def/src/type_ref.rs
index ea29c4176..f308c6bdf 100644
--- a/crates/ra_hir_def/src/type_ref.rs
+++ b/crates/ra_hir_def/src/type_ref.rs
@@ -163,8 +163,16 @@ impl TypeRef {
163 let crate::path::GenericArg::Type(type_ref) = arg; 163 let crate::path::GenericArg::Type(type_ref) = arg;
164 go(type_ref, f); 164 go(type_ref, f);
165 } 165 }
166 for (_, type_ref) in &args_and_bindings.bindings { 166 for binding in &args_and_bindings.bindings {
167 go(type_ref, f); 167 if let Some(type_ref) = &binding.type_ref {
168 go(type_ref, f);
169 }
170 for bound in &binding.bounds {
171 match bound {
172 TypeBound::Path(path) => go_path(path, f),
173 TypeBound::Error => (),
174 }
175 }
168 } 176 }
169 } 177 }
170 } 178 }
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs
index 6c7bbc448..21e7baf8b 100644
--- a/crates/ra_hir_ty/src/lower.rs
+++ b/crates/ra_hir_ty/src/lower.rs
@@ -8,6 +8,8 @@
8use std::iter; 8use std::iter;
9use std::sync::Arc; 9use std::sync::Arc;
10 10
11use smallvec::SmallVec;
12
11use hir_def::{ 13use hir_def::{
12 adt::StructKind, 14 adt::StructKind,
13 builtin_type::BuiltinType, 15 builtin_type::BuiltinType,
@@ -596,21 +598,35 @@ fn assoc_type_bindings_from_type_bound<'a>(
596 .into_iter() 598 .into_iter()
597 .flat_map(|segment| segment.args_and_bindings.into_iter()) 599 .flat_map(|segment| segment.args_and_bindings.into_iter())
598 .flat_map(|args_and_bindings| args_and_bindings.bindings.iter()) 600 .flat_map(|args_and_bindings| args_and_bindings.bindings.iter())
599 .map(move |(name, type_ref)| { 601 .flat_map(move |binding| {
600 let associated_ty = associated_type_by_name_including_super_traits( 602 let associated_ty = associated_type_by_name_including_super_traits(
601 ctx.db.upcast(), 603 ctx.db.upcast(),
602 trait_ref.trait_, 604 trait_ref.trait_,
603 &name, 605 &binding.name,
604 ); 606 );
605 let associated_ty = match associated_ty { 607 let associated_ty = match associated_ty {
606 None => return GenericPredicate::Error, 608 None => return SmallVec::<[GenericPredicate; 1]>::new(),
607 Some(t) => t, 609 Some(t) => t,
608 }; 610 };
609 let projection_ty = 611 let projection_ty =
610 ProjectionTy { associated_ty, parameters: trait_ref.substs.clone() }; 612 ProjectionTy { associated_ty, parameters: trait_ref.substs.clone() };
611 let ty = Ty::from_hir(ctx, type_ref); 613 let mut preds = SmallVec::with_capacity(
612 let projection_predicate = ProjectionPredicate { projection_ty, ty }; 614 binding.type_ref.as_ref().map_or(0, |_| 1) + binding.bounds.len(),
613 GenericPredicate::Projection(projection_predicate) 615 );
616 if let Some(type_ref) = &binding.type_ref {
617 let ty = Ty::from_hir(ctx, type_ref);
618 let projection_predicate =
619 ProjectionPredicate { projection_ty: projection_ty.clone(), ty };
620 preds.push(GenericPredicate::Projection(projection_predicate));
621 }
622 for bound in &binding.bounds {
623 preds.extend(GenericPredicate::from_type_bound(
624 ctx,
625 bound,
626 Ty::Projection(projection_ty.clone()),
627 ));
628 }
629 preds
614 }) 630 })
615} 631}
616 632
diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs
index 22ae6ca90..53461bdbb 100644
--- a/crates/ra_hir_ty/src/tests/traits.rs
+++ b/crates/ra_hir_ty/src/tests/traits.rs
@@ -1924,6 +1924,53 @@ fn test<T, U>() where T: Trait<U::Item>, U: Trait<T::Item> {
1924} 1924}
1925 1925
1926#[test] 1926#[test]
1927fn inline_assoc_type_bounds_1() {
1928 let t = type_at(
1929 r#"
1930//- /main.rs
1931trait Iterator {
1932 type Item;
1933}
1934trait OtherTrait<T> {
1935 fn foo(&self) -> T;
1936}
1937
1938// workaround for Chalk assoc type normalization problems
1939pub struct S<T>;
1940impl<T: Iterator> Iterator for S<T> {
1941 type Item = <T as Iterator>::Item;
1942}
1943
1944fn test<I: Iterator<Item: OtherTrait<u32>>>() {
1945 let x: <S<I> as Iterator>::Item;
1946 x.foo()<|>;
1947}
1948"#,
1949 );
1950 assert_eq!(t, "u32");
1951}
1952
1953#[test]
1954fn inline_assoc_type_bounds_2() {
1955 let t = type_at(
1956 r#"
1957//- /main.rs
1958trait Iterator {
1959 type Item;
1960}
1961
1962fn test<I: Iterator<Item: Iterator<Item = u32>>>() {
1963 let x: <<I as Iterator>::Item as Iterator>::Item;
1964 x<|>;
1965}
1966"#,
1967 );
1968 // assert_eq!(t, "u32");
1969 // doesn't currently work, Chalk #234
1970 assert_eq!(t, "{unknown}");
1971}
1972
1973#[test]
1927fn unify_impl_trait() { 1974fn unify_impl_trait() {
1928 assert_snapshot!( 1975 assert_snapshot!(
1929 infer_with_mismatches(r#" 1976 infer_with_mismatches(r#"