aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-03-16 15:42:36 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-03-16 15:42:36 +0000
commit76572e67eabffa959c665f8450dbb12d71848aba (patch)
treec293e134706908ddc54400dee029271ec9859692 /crates/ra_hir/src/ty.rs
parentb6ddf976f1b00703a35b91a5aa04debf19b7ca25 (diff)
parentc5ee60e05b1fafe20f56b21cfab30e7b80cb9d42 (diff)
Merge #976
976: Replace Display by a pretty printing trait for Ty r=matklad a=flodiebold This allows removing the names from Adt and FnDef (and more later), as a first step towards aligning more with chalk's Ty :) I may have gone a bit overboard with the definition of the PrettyPrint trait... Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r--crates/ra_hir/src/ty.rs94
1 files changed, 60 insertions, 34 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs
index bad811a56..f64877f3b 100644
--- a/crates/ra_hir/src/ty.rs
+++ b/crates/ra_hir/src/ty.rs
@@ -9,16 +9,16 @@ pub(crate) mod method_resolution;
9mod op; 9mod op;
10mod lower; 10mod lower;
11mod infer; 11mod infer;
12pub(crate) mod display;
12 13
13use std::sync::Arc; 14use std::sync::Arc;
14use std::{fmt, mem}; 15use std::{fmt, mem};
15 16
16use join_to_string::join; 17use crate::{Name, AdtDef, type_ref::Mutability, db::HirDatabase};
17
18use crate::{Name, AdtDef, type_ref::Mutability};
19 18
20pub(crate) use lower::{TypableDef, CallableDef, type_for_def, type_for_field}; 19pub(crate) use lower::{TypableDef, CallableDef, type_for_def, type_for_field};
21pub(crate) use infer::{infer, InferenceResult, InferTy}; 20pub(crate) use infer::{infer, InferenceResult, InferTy};
21use display::{HirDisplay, HirFormatter};
22 22
23/// A type. This is based on the `TyKind` enum in rustc (librustc/ty/sty.rs). 23/// A type. This is based on the `TyKind` enum in rustc (librustc/ty/sty.rs).
24/// 24///
@@ -42,8 +42,6 @@ pub enum Ty {
42 Adt { 42 Adt {
43 /// The definition of the struct/enum. 43 /// The definition of the struct/enum.
44 def_id: AdtDef, 44 def_id: AdtDef,
45 /// The name, for displaying.
46 name: Name,
47 /// Substitutions for the generic parameters of the type. 45 /// Substitutions for the generic parameters of the type.
48 substs: Substs, 46 substs: Substs,
49 }, 47 },
@@ -79,8 +77,6 @@ pub enum Ty {
79 FnDef { 77 FnDef {
80 /// The definition of the function / constructor. 78 /// The definition of the function / constructor.
81 def: CallableDef, 79 def: CallableDef,
82 /// For display
83 name: Name,
84 /// Parameters and return type 80 /// Parameters and return type
85 sig: Arc<FnSig>, 81 sig: Arc<FnSig>,
86 /// Substitutions for the generic parameters of the type 82 /// Substitutions for the generic parameters of the type
@@ -265,8 +261,8 @@ impl Ty {
265 /// `Option<u32>` afterwards.) 261 /// `Option<u32>` afterwards.)
266 pub fn apply_substs(self, substs: Substs) -> Ty { 262 pub fn apply_substs(self, substs: Substs) -> Ty {
267 match self { 263 match self {
268 Ty::Adt { def_id, name, .. } => Ty::Adt { def_id, name, substs }, 264 Ty::Adt { def_id, .. } => Ty::Adt { def_id, substs },
269 Ty::FnDef { def, name, sig, .. } => Ty::FnDef { def, name, sig, substs }, 265 Ty::FnDef { def, sig, .. } => Ty::FnDef { def, sig, substs },
270 _ => self, 266 _ => self,
271 } 267 }
272 } 268 }
@@ -297,50 +293,80 @@ impl Ty {
297 } 293 }
298} 294}
299 295
300impl fmt::Display for Ty { 296impl HirDisplay for &Ty {
301 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 297 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
298 HirDisplay::hir_fmt(*self, f)
299 }
300}
301
302impl HirDisplay for Ty {
303 fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
302 match self { 304 match self {
303 Ty::Bool => write!(f, "bool"), 305 Ty::Bool => write!(f, "bool")?,
304 Ty::Char => write!(f, "char"), 306 Ty::Char => write!(f, "char")?,
305 Ty::Int(t) => write!(f, "{}", t.ty_to_string()), 307 Ty::Int(t) => write!(f, "{}", t.ty_to_string())?,
306 Ty::Float(t) => write!(f, "{}", t.ty_to_string()), 308 Ty::Float(t) => write!(f, "{}", t.ty_to_string())?,
307 Ty::Str => write!(f, "str"), 309 Ty::Str => write!(f, "str")?,
308 Ty::Slice(t) | Ty::Array(t) => write!(f, "[{}]", t), 310 Ty::Slice(t) | Ty::Array(t) => {
309 Ty::RawPtr(t, m) => write!(f, "*{}{}", m.as_keyword_for_ptr(), t), 311 write!(f, "[{}]", t.display(f.db))?;
310 Ty::Ref(t, m) => write!(f, "&{}{}", m.as_keyword_for_ref(), t), 312 }
311 Ty::Never => write!(f, "!"), 313 Ty::RawPtr(t, m) => {
314 write!(f, "*{}{}", m.as_keyword_for_ptr(), t.display(f.db))?;
315 }
316 Ty::Ref(t, m) => {
317 write!(f, "&{}{}", m.as_keyword_for_ref(), t.display(f.db))?;
318 }
319 Ty::Never => write!(f, "!")?,
312 Ty::Tuple(ts) => { 320 Ty::Tuple(ts) => {
313 if ts.len() == 1 { 321 if ts.len() == 1 {
314 write!(f, "({},)", ts[0]) 322 write!(f, "({},)", ts[0].display(f.db))?;
315 } else { 323 } else {
316 join(ts.iter()).surround_with("(", ")").separator(", ").to_fmt(f) 324 write!(f, "(")?;
325 f.write_joined(&**ts, ", ")?;
326 write!(f, ")")?;
317 } 327 }
318 } 328 }
319 Ty::FnPtr(sig) => { 329 Ty::FnPtr(sig) => {
320 join(sig.input.iter()).surround_with("fn(", ")").separator(", ").to_fmt(f)?; 330 write!(f, "fn(")?;
321 write!(f, " -> {}", sig.output) 331 f.write_joined(&sig.input, ", ")?;
332 write!(f, ") -> {}", sig.output.display(f.db))?;
322 } 333 }
323 Ty::FnDef { def, name, substs, sig, .. } => { 334 Ty::FnDef { def, substs, sig, .. } => {
335 let name = match def {
336 CallableDef::Function(ff) => ff.name(f.db),
337 CallableDef::Struct(s) => s.name(f.db).unwrap_or_else(Name::missing),
338 CallableDef::EnumVariant(e) => e.name(f.db).unwrap_or_else(Name::missing),
339 };
324 match def { 340 match def {
325 CallableDef::Function(_) => write!(f, "fn {}", name)?, 341 CallableDef::Function(_) => write!(f, "fn {}", name)?,
326 CallableDef::Struct(_) | CallableDef::EnumVariant(_) => write!(f, "{}", name)?, 342 CallableDef::Struct(_) | CallableDef::EnumVariant(_) => write!(f, "{}", name)?,
327 } 343 }
328 if substs.0.len() > 0 { 344 if substs.0.len() > 0 {
329 join(substs.0.iter()).surround_with("<", ">").separator(", ").to_fmt(f)?; 345 write!(f, "<")?;
346 f.write_joined(&*substs.0, ", ")?;
347 write!(f, ">")?;
330 } 348 }
331 join(sig.input.iter()).surround_with("(", ")").separator(", ").to_fmt(f)?; 349 write!(f, "(")?;
332 write!(f, " -> {}", sig.output) 350 f.write_joined(&sig.input, ", ")?;
351 write!(f, ") -> {}", sig.output.display(f.db))?;
333 } 352 }
334 Ty::Adt { name, substs, .. } => { 353 Ty::Adt { def_id, substs, .. } => {
354 let name = match def_id {
355 AdtDef::Struct(s) => s.name(f.db),
356 AdtDef::Enum(e) => e.name(f.db),
357 }
358 .unwrap_or_else(Name::missing);
335 write!(f, "{}", name)?; 359 write!(f, "{}", name)?;
336 if substs.0.len() > 0 { 360 if substs.0.len() > 0 {
337 join(substs.0.iter()).surround_with("<", ">").separator(", ").to_fmt(f)?; 361 write!(f, "<")?;
362 f.write_joined(&*substs.0, ", ")?;
363 write!(f, ">")?;
338 } 364 }
339 Ok(())
340 } 365 }
341 Ty::Param { name, .. } => write!(f, "{}", name), 366 Ty::Param { name, .. } => write!(f, "{}", name)?,
342 Ty::Unknown => write!(f, "{{unknown}}"), 367 Ty::Unknown => write!(f, "{{unknown}}")?,
343 Ty::Infer(..) => write!(f, "_"), 368 Ty::Infer(..) => write!(f, "_")?,
344 } 369 }
370 Ok(())
345 } 371 }
346} 372}