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 19:08:29 +0000
committerGitHub <[email protected]>2021-03-13 19:08:29 +0000
commit17eeb2a6d2ea81b302b6707c63bf8fba489c2bdd (patch)
treea2e152f5f16dd92f4c4be011be2399998dc7eb6f /crates/hir_ty/src/display.rs
parentbe7a31fbd64943f71afe11b0413c99496526dddc (diff)
parentb035c314b4b0ecd2477fde216dbe7e8801f94d0d (diff)
Merge #8001
8001: More Chalk IDs r=flodiebold a=flodiebold Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/hir_ty/src/display.rs')
-rw-r--r--crates/hir_ty/src/display.rs55
1 files changed, 31 insertions, 24 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs
index b7e85e024..e6473586b 100644
--- a/crates/hir_ty/src/display.rs
+++ b/crates/hir_ty/src/display.rs
@@ -11,10 +11,10 @@ use hir_def::{
11use hir_expand::name::Name; 11use hir_expand::name::Name;
12 12
13use crate::{ 13use crate::{
14 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, primitive, to_assoc_type_id, 14 db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive,
15 traits::chalk::from_chalk, utils::generics, AdtId, AliasTy, CallableDefId, CallableSig, 15 to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasTy, CallableDefId,
16 GenericPredicate, Interner, Lifetime, Obligation, OpaqueTy, OpaqueTyId, ProjectionTy, Scalar, 16 CallableSig, GenericPredicate, ImplTraitId, Interner, Lifetime, Obligation, OpaqueTy,
17 Substs, TraitRef, Ty, TyKind, 17 ProjectionTy, Scalar, Substs, TraitRef, Ty, TyKind,
18}; 18};
19 19
20pub struct HirFormatter<'a> { 20pub struct HirFormatter<'a> {
@@ -313,22 +313,26 @@ impl HirDisplay for Ty {
313 )?; 313 )?;
314 } 314 }
315 315
316 // FIXME: all this just to decide whether to use parentheses...
316 let datas; 317 let datas;
317 let predicates = match t.interned(&Interner) { 318 let predicates = match t.interned(&Interner) {
318 TyKind::Dyn(predicates) if predicates.len() > 1 => { 319 TyKind::Dyn(predicates) if predicates.len() > 1 => {
319 Cow::Borrowed(predicates.as_ref()) 320 Cow::Borrowed(predicates.as_ref())
320 } 321 }
321 &TyKind::Alias(AliasTy::Opaque(OpaqueTy { 322 &TyKind::Alias(AliasTy::Opaque(OpaqueTy { opaque_ty_id, ref parameters })) => {
322 opaque_ty_id: OpaqueTyId::ReturnTypeImplTrait(func, idx), 323 let impl_trait_id = f.db.lookup_intern_impl_trait_id(opaque_ty_id.into());
323 ref parameters, 324 if let ImplTraitId::ReturnTypeImplTrait(func, idx) = impl_trait_id {
324 })) => { 325 datas =
325 datas = 326 f.db.return_type_impl_traits(func)
326 f.db.return_type_impl_traits(func).expect("impl trait id without data"); 327 .expect("impl trait id without data");
327 let data = (*datas) 328 let data = (*datas)
328 .as_ref() 329 .as_ref()
329 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone()); 330 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
330 let bounds = data.subst(parameters); 331 let bounds = data.subst(parameters);
331 Cow::Owned(bounds.value) 332 Cow::Owned(bounds.value)
333 } else {
334 Cow::Borrowed(&[][..])
335 }
332 } 336 }
333 _ => Cow::Borrowed(&[][..]), 337 _ => Cow::Borrowed(&[][..]),
334 }; 338 };
@@ -499,8 +503,9 @@ impl HirDisplay for Ty {
499 write!(f, "{}", type_alias.name)?; 503 write!(f, "{}", type_alias.name)?;
500 } 504 }
501 TyKind::OpaqueType(opaque_ty_id, parameters) => { 505 TyKind::OpaqueType(opaque_ty_id, parameters) => {
502 match opaque_ty_id { 506 let impl_trait_id = f.db.lookup_intern_impl_trait_id((*opaque_ty_id).into());
503 &OpaqueTyId::ReturnTypeImplTrait(func, idx) => { 507 match impl_trait_id {
508 ImplTraitId::ReturnTypeImplTrait(func, idx) => {
504 let datas = 509 let datas =
505 f.db.return_type_impl_traits(func).expect("impl trait id without data"); 510 f.db.return_type_impl_traits(func).expect("impl trait id without data");
506 let data = (*datas) 511 let data = (*datas)
@@ -510,7 +515,7 @@ impl HirDisplay for Ty {
510 write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?; 515 write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?;
511 // FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution 516 // FIXME: it would maybe be good to distinguish this from the alias type (when debug printing), and to show the substitution
512 } 517 }
513 OpaqueTyId::AsyncBlockTypeImplTrait(..) => { 518 ImplTraitId::AsyncBlockTypeImplTrait(..) => {
514 write!(f, "impl Future<Output = ")?; 519 write!(f, "impl Future<Output = ")?;
515 parameters[0].hir_fmt(f)?; 520 parameters[0].hir_fmt(f)?;
516 write!(f, ">")?; 521 write!(f, ">")?;
@@ -541,7 +546,8 @@ impl HirDisplay for Ty {
541 write!(f, "{{closure}}")?; 546 write!(f, "{{closure}}")?;
542 } 547 }
543 } 548 }
544 TyKind::Placeholder(id) => { 549 TyKind::Placeholder(idx) => {
550 let id = from_placeholder_idx(f.db, *idx);
545 let generics = generics(f.db.upcast(), id.parent); 551 let generics = generics(f.db.upcast(), id.parent);
546 let param_data = &generics.params.types[id.local_id]; 552 let param_data = &generics.params.types[id.local_id];
547 match param_data.provenance { 553 match param_data.provenance {
@@ -549,8 +555,8 @@ impl HirDisplay for Ty {
549 write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))? 555 write!(f, "{}", param_data.name.clone().unwrap_or_else(Name::missing))?
550 } 556 }
551 TypeParamProvenance::ArgumentImplTrait => { 557 TypeParamProvenance::ArgumentImplTrait => {
552 let bounds = f.db.generic_predicates_for_param(*id); 558 let bounds = f.db.generic_predicates_for_param(id);
553 let substs = Substs::type_params_for_generics(&generics); 559 let substs = Substs::type_params_for_generics(f.db, &generics);
554 write_bounds_like_dyn_trait_with_prefix( 560 write_bounds_like_dyn_trait_with_prefix(
555 "impl", 561 "impl",
556 &bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(), 562 &bounds.iter().map(|b| b.clone().subst(&substs)).collect::<Vec<_>>(),
@@ -565,8 +571,9 @@ impl HirDisplay for Ty {
565 } 571 }
566 TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?, 572 TyKind::Alias(AliasTy::Projection(p_ty)) => p_ty.hir_fmt(f)?,
567 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { 573 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
568 match opaque_ty.opaque_ty_id { 574 let impl_trait_id = f.db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into());
569 OpaqueTyId::ReturnTypeImplTrait(func, idx) => { 575 match impl_trait_id {
576 ImplTraitId::ReturnTypeImplTrait(func, idx) => {
570 let datas = 577 let datas =
571 f.db.return_type_impl_traits(func).expect("impl trait id without data"); 578 f.db.return_type_impl_traits(func).expect("impl trait id without data");
572 let data = (*datas) 579 let data = (*datas)
@@ -575,7 +582,7 @@ impl HirDisplay for Ty {
575 let bounds = data.subst(&opaque_ty.parameters); 582 let bounds = data.subst(&opaque_ty.parameters);
576 write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?; 583 write_bounds_like_dyn_trait_with_prefix("impl", &bounds.value, f)?;
577 } 584 }
578 OpaqueTyId::AsyncBlockTypeImplTrait(..) => { 585 ImplTraitId::AsyncBlockTypeImplTrait(..) => {
579 write!(f, "{{async block}}")?; 586 write!(f, "{{async block}}")?;
580 } 587 }
581 }; 588 };