@@ -14,10 +14,16 @@ use rustc_ast::tokenstream::TokenTree;
1414use rustc_hir as hir;
1515use rustc_hir:: def:: { DefKind , Res } ;
1616use rustc_hir:: def_id:: { DefId , LocalDefId , LOCAL_CRATE } ;
17+ use rustc_infer:: infer:: at:: ToTrace ;
18+ use rustc_infer:: infer:: outlives:: env:: OutlivesEnvironment ;
1719use rustc_middle:: mir;
1820use rustc_middle:: mir:: interpret:: ConstValue ;
21+ use rustc_middle:: traits:: ObligationCause ;
1922use rustc_middle:: ty:: { self , GenericArgKind , GenericArgsRef , TyCtxt } ;
23+ use rustc_middle:: ty:: { TypeVisitable , TypeVisitableExt } ;
2024use rustc_span:: symbol:: { kw, sym, Symbol } ;
25+ use rustc_trait_selection:: infer:: TyCtxtInferExt ;
26+ use rustc_trait_selection:: traits:: ObligationCtxt ;
2127use std:: fmt:: Write as _;
2228use std:: mem;
2329use std:: sync:: LazyLock as Lazy ;
@@ -76,38 +82,129 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
7682
7783pub ( crate ) fn ty_args_to_args < ' tcx > (
7884 cx : & mut DocContext < ' tcx > ,
79- args : ty:: Binder < ' tcx , & ' tcx [ ty:: GenericArg < ' tcx > ] > ,
80- mut skip_first : bool ,
85+ ty_args : ty:: Binder < ' tcx , & ' tcx [ ty:: GenericArg < ' tcx > ] > ,
86+ skip_first : bool ,
8187 container : Option < DefId > ,
8288) -> Vec < GenericArg > {
83- let mut ret_val =
84- Vec :: with_capacity ( args. skip_binder ( ) . len ( ) . saturating_sub ( if skip_first { 1 } else { 0 } ) ) ;
89+ let param_env = ty:: ParamEnv :: empty ( ) ;
90+ let cause = ObligationCause :: dummy ( ) ;
91+ let params = container. map ( |container| & cx. tcx . generics_of ( container) . params ) ;
92+ let mut elision_has_failed_once_before = false ;
8593
86- ret_val. extend ( args. iter ( ) . enumerate ( ) . filter_map ( |( index, kind) | {
87- match kind. skip_binder ( ) . unpack ( ) {
88- GenericArgKind :: Lifetime ( lt) => {
89- Some ( GenericArg :: Lifetime ( clean_middle_region ( lt) . unwrap_or ( Lifetime :: elided ( ) ) ) )
90- }
91- GenericArgKind :: Type ( _) if skip_first => {
92- skip_first = false ;
93- None
94+ let offset = if skip_first { 1 } else { 0 } ;
95+ let mut args = Vec :: with_capacity ( ty_args. skip_binder ( ) . len ( ) . saturating_sub ( offset) ) ;
96+
97+ let ty_arg_to_arg = |( index, arg) : ( usize , & ty:: GenericArg < ' tcx > ) | match arg. unpack ( ) {
98+ GenericArgKind :: Lifetime ( lt) => {
99+ Some ( GenericArg :: Lifetime ( clean_middle_region ( lt) . unwrap_or ( Lifetime :: elided ( ) ) ) )
100+ }
101+ GenericArgKind :: Type ( _) if skip_first && index == 0 => None ,
102+ GenericArgKind :: Type ( ty) => {
103+ if !elision_has_failed_once_before
104+ && let Some ( params) = params
105+ && let Some ( default) = params[ index] . default_value ( cx. tcx )
106+ {
107+ let default =
108+ ty_args. map_bound ( |args| default. instantiate ( cx. tcx , args) . expect_ty ( ) ) ;
109+
110+ if can_elide_generic_arg (
111+ cx. tcx ,
112+ & cause,
113+ param_env,
114+ ty_args. rebind ( ty) ,
115+ default,
116+ params[ index] . def_id ,
117+ ) {
118+ return None ;
119+ }
120+
121+ elision_has_failed_once_before = true ;
94122 }
95- GenericArgKind :: Type ( ty) => Some ( GenericArg :: Type ( clean_middle_ty (
96- kind. rebind ( ty) ,
123+
124+ Some ( GenericArg :: Type ( clean_middle_ty (
125+ ty_args. rebind ( ty) ,
97126 cx,
98127 None ,
99128 container. map ( |container| crate :: clean:: ContainerTy :: Regular {
100129 ty : container,
101- args,
130+ args : ty_args ,
102131 arg : index,
103132 } ) ,
104- ) ) ) ,
105- GenericArgKind :: Const ( ct) => {
106- Some ( GenericArg :: Const ( Box :: new ( clean_middle_const ( kind. rebind ( ct) , cx) ) ) )
133+ ) ) )
134+ }
135+ GenericArgKind :: Const ( ct) => {
136+ if !elision_has_failed_once_before
137+ && let Some ( params) = params
138+ && let Some ( default) = params[ index] . default_value ( cx. tcx )
139+ {
140+ let default =
141+ ty_args. map_bound ( |args| default. instantiate ( cx. tcx , args) . expect_const ( ) ) ;
142+
143+ if can_elide_generic_arg (
144+ cx. tcx ,
145+ & cause,
146+ param_env,
147+ ty_args. rebind ( ct) ,
148+ default,
149+ params[ index] . def_id ,
150+ ) {
151+ return None ;
152+ }
153+
154+ elision_has_failed_once_before = true ;
107155 }
156+
157+ Some ( GenericArg :: Const ( Box :: new ( clean_middle_const ( ty_args. rebind ( ct) , cx) ) ) )
108158 }
109- } ) ) ;
110- ret_val
159+ } ;
160+
161+ // FIXME(fmease): This doesn't look performant to me!
162+ args. extend ( ty_args. skip_binder ( ) . iter ( ) . enumerate ( ) . rev ( ) . filter_map ( ty_arg_to_arg) ) ;
163+ args. reverse ( ) ;
164+ args
165+ }
166+
167+ fn can_elide_generic_arg < ' tcx , T : ToTrace < ' tcx > + TypeVisitable < TyCtxt < ' tcx > > > (
168+ tcx : TyCtxt < ' tcx > ,
169+ cause : & ObligationCause < ' tcx > ,
170+ param_env : ty:: ParamEnv < ' tcx > ,
171+ actual : ty:: Binder < ' tcx , T > ,
172+ default : ty:: Binder < ' tcx , T > ,
173+ did : DefId ,
174+ ) -> bool {
175+ assert ! ( !actual. has_infer( ) ) ;
176+ assert ! ( !default . has_infer( ) ) ;
177+
178+ // We intentionally look for *arbitrary* late-bound regions instead of only *escaping* ones as we
179+ // want to identify non-escaping alpha-equivalent late-bound regions later.
180+ if !actual. has_late_bound_regions ( )
181+ && !actual. has_projections ( )
182+ && !default. has_late_bound_regions ( )
183+ && !default. has_projections ( )
184+ {
185+ // fast path
186+ return actual. skip_binder ( ) == default. skip_binder ( ) ;
187+ }
188+
189+ let infcx = tcx. infer_ctxt ( ) . build ( ) ;
190+ let ocx = ObligationCtxt :: new ( & infcx) ;
191+
192+ // FIXME: This seems incorrect. Not sure what one is supposed to do in this situation though.
193+ let aggressively_liberate = |arg : ty:: Binder < ' tcx , T > | {
194+ tcx. fold_regions ( arg. skip_binder ( ) , |r, _| match * r {
195+ ty:: ReLateBound ( _, br) => ty:: Region :: new_free ( tcx, did, br. kind ) ,
196+ _ => r,
197+ } )
198+ } ;
199+ let actual = aggressively_liberate ( actual) ;
200+ let default = aggressively_liberate ( default) ;
201+
202+ let actual = ocx. normalize ( cause, param_env, actual) ;
203+ let default = ocx. normalize ( cause, param_env, default) ;
204+
205+ ocx. eq ( cause, param_env, actual, default) . is_ok ( )
206+ && ocx. select_all_or_error ( ) . is_empty ( )
207+ && infcx. resolve_regions ( & OutlivesEnvironment :: new ( param_env) ) . is_empty ( )
111208}
112209
113210fn external_generic_args < ' tcx > (
0 commit comments