Skip to content

Commit 4503a7c

Browse files
Merge pull request #21197 from ChayimFriedman2/result-alias-display
minor: Introduce a type alias for `HirDisplay`'s `Result`
2 parents 95cee9c + f523e95 commit 4503a7c

File tree

2 files changed

+70
-118
lines changed

2 files changed

+70
-118
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/display.rs

Lines changed: 37 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ use crate::{
6363
utils::{detect_variant_from_bytes, fn_traits},
6464
};
6565

66+
pub type Result<T = (), E = HirDisplayError> = std::result::Result<T, E>;
67+
6668
pub trait HirWrite: fmt::Write {
6769
fn start_location_link(&mut self, _location: ModuleDefId) {}
6870
fn end_location_link(&mut self) {}
@@ -190,7 +192,7 @@ impl<'db> HirFormatter<'_, 'db> {
190192
}
191193

192194
pub trait HirDisplay<'db> {
193-
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError>;
195+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result;
194196

195197
/// Returns a `Display`able type that is human-readable.
196198
fn into_displayable<'a>(
@@ -399,7 +401,7 @@ impl<'db> HirFormatter<'_, 'db> {
399401
&mut self,
400402
iter: impl IntoIterator<Item = T>,
401403
sep: &str,
402-
) -> Result<(), HirDisplayError> {
404+
) -> Result {
403405
let mut first = true;
404406
for e in iter {
405407
if !first {
@@ -418,7 +420,7 @@ impl<'db> HirFormatter<'_, 'db> {
418420
}
419421

420422
/// This allows using the `write!` macro directly with a `HirFormatter`.
421-
pub fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> Result<(), HirDisplayError> {
423+
pub fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> Result {
422424
// We write to a buffer first to track output size
423425
self.buf.clear();
424426
fmt::write(&mut self.buf, args)?;
@@ -428,12 +430,12 @@ impl<'db> HirFormatter<'_, 'db> {
428430
self.fmt.write_str(&self.buf).map_err(HirDisplayError::from)
429431
}
430432

431-
pub fn write_str(&mut self, s: &str) -> Result<(), HirDisplayError> {
433+
pub fn write_str(&mut self, s: &str) -> Result {
432434
self.fmt.write_str(s)?;
433435
Ok(())
434436
}
435437

436-
pub fn write_char(&mut self, c: char) -> Result<(), HirDisplayError> {
438+
pub fn write_char(&mut self, c: char) -> Result {
437439
self.fmt.write_char(c)?;
438440
Ok(())
439441
}
@@ -541,7 +543,7 @@ pub enum ClosureStyle {
541543
}
542544

543545
impl<'db, T: HirDisplay<'db>> HirDisplayWrapper<'_, 'db, T> {
544-
pub fn write_to<F: HirWrite>(&self, f: &mut F) -> Result<(), HirDisplayError> {
546+
pub fn write_to<F: HirWrite>(&self, f: &mut F) -> Result {
545547
let krate = self.display_target.krate;
546548
let interner = DbInterner::new_with(self.db, krate);
547549
self.t.hir_fmt(&mut HirFormatter {
@@ -594,21 +596,18 @@ where
594596
const TYPE_HINT_TRUNCATION: &str = "…";
595597

596598
impl<'db, T: HirDisplay<'db>> HirDisplay<'db> for &T {
597-
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
599+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result {
598600
HirDisplay::hir_fmt(*self, f)
599601
}
600602
}
601603

602604
impl<'db, T: HirDisplay<'db> + Internable> HirDisplay<'db> for Interned<T> {
603-
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
605+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result {
604606
HirDisplay::hir_fmt(self.as_ref(), f)
605607
}
606608
}
607609

608-
fn write_projection<'db>(
609-
f: &mut HirFormatter<'_, 'db>,
610-
alias: &AliasTy<'db>,
611-
) -> Result<(), HirDisplayError> {
610+
fn write_projection<'db>(f: &mut HirFormatter<'_, 'db>, alias: &AliasTy<'db>) -> Result {
612611
if f.should_truncate() {
613612
return write!(f, "{TYPE_HINT_TRUNCATION}");
614613
}
@@ -665,7 +664,7 @@ fn write_projection<'db>(
665664
}
666665

667666
impl<'db> HirDisplay<'db> for GenericArg<'db> {
668-
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
667+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result {
669668
match self {
670669
GenericArg::Ty(ty) => ty.hir_fmt(f),
671670
GenericArg::Lifetime(lt) => lt.hir_fmt(f),
@@ -675,7 +674,7 @@ impl<'db> HirDisplay<'db> for GenericArg<'db> {
675674
}
676675

677676
impl<'db> HirDisplay<'db> for Const<'db> {
678-
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
677+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result {
679678
match self.kind() {
680679
ConstKind::Placeholder(_) => write!(f, "<placeholder>"),
681680
ConstKind::Bound(BoundVarIndexKind::Bound(db), bound_const) => {
@@ -714,7 +713,7 @@ fn render_const_scalar<'db>(
714713
b: &[u8],
715714
memory_map: &MemoryMap<'db>,
716715
ty: Ty<'db>,
717-
) -> Result<(), HirDisplayError> {
716+
) -> Result {
718717
let param_env = ParamEnv::empty();
719718
let infcx = f.interner.infer_ctxt().build(TypingMode::PostAnalysis);
720719
let ty = infcx.at(&ObligationCause::new(), param_env).deeply_normalize(ty).unwrap_or(ty);
@@ -727,7 +726,7 @@ fn render_const_scalar_inner<'db>(
727726
memory_map: &MemoryMap<'db>,
728727
ty: Ty<'db>,
729728
param_env: ParamEnv<'db>,
730-
) -> Result<(), HirDisplayError> {
729+
) -> Result {
731730
use TyKind;
732731
let param_env = ParamEnvAndCrate { param_env, krate: f.krate() };
733732
match ty.kind() {
@@ -1000,7 +999,7 @@ fn render_variant_after_name<'db>(
1000999
args: GenericArgs<'db>,
10011000
b: &[u8],
10021001
memory_map: &MemoryMap<'db>,
1003-
) -> Result<(), HirDisplayError> {
1002+
) -> Result {
10041003
let param_env = ParamEnvAndCrate { param_env, krate: f.krate() };
10051004
match data.shape {
10061005
FieldsShape::Record | FieldsShape::Tuple => {
@@ -1044,10 +1043,7 @@ fn render_variant_after_name<'db>(
10441043
}
10451044

10461045
impl<'db> HirDisplay<'db> for Ty<'db> {
1047-
fn hir_fmt(
1048-
&self,
1049-
f @ &mut HirFormatter { db, .. }: &mut HirFormatter<'_, 'db>,
1050-
) -> Result<(), HirDisplayError> {
1046+
fn hir_fmt(&self, f @ &mut HirFormatter { db, .. }: &mut HirFormatter<'_, 'db>) -> Result {
10511047
let interner = f.interner;
10521048
if f.should_truncate() {
10531049
return write!(f, "{TYPE_HINT_TRUNCATION}");
@@ -1650,7 +1646,7 @@ fn hir_fmt_generics<'db>(
16501646
parameters: &[GenericArg<'db>],
16511647
generic_def: Option<hir_def::GenericDefId>,
16521648
self_: Option<Ty<'db>>,
1653-
) -> Result<(), HirDisplayError> {
1649+
) -> Result {
16541650
if parameters.is_empty() {
16551651
return Ok(());
16561652
}
@@ -1700,7 +1696,7 @@ fn hir_fmt_generic_args<'db>(
17001696
parameters: &[GenericArg<'db>],
17011697
generic_def: Option<hir_def::GenericDefId>,
17021698
self_: Option<Ty<'db>>,
1703-
) -> Result<(), HirDisplayError> {
1699+
) -> Result {
17041700
if parameters.is_empty() {
17051701
return Ok(());
17061702
}
@@ -1720,7 +1716,7 @@ fn hir_fmt_generic_arguments<'db>(
17201716
f: &mut HirFormatter<'_, 'db>,
17211717
parameters: &[GenericArg<'db>],
17221718
self_: Option<Ty<'db>>,
1723-
) -> Result<(), HirDisplayError> {
1719+
) -> Result {
17241720
let mut first = true;
17251721
let lifetime_offset = parameters.iter().position(|arg| arg.region().is_some());
17261722

@@ -1744,7 +1740,7 @@ fn hir_fmt_tys<'db>(
17441740
f: &mut HirFormatter<'_, 'db>,
17451741
tys: &[Ty<'db>],
17461742
self_: Option<Ty<'db>>,
1747-
) -> Result<(), HirDisplayError> {
1743+
) -> Result {
17481744
let mut first = true;
17491745

17501746
for ty in tys {
@@ -1760,7 +1756,7 @@ fn hir_fmt_tys<'db>(
17601756
}
17611757

17621758
impl<'db> HirDisplay<'db> for PolyFnSig<'db> {
1763-
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
1759+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result {
17641760
let FnSig { inputs_and_output, c_variadic, safety, abi: _ } = self.skip_binder();
17651761
if let Safety::Unsafe = safety {
17661762
write!(f, "unsafe ")?;
@@ -1791,7 +1787,7 @@ impl<'db> HirDisplay<'db> for PolyFnSig<'db> {
17911787
}
17921788

17931789
impl<'db> HirDisplay<'db> for Term<'db> {
1794-
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
1790+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result {
17951791
match self {
17961792
Term::Ty(it) => it.hir_fmt(f),
17971793
Term::Const(it) => it.hir_fmt(f),
@@ -1823,7 +1819,7 @@ pub fn write_bounds_like_dyn_trait_with_prefix<'db>(
18231819
this: Either<Ty<'db>, Region<'db>>,
18241820
predicates: &[Clause<'db>],
18251821
default_sized: SizedByDefault,
1826-
) -> Result<(), HirDisplayError> {
1822+
) -> Result {
18271823
write!(f, "{prefix}")?;
18281824
if !predicates.is_empty()
18291825
|| predicates.is_empty() && matches!(default_sized, SizedByDefault::Sized { .. })
@@ -1840,7 +1836,7 @@ fn write_bounds_like_dyn_trait<'db>(
18401836
this: Either<Ty<'db>, Region<'db>>,
18411837
predicates: &[Clause<'db>],
18421838
default_sized: SizedByDefault,
1843-
) -> Result<(), HirDisplayError> {
1839+
) -> Result {
18441840
// Note: This code is written to produce nice results (i.e.
18451841
// corresponding to surface Rust) for types that can occur in
18461842
// actual Rust. It will have weird results if the predicates
@@ -1984,7 +1980,7 @@ fn write_bounds_like_dyn_trait<'db>(
19841980
}
19851981

19861982
impl<'db> HirDisplay<'db> for TraitRef<'db> {
1987-
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
1983+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result {
19881984
let trait_ = self.def_id.0;
19891985
f.start_location_link(trait_.into());
19901986
write!(f, "{}", f.db.trait_signature(trait_).name.display(f.db, f.edition()))?;
@@ -1995,7 +1991,7 @@ impl<'db> HirDisplay<'db> for TraitRef<'db> {
19951991
}
19961992

19971993
impl<'db> HirDisplay<'db> for Region<'db> {
1998-
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
1994+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result {
19991995
match self.kind() {
20001996
RegionKind::ReEarlyParam(param) => {
20011997
let generics = generics(f.db, param.id.parent);
@@ -2029,7 +2025,7 @@ pub fn write_visibility<'db>(
20292025
module_id: ModuleId,
20302026
vis: Visibility,
20312027
f: &mut HirFormatter<'_, 'db>,
2032-
) -> Result<(), HirDisplayError> {
2028+
) -> Result {
20332029
match vis {
20342030
Visibility::Public => write!(f, "pub "),
20352031
Visibility::PubCrate(_) => write!(f, "pub(crate) "),
@@ -2052,21 +2048,13 @@ pub fn write_visibility<'db>(
20522048
}
20532049

20542050
pub trait HirDisplayWithExpressionStore<'db> {
2055-
fn hir_fmt(
2056-
&self,
2057-
f: &mut HirFormatter<'_, 'db>,
2058-
store: &ExpressionStore,
2059-
) -> Result<(), HirDisplayError>;
2051+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>, store: &ExpressionStore) -> Result;
20602052
}
20612053

20622054
impl<'db, T: ?Sized + HirDisplayWithExpressionStore<'db>> HirDisplayWithExpressionStore<'db>
20632055
for &'_ T
20642056
{
2065-
fn hir_fmt(
2066-
&self,
2067-
f: &mut HirFormatter<'_, 'db>,
2068-
store: &ExpressionStore,
2069-
) -> Result<(), HirDisplayError> {
2057+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>, store: &ExpressionStore) -> Result {
20702058
T::hir_fmt(&**self, f, store)
20712059
}
20722060
}
@@ -2087,16 +2075,12 @@ impl<'a, T> ExpressionStoreAdapter<'a, T> {
20872075
}
20882076

20892077
impl<'db, T: HirDisplayWithExpressionStore<'db>> HirDisplay<'db> for ExpressionStoreAdapter<'_, T> {
2090-
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result<(), HirDisplayError> {
2078+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>) -> Result {
20912079
T::hir_fmt(&self.0, f, self.1)
20922080
}
20932081
}
20942082
impl<'db> HirDisplayWithExpressionStore<'db> for LifetimeRefId {
2095-
fn hir_fmt(
2096-
&self,
2097-
f: &mut HirFormatter<'_, 'db>,
2098-
store: &ExpressionStore,
2099-
) -> Result<(), HirDisplayError> {
2083+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>, store: &ExpressionStore) -> Result {
21002084
match &store[*self] {
21012085
LifetimeRef::Named(name) => write!(f, "{}", name.display(f.db, f.edition())),
21022086
LifetimeRef::Static => write!(f, "'static"),
@@ -2115,11 +2099,7 @@ impl<'db> HirDisplayWithExpressionStore<'db> for LifetimeRefId {
21152099
}
21162100

21172101
impl<'db> HirDisplayWithExpressionStore<'db> for TypeRefId {
2118-
fn hir_fmt(
2119-
&self,
2120-
f: &mut HirFormatter<'_, 'db>,
2121-
store: &ExpressionStore,
2122-
) -> Result<(), HirDisplayError> {
2102+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>, store: &ExpressionStore) -> Result {
21232103
match &store[*self] {
21242104
TypeRef::Never => write!(f, "!")?,
21252105
TypeRef::TypeParam(param) => {
@@ -2244,11 +2224,7 @@ impl<'db> HirDisplayWithExpressionStore<'db> for TypeRefId {
22442224
}
22452225

22462226
impl<'db> HirDisplayWithExpressionStore<'db> for ConstRef {
2247-
fn hir_fmt(
2248-
&self,
2249-
f: &mut HirFormatter<'_, 'db>,
2250-
_store: &ExpressionStore,
2251-
) -> Result<(), HirDisplayError> {
2227+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>, _store: &ExpressionStore) -> Result {
22522228
// FIXME
22532229
write!(f, "{{const}}")?;
22542230

@@ -2257,11 +2233,7 @@ impl<'db> HirDisplayWithExpressionStore<'db> for ConstRef {
22572233
}
22582234

22592235
impl<'db> HirDisplayWithExpressionStore<'db> for TypeBound {
2260-
fn hir_fmt(
2261-
&self,
2262-
f: &mut HirFormatter<'_, 'db>,
2263-
store: &ExpressionStore,
2264-
) -> Result<(), HirDisplayError> {
2236+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>, store: &ExpressionStore) -> Result {
22652237
match self {
22662238
&TypeBound::Path(path, modifier) => {
22672239
match modifier {
@@ -2301,11 +2273,7 @@ impl<'db> HirDisplayWithExpressionStore<'db> for TypeBound {
23012273
}
23022274

23032275
impl<'db> HirDisplayWithExpressionStore<'db> for Path {
2304-
fn hir_fmt(
2305-
&self,
2306-
f: &mut HirFormatter<'_, 'db>,
2307-
store: &ExpressionStore,
2308-
) -> Result<(), HirDisplayError> {
2276+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>, store: &ExpressionStore) -> Result {
23092277
match (self.type_anchor(), self.kind()) {
23102278
(Some(anchor), _) => {
23112279
write!(f, "<")?;
@@ -2453,11 +2421,7 @@ impl<'db> HirDisplayWithExpressionStore<'db> for Path {
24532421
}
24542422

24552423
impl<'db> HirDisplayWithExpressionStore<'db> for hir_def::expr_store::path::GenericArg {
2456-
fn hir_fmt(
2457-
&self,
2458-
f: &mut HirFormatter<'_, 'db>,
2459-
store: &ExpressionStore,
2460-
) -> Result<(), HirDisplayError> {
2424+
fn hir_fmt(&self, f: &mut HirFormatter<'_, 'db>, store: &ExpressionStore) -> Result {
24612425
match self {
24622426
hir_def::expr_store::path::GenericArg::Type(ty) => ty.hir_fmt(f, store),
24632427
hir_def::expr_store::path::GenericArg::Const(_c) => {

0 commit comments

Comments
 (0)