@@ -11,8 +11,8 @@ use base_db::Crate;
1111use chalk_ir:: { BoundVar , Safety , TyKind } ;
1212use either:: Either ;
1313use hir_def:: {
14- GenericDefId , HasModule , ImportPathConfig , LocalFieldId , Lookup , ModuleDefId , ModuleId ,
15- TraitId ,
14+ GeneralConstId , GenericDefId , HasModule , ImportPathConfig , LocalFieldId , Lookup , ModuleDefId ,
15+ ModuleId , TraitId ,
1616 db:: DefDatabase ,
1717 expr_store:: { ExpressionStore , path:: Path } ,
1818 find_path:: { self , PrefixKind } ,
@@ -38,7 +38,7 @@ use rustc_apfloat::{
3838} ;
3939use rustc_hash:: FxHashSet ;
4040use rustc_type_ir:: {
41- AliasTyKind ,
41+ AliasTyKind , RegionKind ,
4242 inherent:: { AdtDef , IntoKind , SliceLike } ,
4343} ;
4444use smallvec:: SmallVec ;
@@ -61,8 +61,9 @@ use crate::{
6161 next_solver:: {
6262 BoundExistentialPredicate , Ctor , DbInterner , GenericArgs , SolverDefId ,
6363 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,
6667 } ,
6768 } ,
6869 primitive, to_assoc_type_id,
@@ -715,28 +716,56 @@ impl HirDisplay for GenericArg {
715716 }
716717}
717718
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+
718729impl HirDisplay for Const {
719730 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 ) ;
726745 let generics = generics ( f. db , id. parent ) ;
727746 let param_data = & generics[ id. local_id ] ;
728747 write ! ( f, "{}" , param_data. name( ) . unwrap( ) . display( f. db, f. edition( ) ) ) ?;
729748 Ok ( ( ) )
730749 }
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>" ) ,
740769 }
741770 }
742771}
@@ -1748,6 +1777,27 @@ fn hir_fmt_generics(
17481777 Ok ( ( ) )
17491778}
17501779
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+
17511801fn generic_args_sans_defaults < ' ga > (
17521802 f : & mut HirFormatter < ' _ > ,
17531803 generic_def : Option < hir_def:: GenericDefId > ,
@@ -1803,6 +1853,87 @@ fn generic_args_sans_defaults<'ga>(
18031853 }
18041854}
18051855
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+
18061937fn hir_fmt_generic_arguments (
18071938 f : & mut HirFormatter < ' _ > ,
18081939 parameters : & [ GenericArg ] ,
@@ -1827,6 +1958,30 @@ fn hir_fmt_generic_arguments(
18271958 Ok ( ( ) )
18281959}
18291960
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+
18301985impl HirDisplay for CallableSig {
18311986 fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
18321987 let CallableSig { params_and_return : _, is_varargs, safety, abi : _ } = * self ;
@@ -2067,6 +2222,20 @@ impl HirDisplay for TraitRef {
20672222 }
20682223}
20692224
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+
20702239impl HirDisplay for WhereClause {
20712240 fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
20722241 if f. should_truncate ( ) {
@@ -2147,6 +2316,35 @@ impl HirDisplay for LifetimeData {
21472316 }
21482317}
21492318
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+
21502348impl HirDisplay for DomainGoal {
21512349 fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
21522350 match self {
0 commit comments