aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/display.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-13 15:18:38 +0000
committerGitHub <[email protected]>2021-03-13 15:18:38 +0000
commitbcf2169e7d7c46276214cac627a70673b7676567 (patch)
treef81c7438f67de3c292a233887e56c7e99bcc0a01 /crates/hir_ty/src/display.rs
parent7accf6bc37c059a83a58c82f463f02a02ed2226f (diff)
parent6c32bbf3ca5980fb33c1ea28dd1c5a1524ce81cb (diff)
Merge #7996
7996: Separate `Ty` and `TyKind` like in Chalk r=flodiebold a=flodiebold Currently `Ty` just wraps `TyKind`, but this allows us to change most places to already use `intern` / `interned`. Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/hir_ty/src/display.rs')
-rw-r--r--crates/hir_ty/src/display.rs116
1 files changed, 59 insertions, 57 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index ab51cb0a6..ee15f4f52 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -12,8 +12,8 @@ use hir_expand::name::Name;
12 12
13use crate::{ 13use crate::{
14 db::HirDatabase, primitive, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, 14 db::HirDatabase, primitive, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig,
15 GenericPredicate, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, Substs, 15 GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar,
16 TraitRef, Ty, 16 Substs, TraitRef, Ty, TyKind,
17}; 17};
18 18
19pub struct HirFormatter<'a> { 19pub struct HirFormatter<'a> {
@@ -267,32 +267,32 @@ impl HirDisplay for Ty {
267 return write!(f, "{}", TYPE_HINT_TRUNCATION); 267 return write!(f, "{}", TYPE_HINT_TRUNCATION);
268 } 268 }
269 269
270 match self { 270 match self.interned(&Interner) {
271 Ty::Never => write!(f, "!")?, 271 TyKind::Never => write!(f, "!")?,
272 Ty::Str => write!(f, "str")?, 272 TyKind::Str => write!(f, "str")?,
273 Ty::Scalar(Scalar::Bool) => write!(f, "bool")?, 273 TyKind::Scalar(Scalar::Bool) => write!(f, "bool")?,
274 Ty::Scalar(Scalar::Char) => write!(f, "char")?, 274 TyKind::Scalar(Scalar::Char) => write!(f, "char")?,
275 &Ty::Scalar(Scalar::Float(t)) => write!(f, "{}", primitive::float_ty_to_string(t))?, 275 &TyKind::Scalar(Scalar::Float(t)) => write!(f, "{}", primitive::float_ty_to_string(t))?,
276 &Ty::Scalar(Scalar::Int(t)) => write!(f, "{}", primitive::int_ty_to_string(t))?, 276 &TyKind::Scalar(Scalar::Int(t)) => write!(f, "{}", primitive::int_ty_to_string(t))?,
277 &Ty::Scalar(Scalar::Uint(t)) => write!(f, "{}", primitive::uint_ty_to_string(t))?, 277 &TyKind::Scalar(Scalar::Uint(t)) => write!(f, "{}", primitive::uint_ty_to_string(t))?,
278 Ty::Slice(parameters) => { 278 TyKind::Slice(parameters) => {
279 let t = parameters.as_single(); 279 let t = parameters.as_single();
280 write!(f, "[")?; 280 write!(f, "[")?;
281 t.hir_fmt(f)?; 281 t.hir_fmt(f)?;
282 write!(f, "]")?; 282 write!(f, "]")?;
283 } 283 }
284 Ty::Array(parameters) => { 284 TyKind::Array(parameters) => {
285 let t = parameters.as_single(); 285 let t = parameters.as_single();
286 write!(f, "[")?; 286 write!(f, "[")?;
287 t.hir_fmt(f)?; 287 t.hir_fmt(f)?;
288 write!(f, "; _]")?; 288 write!(f, "; _]")?;
289 } 289 }
290 Ty::Raw(m, parameters) | Ty::Ref(m, parameters) => { 290 TyKind::Raw(m, parameters) | TyKind::Ref(m, parameters) => {
291 let t = parameters.as_single(); 291 let t = parameters.as_single();
292 let ty_display = 292 let ty_display =
293 t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target); 293 t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target);
294 294
295 if matches!(self, Ty::Raw(..)) { 295 if matches!(self.interned(&Interner), TyKind::Raw(..)) {
296 write!( 296 write!(
297 f, 297 f,
298 "*{}", 298 "*{}",
@@ -313,11 +313,11 @@ impl HirDisplay for Ty {
313 } 313 }
314 314
315 let datas; 315 let datas;
316 let predicates = match t { 316 let predicates = match t.interned(&Interner) {
317 Ty::Dyn(predicates) if predicates.len() > 1 => { 317 TyKind::Dyn(predicates) if predicates.len() > 1 => {
318 Cow::Borrowed(predicates.as_ref()) 318 Cow::Borrowed(predicates.as_ref())
319 } 319 }
320 &Ty::Alias(AliasTy::Opaque(OpaqueTy { 320 &TyKind::Alias(AliasTy::Opaque(OpaqueTy {
321 opaque_ty_id: OpaqueTyId::ReturnTypeImplTrait(func, idx), 321 opaque_ty_id: OpaqueTyId::ReturnTypeImplTrait(func, idx),
322 ref parameters, 322 ref parameters,
323 })) => { 323 })) => {
@@ -347,7 +347,7 @@ impl HirDisplay for Ty {
347 write!(f, "{}", ty_display)?; 347 write!(f, "{}", ty_display)?;
348 } 348 }
349 } 349 }
350 Ty::Tuple(_, substs) => { 350 TyKind::Tuple(_, substs) => {
351 if substs.len() == 1 { 351 if substs.len() == 1 {
352 write!(f, "(")?; 352 write!(f, "(")?;
353 substs[0].hir_fmt(f)?; 353 substs[0].hir_fmt(f)?;
@@ -358,11 +358,11 @@ impl HirDisplay for Ty {
358 write!(f, ")")?; 358 write!(f, ")")?;
359 } 359 }
360 } 360 }
361 Ty::Function(fn_ptr) => { 361 TyKind::Function(fn_ptr) => {
362 let sig = CallableSig::from_fn_ptr(fn_ptr); 362 let sig = CallableSig::from_fn_ptr(fn_ptr);
363 sig.hir_fmt(f)?; 363 sig.hir_fmt(f)?;
364 } 364 }
365 Ty::FnDef(def, parameters) => { 365 TyKind::FnDef(def, parameters) => {
366 let def = *def; 366 let def = *def;
367 let sig = f.db.callable_item_signature(def).subst(parameters); 367 let sig = f.db.callable_item_signature(def).subst(parameters);
368 match def { 368 match def {
@@ -401,7 +401,7 @@ impl HirDisplay for Ty {
401 write!(f, " -> {}", ret_display)?; 401 write!(f, " -> {}", ret_display)?;
402 } 402 }
403 } 403 }
404 Ty::Adt(AdtId(def_id), parameters) => { 404 TyKind::Adt(AdtId(def_id), parameters) => {
405 match f.display_target { 405 match f.display_target {
406 DisplayTarget::Diagnostics | DisplayTarget::Test => { 406 DisplayTarget::Diagnostics | DisplayTarget::Test => {
407 let name = match *def_id { 407 let name = match *def_id {
@@ -427,37 +427,39 @@ impl HirDisplay for Ty {
427 } 427 }
428 428
429 if parameters.len() > 0 { 429 if parameters.len() > 0 {
430 let parameters_to_write = 430 let parameters_to_write = if f.display_target.is_source_code()
431 if f.display_target.is_source_code() || f.omit_verbose_types() { 431 || f.omit_verbose_types()
432 match self 432 {
433 .as_generic_def() 433 match self
434 .map(|generic_def_id| f.db.generic_defaults(generic_def_id)) 434 .as_generic_def()
435 .filter(|defaults| !defaults.is_empty()) 435 .map(|generic_def_id| f.db.generic_defaults(generic_def_id))
436 { 436 .filter(|defaults| !defaults.is_empty())
437 None => parameters.0.as_ref(), 437 {
438 Some(default_parameters) => { 438 None => parameters.0.as_ref(),
439 let mut default_from = 0; 439 Some(default_parameters) => {
440 for (i, parameter) in parameters.iter().enumerate() { 440 let mut default_from = 0;
441 match (parameter, default_parameters.get(i)) { 441 for (i, parameter) in parameters.iter().enumerate() {
442 (&Ty::Unknown, _) | (_, None) => { 442 match (parameter.interned(&Interner), default_parameters.get(i))
443 {
444 (&TyKind::Unknown, _) | (_, None) => {
445 default_from = i + 1;
446 }
447 (_, Some(default_parameter)) => {
448 let actual_default = default_parameter
449 .clone()
450 .subst(&parameters.prefix(i));
451 if parameter != &actual_default {
443 default_from = i + 1; 452 default_from = i + 1;
444 } 453 }
445 (_, Some(default_parameter)) => {
446 let actual_default = default_parameter
447 .clone()
448 .subst(&parameters.prefix(i));
449 if parameter != &actual_default {
450 default_from = i + 1;
451 }
452 }
453 } 454 }
454 } 455 }
455 &parameters.0[0..default_from]
456 } 456 }
457 &parameters.0[0..default_from]
457 } 458 }
458 } else { 459 }
459 parameters.0.as_ref() 460 } else {
460 }; 461 parameters.0.as_ref()
462 };
461 if !parameters_to_write.is_empty() { 463 if !parameters_to_write.is_empty() {
462 write!(f, "<")?; 464 write!(f, "<")?;
463 f.write_joined(parameters_to_write, ", ")?; 465 f.write_joined(parameters_to_write, ", ")?;
@@ -465,7 +467,7 @@ impl HirDisplay for Ty {
465 } 467 }
466 } 468 }
467 } 469 }
468 Ty::AssociatedType(type_alias, parameters) => { 470 TyKind::AssociatedType(type_alias, parameters) => {
469 let trait_ = match type_alias.lookup(f.db.upcast()).container { 471 let trait_ = match type_alias.lookup(f.db.upcast()).container {
470 AssocContainerId::TraitId(it) => it, 472 AssocContainerId::TraitId(it) => it,
471 _ => panic!("not an associated type"), 473 _ => panic!("not an associated type"),
@@ -488,11 +490,11 @@ impl HirDisplay for Ty {
488 projection_ty.hir_fmt(f)?; 490 projection_ty.hir_fmt(f)?;
489 } 491 }
490 } 492 }
491 Ty::ForeignType(type_alias) => { 493 TyKind::ForeignType(type_alias) => {
492 let type_alias = f.db.type_alias_data(*type_alias); 494 let type_alias = f.db.type_alias_data(*type_alias);
493 write!(f, "{}", type_alias.name)?; 495 write!(f, "{}", type_alias.name)?;
494 } 496 }
495 Ty::OpaqueType(opaque_ty_id, parameters) => { 497 TyKind::OpaqueType(opaque_ty_id, parameters) => {
496 match opaque_ty_id { 498 match opaque_ty_id {
497 &OpaqueTyId::ReturnTypeImplTrait(func, idx) => { 499 &OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
498 let datas = 500 let datas =
@@ -511,7 +513,7 @@ impl HirDisplay for Ty {
511 } 513 }
512 } 514 }
513 } 515 }
514 Ty::Closure(.., substs) => { 516 TyKind::Closure(.., substs) => {
515 let sig = substs[0].callable_sig(f.db); 517 let sig = substs[0].callable_sig(f.db);
516 if let Some(sig) = sig { 518 if let Some(sig) = sig {
517 if sig.params().is_empty() { 519 if sig.params().is_empty() {
@@ -535,7 +537,7 @@ impl HirDisplay for Ty {
535 write!(f, "{{closure}}")?; 537 write!(f, "{{closure}}")?;
536 } 538 }
537 } 539 }
538 Ty::Placeholder(id) => { 540 TyKind::Placeholder(id) => {
539 let generics = generics(f.db.upcast(), id.parent); 541 let generics = generics(f.db.upcast(), id.parent);
540 let param_data = &generics.params.types[id.local_id]; 542 let param_data = &generics.params.types[id.local_id];
541 match param_data.provenance { 543 match param_data.provenance {
@@ -553,12 +555,12 @@ impl HirDisplay for Ty {
553 } 555 }
554 } 556 }
555 } 557 }
556 Ty::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?, 558 TyKind::BoundVar(idx) => write!(f, "?{}.{}", idx.debruijn.depth(), idx.index)?,
557 Ty::Dyn(predicates) => { 559 TyKind::Dyn(predicates) => {
558 write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?; 560 write_bounds_like_dyn_trait_with_prefix("dyn", predicates, f)?;
559 } 561 }
560 Ty::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?, 562 TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?,
561 Ty::Alias(AliasTy::Opaque(opaque_ty)) => { 563 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
562 match opaque_ty.opaque_ty_id { 564 match opaque_ty.opaque_ty_id {
563 OpaqueTyId::ReturnTypeImplTrait(func, idx) => { 565 OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
564 let datas = 566 let datas =
@@ -574,7 +576,7 @@ impl HirDisplay for Ty {
574 } 576 }
575 }; 577 };
576 } 578 }
577 Ty::Unknown => { 579 TyKind::Unknown => {
578 if f.display_target.is_source_code() { 580 if f.display_target.is_source_code() {
579 return Err(HirDisplayError::DisplaySourceCodeError( 581 return Err(HirDisplayError::DisplaySourceCodeError(
580 DisplaySourceCodeError::UnknownType, 582 DisplaySourceCodeError::UnknownType,
@@ -582,7 +584,7 @@ impl HirDisplay for Ty {
582 } 584 }
583 write!(f, "{{unknown}}")?; 585 write!(f, "{{unknown}}")?;
584 } 586 }
585 Ty::InferenceVar(..) => write!(f, "_")?, 587 TyKind::InferenceVar(..) => write!(f, "_")?,
586 } 588 }
587 Ok(()) 589 Ok(())
588 } 590 }