diff options
author | Florian Diebold <[email protected]> | 2021-04-01 20:04:02 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2021-04-03 10:17:13 +0100 |
commit | e480d81988fc0c0e4f80f1c54058b95b9aaf1ebf (patch) | |
tree | 079dabc4e0de72e30d2bd04bd50427ff6950b348 /crates/hir_ty/src/display.rs | |
parent | 327f3a0a3017e047be58b8312f8bf3ac690db3fd (diff) |
Introduce `GenericArg` like in Chalk
Plus some more adaptations to Substitution.
Lots of `assert_ty_ref` that we should revisit when introducing
lifetime/const parameters.
Diffstat (limited to 'crates/hir_ty/src/display.rs')
-rw-r--r-- | crates/hir_ty/src/display.rs | 61 |
1 files changed, 37 insertions, 24 deletions
diff --git a/crates/hir_ty/src/display.rs b/crates/hir_ty/src/display.rs index 51480304b..59fd18c2a 100644 --- a/crates/hir_ty/src/display.rs +++ b/crates/hir_ty/src/display.rs | |||
@@ -8,7 +8,7 @@ use hir_def::{ | |||
8 | find_path, | 8 | find_path, |
9 | generics::TypeParamProvenance, | 9 | generics::TypeParamProvenance, |
10 | item_scope::ItemInNs, | 10 | item_scope::ItemInNs, |
11 | path::{GenericArg, Path, PathKind}, | 11 | path::{Path, PathKind}, |
12 | type_ref::{TypeBound, TypeRef}, | 12 | type_ref::{TypeBound, TypeRef}, |
13 | visibility::Visibility, | 13 | visibility::Visibility, |
14 | AssocContainerId, Lookup, ModuleId, TraitId, | 14 | AssocContainerId, Lookup, ModuleId, TraitId, |
@@ -18,7 +18,7 @@ use hir_expand::name::Name; | |||
18 | use crate::{ | 18 | use crate::{ |
19 | db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive, | 19 | db::HirDatabase, from_assoc_type_id, from_foreign_def_id, from_placeholder_idx, primitive, |
20 | to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, | 20 | to_assoc_type_id, traits::chalk::from_chalk, utils::generics, AdtId, AliasEq, AliasTy, |
21 | CallableDefId, CallableSig, DomainGoal, ImplTraitId, Interner, Lifetime, OpaqueTy, | 21 | CallableDefId, CallableSig, DomainGoal, GenericArg, ImplTraitId, Interner, Lifetime, OpaqueTy, |
22 | ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TyKind, WhereClause, | 22 | ProjectionTy, QuantifiedWhereClause, Scalar, Substitution, TraitRef, Ty, TyKind, WhereClause, |
23 | }; | 23 | }; |
24 | 24 | ||
@@ -251,16 +251,16 @@ impl HirDisplay for ProjectionTy { | |||
251 | } | 251 | } |
252 | 252 | ||
253 | let trait_ = f.db.trait_data(self.trait_(f.db)); | 253 | let trait_ = f.db.trait_data(self.trait_(f.db)); |
254 | let first_parameter = self.substitution[0].into_displayable( | 254 | let first_parameter = self.self_type_parameter().into_displayable( |
255 | f.db, | 255 | f.db, |
256 | f.max_size, | 256 | f.max_size, |
257 | f.omit_verbose_types, | 257 | f.omit_verbose_types, |
258 | f.display_target, | 258 | f.display_target, |
259 | ); | 259 | ); |
260 | write!(f, "<{} as {}", first_parameter, trait_.name)?; | 260 | write!(f, "<{} as {}", first_parameter, trait_.name)?; |
261 | if self.substitution.len() > 1 { | 261 | if self.substitution.len(&Interner) > 1 { |
262 | write!(f, "<")?; | 262 | write!(f, "<")?; |
263 | f.write_joined(&self.substitution[1..], ", ")?; | 263 | f.write_joined(&self.substitution.interned(&Interner)[1..], ", ")?; |
264 | write!(f, ">")?; | 264 | write!(f, ">")?; |
265 | } | 265 | } |
266 | write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?; | 266 | write!(f, ">::{}", f.db.type_alias_data(from_assoc_type_id(self.associated_ty_id)).name)?; |
@@ -274,7 +274,15 @@ impl HirDisplay for OpaqueTy { | |||
274 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | 274 | return write!(f, "{}", TYPE_HINT_TRUNCATION); |
275 | } | 275 | } |
276 | 276 | ||
277 | self.substitution[0].hir_fmt(f) | 277 | self.substitution.at(&Interner, 0).hir_fmt(f) |
278 | } | ||
279 | } | ||
280 | |||
281 | impl HirDisplay for GenericArg { | ||
282 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | ||
283 | match self.interned() { | ||
284 | crate::GenericArgData::Ty(ty) => ty.hir_fmt(f), | ||
285 | } | ||
278 | } | 286 | } |
279 | } | 287 | } |
280 | 288 | ||
@@ -373,9 +381,9 @@ impl HirDisplay for Ty { | |||
373 | } | 381 | } |
374 | } | 382 | } |
375 | TyKind::Tuple(_, substs) => { | 383 | TyKind::Tuple(_, substs) => { |
376 | if substs.len() == 1 { | 384 | if substs.len(&Interner) == 1 { |
377 | write!(f, "(")?; | 385 | write!(f, "(")?; |
378 | substs[0].hir_fmt(f)?; | 386 | substs.at(&Interner, 0).hir_fmt(f)?; |
379 | write!(f, ",)")?; | 387 | write!(f, ",)")?; |
380 | } else { | 388 | } else { |
381 | write!(f, "(")?; | 389 | write!(f, "(")?; |
@@ -399,7 +407,7 @@ impl HirDisplay for Ty { | |||
399 | write!(f, "{}", f.db.enum_data(e.parent).variants[e.local_id].name)? | 407 | write!(f, "{}", f.db.enum_data(e.parent).variants[e.local_id].name)? |
400 | } | 408 | } |
401 | }; | 409 | }; |
402 | if parameters.len() > 0 { | 410 | if parameters.len(&Interner) > 0 { |
403 | let generics = generics(f.db.upcast(), def.into()); | 411 | let generics = generics(f.db.upcast(), def.into()); |
404 | let (parent_params, self_param, type_params, _impl_trait_params) = | 412 | let (parent_params, self_param, type_params, _impl_trait_params) = |
405 | generics.provenance_split(); | 413 | generics.provenance_split(); |
@@ -451,7 +459,7 @@ impl HirDisplay for Ty { | |||
451 | } | 459 | } |
452 | } | 460 | } |
453 | 461 | ||
454 | if parameters.len() > 0 { | 462 | if parameters.len(&Interner) > 0 { |
455 | let parameters_to_write = if f.display_target.is_source_code() | 463 | let parameters_to_write = if f.display_target.is_source_code() |
456 | || f.omit_verbose_types() | 464 | || f.omit_verbose_types() |
457 | { | 465 | { |
@@ -463,9 +471,11 @@ impl HirDisplay for Ty { | |||
463 | None => parameters.0.as_ref(), | 471 | None => parameters.0.as_ref(), |
464 | Some(default_parameters) => { | 472 | Some(default_parameters) => { |
465 | let mut default_from = 0; | 473 | let mut default_from = 0; |
466 | for (i, parameter) in parameters.iter().enumerate() { | 474 | for (i, parameter) in parameters.iter(&Interner).enumerate() { |
467 | match (parameter.interned(&Interner), default_parameters.get(i)) | 475 | match ( |
468 | { | 476 | parameter.assert_ty_ref(&Interner).interned(&Interner), |
477 | default_parameters.get(i), | ||
478 | ) { | ||
469 | (&TyKind::Unknown, _) | (_, None) => { | 479 | (&TyKind::Unknown, _) | (_, None) => { |
470 | default_from = i + 1; | 480 | default_from = i + 1; |
471 | } | 481 | } |
@@ -473,7 +483,8 @@ impl HirDisplay for Ty { | |||
473 | let actual_default = default_parameter | 483 | let actual_default = default_parameter |
474 | .clone() | 484 | .clone() |
475 | .subst(¶meters.prefix(i)); | 485 | .subst(¶meters.prefix(i)); |
476 | if parameter != &actual_default { | 486 | if parameter.assert_ty_ref(&Interner) != &actual_default |
487 | { | ||
477 | default_from = i + 1; | 488 | default_from = i + 1; |
478 | } | 489 | } |
479 | } | 490 | } |
@@ -504,7 +515,7 @@ impl HirDisplay for Ty { | |||
504 | // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) | 515 | // Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types) |
505 | if f.display_target.is_test() { | 516 | if f.display_target.is_test() { |
506 | write!(f, "{}::{}", trait_.name, type_alias_data.name)?; | 517 | write!(f, "{}::{}", trait_.name, type_alias_data.name)?; |
507 | if parameters.len() > 0 { | 518 | if parameters.len(&Interner) > 0 { |
508 | write!(f, "<")?; | 519 | write!(f, "<")?; |
509 | f.write_joined(&*parameters.0, ", ")?; | 520 | f.write_joined(&*parameters.0, ", ")?; |
510 | write!(f, ">")?; | 521 | write!(f, ">")?; |
@@ -537,7 +548,7 @@ impl HirDisplay for Ty { | |||
537 | } | 548 | } |
538 | ImplTraitId::AsyncBlockTypeImplTrait(..) => { | 549 | ImplTraitId::AsyncBlockTypeImplTrait(..) => { |
539 | write!(f, "impl Future<Output = ")?; | 550 | write!(f, "impl Future<Output = ")?; |
540 | parameters[0].hir_fmt(f)?; | 551 | parameters.at(&Interner, 0).hir_fmt(f)?; |
541 | write!(f, ">")?; | 552 | write!(f, ">")?; |
542 | } | 553 | } |
543 | } | 554 | } |
@@ -548,7 +559,7 @@ impl HirDisplay for Ty { | |||
548 | DisplaySourceCodeError::Closure, | 559 | DisplaySourceCodeError::Closure, |
549 | )); | 560 | )); |
550 | } | 561 | } |
551 | let sig = substs[0].callable_sig(f.db); | 562 | let sig = substs.at(&Interner, 0).assert_ty_ref(&Interner).callable_sig(f.db); |
552 | if let Some(sig) = sig { | 563 | if let Some(sig) = sig { |
553 | if sig.params().is_empty() { | 564 | if sig.params().is_empty() { |
554 | write!(f, "||")?; | 565 | write!(f, "||")?; |
@@ -718,7 +729,9 @@ fn write_bounds_like_dyn_trait( | |||
718 | write!(f, "{}", f.db.trait_data(trait_).name)?; | 729 | write!(f, "{}", f.db.trait_data(trait_).name)?; |
719 | if let [_, params @ ..] = &*trait_ref.substitution.0 { | 730 | if let [_, params @ ..] = &*trait_ref.substitution.0 { |
720 | if is_fn_trait { | 731 | if is_fn_trait { |
721 | if let Some(args) = params.first().and_then(|it| it.as_tuple()) { | 732 | if let Some(args) = |
733 | params.first().and_then(|it| it.assert_ty_ref(&Interner).as_tuple()) | ||
734 | { | ||
722 | write!(f, "(")?; | 735 | write!(f, "(")?; |
723 | f.write_joined(&*args.0, ", ")?; | 736 | f.write_joined(&*args.0, ", ")?; |
724 | write!(f, ")")?; | 737 | write!(f, ")")?; |
@@ -767,16 +780,16 @@ impl TraitRef { | |||
767 | return write!(f, "{}", TYPE_HINT_TRUNCATION); | 780 | return write!(f, "{}", TYPE_HINT_TRUNCATION); |
768 | } | 781 | } |
769 | 782 | ||
770 | self.substitution[0].hir_fmt(f)?; | 783 | self.self_type_parameter().hir_fmt(f)?; |
771 | if use_as { | 784 | if use_as { |
772 | write!(f, " as ")?; | 785 | write!(f, " as ")?; |
773 | } else { | 786 | } else { |
774 | write!(f, ": ")?; | 787 | write!(f, ": ")?; |
775 | } | 788 | } |
776 | write!(f, "{}", f.db.trait_data(self.hir_trait_id()).name)?; | 789 | write!(f, "{}", f.db.trait_data(self.hir_trait_id()).name)?; |
777 | if self.substitution.len() > 1 { | 790 | if self.substitution.len(&Interner) > 1 { |
778 | write!(f, "<")?; | 791 | write!(f, "<")?; |
779 | f.write_joined(&self.substitution[1..], ", ")?; | 792 | f.write_joined(&self.substitution.interned(&Interner)[1..], ", ")?; |
780 | write!(f, ">")?; | 793 | write!(f, ">")?; |
781 | } | 794 | } |
782 | Ok(()) | 795 | Ok(()) |
@@ -1016,11 +1029,11 @@ impl HirDisplay for Path { | |||
1016 | } | 1029 | } |
1017 | } | 1030 | } |
1018 | 1031 | ||
1019 | impl HirDisplay for GenericArg { | 1032 | impl HirDisplay for hir_def::path::GenericArg { |
1020 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { | 1033 | fn hir_fmt(&self, f: &mut HirFormatter) -> Result<(), HirDisplayError> { |
1021 | match self { | 1034 | match self { |
1022 | GenericArg::Type(ty) => ty.hir_fmt(f), | 1035 | hir_def::path::GenericArg::Type(ty) => ty.hir_fmt(f), |
1023 | GenericArg::Lifetime(lifetime) => write!(f, "{}", lifetime.name), | 1036 | hir_def::path::GenericArg::Lifetime(lifetime) => write!(f, "{}", lifetime.name), |
1024 | } | 1037 | } |
1025 | } | 1038 | } |
1026 | } | 1039 | } |