@@ -16,17 +16,12 @@ use rustc_data_structures::thin_vec::ThinVec;
1616use rustc_hir as hir;
1717use rustc_hir:: def:: { DefKind , Res } ;
1818use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
19- use rustc_middle:: mir;
20- use rustc_middle:: mir:: interpret:: ConstValue ;
2119use rustc_middle:: ty:: subst:: { GenericArgKind , SubstsRef } ;
2220use rustc_middle:: ty:: { self , DefIdTree , TyCtxt } ;
2321use rustc_span:: symbol:: { kw, sym, Symbol } ;
2422use std:: fmt:: Write as _;
2523use std:: mem;
2624
27- #[ cfg( test) ]
28- mod tests;
29-
3025pub ( crate ) fn krate ( cx : & mut DocContext < ' _ > ) -> Crate {
3126 let module = crate :: visit_ast:: RustdocVisitor :: new ( cx) . visit ( ) ;
3227
@@ -238,6 +233,8 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
238233 let mut s = if let Some ( def) = def. as_local ( ) {
239234 print_const_expr ( cx. tcx , cx. tcx . hir ( ) . body_owned_by ( def. did ) )
240235 } else {
236+ // FIXME: This relies on the HIR pretty-printer which leaks private and
237+ // doc(hidden) struct fields.
241238 inline:: print_inlined_const ( cx. tcx , def. did )
242239 } ;
243240 if let Some ( promoted) = promoted {
@@ -261,69 +258,6 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
261258 }
262259}
263260
264- pub ( crate ) fn print_evaluated_const ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> Option < String > {
265- tcx. const_eval_poly ( def_id) . ok ( ) . and_then ( |val| {
266- let ty = tcx. type_of ( def_id) ;
267- match ( val, ty. kind ( ) ) {
268- ( _, & ty:: Ref ( ..) ) => None ,
269- ( ConstValue :: Scalar ( _) , & ty:: Adt ( _, _) ) => None ,
270- ( ConstValue :: Scalar ( _) , _) => {
271- let const_ = mir:: ConstantKind :: from_value ( val, ty) ;
272- Some ( print_const_with_custom_print_scalar ( tcx, const_) )
273- }
274- _ => None ,
275- }
276- } )
277- }
278-
279- fn format_integer_with_underscore_sep ( num : & str ) -> String {
280- let num_chars: Vec < _ > = num. chars ( ) . collect ( ) ;
281- let mut num_start_index = if num_chars. get ( 0 ) == Some ( & '-' ) { 1 } else { 0 } ;
282- let chunk_size = match num[ num_start_index..] . as_bytes ( ) {
283- [ b'0' , b'b' | b'x' , ..] => {
284- num_start_index += 2 ;
285- 4
286- }
287- [ b'0' , b'o' , ..] => {
288- num_start_index += 2 ;
289- let remaining_chars = num_chars. len ( ) - num_start_index;
290- if remaining_chars <= 6 {
291- // don't add underscores to Unix permissions like 0755 or 100755
292- return num. to_string ( ) ;
293- }
294- 3
295- }
296- _ => 3 ,
297- } ;
298-
299- num_chars[ ..num_start_index]
300- . iter ( )
301- . chain ( num_chars[ num_start_index..] . rchunks ( chunk_size) . rev ( ) . intersperse ( & [ '_' ] ) . flatten ( ) )
302- . collect ( )
303- }
304-
305- fn print_const_with_custom_print_scalar ( tcx : TyCtxt < ' _ > , ct : mir:: ConstantKind < ' _ > ) -> String {
306- // Use a slightly different format for integer types which always shows the actual value.
307- // For all other types, fallback to the original `pretty_print_const`.
308- match ( ct, ct. ty ( ) . kind ( ) ) {
309- ( mir:: ConstantKind :: Val ( ConstValue :: Scalar ( int) , _) , ty:: Uint ( ui) ) => {
310- format ! ( "{}{}" , format_integer_with_underscore_sep( & int. to_string( ) ) , ui. name_str( ) )
311- }
312- ( mir:: ConstantKind :: Val ( ConstValue :: Scalar ( int) , _) , ty:: Int ( i) ) => {
313- let ty = tcx. lift ( ct. ty ( ) ) . unwrap ( ) ;
314- let size = tcx. layout_of ( ty:: ParamEnv :: empty ( ) . and ( ty) ) . unwrap ( ) . size ;
315- let data = int. assert_bits ( size) ;
316- let sign_extended_data = size. sign_extend ( data) as i128 ;
317- format ! (
318- "{}{}" ,
319- format_integer_with_underscore_sep( & sign_extended_data. to_string( ) ) ,
320- i. name_str( )
321- )
322- }
323- _ => ct. to_string ( ) ,
324- }
325- }
326-
327261pub ( crate ) fn is_literal_expr ( tcx : TyCtxt < ' _ > , hir_id : hir:: HirId ) -> bool {
328262 if let hir:: Node :: Expr ( expr) = tcx. hir ( ) . get ( hir_id) {
329263 if let hir:: ExprKind :: Lit ( _) = & expr. kind {
0 commit comments