@@ -11,8 +11,8 @@ use base_db::Crate;
11
11
use chalk_ir:: { BoundVar , Safety , TyKind } ;
12
12
use either:: Either ;
13
13
use hir_def:: {
14
- GenericDefId , HasModule , ImportPathConfig , LocalFieldId , Lookup , ModuleDefId , ModuleId ,
15
- TraitId ,
14
+ GeneralConstId , GenericDefId , HasModule , ImportPathConfig , LocalFieldId , Lookup , ModuleDefId ,
15
+ ModuleId , TraitId ,
16
16
db:: DefDatabase ,
17
17
expr_store:: { ExpressionStore , path:: Path } ,
18
18
find_path:: { self , PrefixKind } ,
@@ -38,7 +38,7 @@ use rustc_apfloat::{
38
38
} ;
39
39
use rustc_hash:: FxHashSet ;
40
40
use rustc_type_ir:: {
41
- AliasTyKind ,
41
+ AliasTyKind , RegionKind ,
42
42
inherent:: { AdtDef , IntoKind , SliceLike } ,
43
43
} ;
44
44
use smallvec:: SmallVec ;
@@ -61,8 +61,9 @@ use crate::{
61
61
next_solver:: {
62
62
BoundExistentialPredicate , Ctor , DbInterner , GenericArgs , SolverDefId ,
63
63
mapping:: {
64
- ChalkToNextSolver , convert_args_for_result, convert_const_for_result,
65
- convert_region_for_result, convert_ty_for_result,
64
+ ChalkToNextSolver , bound_var_to_lifetime_idx, bound_var_to_type_or_const_param_idx,
65
+ convert_args_for_result, convert_const_for_result, convert_region_for_result,
66
+ convert_ty_for_result,
66
67
} ,
67
68
} ,
68
69
primitive, to_assoc_type_id,
@@ -715,28 +716,56 @@ impl HirDisplay for GenericArg {
715
716
}
716
717
}
717
718
719
+ impl < ' db > HirDisplay for crate :: next_solver:: GenericArg < ' db > {
720
+ fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
721
+ match self . kind ( ) {
722
+ rustc_type_ir:: GenericArgKind :: Type ( ty) => ty. hir_fmt ( f) ,
723
+ rustc_type_ir:: GenericArgKind :: Lifetime ( lt) => lt. hir_fmt ( f) ,
724
+ rustc_type_ir:: GenericArgKind :: Const ( c) => c. hir_fmt ( f) ,
725
+ }
726
+ }
727
+ }
728
+
718
729
impl HirDisplay for Const {
719
730
fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
720
- let data = self . interned ( ) ;
721
- match & data. value {
722
- ConstValue :: BoundVar ( idx) => idx. hir_fmt ( f) ,
723
- ConstValue :: InferenceVar ( ..) => write ! ( f, "#c#" ) ,
724
- ConstValue :: Placeholder ( idx) => {
725
- let id = from_placeholder_idx ( f. db , * idx) ;
731
+ let c = self . to_nextsolver ( DbInterner :: new_with ( f. db , None , None ) ) ;
732
+ c. hir_fmt ( f)
733
+ }
734
+ }
735
+
736
+ impl < ' db > HirDisplay for crate :: next_solver:: Const < ' db > {
737
+ fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
738
+ match self . kind ( ) {
739
+ rustc_type_ir:: ConstKind :: Bound ( db, bound_const) => {
740
+ write ! ( f, "?{}.{}" , db. as_u32( ) , bound_const. as_u32( ) )
741
+ }
742
+ rustc_type_ir:: ConstKind :: Infer ( ..) => write ! ( f, "#c#" ) ,
743
+ rustc_type_ir:: ConstKind :: Placeholder ( idx) => {
744
+ let id = bound_var_to_type_or_const_param_idx ( f. db , idx. bound ) ;
726
745
let generics = generics ( f. db , id. parent ) ;
727
746
let param_data = & generics[ id. local_id ] ;
728
747
write ! ( f, "{}" , param_data. name( ) . unwrap( ) . display( f. db, f. edition( ) ) ) ?;
729
748
Ok ( ( ) )
730
749
}
731
- ConstValue :: Concrete ( c) => match & c. interned {
732
- ConstScalar :: Bytes ( b, m) => render_const_scalar ( f, b, m, & data. ty ) ,
733
- ConstScalar :: UnevaluatedConst ( c, parameters) => {
734
- write ! ( f, "{}" , c. name( f. db) ) ?;
735
- hir_fmt_generics ( f, parameters. as_slice ( Interner ) , c. generic_def ( f. db ) , None ) ?;
736
- Ok ( ( ) )
737
- }
738
- ConstScalar :: Unknown => f. write_char ( '_' ) ,
739
- } ,
750
+ rustc_type_ir:: ConstKind :: Value ( const_bytes) => render_const_scalar_ns (
751
+ f,
752
+ & const_bytes. value . inner ( ) . 0 ,
753
+ & const_bytes. value . inner ( ) . 1 ,
754
+ const_bytes. ty ,
755
+ ) ,
756
+ rustc_type_ir:: ConstKind :: Unevaluated ( unev) => {
757
+ let c = match unev. def {
758
+ SolverDefId :: ConstId ( id) => GeneralConstId :: ConstId ( id) ,
759
+ SolverDefId :: StaticId ( id) => GeneralConstId :: StaticId ( id) ,
760
+ _ => unreachable ! ( ) ,
761
+ } ;
762
+ write ! ( f, "{}" , c. name( f. db) ) ?;
763
+ hir_fmt_generics_ns ( f, unev. args . as_slice ( ) , c. generic_def ( f. db ) , None ) ?;
764
+ Ok ( ( ) )
765
+ }
766
+ rustc_type_ir:: ConstKind :: Error ( ..) => f. write_char ( '_' ) ,
767
+ rustc_type_ir:: ConstKind :: Expr ( ..) => write ! ( f, "<const-expr>" ) ,
768
+ rustc_type_ir:: ConstKind :: Param ( _) => write ! ( f, "<param>" ) ,
740
769
}
741
770
}
742
771
}
@@ -1748,6 +1777,27 @@ fn hir_fmt_generics(
1748
1777
Ok ( ( ) )
1749
1778
}
1750
1779
1780
+ fn hir_fmt_generics_ns < ' db > (
1781
+ f : & mut HirFormatter < ' _ > ,
1782
+ parameters : & [ crate :: next_solver:: GenericArg < ' db > ] ,
1783
+ generic_def : Option < hir_def:: GenericDefId > ,
1784
+ self_ : Option < crate :: next_solver:: Ty < ' db > > ,
1785
+ ) -> Result < ( ) , HirDisplayError > {
1786
+ if parameters. is_empty ( ) {
1787
+ return Ok ( ( ) ) ;
1788
+ }
1789
+
1790
+ let parameters_to_write = generic_args_sans_defaults_ns ( f, generic_def, parameters) ;
1791
+
1792
+ if !parameters_to_write. is_empty ( ) {
1793
+ write ! ( f, "<" ) ?;
1794
+ hir_fmt_generic_arguments_ns ( f, parameters_to_write, self_) ?;
1795
+ write ! ( f, ">" ) ?;
1796
+ }
1797
+
1798
+ Ok ( ( ) )
1799
+ }
1800
+
1751
1801
fn generic_args_sans_defaults < ' ga > (
1752
1802
f : & mut HirFormatter < ' _ > ,
1753
1803
generic_def : Option < hir_def:: GenericDefId > ,
@@ -1803,6 +1853,87 @@ fn generic_args_sans_defaults<'ga>(
1803
1853
}
1804
1854
}
1805
1855
1856
+ fn hir_fmt_generic_args < ' db > (
1857
+ f : & mut HirFormatter < ' _ > ,
1858
+ parameters : & [ crate :: next_solver:: GenericArg < ' db > ] ,
1859
+ generic_def : Option < hir_def:: GenericDefId > ,
1860
+ self_ : Option < crate :: next_solver:: Ty < ' db > > ,
1861
+ ) -> Result < ( ) , HirDisplayError > {
1862
+ if parameters. is_empty ( ) {
1863
+ return Ok ( ( ) ) ;
1864
+ }
1865
+
1866
+ let parameters_to_write = generic_args_sans_defaults_ns ( f, generic_def, parameters) ;
1867
+
1868
+ if !parameters_to_write. is_empty ( ) {
1869
+ write ! ( f, "<" ) ?;
1870
+ hir_fmt_generic_arguments_ns ( f, parameters_to_write, self_) ?;
1871
+ write ! ( f, ">" ) ?;
1872
+ }
1873
+
1874
+ Ok ( ( ) )
1875
+ }
1876
+
1877
+ fn generic_args_sans_defaults_ns < ' ga , ' db > (
1878
+ f : & mut HirFormatter < ' _ > ,
1879
+ generic_def : Option < hir_def:: GenericDefId > ,
1880
+ parameters : & ' ga [ crate :: next_solver:: GenericArg < ' db > ] ,
1881
+ ) -> & ' ga [ crate :: next_solver:: GenericArg < ' db > ] {
1882
+ let interner = DbInterner :: new_with ( f. db , Some ( f. krate ( ) ) , None ) ;
1883
+ if f. display_kind . is_source_code ( ) || f. omit_verbose_types ( ) {
1884
+ match generic_def
1885
+ . map ( |generic_def_id| f. db . generic_defaults ( generic_def_id) )
1886
+ . filter ( |it| !it. is_empty ( ) )
1887
+ {
1888
+ None => parameters,
1889
+ Some ( default_parameters) => {
1890
+ let should_show = |arg : & crate :: next_solver:: GenericArg < ' db > , i : usize | {
1891
+ let is_err = |arg : & crate :: next_solver:: GenericArg < ' db > | match arg. kind ( ) {
1892
+ rustc_type_ir:: GenericArgKind :: Lifetime ( it) => {
1893
+ matches ! ( it. kind( ) , RegionKind :: ReError ( ..) )
1894
+ }
1895
+ rustc_type_ir:: GenericArgKind :: Type ( it) => {
1896
+ matches ! ( it. kind( ) , rustc_type_ir:: TyKind :: Error ( ..) )
1897
+ }
1898
+ rustc_type_ir:: GenericArgKind :: Const ( it) => {
1899
+ matches ! ( it. kind( ) , rustc_type_ir:: ConstKind :: Error ( ..) , )
1900
+ }
1901
+ } ;
1902
+ // if the arg is error like, render it to inform the user
1903
+ if is_err ( arg) {
1904
+ return true ;
1905
+ }
1906
+ // otherwise, if the arg is equal to the param default, hide it (unless the
1907
+ // default is an error which can happen for the trait Self type)
1908
+ match default_parameters. get ( i) {
1909
+ None => true ,
1910
+ Some ( default_parameter) => {
1911
+ // !is_err(default_parameter.skip_binders())
1912
+ // &&
1913
+ arg != & default_parameter
1914
+ . clone ( )
1915
+ . substitute (
1916
+ Interner ,
1917
+ & convert_args_for_result ( interner, & parameters[ ..i] ) ,
1918
+ )
1919
+ . to_nextsolver ( interner)
1920
+ }
1921
+ }
1922
+ } ;
1923
+ let mut default_from = 0 ;
1924
+ for ( i, parameter) in parameters. iter ( ) . enumerate ( ) {
1925
+ if should_show ( parameter, i) {
1926
+ default_from = i + 1 ;
1927
+ }
1928
+ }
1929
+ & parameters[ 0 ..default_from]
1930
+ }
1931
+ }
1932
+ } else {
1933
+ parameters
1934
+ }
1935
+ }
1936
+
1806
1937
fn hir_fmt_generic_arguments (
1807
1938
f : & mut HirFormatter < ' _ > ,
1808
1939
parameters : & [ GenericArg ] ,
@@ -1827,6 +1958,30 @@ fn hir_fmt_generic_arguments(
1827
1958
Ok ( ( ) )
1828
1959
}
1829
1960
1961
+ fn hir_fmt_generic_arguments_ns < ' db > (
1962
+ f : & mut HirFormatter < ' _ > ,
1963
+ parameters : & [ crate :: next_solver:: GenericArg < ' db > ] ,
1964
+ self_ : Option < crate :: next_solver:: Ty < ' db > > ,
1965
+ ) -> Result < ( ) , HirDisplayError > {
1966
+ let mut first = true ;
1967
+ let lifetime_offset = parameters. iter ( ) . position ( |arg| arg. region ( ) . is_some ( ) ) ;
1968
+
1969
+ let ( ty_or_const, lifetimes) = match lifetime_offset {
1970
+ Some ( offset) => parameters. split_at ( offset) ,
1971
+ None => ( parameters, & [ ] [ ..] ) ,
1972
+ } ;
1973
+ for generic_arg in lifetimes. iter ( ) . chain ( ty_or_const) {
1974
+ if !mem:: take ( & mut first) {
1975
+ write ! ( f, ", " ) ?;
1976
+ }
1977
+ match self_ {
1978
+ self_ @ Some ( _) if generic_arg. ty ( ) == self_ => write ! ( f, "Self" ) ?,
1979
+ _ => generic_arg. hir_fmt ( f) ?,
1980
+ }
1981
+ }
1982
+ Ok ( ( ) )
1983
+ }
1984
+
1830
1985
impl HirDisplay for CallableSig {
1831
1986
fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
1832
1987
let CallableSig { params_and_return : _, is_varargs, safety, abi : _ } = * self ;
@@ -2067,6 +2222,20 @@ impl HirDisplay for TraitRef {
2067
2222
}
2068
2223
}
2069
2224
2225
+ impl < ' db > HirDisplay for crate :: next_solver:: TraitRef < ' db > {
2226
+ fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
2227
+ let trait_ = match self . def_id {
2228
+ SolverDefId :: TraitId ( id) => id,
2229
+ _ => unreachable ! ( ) ,
2230
+ } ;
2231
+ f. start_location_link ( trait_. into ( ) ) ;
2232
+ write ! ( f, "{}" , f. db. trait_signature( trait_) . name. display( f. db, f. edition( ) ) ) ?;
2233
+ f. end_location_link ( ) ;
2234
+ let substs = self . args . as_slice ( ) ;
2235
+ hir_fmt_generic_args ( f, & substs[ 1 ..] , None , substs[ 0 ] . ty ( ) )
2236
+ }
2237
+ }
2238
+
2070
2239
impl HirDisplay for WhereClause {
2071
2240
fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
2072
2241
if f. should_truncate ( ) {
@@ -2147,6 +2316,35 @@ impl HirDisplay for LifetimeData {
2147
2316
}
2148
2317
}
2149
2318
2319
+ impl < ' db > HirDisplay for crate :: next_solver:: Region < ' db > {
2320
+ fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
2321
+ match self . kind ( ) {
2322
+ rustc_type_ir:: RegionKind :: RePlaceholder ( idx) => {
2323
+ let id = bound_var_to_lifetime_idx ( f. db , idx. bound . var ) ;
2324
+ let generics = generics ( f. db , id. parent ) ;
2325
+ let param_data = & generics[ id. local_id ] ;
2326
+ write ! ( f, "{}" , param_data. name. display( f. db, f. edition( ) ) ) ?;
2327
+ Ok ( ( ) )
2328
+ }
2329
+ rustc_type_ir:: RegionKind :: ReBound ( db, idx) => {
2330
+ write ! ( f, "?{}.{}" , db. as_u32( ) , idx. var. as_u32( ) )
2331
+ }
2332
+ rustc_type_ir:: RegionKind :: ReVar ( _) => write ! ( f, "_" ) ,
2333
+ rustc_type_ir:: RegionKind :: ReStatic => write ! ( f, "'static" ) ,
2334
+ rustc_type_ir:: RegionKind :: ReError ( ..) => {
2335
+ if cfg ! ( test) {
2336
+ write ! ( f, "'?" )
2337
+ } else {
2338
+ write ! ( f, "'_" )
2339
+ }
2340
+ }
2341
+ rustc_type_ir:: RegionKind :: ReErased => write ! ( f, "'<erased>" ) ,
2342
+ rustc_type_ir:: RegionKind :: ReEarlyParam ( _) => write ! ( f, "<param>" ) ,
2343
+ rustc_type_ir:: RegionKind :: ReLateParam ( _) => write ! ( f, "<late-param>" ) ,
2344
+ }
2345
+ }
2346
+ }
2347
+
2150
2348
impl HirDisplay for DomainGoal {
2151
2349
fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
2152
2350
match self {
0 commit comments