diff options
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 2ea3b341f..e659f903c 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -89,13 +89,13 @@ pub enum Ty { | |||
89 | /// fn foo() -> i32 { 1 } | 89 | /// fn foo() -> i32 { 1 } |
90 | /// let bar: fn() -> i32 = foo; | 90 | /// let bar: fn() -> i32 = foo; |
91 | /// ``` | 91 | /// ``` |
92 | FnPtr(FnSig), | 92 | FnPtr(Substs), |
93 | 93 | ||
94 | /// The never type `!`. | 94 | /// The never type `!`. |
95 | Never, | 95 | Never, |
96 | 96 | ||
97 | /// A tuple type. For example, `(i32, bool)`. | 97 | /// A tuple type. For example, `(i32, bool)`. |
98 | Tuple(Arc<[Ty]>), | 98 | Tuple(Substs), |
99 | 99 | ||
100 | /// A type parameter; for example, `T` in `fn f<T>(x: T) {} | 100 | /// A type parameter; for example, `T` in `fn f<T>(x: T) {} |
101 | Param { | 101 | Param { |
@@ -127,6 +127,10 @@ impl Substs { | |||
127 | Substs(Arc::new([])) | 127 | Substs(Arc::new([])) |
128 | } | 128 | } |
129 | 129 | ||
130 | pub fn iter(&self) -> impl Iterator<Item = &Ty> { | ||
131 | self.0.iter() | ||
132 | } | ||
133 | |||
130 | pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { | 134 | pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { |
131 | // Without an Arc::make_mut_slice, we can't avoid the clone here: | 135 | // Without an Arc::make_mut_slice, we can't avoid the clone here: |
132 | let mut v: Vec<_> = self.0.iter().cloned().collect(); | 136 | let mut v: Vec<_> = self.0.iter().cloned().collect(); |
@@ -148,6 +152,11 @@ impl FnSig { | |||
148 | params.push(ret); | 152 | params.push(ret); |
149 | FnSig { params_and_return: params.into() } | 153 | FnSig { params_and_return: params.into() } |
150 | } | 154 | } |
155 | |||
156 | pub fn from_fn_ptr_substs(substs: &Substs) -> FnSig { | ||
157 | FnSig { params_and_return: Arc::clone(&substs.0) } | ||
158 | } | ||
159 | |||
151 | pub fn params(&self) -> &[Ty] { | 160 | pub fn params(&self) -> &[Ty] { |
152 | &self.params_and_return[0..self.params_and_return.len() - 1] | 161 | &self.params_and_return[0..self.params_and_return.len() - 1] |
153 | } | 162 | } |
@@ -168,7 +177,7 @@ impl FnSig { | |||
168 | 177 | ||
169 | impl Ty { | 178 | impl Ty { |
170 | pub fn unit() -> Self { | 179 | pub fn unit() -> Self { |
171 | Ty::Tuple(Arc::new([])) | 180 | Ty::Tuple(Substs::empty()) |
172 | } | 181 | } |
173 | 182 | ||
174 | pub fn walk(&self, f: &mut impl FnMut(&Ty)) { | 183 | pub fn walk(&self, f: &mut impl FnMut(&Ty)) { |
@@ -182,10 +191,9 @@ impl Ty { | |||
182 | } | 191 | } |
183 | } | 192 | } |
184 | Ty::FnPtr(sig) => { | 193 | Ty::FnPtr(sig) => { |
185 | for input in sig.params() { | 194 | for t in sig.iter() { |
186 | input.walk(f); | 195 | t.walk(f); |
187 | } | 196 | } |
188 | sig.ret().walk(f); | ||
189 | } | 197 | } |
190 | Ty::FnDef { substs, .. } => { | 198 | Ty::FnDef { substs, .. } => { |
191 | for t in substs.0.iter() { | 199 | for t in substs.0.iter() { |
@@ -216,12 +224,7 @@ impl Ty { | |||
216 | Ty::RawPtr(t, _) => Arc::make_mut(t).walk_mut(f), | 224 | Ty::RawPtr(t, _) => Arc::make_mut(t).walk_mut(f), |
217 | Ty::Ref(t, _) => Arc::make_mut(t).walk_mut(f), | 225 | Ty::Ref(t, _) => Arc::make_mut(t).walk_mut(f), |
218 | Ty::Tuple(ts) => { | 226 | Ty::Tuple(ts) => { |
219 | // Without an Arc::make_mut_slice, we can't avoid the clone here: | 227 | ts.walk_mut(f); |
220 | let mut v: Vec<_> = ts.iter().cloned().collect(); | ||
221 | for t in &mut v { | ||
222 | t.walk_mut(f); | ||
223 | } | ||
224 | *ts = v.into(); | ||
225 | } | 228 | } |
226 | Ty::FnPtr(sig) => { | 229 | Ty::FnPtr(sig) => { |
227 | sig.walk_mut(f); | 230 | sig.walk_mut(f); |
@@ -324,15 +327,16 @@ impl HirDisplay for Ty { | |||
324 | } | 327 | } |
325 | Ty::Never => write!(f, "!")?, | 328 | Ty::Never => write!(f, "!")?, |
326 | Ty::Tuple(ts) => { | 329 | Ty::Tuple(ts) => { |
327 | if ts.len() == 1 { | 330 | if ts.0.len() == 1 { |
328 | write!(f, "({},)", ts[0].display(f.db))?; | 331 | write!(f, "({},)", ts.0[0].display(f.db))?; |
329 | } else { | 332 | } else { |
330 | write!(f, "(")?; | 333 | write!(f, "(")?; |
331 | f.write_joined(&**ts, ", ")?; | 334 | f.write_joined(&*ts.0, ", ")?; |
332 | write!(f, ")")?; | 335 | write!(f, ")")?; |
333 | } | 336 | } |
334 | } | 337 | } |
335 | Ty::FnPtr(sig) => { | 338 | Ty::FnPtr(sig) => { |
339 | let sig = FnSig::from_fn_ptr_substs(sig); | ||
336 | write!(f, "fn(")?; | 340 | write!(f, "fn(")?; |
337 | f.write_joined(sig.params(), ", ")?; | 341 | f.write_joined(sig.params(), ", ")?; |
338 | write!(f, ") -> {}", sig.ret().display(f.db))?; | 342 | write!(f, ") -> {}", sig.ret().display(f.db))?; |