@@ -63,6 +63,18 @@ thread_local! {
6363 static FORCE_TRIMMED_PATH : Cell <bool > = const { Cell :: new( false ) } ;
6464 static REDUCED_QUERIES : Cell <bool > = const { Cell :: new( false ) } ;
6565 static NO_VISIBLE_PATH : Cell <bool > = const { Cell :: new( false ) } ;
66+ static RTN_MODE : Cell <RtnMode > = const { Cell :: new( RtnMode :: ForDiagnostic ) } ;
67+ }
68+
69+ /// Rendering style for RTN types.
70+ #[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
71+ pub enum RtnMode {
72+ /// Print the RTN type as an impl trait with its path, i.e.e `impl Sized { T::method(..) }`.
73+ ForDiagnostic ,
74+ /// Print the RTN type as an impl trait, i.e. `impl Sized`.
75+ ForSignature ,
76+ /// Print the RTN type as a value path, i.e. `T::method(..): ...`.
77+ ForSuggestion ,
6678}
6779
6880macro_rules! define_helper {
@@ -124,6 +136,38 @@ define_helper!(
124136 fn with_no_visible_paths( NoVisibleGuard , NO_VISIBLE_PATH ) ;
125137) ;
126138
139+ #[ must_use]
140+ pub struct RtnModeHelper ( RtnMode ) ;
141+
142+ impl RtnModeHelper {
143+ pub fn with ( mode : RtnMode ) -> RtnModeHelper {
144+ RtnModeHelper ( RTN_MODE . with ( |c| c. replace ( mode) ) )
145+ }
146+ }
147+
148+ impl Drop for RtnModeHelper {
149+ fn drop ( & mut self ) {
150+ RTN_MODE . with ( |c| c. set ( self . 0 ) )
151+ }
152+ }
153+
154+ /// Print types for the purposes of a suggestion.
155+ ///
156+ /// Specifically, this will render RPITITs as `T::method(..)` which is suitable for
157+ /// things like where-clauses.
158+ pub macro with_types_for_suggestion ( $e: expr) { {
159+ let _guard = $crate:: ty:: print:: pretty:: RtnModeHelper :: with ( RtnMode :: ForSuggestion ) ;
160+ $e
161+ } }
162+
163+ /// Print types for the purposes of a signature suggestion.
164+ ///
165+ /// Specifically, this will render RPITITs as `impl Trait` rather than `T::method(..)`.
166+ pub macro with_types_for_signature ( $e: expr) { {
167+ let _guard = $crate:: ty:: print:: pretty:: RtnModeHelper :: with ( RtnMode :: ForSignature ) ;
168+ $e
169+ } }
170+
127171/// Avoids running any queries during prints.
128172pub macro with_no_queries ( $e: expr) { {
129173 $crate:: ty:: print:: with_reduced_queries!( $crate :: ty:: print:: with_forced_impl_filename_line!(
@@ -1223,22 +1267,6 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
12231267 }
12241268 }
12251269
1226- if self . tcx ( ) . features ( ) . return_type_notation ( )
1227- && let Some ( ty:: ImplTraitInTraitData :: Trait { fn_def_id, .. } ) =
1228- self . tcx ( ) . opt_rpitit_info ( def_id)
1229- && let ty:: Alias ( _, alias_ty) =
1230- self . tcx ( ) . fn_sig ( fn_def_id) . skip_binder ( ) . output ( ) . skip_binder ( ) . kind ( )
1231- && alias_ty. def_id == def_id
1232- && let generics = self . tcx ( ) . generics_of ( fn_def_id)
1233- // FIXME(return_type_notation): We only support lifetime params for now.
1234- && generics. own_params . iter ( ) . all ( |param| matches ! ( param. kind, ty:: GenericParamDefKind :: Lifetime ) )
1235- {
1236- let num_args = generics. count ( ) ;
1237- write ! ( self , " {{ " ) ?;
1238- self . print_def_path ( fn_def_id, & args[ ..num_args] ) ?;
1239- write ! ( self , "(..) }}" ) ?;
1240- }
1241-
12421270 Ok ( ( ) )
12431271 }
12441272
@@ -1306,6 +1334,46 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
13061334 )
13071335 }
13081336
1337+ fn pretty_print_rpitit (
1338+ & mut self ,
1339+ def_id : DefId ,
1340+ args : ty:: GenericArgsRef < ' tcx > ,
1341+ ) -> Result < ( ) , PrintError > {
1342+ let fn_args = if self . tcx ( ) . features ( ) . return_type_notation ( )
1343+ && let Some ( ty:: ImplTraitInTraitData :: Trait { fn_def_id, .. } ) =
1344+ self . tcx ( ) . opt_rpitit_info ( def_id)
1345+ && let ty:: Alias ( _, alias_ty) =
1346+ self . tcx ( ) . fn_sig ( fn_def_id) . skip_binder ( ) . output ( ) . skip_binder ( ) . kind ( )
1347+ && alias_ty. def_id == def_id
1348+ && let generics = self . tcx ( ) . generics_of ( fn_def_id)
1349+ // FIXME(return_type_notation): We only support lifetime params for now.
1350+ && generics. own_params . iter ( ) . all ( |param| matches ! ( param. kind, ty:: GenericParamDefKind :: Lifetime ) )
1351+ {
1352+ let num_args = generics. count ( ) ;
1353+ Some ( ( fn_def_id, & args[ ..num_args] ) )
1354+ } else {
1355+ None
1356+ } ;
1357+
1358+ match ( fn_args, RTN_MODE . with ( |c| c. get ( ) ) ) {
1359+ ( Some ( ( fn_def_id, fn_args) ) , RtnMode :: ForDiagnostic ) => {
1360+ self . pretty_print_opaque_impl_type ( def_id, args) ?;
1361+ write ! ( self , " {{ " ) ?;
1362+ self . print_def_path ( fn_def_id, fn_args) ?;
1363+ write ! ( self , "(..) }}" ) ?;
1364+ }
1365+ ( Some ( ( fn_def_id, fn_args) ) , RtnMode :: ForSuggestion ) => {
1366+ self . print_def_path ( fn_def_id, fn_args) ?;
1367+ write ! ( self , "(..)" ) ?;
1368+ }
1369+ _ => {
1370+ self . pretty_print_opaque_impl_type ( def_id, args) ?;
1371+ }
1372+ }
1373+
1374+ Ok ( ( ) )
1375+ }
1376+
13091377 fn ty_infer_name ( & self , _: ty:: TyVid ) -> Option < Symbol > {
13101378 None
13111379 }
@@ -3123,22 +3191,21 @@ define_print! {
31233191 ty:: AliasTerm <' tcx> {
31243192 match self . kind( cx. tcx( ) ) {
31253193 ty:: AliasTermKind :: InherentTy => p!( pretty_print_inherent_projection( * self ) ) ,
3126- ty:: AliasTermKind :: ProjectionTy
3127- | ty:: AliasTermKind :: WeakTy
3128- | ty:: AliasTermKind :: OpaqueTy
3129- | ty:: AliasTermKind :: UnevaluatedConst
3130- | ty:: AliasTermKind :: ProjectionConst => {
3131- // If we're printing verbosely, or don't want to invoke queries
3132- // (`is_impl_trait_in_trait`), then fall back to printing the def path.
3133- // This is likely what you want if you're debugging the compiler anyways.
3194+ ty:: AliasTermKind :: ProjectionTy => {
31343195 if !( cx. should_print_verbose( ) || with_reduced_queries( ) )
31353196 && cx. tcx( ) . is_impl_trait_in_trait( self . def_id)
31363197 {
3137- return cx . pretty_print_opaque_impl_type ( self . def_id, self . args) ;
3198+ p! ( pretty_print_rpitit ( self . def_id, self . args) )
31383199 } else {
31393200 p!( print_def_path( self . def_id, self . args) ) ;
31403201 }
31413202 }
3203+ | ty:: AliasTermKind :: WeakTy
3204+ | ty:: AliasTermKind :: OpaqueTy
3205+ | ty:: AliasTermKind :: UnevaluatedConst
3206+ | ty:: AliasTermKind :: ProjectionConst => {
3207+ p!( print_def_path( self . def_id, self . args) ) ;
3208+ }
31423209 }
31433210 }
31443211
0 commit comments