diff options
21 files changed, 1058 insertions, 212 deletions
diff --git a/crates/hir/src/display.rs b/crates/hir/src/display.rs new file mode 100644 index 000000000..44cdcc296 --- /dev/null +++ b/crates/hir/src/display.rs | |||
@@ -0,0 +1,441 @@ | |||
1 | //! HirDisplay implementations for various hir types. | ||
2 | use hir_def::{ | ||
3 | adt::VariantData, | ||
4 | generics::{TypeParamProvenance, WherePredicate, WherePredicateTypeTarget}, | ||
5 | type_ref::{TypeBound, TypeRef}, | ||
6 | AdtId, GenericDefId, | ||
7 | }; | ||
8 | use hir_ty::display::{ | ||
9 | write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError, | ||
10 | HirFormatter, | ||
11 | }; | ||
12 | use syntax::ast::{self, NameOwner}; | ||
13 | |||
14 | use crate::{ | ||
15 | Const, ConstParam, Enum, Field, Function, HasVisibility, Module, Static, Struct, Substs, Trait, | ||
16 | Type, TypeAlias, TypeParam, Union, Variant, | ||
17 | }; | ||
18 | |||
19 | impl HirDisplay for Function { | ||
20 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
21 | let data = f.db.function_data(self.id); | ||
22 | write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; | ||
23 | let qual = &data.qualifier; | ||
24 | if qual.is_default { | ||
25 | write!(f, "default ")?; | ||
26 | } | ||
27 | if qual.is_const { | ||
28 | write!(f, "const ")?; | ||
29 | } | ||
30 | if qual.is_async { | ||
31 | write!(f, "async ")?; | ||
32 | } | ||
33 | if qual.is_unsafe { | ||
34 | write!(f, "unsafe ")?; | ||
35 | } | ||
36 | if let Some(abi) = &qual.abi { | ||
37 | // FIXME: String escape? | ||
38 | write!(f, "extern \"{}\" ", abi)?; | ||
39 | } | ||
40 | write!(f, "fn {}", data.name)?; | ||
41 | |||
42 | write_generic_params(GenericDefId::FunctionId(self.id), f)?; | ||
43 | |||
44 | write!(f, "(")?; | ||
45 | |||
46 | let write_self_param = |ty: &TypeRef, f: &mut HirFormatter| match ty { | ||
47 | TypeRef::Path(p) if p.is_self_type() => write!(f, "self"), | ||
48 | TypeRef::Reference(inner, lifetime, mut_) if matches!(&**inner,TypeRef::Path(p) if p.is_self_type()) => | ||
49 | { | ||
50 | write!(f, "&")?; | ||
51 | if let Some(lifetime) = lifetime { | ||
52 | write!(f, "{} ", lifetime.name)?; | ||
53 | } | ||
54 | if let hir_def::type_ref::Mutability::Mut = mut_ { | ||
55 | write!(f, "mut ")?; | ||
56 | } | ||
57 | write!(f, "self") | ||
58 | } | ||
59 | _ => { | ||
60 | write!(f, "self: ")?; | ||
61 | ty.hir_fmt(f) | ||
62 | } | ||
63 | }; | ||
64 | |||
65 | let mut first = true; | ||
66 | for (param, type_ref) in self.assoc_fn_params(f.db).into_iter().zip(&data.params) { | ||
67 | if !first { | ||
68 | write!(f, ", ")?; | ||
69 | } else { | ||
70 | first = false; | ||
71 | if data.has_self_param { | ||
72 | write_self_param(type_ref, f)?; | ||
73 | continue; | ||
74 | } | ||
75 | } | ||
76 | match param.pattern_source(f.db) { | ||
77 | Some(ast::Pat::IdentPat(p)) if p.name().is_some() => { | ||
78 | write!(f, "{}: ", p.name().unwrap())? | ||
79 | } | ||
80 | _ => write!(f, "_: ")?, | ||
81 | } | ||
82 | // FIXME: Use resolved `param.ty` or raw `type_ref`? | ||
83 | // The former will ignore lifetime arguments currently. | ||
84 | type_ref.hir_fmt(f)?; | ||
85 | } | ||
86 | write!(f, ")")?; | ||
87 | |||
88 | // `FunctionData::ret_type` will be `::core::future::Future<Output = ...>` for async fns. | ||
89 | // Use ugly pattern match to strip the Future trait. | ||
90 | // Better way? | ||
91 | let ret_type = if !qual.is_async { | ||
92 | &data.ret_type | ||
93 | } else { | ||
94 | match &data.ret_type { | ||
95 | TypeRef::ImplTrait(bounds) => match &bounds[0] { | ||
96 | TypeBound::Path(path) => { | ||
97 | path.segments().iter().last().unwrap().args_and_bindings.unwrap().bindings | ||
98 | [0] | ||
99 | .type_ref | ||
100 | .as_ref() | ||
101 | .unwrap() | ||
102 | } | ||
103 | _ => panic!("Async fn ret_type should be impl Future"), | ||
104 | }, | ||
105 | _ => panic!("Async fn ret_type should be impl Future"), | ||
106 | } | ||
107 | }; | ||
108 | |||
109 | match ret_type { | ||
110 | TypeRef::Tuple(tup) if tup.is_empty() => {} | ||
111 | ty => { | ||
112 | write!(f, " -> ")?; | ||
113 | ty.hir_fmt(f)?; | ||
114 | } | ||
115 | } | ||
116 | |||
117 | write_where_clause(GenericDefId::FunctionId(self.id), f)?; | ||
118 | |||
119 | Ok(()) | ||
120 | } | ||
121 | } | ||
122 | |||
123 | impl HirDisplay for Struct { | ||
124 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
125 | write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; | ||
126 | write!(f, "struct ")?; | ||
127 | write!(f, "{}", self.name(f.db))?; | ||
128 | let def_id = GenericDefId::AdtId(AdtId::StructId(self.id)); | ||
129 | write_generic_params(def_id, f)?; | ||
130 | write_where_clause(def_id, f)?; | ||
131 | Ok(()) | ||
132 | } | ||
133 | } | ||
134 | |||
135 | impl HirDisplay for Enum { | ||
136 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
137 | write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; | ||
138 | write!(f, "enum ")?; | ||
139 | write!(f, "{}", self.name(f.db))?; | ||
140 | let def_id = GenericDefId::AdtId(AdtId::EnumId(self.id)); | ||
141 | write_generic_params(def_id, f)?; | ||
142 | write_where_clause(def_id, f)?; | ||
143 | Ok(()) | ||
144 | } | ||
145 | } | ||
146 | |||
147 | impl HirDisplay for Union { | ||
148 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
149 | write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; | ||
150 | write!(f, "union ")?; | ||
151 | write!(f, "{}", self.name(f.db))?; | ||
152 | let def_id = GenericDefId::AdtId(AdtId::UnionId(self.id)); | ||
153 | write_generic_params(def_id, f)?; | ||
154 | write_where_clause(def_id, f)?; | ||
155 | Ok(()) | ||
156 | } | ||
157 | } | ||
158 | |||
159 | impl HirDisplay for Field { | ||
160 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
161 | write_visibility(self.parent.module(f.db).id, self.visibility(f.db), f)?; | ||
162 | write!(f, "{}: ", self.name(f.db))?; | ||
163 | self.signature_ty(f.db).hir_fmt(f) | ||
164 | } | ||
165 | } | ||
166 | |||
167 | impl HirDisplay for Variant { | ||
168 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
169 | write!(f, "{}", self.name(f.db))?; | ||
170 | let data = self.variant_data(f.db); | ||
171 | match &*data { | ||
172 | VariantData::Unit => {} | ||
173 | VariantData::Tuple(fields) => { | ||
174 | write!(f, "(")?; | ||
175 | let mut first = true; | ||
176 | for (_, field) in fields.iter() { | ||
177 | if first { | ||
178 | first = false; | ||
179 | } else { | ||
180 | write!(f, ", ")?; | ||
181 | } | ||
182 | // Enum variant fields must be pub. | ||
183 | field.type_ref.hir_fmt(f)?; | ||
184 | } | ||
185 | write!(f, ")")?; | ||
186 | } | ||
187 | VariantData::Record(fields) => { | ||
188 | write!(f, " {{")?; | ||
189 | let mut first = true; | ||
190 | for (_, field) in fields.iter() { | ||
191 | if first { | ||
192 | first = false; | ||
193 | write!(f, " ")?; | ||
194 | } else { | ||
195 | write!(f, ", ")?; | ||
196 | } | ||
197 | // Enum variant fields must be pub. | ||
198 | write!(f, "{}: ", field.name)?; | ||
199 | field.type_ref.hir_fmt(f)?; | ||
200 | } | ||
201 | write!(f, " }}")?; | ||
202 | } | ||
203 | } | ||
204 | Ok(()) | ||
205 | } | ||
206 | } | ||
207 | |||
208 | impl HirDisplay for Type { | ||
209 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
210 | self.ty.value.hir_fmt(f) | ||
211 | } | ||
212 | } | ||
213 | |||
214 | impl HirDisplay for TypeParam { | ||
215 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
216 | write!(f, "{}", self.name(f.db))?; | ||
217 | let bounds = f.db.generic_predicates_for_param(self.id); | ||
218 | let substs = Substs::type_params(f.db, self.id.parent); | ||
219 | let predicates = bounds.iter().cloned().map(|b| b.subst(&substs)).collect::<Vec<_>>(); | ||
220 | if !(predicates.is_empty() || f.omit_verbose_types()) { | ||
221 | write_bounds_like_dyn_trait_with_prefix(":", &predicates, f)?; | ||
222 | } | ||
223 | Ok(()) | ||
224 | } | ||
225 | } | ||
226 | |||
227 | impl HirDisplay for ConstParam { | ||
228 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
229 | write!(f, "const {}: ", self.name(f.db))?; | ||
230 | self.ty(f.db).hir_fmt(f) | ||
231 | } | ||
232 | } | ||
233 | |||
234 | fn write_generic_params(def: GenericDefId, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
235 | let params = f.db.generic_params(def); | ||
236 | if params.lifetimes.is_empty() | ||
237 | && params.consts.is_empty() | ||
238 | && params | ||
239 | .types | ||
240 | .iter() | ||
241 | .all(|(_, param)| !matches!(param.provenance, TypeParamProvenance::TypeParamList)) | ||
242 | { | ||
243 | return Ok(()); | ||
244 | } | ||
245 | write!(f, "<")?; | ||
246 | |||
247 | let mut first = true; | ||
248 | let mut delim = |f: &mut HirFormatter| { | ||
249 | if first { | ||
250 | first = false; | ||
251 | Ok(()) | ||
252 | } else { | ||
253 | write!(f, ", ") | ||
254 | } | ||
255 | }; | ||
256 | for (_, lifetime) in params.lifetimes.iter() { | ||
257 | delim(f)?; | ||
258 | write!(f, "{}", lifetime.name)?; | ||
259 | } | ||
260 | for (_, ty) in params.types.iter() { | ||
261 | if ty.provenance != TypeParamProvenance::TypeParamList { | ||
262 | continue; | ||
263 | } | ||
264 | if let Some(name) = &ty.name { | ||
265 | delim(f)?; | ||
266 | write!(f, "{}", name)?; | ||
267 | if let Some(default) = &ty.default { | ||
268 | write!(f, " = ")?; | ||
269 | default.hir_fmt(f)?; | ||
270 | } | ||
271 | } | ||
272 | } | ||
273 | for (_, konst) in params.consts.iter() { | ||
274 | delim(f)?; | ||
275 | write!(f, "const {}: ", konst.name)?; | ||
276 | konst.ty.hir_fmt(f)?; | ||
277 | } | ||
278 | |||
279 | write!(f, ">")?; | ||
280 | Ok(()) | ||
281 | } | ||
282 | |||
283 | fn write_where_clause(def: GenericDefId, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
284 | let params = f.db.generic_params(def); | ||
285 | if params.where_predicates.is_empty() { | ||
286 | return Ok(()); | ||
287 | } | ||
288 | |||
289 | let write_target = |target: &WherePredicateTypeTarget, f: &mut HirFormatter| match target { | ||
290 | WherePredicateTypeTarget::TypeRef(ty) => ty.hir_fmt(f), | ||
291 | WherePredicateTypeTarget::TypeParam(id) => match ¶ms.types[*id].name { | ||
292 | Some(name) => write!(f, "{}", name), | ||
293 | None => write!(f, "{{unnamed}}"), | ||
294 | }, | ||
295 | }; | ||
296 | |||
297 | write!(f, "\nwhere")?; | ||
298 | |||
299 | for (pred_idx, pred) in params.where_predicates.iter().enumerate() { | ||
300 | let prev_pred = | ||
301 | if pred_idx == 0 { None } else { Some(¶ms.where_predicates[pred_idx - 1]) }; | ||
302 | |||
303 | let new_predicate = |f: &mut HirFormatter| { | ||
304 | write!(f, "{}", if pred_idx == 0 { "\n " } else { ",\n " }) | ||
305 | }; | ||
306 | |||
307 | match pred { | ||
308 | WherePredicate::TypeBound { target, bound } => { | ||
309 | if matches!(prev_pred, Some(WherePredicate::TypeBound { target: target_, .. }) if target_ == target) | ||
310 | { | ||
311 | write!(f, " + ")?; | ||
312 | } else { | ||
313 | new_predicate(f)?; | ||
314 | write_target(target, f)?; | ||
315 | write!(f, ": ")?; | ||
316 | } | ||
317 | bound.hir_fmt(f)?; | ||
318 | } | ||
319 | WherePredicate::Lifetime { target, bound } => { | ||
320 | if matches!(prev_pred, Some(WherePredicate::Lifetime { target: target_, .. }) if target_ == target) | ||
321 | { | ||
322 | write!(f, " + {}", bound.name)?; | ||
323 | } else { | ||
324 | new_predicate(f)?; | ||
325 | write!(f, "{}: {}", target.name, bound.name)?; | ||
326 | } | ||
327 | } | ||
328 | WherePredicate::ForLifetime { lifetimes, target, bound } => { | ||
329 | if matches!( | ||
330 | prev_pred, | ||
331 | Some(WherePredicate::ForLifetime { lifetimes: lifetimes_, target: target_, .. }) | ||
332 | if lifetimes_ == lifetimes && target_ == target, | ||
333 | ) { | ||
334 | write!(f, " + ")?; | ||
335 | } else { | ||
336 | new_predicate(f)?; | ||
337 | write!(f, "for<")?; | ||
338 | for (idx, lifetime) in lifetimes.iter().enumerate() { | ||
339 | if idx != 0 { | ||
340 | write!(f, ", ")?; | ||
341 | } | ||
342 | write!(f, "{}", lifetime)?; | ||
343 | } | ||
344 | write!(f, "> ")?; | ||
345 | write_target(target, f)?; | ||
346 | write!(f, ": ")?; | ||
347 | } | ||
348 | bound.hir_fmt(f)?; | ||
349 | } | ||
350 | } | ||
351 | } | ||
352 | |||
353 | // End of final predicate. There must be at least one predicate here. | ||
354 | write!(f, ",")?; | ||
355 | |||
356 | Ok(()) | ||
357 | } | ||
358 | |||
359 | impl HirDisplay for Const { | ||
360 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
361 | write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; | ||
362 | let data = f.db.const_data(self.id); | ||
363 | write!(f, "const ")?; | ||
364 | match &data.name { | ||
365 | Some(name) => write!(f, "{}: ", name)?, | ||
366 | None => write!(f, "_: ")?, | ||
367 | } | ||
368 | data.type_ref.hir_fmt(f)?; | ||
369 | Ok(()) | ||
370 | } | ||
371 | } | ||
372 | |||
373 | impl HirDisplay for Static { | ||
374 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
375 | write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; | ||
376 | let data = f.db.static_data(self.id); | ||
377 | write!(f, "static ")?; | ||
378 | if data.mutable { | ||
379 | write!(f, "mut ")?; | ||
380 | } | ||
381 | match &data.name { | ||
382 | Some(name) => write!(f, "{}: ", name)?, | ||
383 | None => write!(f, "_: ")?, | ||
384 | } | ||
385 | data.type_ref.hir_fmt(f)?; | ||
386 | Ok(()) | ||
387 | } | ||
388 | } | ||
389 | |||
390 | impl HirDisplay for Trait { | ||
391 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
392 | write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; | ||
393 | let data = f.db.trait_data(self.id); | ||
394 | if data.is_unsafe { | ||
395 | write!(f, "unsafe ")?; | ||
396 | } | ||
397 | if data.is_auto { | ||
398 | write!(f, "auto ")?; | ||
399 | } | ||
400 | write!(f, "trait {}", data.name)?; | ||
401 | let def_id = GenericDefId::TraitId(self.id); | ||
402 | write_generic_params(def_id, f)?; | ||
403 | if !data.bounds.is_empty() { | ||
404 | write!(f, ": ")?; | ||
405 | f.write_joined(&*data.bounds, " + ")?; | ||
406 | } | ||
407 | write_where_clause(def_id, f)?; | ||
408 | Ok(()) | ||
409 | } | ||
410 | } | ||
411 | |||
412 | impl HirDisplay for TypeAlias { | ||
413 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
414 | write_visibility(self.module(f.db).id, self.visibility(f.db), f)?; | ||
415 | let data = f.db.type_alias_data(self.id); | ||
416 | write!(f, "type {}", data.name)?; | ||
417 | if !data.bounds.is_empty() { | ||
418 | write!(f, ": ")?; | ||
419 | f.write_joined(&data.bounds, " + ")?; | ||
420 | } | ||
421 | if let Some(ty) = &data.type_ref { | ||
422 | write!(f, " = ")?; | ||
423 | ty.hir_fmt(f)?; | ||
424 | } | ||
425 | Ok(()) | ||
426 | } | ||
427 | } | ||
428 | |||
429 | impl HirDisplay for Module { | ||
430 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
431 | // FIXME: Module doesn't have visibility saved in data. | ||
432 | match self.name(f.db) { | ||
433 | Some(name) => write!(f, "mod {}", name), | ||
434 | None if self.crate_root(f.db) == *self => match self.krate().display_name(f.db) { | ||
435 | Some(name) => write!(f, "extern crate {}", name), | ||
436 | None => write!(f, "extern crate {{unknown}}"), | ||
437 | }, | ||
438 | None => write!(f, "mod {{unnamed}}"), | ||
439 | } | ||
440 | } | ||
441 | } | ||
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 52939e990..f0bc2c7b9 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs | |||
@@ -29,6 +29,8 @@ mod has_source; | |||
29 | pub mod diagnostics; | 29 | pub mod diagnostics; |
30 | pub mod db; | 30 | pub mod db; |
31 | 31 | ||
32 | mod display; | ||
33 | |||
32 | use std::{iter, sync::Arc}; | 34 | use std::{iter, sync::Arc}; |
33 | 35 | ||
34 | use arrayvec::ArrayVec; | 36 | use arrayvec::ArrayVec; |
@@ -50,7 +52,6 @@ use hir_def::{ | |||
50 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; | 52 | use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; |
51 | use hir_ty::{ | 53 | use hir_ty::{ |
52 | autoderef, | 54 | autoderef, |
53 | display::{write_bounds_like_dyn_trait_with_prefix, HirDisplayError, HirFormatter}, | ||
54 | method_resolution::{self, TyFingerprint}, | 55 | method_resolution::{self, TyFingerprint}, |
55 | primitive::UintTy, | 56 | primitive::UintTy, |
56 | to_assoc_type_id, | 57 | to_assoc_type_id, |
@@ -572,6 +573,12 @@ impl Struct { | |||
572 | } | 573 | } |
573 | } | 574 | } |
574 | 575 | ||
576 | impl HasVisibility for Struct { | ||
577 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
578 | db.struct_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
579 | } | ||
580 | } | ||
581 | |||
575 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 582 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
576 | pub struct Union { | 583 | pub struct Union { |
577 | pub(crate) id: UnionId, | 584 | pub(crate) id: UnionId, |
@@ -604,6 +611,12 @@ impl Union { | |||
604 | } | 611 | } |
605 | } | 612 | } |
606 | 613 | ||
614 | impl HasVisibility for Union { | ||
615 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
616 | db.union_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
617 | } | ||
618 | } | ||
619 | |||
607 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 620 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
608 | pub struct Enum { | 621 | pub struct Enum { |
609 | pub(crate) id: EnumId, | 622 | pub(crate) id: EnumId, |
@@ -631,6 +644,12 @@ impl Enum { | |||
631 | } | 644 | } |
632 | } | 645 | } |
633 | 646 | ||
647 | impl HasVisibility for Enum { | ||
648 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
649 | db.enum_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
650 | } | ||
651 | } | ||
652 | |||
634 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 653 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
635 | pub struct Variant { | 654 | pub struct Variant { |
636 | pub(crate) parent: Enum, | 655 | pub(crate) parent: Enum, |
@@ -822,7 +841,8 @@ impl Function { | |||
822 | db.function_data(self.id) | 841 | db.function_data(self.id) |
823 | .params | 842 | .params |
824 | .iter() | 843 | .iter() |
825 | .map(|type_ref| { | 844 | .enumerate() |
845 | .map(|(idx, type_ref)| { | ||
826 | let ty = Type { | 846 | let ty = Type { |
827 | krate, | 847 | krate, |
828 | ty: InEnvironment { | 848 | ty: InEnvironment { |
@@ -830,7 +850,7 @@ impl Function { | |||
830 | environment: environment.clone(), | 850 | environment: environment.clone(), |
831 | }, | 851 | }, |
832 | }; | 852 | }; |
833 | Param { ty } | 853 | Param { func: self, ty, idx } |
834 | }) | 854 | }) |
835 | .collect() | 855 | .collect() |
836 | } | 856 | } |
@@ -844,7 +864,7 @@ impl Function { | |||
844 | } | 864 | } |
845 | 865 | ||
846 | pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool { | 866 | pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool { |
847 | db.function_data(self.id).is_unsafe | 867 | db.function_data(self.id).qualifier.is_unsafe |
848 | } | 868 | } |
849 | 869 | ||
850 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { | 870 | pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { |
@@ -893,6 +913,9 @@ impl From<hir_ty::Mutability> for Access { | |||
893 | 913 | ||
894 | #[derive(Debug)] | 914 | #[derive(Debug)] |
895 | pub struct Param { | 915 | pub struct Param { |
916 | func: Function, | ||
917 | /// The index in parameter list, including self parameter. | ||
918 | idx: usize, | ||
896 | ty: Type, | 919 | ty: Type, |
897 | } | 920 | } |
898 | 921 | ||
@@ -900,6 +923,15 @@ impl Param { | |||
900 | pub fn ty(&self) -> &Type { | 923 | pub fn ty(&self) -> &Type { |
901 | &self.ty | 924 | &self.ty |
902 | } | 925 | } |
926 | |||
927 | pub fn pattern_source(&self, db: &dyn HirDatabase) -> Option<ast::Pat> { | ||
928 | let params = self.func.source(db)?.value.param_list()?; | ||
929 | if params.self_param().is_some() { | ||
930 | params.params().nth(self.idx.checked_sub(1)?)?.pat() | ||
931 | } else { | ||
932 | params.params().nth(self.idx)?.pat() | ||
933 | } | ||
934 | } | ||
903 | } | 935 | } |
904 | 936 | ||
905 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 937 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -922,6 +954,14 @@ impl SelfParam { | |||
922 | }) | 954 | }) |
923 | .unwrap_or(Access::Owned) | 955 | .unwrap_or(Access::Owned) |
924 | } | 956 | } |
957 | |||
958 | pub fn display(self, db: &dyn HirDatabase) -> &'static str { | ||
959 | match self.access(db) { | ||
960 | Access::Shared => "&self", | ||
961 | Access::Exclusive => "&mut self", | ||
962 | Access::Owned => "self", | ||
963 | } | ||
964 | } | ||
925 | } | 965 | } |
926 | 966 | ||
927 | impl HasVisibility for Function { | 967 | impl HasVisibility for Function { |
@@ -949,6 +989,10 @@ impl Const { | |||
949 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { | 989 | pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { |
950 | db.const_data(self.id).name.clone() | 990 | db.const_data(self.id).name.clone() |
951 | } | 991 | } |
992 | |||
993 | pub fn type_ref(self, db: &dyn HirDatabase) -> TypeRef { | ||
994 | db.const_data(self.id).type_ref.clone() | ||
995 | } | ||
952 | } | 996 | } |
953 | 997 | ||
954 | impl HasVisibility for Const { | 998 | impl HasVisibility for Const { |
@@ -982,6 +1026,12 @@ impl Static { | |||
982 | } | 1026 | } |
983 | } | 1027 | } |
984 | 1028 | ||
1029 | impl HasVisibility for Static { | ||
1030 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
1031 | db.static_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
1032 | } | ||
1033 | } | ||
1034 | |||
985 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 1035 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
986 | pub struct Trait { | 1036 | pub struct Trait { |
987 | pub(crate) id: TraitId, | 1037 | pub(crate) id: TraitId, |
@@ -1001,7 +1051,13 @@ impl Trait { | |||
1001 | } | 1051 | } |
1002 | 1052 | ||
1003 | pub fn is_auto(self, db: &dyn HirDatabase) -> bool { | 1053 | pub fn is_auto(self, db: &dyn HirDatabase) -> bool { |
1004 | db.trait_data(self.id).auto | 1054 | db.trait_data(self.id).is_auto |
1055 | } | ||
1056 | } | ||
1057 | |||
1058 | impl HasVisibility for Trait { | ||
1059 | fn visibility(&self, db: &dyn HirDatabase) -> Visibility { | ||
1060 | db.trait_data(self.id).visibility.resolve(db.upcast(), &self.id.resolver(db.upcast())) | ||
1005 | } | 1061 | } |
1006 | } | 1062 | } |
1007 | 1063 | ||
@@ -1413,19 +1469,6 @@ impl TypeParam { | |||
1413 | } | 1469 | } |
1414 | } | 1470 | } |
1415 | 1471 | ||
1416 | impl HirDisplay for TypeParam { | ||
1417 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
1418 | write!(f, "{}", self.name(f.db))?; | ||
1419 | let bounds = f.db.generic_predicates_for_param(self.id); | ||
1420 | let substs = Substs::type_params(f.db, self.id.parent); | ||
1421 | let predicates = bounds.iter().cloned().map(|b| b.subst(&substs)).collect::<Vec<_>>(); | ||
1422 | if !(predicates.is_empty() || f.omit_verbose_types()) { | ||
1423 | write_bounds_like_dyn_trait_with_prefix(":", &predicates, f)?; | ||
1424 | } | ||
1425 | Ok(()) | ||
1426 | } | ||
1427 | } | ||
1428 | |||
1429 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 1472 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
1430 | pub struct LifetimeParam { | 1473 | pub struct LifetimeParam { |
1431 | pub(crate) id: LifetimeParamId, | 1474 | pub(crate) id: LifetimeParamId, |
@@ -2059,12 +2102,6 @@ impl Type { | |||
2059 | } | 2102 | } |
2060 | } | 2103 | } |
2061 | 2104 | ||
2062 | impl HirDisplay for Type { | ||
2063 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
2064 | self.ty.value.hir_fmt(f) | ||
2065 | } | ||
2066 | } | ||
2067 | |||
2068 | // FIXME: closures | 2105 | // FIXME: closures |
2069 | #[derive(Debug)] | 2106 | #[derive(Debug)] |
2070 | pub struct Callable { | 2107 | pub struct Callable { |
diff --git a/crates/hir_def/src/adt.rs b/crates/hir_def/src/adt.rs index efbde17d8..1b9bb8235 100644 --- a/crates/hir_def/src/adt.rs +++ b/crates/hir_def/src/adt.rs | |||
@@ -31,12 +31,14 @@ pub struct StructData { | |||
31 | pub name: Name, | 31 | pub name: Name, |
32 | pub variant_data: Arc<VariantData>, | 32 | pub variant_data: Arc<VariantData>, |
33 | pub repr: Option<ReprKind>, | 33 | pub repr: Option<ReprKind>, |
34 | pub visibility: RawVisibility, | ||
34 | } | 35 | } |
35 | 36 | ||
36 | #[derive(Debug, Clone, PartialEq, Eq)] | 37 | #[derive(Debug, Clone, PartialEq, Eq)] |
37 | pub struct EnumData { | 38 | pub struct EnumData { |
38 | pub name: Name, | 39 | pub name: Name, |
39 | pub variants: Arena<EnumVariantData>, | 40 | pub variants: Arena<EnumVariantData>, |
41 | pub visibility: RawVisibility, | ||
40 | } | 42 | } |
41 | 43 | ||
42 | #[derive(Debug, Clone, PartialEq, Eq)] | 44 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -102,6 +104,7 @@ impl StructData { | |||
102 | name: strukt.name.clone(), | 104 | name: strukt.name.clone(), |
103 | variant_data: Arc::new(variant_data), | 105 | variant_data: Arc::new(variant_data), |
104 | repr, | 106 | repr, |
107 | visibility: item_tree[strukt.visibility].clone(), | ||
105 | }) | 108 | }) |
106 | } | 109 | } |
107 | pub(crate) fn union_data_query(db: &dyn DefDatabase, id: UnionId) -> Arc<StructData> { | 110 | pub(crate) fn union_data_query(db: &dyn DefDatabase, id: UnionId) -> Arc<StructData> { |
@@ -118,6 +121,7 @@ impl StructData { | |||
118 | name: union.name.clone(), | 121 | name: union.name.clone(), |
119 | variant_data: Arc::new(variant_data), | 122 | variant_data: Arc::new(variant_data), |
120 | repr, | 123 | repr, |
124 | visibility: item_tree[union.visibility].clone(), | ||
121 | }) | 125 | }) |
122 | } | 126 | } |
123 | } | 127 | } |
@@ -150,7 +154,11 @@ impl EnumData { | |||
150 | } | 154 | } |
151 | } | 155 | } |
152 | 156 | ||
153 | Arc::new(EnumData { name: enum_.name.clone(), variants }) | 157 | Arc::new(EnumData { |
158 | name: enum_.name.clone(), | ||
159 | variants, | ||
160 | visibility: item_tree[enum_.visibility].clone(), | ||
161 | }) | ||
154 | } | 162 | } |
155 | 163 | ||
156 | pub fn variant(&self, name: &Name) -> Option<LocalEnumVariantId> { | 164 | pub fn variant(&self, name: &Name) -> Option<LocalEnumVariantId> { |
diff --git a/crates/hir_def/src/data.rs b/crates/hir_def/src/data.rs index aea53d527..74a2194e5 100644 --- a/crates/hir_def/src/data.rs +++ b/crates/hir_def/src/data.rs | |||
@@ -9,7 +9,7 @@ use crate::{ | |||
9 | attr::Attrs, | 9 | attr::Attrs, |
10 | body::Expander, | 10 | body::Expander, |
11 | db::DefDatabase, | 11 | db::DefDatabase, |
12 | item_tree::{AssocItem, ItemTreeId, ModItem}, | 12 | item_tree::{AssocItem, FunctionQualifier, ItemTreeId, ModItem}, |
13 | type_ref::{TypeBound, TypeRef}, | 13 | type_ref::{TypeBound, TypeRef}, |
14 | visibility::RawVisibility, | 14 | visibility::RawVisibility, |
15 | AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId, | 15 | AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId, |
@@ -26,9 +26,9 @@ pub struct FunctionData { | |||
26 | /// can be called as a method. | 26 | /// can be called as a method. |
27 | pub has_self_param: bool, | 27 | pub has_self_param: bool, |
28 | pub has_body: bool, | 28 | pub has_body: bool, |
29 | pub is_unsafe: bool, | 29 | pub qualifier: FunctionQualifier, |
30 | pub is_in_extern_block: bool, | ||
30 | pub is_varargs: bool, | 31 | pub is_varargs: bool, |
31 | pub is_extern: bool, | ||
32 | pub visibility: RawVisibility, | 32 | pub visibility: RawVisibility, |
33 | } | 33 | } |
34 | 34 | ||
@@ -46,9 +46,9 @@ impl FunctionData { | |||
46 | attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()), | 46 | attrs: item_tree.attrs(db, krate, ModItem::from(loc.id.value).into()), |
47 | has_self_param: func.has_self_param, | 47 | has_self_param: func.has_self_param, |
48 | has_body: func.has_body, | 48 | has_body: func.has_body, |
49 | is_unsafe: func.is_unsafe, | 49 | qualifier: func.qualifier.clone(), |
50 | is_in_extern_block: func.is_in_extern_block, | ||
50 | is_varargs: func.is_varargs, | 51 | is_varargs: func.is_varargs, |
51 | is_extern: func.is_extern, | ||
52 | visibility: item_tree[func.visibility].clone(), | 52 | visibility: item_tree[func.visibility].clone(), |
53 | }) | 53 | }) |
54 | } | 54 | } |
@@ -87,7 +87,10 @@ impl TypeAliasData { | |||
87 | pub struct TraitData { | 87 | pub struct TraitData { |
88 | pub name: Name, | 88 | pub name: Name, |
89 | pub items: Vec<(Name, AssocItemId)>, | 89 | pub items: Vec<(Name, AssocItemId)>, |
90 | pub auto: bool, | 90 | pub is_auto: bool, |
91 | pub is_unsafe: bool, | ||
92 | pub visibility: RawVisibility, | ||
93 | pub bounds: Box<[TypeBound]>, | ||
91 | } | 94 | } |
92 | 95 | ||
93 | impl TraitData { | 96 | impl TraitData { |
@@ -96,10 +99,13 @@ impl TraitData { | |||
96 | let item_tree = db.item_tree(tr_loc.id.file_id); | 99 | let item_tree = db.item_tree(tr_loc.id.file_id); |
97 | let tr_def = &item_tree[tr_loc.id.value]; | 100 | let tr_def = &item_tree[tr_loc.id.value]; |
98 | let name = tr_def.name.clone(); | 101 | let name = tr_def.name.clone(); |
99 | let auto = tr_def.auto; | 102 | let is_auto = tr_def.is_auto; |
103 | let is_unsafe = tr_def.is_unsafe; | ||
100 | let module_id = tr_loc.container; | 104 | let module_id = tr_loc.container; |
101 | let container = AssocContainerId::TraitId(tr); | 105 | let container = AssocContainerId::TraitId(tr); |
102 | let mut expander = Expander::new(db, tr_loc.id.file_id, module_id); | 106 | let mut expander = Expander::new(db, tr_loc.id.file_id, module_id); |
107 | let visibility = item_tree[tr_def.visibility].clone(); | ||
108 | let bounds = tr_def.bounds.clone(); | ||
103 | 109 | ||
104 | let items = collect_items( | 110 | let items = collect_items( |
105 | db, | 111 | db, |
@@ -111,7 +117,7 @@ impl TraitData { | |||
111 | 100, | 117 | 100, |
112 | ); | 118 | ); |
113 | 119 | ||
114 | Arc::new(TraitData { name, items, auto }) | 120 | Arc::new(TraitData { name, items, is_auto, is_unsafe, visibility, bounds }) |
115 | } | 121 | } |
116 | 122 | ||
117 | pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ { | 123 | pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ { |
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index 86239d903..7bb22c4c4 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs | |||
@@ -24,7 +24,7 @@ use la_arena::{Arena, Idx, RawIdx}; | |||
24 | use profile::Count; | 24 | use profile::Count; |
25 | use rustc_hash::FxHashMap; | 25 | use rustc_hash::FxHashMap; |
26 | use smallvec::SmallVec; | 26 | use smallvec::SmallVec; |
27 | use syntax::{ast, match_ast, SyntaxKind}; | 27 | use syntax::{ast, match_ast, SmolStr, SyntaxKind}; |
28 | 28 | ||
29 | use crate::{ | 29 | use crate::{ |
30 | attr::{Attrs, RawAttrs}, | 30 | attr::{Attrs, RawAttrs}, |
@@ -556,16 +556,25 @@ pub struct Function { | |||
556 | pub generic_params: GenericParamsId, | 556 | pub generic_params: GenericParamsId, |
557 | pub has_self_param: bool, | 557 | pub has_self_param: bool, |
558 | pub has_body: bool, | 558 | pub has_body: bool, |
559 | pub is_unsafe: bool, | 559 | pub qualifier: FunctionQualifier, |
560 | /// Whether the function is located in an `extern` block (*not* whether it is an | 560 | /// Whether the function is located in an `extern` block (*not* whether it is an |
561 | /// `extern "abi" fn`). | 561 | /// `extern "abi" fn`). |
562 | pub is_extern: bool, | 562 | pub is_in_extern_block: bool, |
563 | pub params: Box<[Idx<TypeRef>]>, | 563 | pub params: Box<[Idx<TypeRef>]>, |
564 | pub is_varargs: bool, | 564 | pub is_varargs: bool, |
565 | pub ret_type: Idx<TypeRef>, | 565 | pub ret_type: Idx<TypeRef>, |
566 | pub ast_id: FileAstId<ast::Fn>, | 566 | pub ast_id: FileAstId<ast::Fn>, |
567 | } | 567 | } |
568 | 568 | ||
569 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
570 | pub struct FunctionQualifier { | ||
571 | pub is_default: bool, | ||
572 | pub is_const: bool, | ||
573 | pub is_async: bool, | ||
574 | pub is_unsafe: bool, | ||
575 | pub abi: Option<SmolStr>, | ||
576 | } | ||
577 | |||
569 | #[derive(Debug, Clone, Eq, PartialEq)] | 578 | #[derive(Debug, Clone, Eq, PartialEq)] |
570 | pub struct Struct { | 579 | pub struct Struct { |
571 | pub name: Name, | 580 | pub name: Name, |
@@ -629,7 +638,9 @@ pub struct Trait { | |||
629 | pub name: Name, | 638 | pub name: Name, |
630 | pub visibility: RawVisibilityId, | 639 | pub visibility: RawVisibilityId, |
631 | pub generic_params: GenericParamsId, | 640 | pub generic_params: GenericParamsId, |
632 | pub auto: bool, | 641 | pub is_auto: bool, |
642 | pub is_unsafe: bool, | ||
643 | pub bounds: Box<[TypeBound]>, | ||
633 | pub items: Box<[AssocItem]>, | 644 | pub items: Box<[AssocItem]>, |
634 | pub ast_id: FileAstId<ast::Trait>, | 645 | pub ast_id: FileAstId<ast::Trait>, |
635 | } | 646 | } |
diff --git a/crates/hir_def/src/item_tree/lower.rs b/crates/hir_def/src/item_tree/lower.rs index 240fdacf9..7e91b991d 100644 --- a/crates/hir_def/src/item_tree/lower.rs +++ b/crates/hir_def/src/item_tree/lower.rs | |||
@@ -391,14 +391,33 @@ impl Ctx { | |||
391 | let has_body = func.body().is_some(); | 391 | let has_body = func.body().is_some(); |
392 | 392 | ||
393 | let ast_id = self.source_ast_id_map.ast_id(func); | 393 | let ast_id = self.source_ast_id_map.ast_id(func); |
394 | let qualifier = FunctionQualifier { | ||
395 | is_default: func.default_token().is_some(), | ||
396 | is_const: func.const_token().is_some(), | ||
397 | is_async: func.async_token().is_some(), | ||
398 | is_unsafe: func.unsafe_token().is_some(), | ||
399 | abi: func.abi().map(|abi| { | ||
400 | // FIXME: Abi::abi() -> Option<SyntaxToken>? | ||
401 | match abi.syntax().last_token() { | ||
402 | Some(tok) if tok.kind() == SyntaxKind::STRING => { | ||
403 | // FIXME: Better way to unescape? | ||
404 | tok.text().trim_matches('"').into() | ||
405 | } | ||
406 | _ => { | ||
407 | // `extern` default to be `extern "C"`. | ||
408 | "C".into() | ||
409 | } | ||
410 | } | ||
411 | }), | ||
412 | }; | ||
394 | let mut res = Function { | 413 | let mut res = Function { |
395 | name, | 414 | name, |
396 | visibility, | 415 | visibility, |
397 | generic_params: GenericParamsId::EMPTY, | 416 | generic_params: GenericParamsId::EMPTY, |
398 | has_self_param, | 417 | has_self_param, |
399 | has_body, | 418 | has_body, |
400 | is_unsafe: func.unsafe_token().is_some(), | 419 | qualifier, |
401 | is_extern: false, | 420 | is_in_extern_block: false, |
402 | params, | 421 | params, |
403 | is_varargs, | 422 | is_varargs, |
404 | ret_type, | 423 | ret_type, |
@@ -481,7 +500,9 @@ impl Ctx { | |||
481 | let visibility = self.lower_visibility(trait_def); | 500 | let visibility = self.lower_visibility(trait_def); |
482 | let generic_params = | 501 | let generic_params = |
483 | self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def); | 502 | self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def); |
484 | let auto = trait_def.auto_token().is_some(); | 503 | let is_auto = trait_def.auto_token().is_some(); |
504 | let is_unsafe = trait_def.unsafe_token().is_some(); | ||
505 | let bounds = self.lower_type_bounds(trait_def); | ||
485 | let items = trait_def.assoc_item_list().map(|list| { | 506 | let items = trait_def.assoc_item_list().map(|list| { |
486 | self.with_inherited_visibility(visibility, |this| { | 507 | self.with_inherited_visibility(visibility, |this| { |
487 | list.assoc_items() | 508 | list.assoc_items() |
@@ -501,7 +522,9 @@ impl Ctx { | |||
501 | name, | 522 | name, |
502 | visibility, | 523 | visibility, |
503 | generic_params, | 524 | generic_params, |
504 | auto, | 525 | is_auto, |
526 | is_unsafe, | ||
527 | bounds: bounds.into(), | ||
505 | items: items.unwrap_or_default(), | 528 | items: items.unwrap_or_default(), |
506 | ast_id, | 529 | ast_id, |
507 | }; | 530 | }; |
@@ -608,8 +631,8 @@ impl Ctx { | |||
608 | ast::ExternItem::Fn(ast) => { | 631 | ast::ExternItem::Fn(ast) => { |
609 | let func_id = self.lower_function(&ast)?; | 632 | let func_id = self.lower_function(&ast)?; |
610 | let func = &mut self.data().functions[func_id.index]; | 633 | let func = &mut self.data().functions[func_id.index]; |
611 | func.is_unsafe = is_intrinsic_fn_unsafe(&func.name); | 634 | func.qualifier.is_unsafe = is_intrinsic_fn_unsafe(&func.name); |
612 | func.is_extern = true; | 635 | func.is_in_extern_block = true; |
613 | func_id.into() | 636 | func_id.into() |
614 | } | 637 | } |
615 | ast::ExternItem::Static(ast) => { | 638 | ast::ExternItem::Static(ast) => { |
diff --git a/crates/hir_def/src/path.rs b/crates/hir_def/src/path.rs index 0e60dc2b6..8c923bb7b 100644 --- a/crates/hir_def/src/path.rs +++ b/crates/hir_def/src/path.rs | |||
@@ -9,7 +9,10 @@ use std::{ | |||
9 | 9 | ||
10 | use crate::{body::LowerCtx, type_ref::LifetimeRef}; | 10 | use crate::{body::LowerCtx, type_ref::LifetimeRef}; |
11 | use base_db::CrateId; | 11 | use base_db::CrateId; |
12 | use hir_expand::{hygiene::Hygiene, name::Name}; | 12 | use hir_expand::{ |
13 | hygiene::Hygiene, | ||
14 | name::{name, Name}, | ||
15 | }; | ||
13 | use syntax::ast; | 16 | use syntax::ast; |
14 | 17 | ||
15 | use crate::{ | 18 | use crate::{ |
@@ -209,6 +212,12 @@ impl Path { | |||
209 | }; | 212 | }; |
210 | Some(res) | 213 | Some(res) |
211 | } | 214 | } |
215 | |||
216 | pub fn is_self_type(&self) -> bool { | ||
217 | self.type_anchor.is_none() | ||
218 | && self.generic_args == &[None] | ||
219 | && self.mod_path.as_ident() == Some(&name!(Self)) | ||
220 | } | ||
212 | } | 221 | } |
213 | 222 | ||
214 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 223 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
diff --git a/crates/hir_ty/src/diagnostics/decl_check.rs b/crates/hir_ty/src/diagnostics/decl_check.rs index 3605ca581..982ad5b9e 100644 --- a/crates/hir_ty/src/diagnostics/decl_check.rs +++ b/crates/hir_ty/src/diagnostics/decl_check.rs | |||
@@ -91,7 +91,7 @@ impl<'a, 'b> DeclValidator<'a, 'b> { | |||
91 | 91 | ||
92 | fn validate_func(&mut self, func: FunctionId) { | 92 | fn validate_func(&mut self, func: FunctionId) { |
93 | let data = self.db.function_data(func); | 93 | let data = self.db.function_data(func); |
94 | if data.is_extern { | 94 | if data.is_in_extern_block { |
95 | cov_mark::hit!(extern_func_incorrect_case_ignored); | 95 | cov_mark::hit!(extern_func_incorrect_case_ignored); |
96 | return; | 96 | return; |
97 | } | 97 | } |
diff --git a/crates/hir_ty/src/diagnostics/unsafe_check.rs b/crates/hir_ty/src/diagnostics/unsafe_check.rs index 20bb64827..44a7e5506 100644 --- a/crates/hir_ty/src/diagnostics/unsafe_check.rs +++ b/crates/hir_ty/src/diagnostics/unsafe_check.rs | |||
@@ -32,7 +32,7 @@ impl<'a, 'b> UnsafeValidator<'a, 'b> { | |||
32 | let def = self.owner.into(); | 32 | let def = self.owner.into(); |
33 | let unsafe_expressions = unsafe_expressions(db, self.infer.as_ref(), def); | 33 | let unsafe_expressions = unsafe_expressions(db, self.infer.as_ref(), def); |
34 | let is_unsafe = match self.owner { | 34 | let is_unsafe = match self.owner { |
35 | DefWithBodyId::FunctionId(it) => db.function_data(it).is_unsafe, | 35 | DefWithBodyId::FunctionId(it) => db.function_data(it).qualifier.is_unsafe, |
36 | DefWithBodyId::StaticId(_) | DefWithBodyId::ConstId(_) => false, | 36 | DefWithBodyId::StaticId(_) | DefWithBodyId::ConstId(_) => false, |
37 | }; | 37 | }; |
38 | if is_unsafe | 38 | if is_unsafe |
@@ -86,7 +86,7 @@ fn walk_unsafe( | |||
86 | match expr { | 86 | match expr { |
87 | &Expr::Call { callee, .. } => { | 87 | &Expr::Call { callee, .. } => { |
88 | if let Some(func) = infer[callee].as_fn_def(db) { | 88 | if let Some(func) = infer[callee].as_fn_def(db) { |
89 | if db.function_data(func).is_unsafe { | 89 | if db.function_data(func).qualifier.is_unsafe { |
90 | unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); | 90 | unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); |
91 | } | 91 | } |
92 | } | 92 | } |
@@ -103,7 +103,7 @@ fn walk_unsafe( | |||
103 | Expr::MethodCall { .. } => { | 103 | Expr::MethodCall { .. } => { |
104 | if infer | 104 | if infer |
105 | .method_resolution(current) | 105 | .method_resolution(current) |
106 | .map(|func| db.function_data(func).is_unsafe) | 106 | .map(|func| db.function_data(func).qualifier.is_unsafe) |
107 | .unwrap_or(false) | 107 | .unwrap_or(false) |
108 | { | 108 | { |
109 | unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); | 109 | unsafe_exprs.push(UnsafeExpr { expr: current, inside_unsafe_block }); |
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index c1062387e..c572bb114 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs | |||
@@ -5,7 +5,13 @@ use std::{borrow::Cow, fmt}; | |||
5 | use arrayvec::ArrayVec; | 5 | use arrayvec::ArrayVec; |
6 | use chalk_ir::Mutability; | 6 | use chalk_ir::Mutability; |
7 | use hir_def::{ | 7 | use hir_def::{ |
8 | db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs, | 8 | db::DefDatabase, |
9 | find_path, | ||
10 | generics::TypeParamProvenance, | ||
11 | item_scope::ItemInNs, | ||
12 | path::{GenericArg, Path, PathKind}, | ||
13 | type_ref::{TypeBound, TypeRef}, | ||
14 | visibility::Visibility, | ||
9 | AssocContainerId, Lookup, ModuleId, TraitId, | 15 | AssocContainerId, Lookup, ModuleId, TraitId, |
10 | }; | 16 | }; |
11 | use hir_expand::name::Name; | 17 | use hir_expand::name::Name; |
@@ -232,7 +238,7 @@ where | |||
232 | 238 | ||
233 | const TYPE_HINT_TRUNCATION: &str = "…"; | 239 | const TYPE_HINT_TRUNCATION: &str = "…"; |
234 | 240 | ||
235 | impl HirDisplay for &Ty { | 241 | impl<T: HirDisplay> HirDisplay for &'_ T { |
236 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | 242 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { |
237 | HirDisplay::hir_fmt(*self, f) | 243 | HirDisplay::hir_fmt(*self, f) |
238 | } | 244 | } |
@@ -761,12 +767,6 @@ impl HirDisplay for TraitRef { | |||
761 | } | 767 | } |
762 | } | 768 | } |
763 | 769 | ||
764 | impl HirDisplay for &GenericPredicate { | ||
765 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
766 | HirDisplay::hir_fmt(*self, f) | ||
767 | } | ||
768 | } | ||
769 | |||
770 | impl HirDisplay for GenericPredicate { | 770 | impl HirDisplay for GenericPredicate { |
771 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | 771 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { |
772 | if f.should_truncate() { | 772 | if f.should_truncate() { |
@@ -825,3 +825,190 @@ impl HirDisplay for Obligation { | |||
825 | } | 825 | } |
826 | } | 826 | } |
827 | } | 827 | } |
828 | |||
829 | pub fn write_visibility( | ||
830 | module_id: ModuleId, | ||
831 | vis: Visibility, | ||
832 | f: &mut HirFormatter, | ||
833 | ) -> Result<(), HirDisplayError> { | ||
834 | match vis { | ||
835 | Visibility::Public => write!(f, "pub "), | ||
836 | Visibility::Module(vis_id) => { | ||
837 | let def_map = module_id.def_map(f.db.upcast()); | ||
838 | let root_module_id = def_map.module_id(def_map.root()); | ||
839 | if vis_id == module_id { | ||
840 | // pub(self) or omitted | ||
841 | Ok(()) | ||
842 | } else if root_module_id == vis_id { | ||
843 | write!(f, "pub(crate) ") | ||
844 | } else if module_id.containing_module(f.db.upcast()) == Some(vis_id) { | ||
845 | write!(f, "pub(super) ") | ||
846 | } else { | ||
847 | write!(f, "pub(in ...) ") | ||
848 | } | ||
849 | } | ||
850 | } | ||
851 | } | ||
852 | |||
853 | impl HirDisplay for TypeRef { | ||
854 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
855 | match self { | ||
856 | TypeRef::Never => write!(f, "!")?, | ||
857 | TypeRef::Placeholder => write!(f, "_")?, | ||
858 | TypeRef::Tuple(elems) => { | ||
859 | write!(f, "(")?; | ||
860 | f.write_joined(elems, ", ")?; | ||
861 | if elems.len() == 1 { | ||
862 | write!(f, ",")?; | ||
863 | } | ||
864 | write!(f, ")")?; | ||
865 | } | ||
866 | TypeRef::Path(path) => path.hir_fmt(f)?, | ||
867 | TypeRef::RawPtr(inner, mutability) => { | ||
868 | let mutability = match mutability { | ||
869 | hir_def::type_ref::Mutability::Shared => "*const ", | ||
870 | hir_def::type_ref::Mutability::Mut => "*mut ", | ||
871 | }; | ||
872 | write!(f, "{}", mutability)?; | ||
873 | inner.hir_fmt(f)?; | ||
874 | } | ||
875 | TypeRef::Reference(inner, lifetime, mutability) => { | ||
876 | let mutability = match mutability { | ||
877 | hir_def::type_ref::Mutability::Shared => "", | ||
878 | hir_def::type_ref::Mutability::Mut => "mut ", | ||
879 | }; | ||
880 | write!(f, "&")?; | ||
881 | if let Some(lifetime) = lifetime { | ||
882 | write!(f, "{} ", lifetime.name)?; | ||
883 | } | ||
884 | write!(f, "{}", mutability)?; | ||
885 | inner.hir_fmt(f)?; | ||
886 | } | ||
887 | TypeRef::Array(inner) => { | ||
888 | write!(f, "[")?; | ||
889 | inner.hir_fmt(f)?; | ||
890 | // FIXME: Array length? | ||
891 | write!(f, "; _]")?; | ||
892 | } | ||
893 | TypeRef::Slice(inner) => { | ||
894 | write!(f, "[")?; | ||
895 | inner.hir_fmt(f)?; | ||
896 | write!(f, "]")?; | ||
897 | } | ||
898 | TypeRef::Fn(tys, is_varargs) => { | ||
899 | // FIXME: Function pointer qualifiers. | ||
900 | write!(f, "fn(")?; | ||
901 | f.write_joined(&tys[..tys.len() - 1], ", ")?; | ||
902 | if *is_varargs { | ||
903 | write!(f, "{}...", if tys.len() == 1 { "" } else { ", " })?; | ||
904 | } | ||
905 | write!(f, ")")?; | ||
906 | let ret_ty = tys.last().unwrap(); | ||
907 | match ret_ty { | ||
908 | TypeRef::Tuple(tup) if tup.is_empty() => {} | ||
909 | _ => { | ||
910 | write!(f, " -> ")?; | ||
911 | ret_ty.hir_fmt(f)?; | ||
912 | } | ||
913 | } | ||
914 | } | ||
915 | TypeRef::ImplTrait(bounds) => { | ||
916 | write!(f, "impl ")?; | ||
917 | f.write_joined(bounds, " + ")?; | ||
918 | } | ||
919 | TypeRef::DynTrait(bounds) => { | ||
920 | write!(f, "dyn ")?; | ||
921 | f.write_joined(bounds, " + ")?; | ||
922 | } | ||
923 | TypeRef::Error => write!(f, "{{error}}")?, | ||
924 | } | ||
925 | Ok(()) | ||
926 | } | ||
927 | } | ||
928 | |||
929 | impl HirDisplay for TypeBound { | ||
930 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
931 | match self { | ||
932 | TypeBound::Path(path) => path.hir_fmt(f), | ||
933 | TypeBound::Lifetime(lifetime) => write!(f, "{}", lifetime.name), | ||
934 | TypeBound::Error => write!(f, "{{error}}"), | ||
935 | } | ||
936 | } | ||
937 | } | ||
938 | |||
939 | impl HirDisplay for Path { | ||
940 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
941 | match (self.type_anchor(), self.kind()) { | ||
942 | (Some(anchor), _) => { | ||
943 | write!(f, "<")?; | ||
944 | anchor.hir_fmt(f)?; | ||
945 | write!(f, ">")?; | ||
946 | } | ||
947 | (_, PathKind::Plain) => {} | ||
948 | (_, PathKind::Abs) => write!(f, "::")?, | ||
949 | (_, PathKind::Crate) => write!(f, "crate")?, | ||
950 | (_, PathKind::Super(0)) => write!(f, "self")?, | ||
951 | (_, PathKind::Super(n)) => { | ||
952 | write!(f, "super")?; | ||
953 | for _ in 0..*n { | ||
954 | write!(f, "::super")?; | ||
955 | } | ||
956 | } | ||
957 | (_, PathKind::DollarCrate(_)) => write!(f, "{{extern_crate}}")?, | ||
958 | } | ||
959 | |||
960 | for (seg_idx, segment) in self.segments().iter().enumerate() { | ||
961 | if seg_idx != 0 { | ||
962 | write!(f, "::")?; | ||
963 | } | ||
964 | write!(f, "{}", segment.name)?; | ||
965 | if let Some(generic_args) = segment.args_and_bindings { | ||
966 | // We should be in type context, so format as `Foo<Bar>` instead of `Foo::<Bar>`. | ||
967 | // Do we actually format expressions? | ||
968 | write!(f, "<")?; | ||
969 | let mut first = true; | ||
970 | for arg in &generic_args.args { | ||
971 | if first { | ||
972 | first = false; | ||
973 | if generic_args.has_self_type { | ||
974 | // FIXME: Convert to `<Ty as Trait>` form. | ||
975 | write!(f, "Self = ")?; | ||
976 | } | ||
977 | } else { | ||
978 | write!(f, ", ")?; | ||
979 | } | ||
980 | arg.hir_fmt(f)?; | ||
981 | } | ||
982 | for binding in &generic_args.bindings { | ||
983 | if first { | ||
984 | first = false; | ||
985 | } else { | ||
986 | write!(f, ", ")?; | ||
987 | } | ||
988 | write!(f, "{}", binding.name)?; | ||
989 | match &binding.type_ref { | ||
990 | Some(ty) => { | ||
991 | write!(f, " = ")?; | ||
992 | ty.hir_fmt(f)? | ||
993 | } | ||
994 | None => { | ||
995 | write!(f, ": ")?; | ||
996 | f.write_joined(&binding.bounds, " + ")?; | ||
997 | } | ||
998 | } | ||
999 | } | ||
1000 | write!(f, ">")?; | ||
1001 | } | ||
1002 | } | ||
1003 | Ok(()) | ||
1004 | } | ||
1005 | } | ||
1006 | |||
1007 | impl HirDisplay for GenericArg { | ||
1008 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
1009 | match self { | ||
1010 | GenericArg::Type(ty) => ty.hir_fmt(f), | ||
1011 | GenericArg::Lifetime(lifetime) => write!(f, "{}", lifetime.name), | ||
1012 | } | ||
1013 | } | ||
1014 | } | ||
diff --git a/crates/hir_ty/src/traits/chalk.rs b/crates/hir_ty/src/traits/chalk.rs index 232cf9cd0..4bd8ba303 100644 --- a/crates/hir_ty/src/traits/chalk.rs +++ b/crates/hir_ty/src/traits/chalk.rs | |||
@@ -429,7 +429,7 @@ pub(crate) fn trait_datum_query( | |||
429 | let generic_params = generics(db.upcast(), trait_.into()); | 429 | let generic_params = generics(db.upcast(), trait_.into()); |
430 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); | 430 | let bound_vars = Substs::bound_vars(&generic_params, DebruijnIndex::INNERMOST); |
431 | let flags = rust_ir::TraitFlags { | 431 | let flags = rust_ir::TraitFlags { |
432 | auto: trait_data.auto, | 432 | auto: trait_data.is_auto, |
433 | upstream: trait_.lookup(db.upcast()).container.krate() != krate, | 433 | upstream: trait_.lookup(db.upcast()).container.krate() != krate, |
434 | non_enumerable: true, | 434 | non_enumerable: true, |
435 | coinductive: false, // only relevant for Chalk testing | 435 | coinductive: false, // only relevant for Chalk testing |
diff --git a/crates/ide/src/display.rs b/crates/ide/src/display.rs index bae9e40df..1f7b665c0 100644 --- a/crates/ide/src/display.rs +++ b/crates/ide/src/display.rs | |||
@@ -5,6 +5,5 @@ pub(crate) mod navigation_target; | |||
5 | mod short_label; | 5 | mod short_label; |
6 | 6 | ||
7 | pub(crate) use navigation_target::{ToNav, TryToNav}; | 7 | pub(crate) use navigation_target::{ToNav, TryToNav}; |
8 | pub(crate) use short_label::ShortLabel; | ||
9 | 8 | ||
10 | pub(crate) use syntax::display::{function_declaration, macro_label}; | 9 | pub(crate) use syntax::display::{function_declaration, macro_label}; |
diff --git a/crates/ide/src/display/navigation_target.rs b/crates/ide/src/display/navigation_target.rs index 198243466..69c3751a1 100644 --- a/crates/ide/src/display/navigation_target.rs +++ b/crates/ide/src/display/navigation_target.rs | |||
@@ -3,7 +3,9 @@ | |||
3 | use std::fmt; | 3 | use std::fmt; |
4 | 4 | ||
5 | use either::Either; | 5 | use either::Either; |
6 | use hir::{AssocItem, Documentation, FieldSource, HasAttrs, HasSource, InFile, ModuleSource}; | 6 | use hir::{ |
7 | AssocItem, Documentation, FieldSource, HasAttrs, HasSource, HirDisplay, InFile, ModuleSource, | ||
8 | }; | ||
7 | use ide_db::{ | 9 | use ide_db::{ |
8 | base_db::{FileId, FileRange, SourceDatabase}, | 10 | base_db::{FileId, FileRange, SourceDatabase}, |
9 | symbol_index::FileSymbolKind, | 11 | symbol_index::FileSymbolKind, |
@@ -98,7 +100,7 @@ impl NavigationTarget { | |||
98 | SymbolKind::Module, | 100 | SymbolKind::Module, |
99 | ); | 101 | ); |
100 | res.docs = module.attrs(db).docs(); | 102 | res.docs = module.attrs(db).docs(); |
101 | res.description = src.value.short_label(); | 103 | res.description = Some(module.display(db).to_string()); |
102 | return res; | 104 | return res; |
103 | } | 105 | } |
104 | module.to_nav(db) | 106 | module.to_nav(db) |
@@ -251,8 +253,8 @@ impl ToNavFromAst for hir::Trait { | |||
251 | 253 | ||
252 | impl<D> TryToNav for D | 254 | impl<D> TryToNav for D |
253 | where | 255 | where |
254 | D: HasSource + ToNavFromAst + Copy + HasAttrs, | 256 | D: HasSource + ToNavFromAst + Copy + HasAttrs + HirDisplay, |
255 | D::Ast: ast::NameOwner + ShortLabel, | 257 | D::Ast: ast::NameOwner, |
256 | { | 258 | { |
257 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { | 259 | fn try_to_nav(&self, db: &RootDatabase) -> Option<NavigationTarget> { |
258 | let src = self.source(db)?; | 260 | let src = self.source(db)?; |
@@ -262,7 +264,7 @@ where | |||
262 | D::KIND, | 264 | D::KIND, |
263 | ); | 265 | ); |
264 | res.docs = self.docs(db); | 266 | res.docs = self.docs(db); |
265 | res.description = src.value.short_label(); | 267 | res.description = Some(self.display(db).to_string()); |
266 | Some(res) | 268 | Some(res) |
267 | } | 269 | } |
268 | } | 270 | } |
@@ -317,7 +319,7 @@ impl TryToNav for hir::Field { | |||
317 | let mut res = | 319 | let mut res = |
318 | NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); | 320 | NavigationTarget::from_named(db, src.with_value(it), SymbolKind::Field); |
319 | res.docs = self.docs(db); | 321 | res.docs = self.docs(db); |
320 | res.description = it.short_label(); | 322 | res.description = Some(self.display(db).to_string()); |
321 | res | 323 | res |
322 | } | 324 | } |
323 | FieldSource::Pos(it) => { | 325 | FieldSource::Pos(it) => { |
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index ea45086ce..a35805c5e 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use either::Either; | 1 | use either::Either; |
2 | use hir::{ | 2 | use hir::{ |
3 | Adt, AsAssocItem, AssocItemContainer, FieldSource, GenericParam, HasAttrs, HasSource, | 3 | Adt, AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, Module, |
4 | HirDisplay, Module, ModuleDef, ModuleSource, Semantics, | 4 | ModuleDef, Semantics, |
5 | }; | 5 | }; |
6 | use ide_db::{ | 6 | use ide_db::{ |
7 | base_db::SourceDatabase, | 7 | base_db::SourceDatabase, |
@@ -14,7 +14,7 @@ use stdx::format_to; | |||
14 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; | 14 | use syntax::{ast, match_ast, AstNode, SyntaxKind::*, SyntaxToken, TokenAtOffset, T}; |
15 | 15 | ||
16 | use crate::{ | 16 | use crate::{ |
17 | display::{macro_label, ShortLabel, TryToNav}, | 17 | display::{macro_label, TryToNav}, |
18 | doc_links::{remove_links, rewrite_links}, | 18 | doc_links::{remove_links, rewrite_links}, |
19 | markdown_remove::remove_markdown, | 19 | markdown_remove::remove_markdown, |
20 | markup::Markup, | 20 | markup::Markup, |
@@ -335,34 +335,18 @@ fn hover_for_definition( | |||
335 | let label = macro_label(&it.source(db)?.value); | 335 | let label = macro_label(&it.source(db)?.value); |
336 | from_def_source_labeled(db, it, Some(label), mod_path) | 336 | from_def_source_labeled(db, it, Some(label), mod_path) |
337 | } | 337 | } |
338 | Definition::Field(def) => { | 338 | Definition::Field(def) => from_hir_fmt(db, def, mod_path), |
339 | let src = def.source(db)?.value; | ||
340 | if let FieldSource::Named(it) = src { | ||
341 | from_def_source_labeled(db, def, it.short_label(), mod_path) | ||
342 | } else { | ||
343 | None | ||
344 | } | ||
345 | } | ||
346 | Definition::ModuleDef(it) => match it { | 339 | Definition::ModuleDef(it) => match it { |
347 | ModuleDef::Module(it) => from_def_source_labeled( | 340 | ModuleDef::Module(it) => from_hir_fmt(db, it, mod_path), |
348 | db, | 341 | ModuleDef::Function(it) => from_hir_fmt(db, it, mod_path), |
349 | it, | 342 | ModuleDef::Adt(Adt::Struct(it)) => from_hir_fmt(db, it, mod_path), |
350 | match it.definition_source(db).value { | 343 | ModuleDef::Adt(Adt::Union(it)) => from_hir_fmt(db, it, mod_path), |
351 | ModuleSource::Module(it) => it.short_label(), | 344 | ModuleDef::Adt(Adt::Enum(it)) => from_hir_fmt(db, it, mod_path), |
352 | ModuleSource::SourceFile(it) => it.short_label(), | 345 | ModuleDef::Variant(it) => from_hir_fmt(db, it, mod_path), |
353 | ModuleSource::BlockExpr(it) => it.short_label(), | 346 | ModuleDef::Const(it) => from_hir_fmt(db, it, mod_path), |
354 | }, | 347 | ModuleDef::Static(it) => from_hir_fmt(db, it, mod_path), |
355 | mod_path, | 348 | ModuleDef::Trait(it) => from_hir_fmt(db, it, mod_path), |
356 | ), | 349 | ModuleDef::TypeAlias(it) => from_hir_fmt(db, it, mod_path), |
357 | ModuleDef::Function(it) => from_def_source(db, it, mod_path), | ||
358 | ModuleDef::Adt(Adt::Struct(it)) => from_def_source(db, it, mod_path), | ||
359 | ModuleDef::Adt(Adt::Union(it)) => from_def_source(db, it, mod_path), | ||
360 | ModuleDef::Adt(Adt::Enum(it)) => from_def_source(db, it, mod_path), | ||
361 | ModuleDef::Variant(it) => from_def_source(db, it, mod_path), | ||
362 | ModuleDef::Const(it) => from_def_source(db, it, mod_path), | ||
363 | ModuleDef::Static(it) => from_def_source(db, it, mod_path), | ||
364 | ModuleDef::Trait(it) => from_def_source(db, it, mod_path), | ||
365 | ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path), | ||
366 | ModuleDef::BuiltinType(it) => famous_defs | 350 | ModuleDef::BuiltinType(it) => famous_defs |
367 | .and_then(|fd| hover_for_builtin(fd, it)) | 351 | .and_then(|fd| hover_for_builtin(fd, it)) |
368 | .or_else(|| Some(Markup::fenced_block(&it.name()))), | 352 | .or_else(|| Some(Markup::fenced_block(&it.name()))), |
@@ -370,26 +354,25 @@ fn hover_for_definition( | |||
370 | Definition::Local(it) => hover_for_local(it, db), | 354 | Definition::Local(it) => hover_for_local(it, db), |
371 | Definition::SelfType(impl_def) => { | 355 | Definition::SelfType(impl_def) => { |
372 | impl_def.target_ty(db).as_adt().and_then(|adt| match adt { | 356 | impl_def.target_ty(db).as_adt().and_then(|adt| match adt { |
373 | Adt::Struct(it) => from_def_source(db, it, mod_path), | 357 | Adt::Struct(it) => from_hir_fmt(db, it, mod_path), |
374 | Adt::Union(it) => from_def_source(db, it, mod_path), | 358 | Adt::Union(it) => from_hir_fmt(db, it, mod_path), |
375 | Adt::Enum(it) => from_def_source(db, it, mod_path), | 359 | Adt::Enum(it) => from_hir_fmt(db, it, mod_path), |
376 | }) | 360 | }) |
377 | } | 361 | } |
378 | Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))), | 362 | Definition::Label(it) => Some(Markup::fenced_block(&it.name(db))), |
379 | Definition::GenericParam(it) => match it { | 363 | Definition::GenericParam(it) => match it { |
380 | GenericParam::TypeParam(it) => Some(Markup::fenced_block(&it.display(db))), | 364 | GenericParam::TypeParam(it) => Some(Markup::fenced_block(&it.display(db))), |
381 | GenericParam::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))), | 365 | GenericParam::LifetimeParam(it) => Some(Markup::fenced_block(&it.name(db))), |
382 | GenericParam::ConstParam(it) => from_def_source(db, it, None), | 366 | GenericParam::ConstParam(it) => Some(Markup::fenced_block(&it.display(db))), |
383 | }, | 367 | }, |
384 | }; | 368 | }; |
385 | 369 | ||
386 | fn from_def_source<A, D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup> | 370 | fn from_hir_fmt<D>(db: &RootDatabase, def: D, mod_path: Option<String>) -> Option<Markup> |
387 | where | 371 | where |
388 | D: HasSource<Ast = A> + HasAttrs + Copy, | 372 | D: HasAttrs + HirDisplay, |
389 | A: ShortLabel, | ||
390 | { | 373 | { |
391 | let short_label = def.source(db)?.value.short_label(); | 374 | let label = def.display(db).to_string(); |
392 | from_def_source_labeled(db, def, short_label, mod_path) | 375 | from_def_source_labeled(db, def, Some(label), mod_path) |
393 | } | 376 | } |
394 | 377 | ||
395 | fn from_def_source_labeled<D>( | 378 | fn from_def_source_labeled<D>( |
@@ -670,7 +653,9 @@ fn main() { let foo_test = fo$0o(); } | |||
670 | ``` | 653 | ``` |
671 | 654 | ||
672 | ```rust | 655 | ```rust |
673 | pub fn foo<'a, T: AsRef<str>>(b: &'a T) -> &'a str | 656 | pub fn foo<'a, T>(b: &'a T) -> &'a str |
657 | where | ||
658 | T: AsRef<str>, | ||
674 | ``` | 659 | ``` |
675 | "#]], | 660 | "#]], |
676 | ); | 661 | ); |
@@ -878,7 +863,7 @@ fn main() { So$0me(12); } | |||
878 | ``` | 863 | ``` |
879 | 864 | ||
880 | ```rust | 865 | ```rust |
881 | Some | 866 | Some(T) |
882 | ``` | 867 | ``` |
883 | "#]], | 868 | "#]], |
884 | ); | 869 | ); |
@@ -944,7 +929,7 @@ fn main() { | |||
944 | ``` | 929 | ``` |
945 | 930 | ||
946 | ```rust | 931 | ```rust |
947 | Some | 932 | Some(T) |
948 | ``` | 933 | ``` |
949 | 934 | ||
950 | --- | 935 | --- |
@@ -1441,13 +1426,14 @@ fn bar() { fo$0o(); } | |||
1441 | ``` | 1426 | ``` |
1442 | "#]], | 1427 | "#]], |
1443 | ); | 1428 | ); |
1429 | // Top level `pub(crate)` will be displayed as no visibility. | ||
1444 | check( | 1430 | check( |
1445 | r#"pub(crate) async unsafe extern "C" fn foo$0() {}"#, | 1431 | r#"mod m { pub(crate) async unsafe extern "C" fn foo$0() {} }"#, |
1446 | expect![[r#" | 1432 | expect![[r#" |
1447 | *foo* | 1433 | *foo* |
1448 | 1434 | ||
1449 | ```rust | 1435 | ```rust |
1450 | test | 1436 | test::m |
1451 | ``` | 1437 | ``` |
1452 | 1438 | ||
1453 | ```rust | 1439 | ```rust |
@@ -1489,11 +1475,18 @@ extern crate st$0d; | |||
1489 | //! abc123 | 1475 | //! abc123 |
1490 | "#, | 1476 | "#, |
1491 | expect![[r#" | 1477 | expect![[r#" |
1492 | *std* | 1478 | *std* |
1493 | Standard library for this test | 1479 | |
1480 | ```rust | ||
1481 | extern crate std | ||
1482 | ``` | ||
1494 | 1483 | ||
1495 | Printed? | 1484 | --- |
1496 | abc123 | 1485 | |
1486 | Standard library for this test | ||
1487 | |||
1488 | Printed? | ||
1489 | abc123 | ||
1497 | "#]], | 1490 | "#]], |
1498 | ); | 1491 | ); |
1499 | check( | 1492 | check( |
@@ -1507,11 +1500,18 @@ extern crate std as ab$0c; | |||
1507 | //! abc123 | 1500 | //! abc123 |
1508 | "#, | 1501 | "#, |
1509 | expect![[r#" | 1502 | expect![[r#" |
1510 | *abc* | 1503 | *abc* |
1511 | Standard library for this test | 1504 | |
1505 | ```rust | ||
1506 | extern crate std | ||
1507 | ``` | ||
1508 | |||
1509 | --- | ||
1512 | 1510 | ||
1513 | Printed? | 1511 | Standard library for this test |
1514 | abc123 | 1512 | |
1513 | Printed? | ||
1514 | abc123 | ||
1515 | "#]], | 1515 | "#]], |
1516 | ); | 1516 | ); |
1517 | } | 1517 | } |
@@ -2021,7 +2021,7 @@ enum E { | |||
2021 | ``` | 2021 | ``` |
2022 | 2022 | ||
2023 | ```rust | 2023 | ```rust |
2024 | V | 2024 | V { field: i32 } |
2025 | ``` | 2025 | ``` |
2026 | 2026 | ||
2027 | --- | 2027 | --- |
@@ -2417,7 +2417,7 @@ fn main() { let s$0t = S{ f1:Arg(0) }; } | |||
2417 | focus_range: 24..25, | 2417 | focus_range: 24..25, |
2418 | name: "S", | 2418 | name: "S", |
2419 | kind: Struct, | 2419 | kind: Struct, |
2420 | description: "struct S", | 2420 | description: "struct S<T>", |
2421 | }, | 2421 | }, |
2422 | }, | 2422 | }, |
2423 | HoverGotoTypeData { | 2423 | HoverGotoTypeData { |
@@ -2463,7 +2463,7 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; } | |||
2463 | focus_range: 24..25, | 2463 | focus_range: 24..25, |
2464 | name: "S", | 2464 | name: "S", |
2465 | kind: Struct, | 2465 | kind: Struct, |
2466 | description: "struct S", | 2466 | description: "struct S<T>", |
2467 | }, | 2467 | }, |
2468 | }, | 2468 | }, |
2469 | HoverGotoTypeData { | 2469 | HoverGotoTypeData { |
@@ -2605,7 +2605,7 @@ fn main() { let s$0t = foo(); } | |||
2605 | focus_range: 6..9, | 2605 | focus_range: 6..9, |
2606 | name: "Foo", | 2606 | name: "Foo", |
2607 | kind: Trait, | 2607 | kind: Trait, |
2608 | description: "trait Foo", | 2608 | description: "trait Foo<T>", |
2609 | }, | 2609 | }, |
2610 | }, | 2610 | }, |
2611 | HoverGotoTypeData { | 2611 | HoverGotoTypeData { |
@@ -2702,7 +2702,7 @@ fn main() { let s$0t = foo(); } | |||
2702 | focus_range: 6..9, | 2702 | focus_range: 6..9, |
2703 | name: "Foo", | 2703 | name: "Foo", |
2704 | kind: Trait, | 2704 | kind: Trait, |
2705 | description: "trait Foo", | 2705 | description: "trait Foo<T>", |
2706 | }, | 2706 | }, |
2707 | }, | 2707 | }, |
2708 | HoverGotoTypeData { | 2708 | HoverGotoTypeData { |
@@ -2715,7 +2715,7 @@ fn main() { let s$0t = foo(); } | |||
2715 | focus_range: 22..25, | 2715 | focus_range: 22..25, |
2716 | name: "Bar", | 2716 | name: "Bar", |
2717 | kind: Trait, | 2717 | kind: Trait, |
2718 | description: "trait Bar", | 2718 | description: "trait Bar<T>", |
2719 | }, | 2719 | }, |
2720 | }, | 2720 | }, |
2721 | HoverGotoTypeData { | 2721 | HoverGotoTypeData { |
@@ -2819,7 +2819,7 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {} | |||
2819 | focus_range: 19..22, | 2819 | focus_range: 19..22, |
2820 | name: "Bar", | 2820 | name: "Bar", |
2821 | kind: Trait, | 2821 | kind: Trait, |
2822 | description: "trait Bar", | 2822 | description: "trait Bar<T>", |
2823 | }, | 2823 | }, |
2824 | }, | 2824 | }, |
2825 | HoverGotoTypeData { | 2825 | HoverGotoTypeData { |
@@ -2916,7 +2916,7 @@ fn foo(ar$0g: &impl Foo<S>) {} | |||
2916 | focus_range: 6..9, | 2916 | focus_range: 6..9, |
2917 | name: "Foo", | 2917 | name: "Foo", |
2918 | kind: Trait, | 2918 | kind: Trait, |
2919 | description: "trait Foo", | 2919 | description: "trait Foo<T>", |
2920 | }, | 2920 | }, |
2921 | }, | 2921 | }, |
2922 | HoverGotoTypeData { | 2922 | HoverGotoTypeData { |
@@ -2966,7 +2966,7 @@ fn main() { let s$0t = foo(); } | |||
2966 | focus_range: 49..50, | 2966 | focus_range: 49..50, |
2967 | name: "B", | 2967 | name: "B", |
2968 | kind: Struct, | 2968 | kind: Struct, |
2969 | description: "struct B", | 2969 | description: "struct B<T>", |
2970 | }, | 2970 | }, |
2971 | }, | 2971 | }, |
2972 | HoverGotoTypeData { | 2972 | HoverGotoTypeData { |
@@ -3042,7 +3042,7 @@ fn foo(ar$0g: &dyn Foo<S>) {} | |||
3042 | focus_range: 6..9, | 3042 | focus_range: 6..9, |
3043 | name: "Foo", | 3043 | name: "Foo", |
3044 | kind: Trait, | 3044 | kind: Trait, |
3045 | description: "trait Foo", | 3045 | description: "trait Foo<T>", |
3046 | }, | 3046 | }, |
3047 | }, | 3047 | }, |
3048 | HoverGotoTypeData { | 3048 | HoverGotoTypeData { |
@@ -3090,7 +3090,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {} | |||
3090 | focus_range: 6..15, | 3090 | focus_range: 6..15, |
3091 | name: "ImplTrait", | 3091 | name: "ImplTrait", |
3092 | kind: Trait, | 3092 | kind: Trait, |
3093 | description: "trait ImplTrait", | 3093 | description: "trait ImplTrait<T>", |
3094 | }, | 3094 | }, |
3095 | }, | 3095 | }, |
3096 | HoverGotoTypeData { | 3096 | HoverGotoTypeData { |
@@ -3103,7 +3103,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {} | |||
3103 | focus_range: 50..51, | 3103 | focus_range: 50..51, |
3104 | name: "B", | 3104 | name: "B", |
3105 | kind: Struct, | 3105 | kind: Struct, |
3106 | description: "struct B", | 3106 | description: "struct B<T>", |
3107 | }, | 3107 | }, |
3108 | }, | 3108 | }, |
3109 | HoverGotoTypeData { | 3109 | HoverGotoTypeData { |
@@ -3116,7 +3116,7 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {} | |||
3116 | focus_range: 28..36, | 3116 | focus_range: 28..36, |
3117 | name: "DynTrait", | 3117 | name: "DynTrait", |
3118 | kind: Trait, | 3118 | kind: Trait, |
3119 | description: "trait DynTrait", | 3119 | description: "trait DynTrait<T>", |
3120 | }, | 3120 | }, |
3121 | }, | 3121 | }, |
3122 | HoverGotoTypeData { | 3122 | HoverGotoTypeData { |
@@ -3582,6 +3582,17 @@ mod foo$0; | |||
3582 | "#, | 3582 | "#, |
3583 | expect![[r#" | 3583 | expect![[r#" |
3584 | *foo* | 3584 | *foo* |
3585 | |||
3586 | ```rust | ||
3587 | test | ||
3588 | ``` | ||
3589 | |||
3590 | ```rust | ||
3591 | mod foo | ||
3592 | ``` | ||
3593 | |||
3594 | --- | ||
3595 | |||
3585 | For the horde! | 3596 | For the horde! |
3586 | "#]], | 3597 | "#]], |
3587 | ); | 3598 | ); |
@@ -3606,7 +3617,7 @@ use foo::bar::{self$0}; | |||
3606 | ``` | 3617 | ``` |
3607 | 3618 | ||
3608 | ```rust | 3619 | ```rust |
3609 | pub mod bar | 3620 | mod bar |
3610 | ``` | 3621 | ``` |
3611 | 3622 | ||
3612 | --- | 3623 | --- |
@@ -3657,4 +3668,43 @@ cosnt _: &str$0 = ""; }"#; | |||
3657 | "#]], | 3668 | "#]], |
3658 | ); | 3669 | ); |
3659 | } | 3670 | } |
3671 | |||
3672 | #[test] | ||
3673 | fn hover_macro_expanded_function() { | ||
3674 | check( | ||
3675 | r#" | ||
3676 | struct S<'a, T>(&'a T); | ||
3677 | trait Clone {} | ||
3678 | macro_rules! foo { | ||
3679 | () => { | ||
3680 | fn bar<'t, T: Clone + 't>(s: &mut S<'t, T>, t: u32) -> *mut u32 where | ||
3681 | 't: 't + 't, | ||
3682 | for<'a> T: Clone + 'a | ||
3683 | { 0 as _ } | ||
3684 | }; | ||
3685 | } | ||
3686 | |||
3687 | foo!(); | ||
3688 | |||
3689 | fn main() { | ||
3690 | bar$0; | ||
3691 | } | ||
3692 | "#, | ||
3693 | expect![[r#" | ||
3694 | *bar* | ||
3695 | |||
3696 | ```rust | ||
3697 | test | ||
3698 | ``` | ||
3699 | |||
3700 | ```rust | ||
3701 | fn bar<'t, T>(s: &mut S<'t, T>, t: u32) -> *mut u32 | ||
3702 | where | ||
3703 | T: Clone + 't, | ||
3704 | 't: 't + 't, | ||
3705 | for<'a> T: Clone + 'a, | ||
3706 | ``` | ||
3707 | "#]], | ||
3708 | ) | ||
3709 | } | ||
3660 | } | 3710 | } |
diff --git a/crates/ide_completion/src/completions/dot.rs b/crates/ide_completion/src/completions/dot.rs index 5ee9a9f07..cec2d0c3a 100644 --- a/crates/ide_completion/src/completions/dot.rs +++ b/crates/ide_completion/src/completions/dot.rs | |||
@@ -81,7 +81,7 @@ fn foo(s: S) { s.$0 } | |||
81 | "#, | 81 | "#, |
82 | expect![[r#" | 82 | expect![[r#" |
83 | fd foo u32 | 83 | fd foo u32 |
84 | me bar() -> () | 84 | me bar() fn(&self) |
85 | "#]], | 85 | "#]], |
86 | ); | 86 | ); |
87 | } | 87 | } |
@@ -97,7 +97,7 @@ impl S { | |||
97 | "#, | 97 | "#, |
98 | expect![[r#" | 98 | expect![[r#" |
99 | fd the_field (u32,) | 99 | fd the_field (u32,) |
100 | me foo() -> () | 100 | me foo() fn(self) |
101 | "#]], | 101 | "#]], |
102 | ) | 102 | ) |
103 | } | 103 | } |
@@ -113,7 +113,7 @@ impl A { | |||
113 | "#, | 113 | "#, |
114 | expect![[r#" | 114 | expect![[r#" |
115 | fd the_field (u32, i32) | 115 | fd the_field (u32, i32) |
116 | me foo() -> () | 116 | me foo() fn(&self) |
117 | "#]], | 117 | "#]], |
118 | ) | 118 | ) |
119 | } | 119 | } |
@@ -163,7 +163,7 @@ mod m { | |||
163 | fn foo(a: A) { a.$0 } | 163 | fn foo(a: A) { a.$0 } |
164 | "#, | 164 | "#, |
165 | expect![[r#" | 165 | expect![[r#" |
166 | me the_method() -> () | 166 | me the_method() fn(&self) |
167 | "#]], | 167 | "#]], |
168 | ); | 168 | ); |
169 | } | 169 | } |
@@ -196,7 +196,7 @@ impl A<i32> { | |||
196 | fn foo(a: A<u32>) { a.$0 } | 196 | fn foo(a: A<u32>) { a.$0 } |
197 | "#, | 197 | "#, |
198 | expect![[r#" | 198 | expect![[r#" |
199 | me the_method() -> () | 199 | me the_method() fn(&self) |
200 | "#]], | 200 | "#]], |
201 | ) | 201 | ) |
202 | } | 202 | } |
@@ -211,7 +211,7 @@ impl Trait for A {} | |||
211 | fn foo(a: A) { a.$0 } | 211 | fn foo(a: A) { a.$0 } |
212 | "#, | 212 | "#, |
213 | expect![[r#" | 213 | expect![[r#" |
214 | me the_method() -> () | 214 | me the_method() fn(&self) |
215 | "#]], | 215 | "#]], |
216 | ); | 216 | ); |
217 | } | 217 | } |
@@ -226,7 +226,7 @@ impl<T> Trait for T {} | |||
226 | fn foo(a: &A) { a.$0 } | 226 | fn foo(a: &A) { a.$0 } |
227 | ", | 227 | ", |
228 | expect![[r#" | 228 | expect![[r#" |
229 | me the_method() -> () | 229 | me the_method() fn(&self) |
230 | "#]], | 230 | "#]], |
231 | ); | 231 | ); |
232 | } | 232 | } |
@@ -244,7 +244,7 @@ impl Trait for A {} | |||
244 | fn foo(a: A) { a.$0 } | 244 | fn foo(a: A) { a.$0 } |
245 | ", | 245 | ", |
246 | expect![[r#" | 246 | expect![[r#" |
247 | me the_method() -> () | 247 | me the_method() fn(&self) |
248 | "#]], | 248 | "#]], |
249 | ); | 249 | ); |
250 | } | 250 | } |
@@ -298,7 +298,7 @@ impl T { | |||
298 | } | 298 | } |
299 | "#, | 299 | "#, |
300 | expect![[r#" | 300 | expect![[r#" |
301 | me blah() -> () | 301 | me blah() fn(&self) |
302 | "#]], | 302 | "#]], |
303 | ); | 303 | ); |
304 | } | 304 | } |
@@ -407,7 +407,7 @@ fn foo() { | |||
407 | } | 407 | } |
408 | "#, | 408 | "#, |
409 | expect![[r#" | 409 | expect![[r#" |
410 | me the_method() -> () | 410 | me the_method() fn(&self) |
411 | "#]], | 411 | "#]], |
412 | ); | 412 | ); |
413 | } | 413 | } |
@@ -422,7 +422,7 @@ macro_rules! make_s { () => { S }; } | |||
422 | fn main() { make_s!().f$0; } | 422 | fn main() { make_s!().f$0; } |
423 | "#, | 423 | "#, |
424 | expect![[r#" | 424 | expect![[r#" |
425 | me foo() -> () | 425 | me foo() fn(&self) |
426 | "#]], | 426 | "#]], |
427 | ) | 427 | ) |
428 | } | 428 | } |
@@ -450,7 +450,7 @@ mod foo { | |||
450 | } | 450 | } |
451 | "#, | 451 | "#, |
452 | expect![[r#" | 452 | expect![[r#" |
453 | me private() -> () | 453 | me private() fn(&self) |
454 | "#]], | 454 | "#]], |
455 | ); | 455 | ); |
456 | } | 456 | } |
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs index 391a11c91..08df2df3f 100644 --- a/crates/ide_completion/src/completions/flyimport.rs +++ b/crates/ide_completion/src/completions/flyimport.rs | |||
@@ -402,7 +402,7 @@ fn main() { | |||
402 | check( | 402 | check( |
403 | fixture, | 403 | fixture, |
404 | expect![[r#" | 404 | expect![[r#" |
405 | fn weird_function() (dep::test_mod::TestTrait) -> () | 405 | fn weird_function() (dep::test_mod::TestTrait) fn() |
406 | "#]], | 406 | "#]], |
407 | ); | 407 | ); |
408 | 408 | ||
@@ -495,7 +495,7 @@ fn main() { | |||
495 | check( | 495 | check( |
496 | fixture, | 496 | fixture, |
497 | expect![[r#" | 497 | expect![[r#" |
498 | me random_method() (dep::test_mod::TestTrait) -> () | 498 | me random_method() (dep::test_mod::TestTrait) fn(&self) |
499 | "#]], | 499 | "#]], |
500 | ); | 500 | ); |
501 | 501 | ||
@@ -665,7 +665,7 @@ fn main() { | |||
665 | } | 665 | } |
666 | "#, | 666 | "#, |
667 | expect![[r#" | 667 | expect![[r#" |
668 | me random_method() (dep::test_mod::TestTrait) -> () DEPRECATED | 668 | me random_method() (dep::test_mod::TestTrait) fn(&self) DEPRECATED |
669 | "#]], | 669 | "#]], |
670 | ); | 670 | ); |
671 | 671 | ||
@@ -696,7 +696,7 @@ fn main() { | |||
696 | "#, | 696 | "#, |
697 | expect![[r#" | 697 | expect![[r#" |
698 | ct SPECIAL_CONST (dep::test_mod::TestTrait) DEPRECATED | 698 | ct SPECIAL_CONST (dep::test_mod::TestTrait) DEPRECATED |
699 | fn weird_function() (dep::test_mod::TestTrait) -> () DEPRECATED | 699 | fn weird_function() (dep::test_mod::TestTrait) fn() DEPRECATED |
700 | "#]], | 700 | "#]], |
701 | ); | 701 | ); |
702 | } | 702 | } |
diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs index df74b739e..105ff6013 100644 --- a/crates/ide_completion/src/completions/qualified_path.rs +++ b/crates/ide_completion/src/completions/qualified_path.rs | |||
@@ -359,8 +359,8 @@ impl S { | |||
359 | fn foo() { let _ = S::$0 } | 359 | fn foo() { let _ = S::$0 } |
360 | "#, | 360 | "#, |
361 | expect![[r#" | 361 | expect![[r#" |
362 | fn a() -> () | 362 | fn a() fn() |
363 | me b(…) -> () | 363 | me b(…) fn(&self) |
364 | ct C const C: i32 = 42; | 364 | ct C const C: i32 = 42; |
365 | ta T type T = i32; | 365 | ta T type T = i32; |
366 | "#]], | 366 | "#]], |
@@ -387,7 +387,7 @@ mod m { | |||
387 | fn foo() { let _ = S::$0 } | 387 | fn foo() { let _ = S::$0 } |
388 | "#, | 388 | "#, |
389 | expect![[r#" | 389 | expect![[r#" |
390 | fn public_method() -> () | 390 | fn public_method() fn() |
391 | ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1; | 391 | ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1; |
392 | ta PublicType pub(crate) type PublicType = u32; | 392 | ta PublicType pub(crate) type PublicType = u32; |
393 | "#]], | 393 | "#]], |
@@ -404,7 +404,7 @@ impl E { fn m() { } } | |||
404 | fn foo() { let _ = E::$0 } | 404 | fn foo() { let _ = E::$0 } |
405 | "#, | 405 | "#, |
406 | expect![[r#" | 406 | expect![[r#" |
407 | fn m() -> () | 407 | fn m() fn() |
408 | "#]], | 408 | "#]], |
409 | ); | 409 | ); |
410 | } | 410 | } |
@@ -419,7 +419,7 @@ impl U { fn m() { } } | |||
419 | fn foo() { let _ = U::$0 } | 419 | fn foo() { let _ = U::$0 } |
420 | "#, | 420 | "#, |
421 | expect![[r#" | 421 | expect![[r#" |
422 | fn m() -> () | 422 | fn m() fn() |
423 | "#]], | 423 | "#]], |
424 | ); | 424 | ); |
425 | } | 425 | } |
@@ -449,7 +449,7 @@ trait Trait { fn m(); } | |||
449 | fn foo() { let _ = Trait::$0 } | 449 | fn foo() { let _ = Trait::$0 } |
450 | "#, | 450 | "#, |
451 | expect![[r#" | 451 | expect![[r#" |
452 | fn m() -> () | 452 | fn m() fn() |
453 | "#]], | 453 | "#]], |
454 | ); | 454 | ); |
455 | } | 455 | } |
@@ -466,7 +466,7 @@ impl Trait for S {} | |||
466 | fn foo() { let _ = S::$0 } | 466 | fn foo() { let _ = S::$0 } |
467 | "#, | 467 | "#, |
468 | expect![[r#" | 468 | expect![[r#" |
469 | fn m() -> () | 469 | fn m() fn() |
470 | "#]], | 470 | "#]], |
471 | ); | 471 | ); |
472 | } | 472 | } |
@@ -483,7 +483,7 @@ impl Trait for S {} | |||
483 | fn foo() { let _ = <S as Trait>::$0 } | 483 | fn foo() { let _ = <S as Trait>::$0 } |
484 | "#, | 484 | "#, |
485 | expect![[r#" | 485 | expect![[r#" |
486 | fn m() -> () | 486 | fn m() fn() |
487 | "#]], | 487 | "#]], |
488 | ); | 488 | ); |
489 | } | 489 | } |
@@ -512,11 +512,11 @@ fn foo<T: Sub>() { T::$0 } | |||
512 | ta SubTy type SubTy; | 512 | ta SubTy type SubTy; |
513 | ta Ty type Ty; | 513 | ta Ty type Ty; |
514 | ct C2 const C2: (); | 514 | ct C2 const C2: (); |
515 | fn subfunc() -> () | 515 | fn subfunc() fn() |
516 | me submethod(…) -> () | 516 | me submethod(…) fn(&self) |
517 | ct CONST const CONST: u8; | 517 | ct CONST const CONST: u8; |
518 | fn func() -> () | 518 | fn func() fn() |
519 | me method(…) -> () | 519 | me method(…) fn(&self) |
520 | "#]], | 520 | "#]], |
521 | ); | 521 | ); |
522 | } | 522 | } |
@@ -552,11 +552,11 @@ impl<T> Sub for Wrap<T> { | |||
552 | ta SubTy type SubTy; | 552 | ta SubTy type SubTy; |
553 | ta Ty type Ty; | 553 | ta Ty type Ty; |
554 | ct CONST const CONST: u8 = 0; | 554 | ct CONST const CONST: u8 = 0; |
555 | fn func() -> () | 555 | fn func() fn() |
556 | me method(…) -> () | 556 | me method(…) fn(&self) |
557 | ct C2 const C2: () = (); | 557 | ct C2 const C2: () = (); |
558 | fn subfunc() -> () | 558 | fn subfunc() fn() |
559 | me submethod(…) -> () | 559 | me submethod(…) fn(&self) |
560 | "#]], | 560 | "#]], |
561 | ); | 561 | ); |
562 | } | 562 | } |
@@ -573,8 +573,8 @@ impl T { fn bar() {} } | |||
573 | fn main() { T::$0; } | 573 | fn main() { T::$0; } |
574 | "#, | 574 | "#, |
575 | expect![[r#" | 575 | expect![[r#" |
576 | fn foo() -> () | 576 | fn foo() fn() |
577 | fn bar() -> () | 577 | fn bar() fn() |
578 | "#]], | 578 | "#]], |
579 | ); | 579 | ); |
580 | } | 580 | } |
@@ -589,7 +589,7 @@ macro_rules! foo { () => {} } | |||
589 | fn main() { let _ = crate::$0 } | 589 | fn main() { let _ = crate::$0 } |
590 | "#, | 590 | "#, |
591 | expect![[r##" | 591 | expect![[r##" |
592 | fn main() -> () | 592 | fn main() fn() |
593 | ma foo!(…) #[macro_export] macro_rules! foo | 593 | ma foo!(…) #[macro_export] macro_rules! foo |
594 | "##]], | 594 | "##]], |
595 | ); | 595 | ); |
@@ -633,7 +633,7 @@ mod p { | |||
633 | "#, | 633 | "#, |
634 | expect![[r#" | 634 | expect![[r#" |
635 | ct RIGHT_CONST | 635 | ct RIGHT_CONST |
636 | fn right_fn() -> () | 636 | fn right_fn() fn() |
637 | st RightType | 637 | st RightType |
638 | "#]], | 638 | "#]], |
639 | ); | 639 | ); |
@@ -680,8 +680,8 @@ fn main() { m!(self::f$0); } | |||
680 | fn foo() {} | 680 | fn foo() {} |
681 | "#, | 681 | "#, |
682 | expect![[r#" | 682 | expect![[r#" |
683 | fn main() -> () | 683 | fn main() fn() |
684 | fn foo() -> () | 684 | fn foo() fn() |
685 | "#]], | 685 | "#]], |
686 | ); | 686 | ); |
687 | } | 687 | } |
@@ -699,7 +699,7 @@ mod m { | |||
699 | "#, | 699 | "#, |
700 | expect![[r#" | 700 | expect![[r#" |
701 | md z | 701 | md z |
702 | fn z() -> () | 702 | fn z() fn() |
703 | "#]], | 703 | "#]], |
704 | ); | 704 | ); |
705 | } | 705 | } |
@@ -719,7 +719,7 @@ fn foo() { | |||
719 | } | 719 | } |
720 | "#, | 720 | "#, |
721 | expect![[r#" | 721 | expect![[r#" |
722 | fn new() -> HashMap<K, V, RandomState> | 722 | fn new() fn() -> HashMap<K, V, RandomState> |
723 | "#]], | 723 | "#]], |
724 | ); | 724 | ); |
725 | } | 725 | } |
@@ -752,8 +752,8 @@ fn main() { | |||
752 | } | 752 | } |
753 | "#, | 753 | "#, |
754 | expect![[r#" | 754 | expect![[r#" |
755 | fn main() -> () | 755 | fn main() fn() |
756 | fn foo(…) -> () | 756 | fn foo(…) fn(i32, i32) |
757 | "#]], | 757 | "#]], |
758 | ); | 758 | ); |
759 | } | 759 | } |
@@ -776,7 +776,7 @@ impl Foo { | |||
776 | expect![[r#" | 776 | expect![[r#" |
777 | ev Bar () | 777 | ev Bar () |
778 | ev Baz () | 778 | ev Baz () |
779 | me foo(…) -> () | 779 | me foo(…) fn(self) |
780 | "#]], | 780 | "#]], |
781 | ); | 781 | ); |
782 | } | 782 | } |
@@ -800,7 +800,7 @@ impl u8 { | |||
800 | "#, | 800 | "#, |
801 | expect![[r#" | 801 | expect![[r#" |
802 | ct MAX pub const MAX: Self = 255; | 802 | ct MAX pub const MAX: Self = 255; |
803 | me func(…) -> () | 803 | me func(…) fn(self) |
804 | "#]], | 804 | "#]], |
805 | ); | 805 | ); |
806 | } | 806 | } |
diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index 5ef80f6a7..1b8b063e7 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs | |||
@@ -135,7 +135,7 @@ fn quux(x: i32) { | |||
135 | expect![[r#" | 135 | expect![[r#" |
136 | lc y i32 | 136 | lc y i32 |
137 | lc x i32 | 137 | lc x i32 |
138 | fn quux(…) -> () | 138 | fn quux(…) fn(i32) |
139 | "#]], | 139 | "#]], |
140 | ); | 140 | ); |
141 | } | 141 | } |
@@ -157,7 +157,7 @@ fn quux() { | |||
157 | expect![[r#" | 157 | expect![[r#" |
158 | lc b i32 | 158 | lc b i32 |
159 | lc a | 159 | lc a |
160 | fn quux() -> () | 160 | fn quux() fn() |
161 | "#]], | 161 | "#]], |
162 | ); | 162 | ); |
163 | } | 163 | } |
@@ -172,7 +172,7 @@ fn quux() { | |||
172 | "#, | 172 | "#, |
173 | expect![[r#" | 173 | expect![[r#" |
174 | lc x | 174 | lc x |
175 | fn quux() -> () | 175 | fn quux() fn() |
176 | "#]], | 176 | "#]], |
177 | ); | 177 | ); |
178 | } | 178 | } |
@@ -203,14 +203,14 @@ fn main() { | |||
203 | r#"fn quux<T>() { $0 }"#, | 203 | r#"fn quux<T>() { $0 }"#, |
204 | expect![[r#" | 204 | expect![[r#" |
205 | tp T | 205 | tp T |
206 | fn quux() -> () | 206 | fn quux() fn() |
207 | "#]], | 207 | "#]], |
208 | ); | 208 | ); |
209 | check( | 209 | check( |
210 | r#"fn quux<const C: usize>() { $0 }"#, | 210 | r#"fn quux<const C: usize>() { $0 }"#, |
211 | expect![[r#" | 211 | expect![[r#" |
212 | cp C | 212 | cp C |
213 | fn quux() -> () | 213 | fn quux() fn() |
214 | "#]], | 214 | "#]], |
215 | ); | 215 | ); |
216 | } | 216 | } |
@@ -221,7 +221,7 @@ fn main() { | |||
221 | check( | 221 | check( |
222 | r#"fn quux<'a>() { $0 }"#, | 222 | r#"fn quux<'a>() { $0 }"#, |
223 | expect![[r#" | 223 | expect![[r#" |
224 | fn quux() -> () | 224 | fn quux() fn() |
225 | "#]], | 225 | "#]], |
226 | ); | 226 | ); |
227 | } | 227 | } |
@@ -259,7 +259,7 @@ fn quux() { $0 } | |||
259 | "#, | 259 | "#, |
260 | expect![[r#" | 260 | expect![[r#" |
261 | st S | 261 | st S |
262 | fn quux() -> () | 262 | fn quux() fn() |
263 | en E | 263 | en E |
264 | "#]], | 264 | "#]], |
265 | ); | 265 | ); |
@@ -312,7 +312,7 @@ mod m { | |||
312 | } | 312 | } |
313 | "#, | 313 | "#, |
314 | expect![[r#" | 314 | expect![[r#" |
315 | fn quux() -> () | 315 | fn quux() fn() |
316 | st Bar | 316 | st Bar |
317 | "#]], | 317 | "#]], |
318 | ); | 318 | ); |
@@ -327,7 +327,7 @@ fn x() -> $0 | |||
327 | "#, | 327 | "#, |
328 | expect![[r#" | 328 | expect![[r#" |
329 | st Foo | 329 | st Foo |
330 | fn x() -> () | 330 | fn x() fn() |
331 | "#]], | 331 | "#]], |
332 | ); | 332 | ); |
333 | } | 333 | } |
@@ -348,7 +348,7 @@ fn foo() { | |||
348 | expect![[r#" | 348 | expect![[r#" |
349 | lc bar i32 | 349 | lc bar i32 |
350 | lc bar i32 | 350 | lc bar i32 |
351 | fn foo() -> () | 351 | fn foo() fn() |
352 | "#]], | 352 | "#]], |
353 | ); | 353 | ); |
354 | } | 354 | } |
@@ -378,7 +378,7 @@ use prelude::*; | |||
378 | mod prelude { struct Option; } | 378 | mod prelude { struct Option; } |
379 | "#, | 379 | "#, |
380 | expect![[r#" | 380 | expect![[r#" |
381 | fn foo() -> () | 381 | fn foo() fn() |
382 | md std | 382 | md std |
383 | st Option | 383 | st Option |
384 | "#]], | 384 | "#]], |
@@ -408,7 +408,7 @@ mod macros { | |||
408 | } | 408 | } |
409 | "#, | 409 | "#, |
410 | expect![[r##" | 410 | expect![[r##" |
411 | fn f() -> () | 411 | fn f() fn() |
412 | ma concat!(…) #[macro_export] macro_rules! concat | 412 | ma concat!(…) #[macro_export] macro_rules! concat |
413 | md std | 413 | md std |
414 | "##]], | 414 | "##]], |
@@ -435,7 +435,7 @@ use prelude::*; | |||
435 | mod prelude { struct String; } | 435 | mod prelude { struct String; } |
436 | "#, | 436 | "#, |
437 | expect![[r#" | 437 | expect![[r#" |
438 | fn foo() -> () | 438 | fn foo() fn() |
439 | md std | 439 | md std |
440 | md core | 440 | md core |
441 | st String | 441 | st String |
@@ -466,7 +466,7 @@ fn main() { let v = $0 } | |||
466 | expect![[r##" | 466 | expect![[r##" |
467 | md m1 | 467 | md m1 |
468 | ma baz!(…) #[macro_export] macro_rules! baz | 468 | ma baz!(…) #[macro_export] macro_rules! baz |
469 | fn main() -> () | 469 | fn main() fn() |
470 | md m2 | 470 | md m2 |
471 | ma bar!(…) macro_rules! bar | 471 | ma bar!(…) macro_rules! bar |
472 | ma foo!(…) macro_rules! foo | 472 | ma foo!(…) macro_rules! foo |
@@ -482,7 +482,7 @@ macro_rules! foo { () => {} } | |||
482 | fn foo() { $0 } | 482 | fn foo() { $0 } |
483 | "#, | 483 | "#, |
484 | expect![[r#" | 484 | expect![[r#" |
485 | fn foo() -> () | 485 | fn foo() fn() |
486 | ma foo!(…) macro_rules! foo | 486 | ma foo!(…) macro_rules! foo |
487 | "#]], | 487 | "#]], |
488 | ); | 488 | ); |
@@ -496,7 +496,7 @@ macro_rules! foo { () => {} } | |||
496 | fn main() { let x: $0 } | 496 | fn main() { let x: $0 } |
497 | "#, | 497 | "#, |
498 | expect![[r#" | 498 | expect![[r#" |
499 | fn main() -> () | 499 | fn main() fn() |
500 | ma foo!(…) macro_rules! foo | 500 | ma foo!(…) macro_rules! foo |
501 | "#]], | 501 | "#]], |
502 | ); | 502 | ); |
@@ -510,7 +510,7 @@ macro_rules! foo { () => {} } | |||
510 | fn main() { $0 } | 510 | fn main() { $0 } |
511 | "#, | 511 | "#, |
512 | expect![[r#" | 512 | expect![[r#" |
513 | fn main() -> () | 513 | fn main() fn() |
514 | ma foo!(…) macro_rules! foo | 514 | ma foo!(…) macro_rules! foo |
515 | "#]], | 515 | "#]], |
516 | ); | 516 | ); |
@@ -526,8 +526,8 @@ fn main() { | |||
526 | } | 526 | } |
527 | "#, | 527 | "#, |
528 | expect![[r#" | 528 | expect![[r#" |
529 | fn frobnicate() -> () | 529 | fn frobnicate() fn() |
530 | fn main() -> () | 530 | fn main() fn() |
531 | "#]], | 531 | "#]], |
532 | ); | 532 | ); |
533 | } | 533 | } |
@@ -545,7 +545,7 @@ fn quux(x: i32) { | |||
545 | expect![[r#" | 545 | expect![[r#" |
546 | lc y i32 | 546 | lc y i32 |
547 | lc x i32 | 547 | lc x i32 |
548 | fn quux(…) -> () | 548 | fn quux(…) fn(i32) |
549 | ma m!(…) macro_rules! m | 549 | ma m!(…) macro_rules! m |
550 | "#]], | 550 | "#]], |
551 | ); | 551 | ); |
@@ -564,7 +564,7 @@ fn quux(x: i32) { | |||
564 | expect![[r#" | 564 | expect![[r#" |
565 | lc y i32 | 565 | lc y i32 |
566 | lc x i32 | 566 | lc x i32 |
567 | fn quux(…) -> () | 567 | fn quux(…) fn(i32) |
568 | ma m!(…) macro_rules! m | 568 | ma m!(…) macro_rules! m |
569 | "#]], | 569 | "#]], |
570 | ); | 570 | ); |
@@ -583,7 +583,7 @@ fn quux(x: i32) { | |||
583 | expect![[r#" | 583 | expect![[r#" |
584 | lc y i32 | 584 | lc y i32 |
585 | lc x i32 | 585 | lc x i32 |
586 | fn quux(…) -> () | 586 | fn quux(…) fn(i32) |
587 | ma m!(…) macro_rules! m | 587 | ma m!(…) macro_rules! m |
588 | "#]], | 588 | "#]], |
589 | ); | 589 | ); |
@@ -598,7 +598,7 @@ use spam::Quux; | |||
598 | fn main() { $0 } | 598 | fn main() { $0 } |
599 | "#, | 599 | "#, |
600 | expect![[r#" | 600 | expect![[r#" |
601 | fn main() -> () | 601 | fn main() fn() |
602 | ?? Quux | 602 | ?? Quux |
603 | "#]], | 603 | "#]], |
604 | ); | 604 | ); |
@@ -616,7 +616,7 @@ fn main() { let foo: Foo = Q$0 } | |||
616 | ev Foo::Baz () | 616 | ev Foo::Baz () |
617 | ev Foo::Quux () | 617 | ev Foo::Quux () |
618 | en Foo | 618 | en Foo |
619 | fn main() -> () | 619 | fn main() fn() |
620 | "#]], | 620 | "#]], |
621 | ) | 621 | ) |
622 | } | 622 | } |
@@ -631,7 +631,7 @@ fn f() -> m::E { V$0 } | |||
631 | expect![[r#" | 631 | expect![[r#" |
632 | ev m::E::V () | 632 | ev m::E::V () |
633 | md m | 633 | md m |
634 | fn f() -> E | 634 | fn f() fn() -> E |
635 | "#]], | 635 | "#]], |
636 | ) | 636 | ) |
637 | } | 637 | } |
diff --git a/crates/ide_completion/src/lib.rs b/crates/ide_completion/src/lib.rs index 263554ecf..5b7ad38d5 100644 --- a/crates/ide_completion/src/lib.rs +++ b/crates/ide_completion/src/lib.rs | |||
@@ -230,7 +230,7 @@ fn foo() { | |||
230 | bar.fo$0; | 230 | bar.fo$0; |
231 | } | 231 | } |
232 | "#, | 232 | "#, |
233 | DetailAndDocumentation { detail: "-> ()", documentation: "Do the foo" }, | 233 | DetailAndDocumentation { detail: "fn(&self)", documentation: "Do the foo" }, |
234 | ); | 234 | ); |
235 | } | 235 | } |
236 | 236 | ||
@@ -255,7 +255,7 @@ fn foo() { | |||
255 | bar.fo$0; | 255 | bar.fo$0; |
256 | } | 256 | } |
257 | "#, | 257 | "#, |
258 | DetailAndDocumentation { detail: "-> ()", documentation: " Do the foo" }, | 258 | DetailAndDocumentation { detail: "fn(&self)", documentation: " Do the foo" }, |
259 | ); | 259 | ); |
260 | } | 260 | } |
261 | 261 | ||
@@ -273,7 +273,7 @@ fn bar() { | |||
273 | for c in fo$0 | 273 | for c in fo$0 |
274 | } | 274 | } |
275 | "#, | 275 | "#, |
276 | DetailAndDocumentation { detail: "-> &str", documentation: "Do the foo" }, | 276 | DetailAndDocumentation { detail: "fn() -> &str", documentation: "Do the foo" }, |
277 | ); | 277 | ); |
278 | } | 278 | } |
279 | } | 279 | } |
diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index 09a27de71..36655667c 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs | |||
@@ -452,6 +452,44 @@ fn main() { Foo::Fo$0 } | |||
452 | } | 452 | } |
453 | 453 | ||
454 | #[test] | 454 | #[test] |
455 | fn fn_detail_includes_args_and_return_type() { | ||
456 | check( | ||
457 | r#" | ||
458 | fn foo<T>(a: u32, b: u32, t: T) -> (u32, T) { (a, t) } | ||
459 | |||
460 | fn main() { fo$0 } | ||
461 | "#, | ||
462 | expect![[r#" | ||
463 | [ | ||
464 | CompletionItem { | ||
465 | label: "foo(…)", | ||
466 | source_range: 68..70, | ||
467 | delete: 68..70, | ||
468 | insert: "foo(${1:a}, ${2:b}, ${3:t})$0", | ||
469 | kind: SymbolKind( | ||
470 | Function, | ||
471 | ), | ||
472 | lookup: "foo", | ||
473 | detail: "fn(u32, u32, T) -> (u32, T)", | ||
474 | trigger_call_info: true, | ||
475 | }, | ||
476 | CompletionItem { | ||
477 | label: "main()", | ||
478 | source_range: 68..70, | ||
479 | delete: 68..70, | ||
480 | insert: "main()$0", | ||
481 | kind: SymbolKind( | ||
482 | Function, | ||
483 | ), | ||
484 | lookup: "main", | ||
485 | detail: "fn()", | ||
486 | }, | ||
487 | ] | ||
488 | "#]], | ||
489 | ); | ||
490 | } | ||
491 | |||
492 | #[test] | ||
455 | fn enum_detail_just_parentheses_for_unit() { | 493 | fn enum_detail_just_parentheses_for_unit() { |
456 | check( | 494 | check( |
457 | r#" | 495 | r#" |
@@ -538,7 +576,7 @@ fn main() { let _: m::Spam = S$0 } | |||
538 | Function, | 576 | Function, |
539 | ), | 577 | ), |
540 | lookup: "main", | 578 | lookup: "main", |
541 | detail: "-> ()", | 579 | detail: "fn()", |
542 | }, | 580 | }, |
543 | ] | 581 | ] |
544 | "#]], | 582 | "#]], |
@@ -567,7 +605,7 @@ fn main() { som$0 } | |||
567 | Function, | 605 | Function, |
568 | ), | 606 | ), |
569 | lookup: "main", | 607 | lookup: "main", |
570 | detail: "-> ()", | 608 | detail: "fn()", |
571 | }, | 609 | }, |
572 | CompletionItem { | 610 | CompletionItem { |
573 | label: "something_deprecated()", | 611 | label: "something_deprecated()", |
@@ -578,7 +616,7 @@ fn main() { som$0 } | |||
578 | Function, | 616 | Function, |
579 | ), | 617 | ), |
580 | lookup: "something_deprecated", | 618 | lookup: "something_deprecated", |
581 | detail: "-> ()", | 619 | detail: "fn()", |
582 | deprecated: true, | 620 | deprecated: true, |
583 | }, | 621 | }, |
584 | CompletionItem { | 622 | CompletionItem { |
@@ -590,7 +628,7 @@ fn main() { som$0 } | |||
590 | Function, | 628 | Function, |
591 | ), | 629 | ), |
592 | lookup: "something_else_deprecated", | 630 | lookup: "something_else_deprecated", |
593 | detail: "-> ()", | 631 | detail: "fn()", |
594 | deprecated: true, | 632 | deprecated: true, |
595 | }, | 633 | }, |
596 | ] | 634 | ] |
@@ -641,7 +679,7 @@ impl S { | |||
641 | insert: "bar()$0", | 679 | insert: "bar()$0", |
642 | kind: Method, | 680 | kind: Method, |
643 | lookup: "bar", | 681 | lookup: "bar", |
644 | detail: "-> ()", | 682 | detail: "fn(self)", |
645 | documentation: Documentation( | 683 | documentation: Documentation( |
646 | "Method docs", | 684 | "Method docs", |
647 | ), | 685 | ), |
@@ -741,7 +779,7 @@ fn foo(s: S) { s.$0 } | |||
741 | insert: "the_method()$0", | 779 | insert: "the_method()$0", |
742 | kind: Method, | 780 | kind: Method, |
743 | lookup: "the_method", | 781 | lookup: "the_method", |
744 | detail: "-> ()", | 782 | detail: "fn(&self)", |
745 | }, | 783 | }, |
746 | ] | 784 | ] |
747 | "#]], | 785 | "#]], |
@@ -1049,7 +1087,7 @@ fn main() { | |||
1049 | Function, | 1087 | Function, |
1050 | ), | 1088 | ), |
1051 | lookup: "foo", | 1089 | lookup: "foo", |
1052 | detail: "-> ()", | 1090 | detail: "fn(&mut S)", |
1053 | trigger_call_info: true, | 1091 | trigger_call_info: true, |
1054 | }, | 1092 | }, |
1055 | CompletionItem { | 1093 | CompletionItem { |
@@ -1061,7 +1099,7 @@ fn main() { | |||
1061 | Function, | 1099 | Function, |
1062 | ), | 1100 | ), |
1063 | lookup: "main", | 1101 | lookup: "main", |
1064 | detail: "-> ()", | 1102 | detail: "fn()", |
1065 | }, | 1103 | }, |
1066 | CompletionItem { | 1104 | CompletionItem { |
1067 | label: "s", | 1105 | label: "s", |
diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs index 194ea135e..010303182 100644 --- a/crates/ide_completion/src/render/function.rs +++ b/crates/ide_completion/src/render/function.rs | |||
@@ -2,6 +2,7 @@ | |||
2 | 2 | ||
3 | use hir::{HasSource, HirDisplay, Type}; | 3 | use hir::{HasSource, HirDisplay, Type}; |
4 | use ide_db::SymbolKind; | 4 | use ide_db::SymbolKind; |
5 | use itertools::Itertools; | ||
5 | use syntax::ast::Fn; | 6 | use syntax::ast::Fn; |
6 | 7 | ||
7 | use crate::{ | 8 | use crate::{ |
@@ -73,8 +74,42 @@ impl<'a> FunctionRender<'a> { | |||
73 | } | 74 | } |
74 | 75 | ||
75 | fn detail(&self) -> String { | 76 | fn detail(&self) -> String { |
76 | let ty = self.func.ret_type(self.ctx.db()); | 77 | let ret_ty = self.func.ret_type(self.ctx.db()); |
77 | format!("-> {}", ty.display(self.ctx.db())) | 78 | let ret = if ret_ty.is_unit() { |
79 | // Omit the return type if it is the unit type | ||
80 | String::new() | ||
81 | } else { | ||
82 | format!(" {}", self.ty_display()) | ||
83 | }; | ||
84 | |||
85 | format!("fn({}){}", self.params_display(), ret) | ||
86 | } | ||
87 | |||
88 | fn params_display(&self) -> String { | ||
89 | if let Some(self_param) = self.func.self_param(self.ctx.db()) { | ||
90 | let params = self | ||
91 | .func | ||
92 | .assoc_fn_params(self.ctx.db()) | ||
93 | .into_iter() | ||
94 | .skip(1) // skip the self param because we are manually handling that | ||
95 | .map(|p| p.ty().display(self.ctx.db()).to_string()); | ||
96 | |||
97 | std::iter::once(self_param.display(self.ctx.db()).to_owned()).chain(params).join(", ") | ||
98 | } else { | ||
99 | let params = self | ||
100 | .func | ||
101 | .assoc_fn_params(self.ctx.db()) | ||
102 | .into_iter() | ||
103 | .map(|p| p.ty().display(self.ctx.db()).to_string()) | ||
104 | .join(", "); | ||
105 | params | ||
106 | } | ||
107 | } | ||
108 | |||
109 | fn ty_display(&self) -> String { | ||
110 | let ret_ty = self.func.ret_type(self.ctx.db()); | ||
111 | |||
112 | format!("-> {}", ret_ty.display(self.ctx.db())) | ||
78 | } | 113 | } |
79 | 114 | ||
80 | fn add_arg(&self, arg: &str, ty: &Type) -> String { | 115 | fn add_arg(&self, arg: &str, ty: &Type) -> String { |