@@ -6,16 +6,14 @@ use rustc_hir as hir;
6
6
use rustc_hir:: def:: CtorKind ;
7
7
use rustc_hir:: def_id:: DefId ;
8
8
use rustc_middle:: middle:: stability;
9
- use rustc_middle:: span_bug;
10
- use rustc_middle:: ty:: layout:: LayoutError ;
11
- use rustc_middle:: ty:: { self , Adt , TyCtxt } ;
9
+ use rustc_middle:: ty:: { self , TyCtxt } ;
12
10
use rustc_span:: hygiene:: MacroKind ;
13
11
use rustc_span:: symbol:: { kw, sym, Symbol } ;
14
- use rustc_target:: abi:: { LayoutS , Primitive , TagEncoding , Variants } ;
15
12
use std:: cmp:: Ordering ;
16
13
use std:: fmt;
17
14
use std:: rc:: Rc ;
18
15
16
+ use super :: type_layout:: document_type_layout;
19
17
use super :: {
20
18
collect_paths_for_type, document, ensure_trailing_slash, get_filtered_impls_for_reference,
21
19
item_ty_to_section, notable_traits_button, notable_traits_json, render_all_impls,
@@ -1933,118 +1931,6 @@ fn document_non_exhaustive<'a>(item: &'a clean::Item) -> impl fmt::Display + 'a
1933
1931
} )
1934
1932
}
1935
1933
1936
- fn document_type_layout < ' a , ' cx : ' a > (
1937
- cx : & ' a Context < ' cx > ,
1938
- ty_def_id : DefId ,
1939
- ) -> impl fmt:: Display + ' a + Captures < ' cx > {
1940
- fn write_size_of_layout ( mut w : impl fmt:: Write , layout : & LayoutS , tag_size : u64 ) {
1941
- if layout. abi . is_unsized ( ) {
1942
- write ! ( w, "(unsized)" ) . unwrap ( ) ;
1943
- } else {
1944
- let size = layout. size . bytes ( ) - tag_size;
1945
- write ! ( w, "{size} byte{pl}" , pl = if size == 1 { "" } else { "s" } ) . unwrap ( ) ;
1946
- if layout. abi . is_uninhabited ( ) {
1947
- write ! (
1948
- w,
1949
- " (<a href=\" https://doc.rust-lang.org/stable/reference/glossary.html#uninhabited\" >uninhabited</a>)"
1950
- ) . unwrap ( ) ;
1951
- }
1952
- }
1953
- }
1954
-
1955
- display_fn ( move |mut f| {
1956
- if !cx. shared . show_type_layout {
1957
- return Ok ( ( ) ) ;
1958
- }
1959
-
1960
- writeln ! (
1961
- f,
1962
- "<h2 id=\" layout\" class=\" small-section-header\" > \
1963
- Layout<a href=\" #layout\" class=\" anchor\" >§</a></h2>"
1964
- ) ?;
1965
- writeln ! ( f, "<div class=\" docblock\" >" ) ?;
1966
-
1967
- let tcx = cx. tcx ( ) ;
1968
- let param_env = tcx. param_env ( ty_def_id) ;
1969
- let ty = tcx. type_of ( ty_def_id) . subst_identity ( ) ;
1970
- match tcx. layout_of ( param_env. and ( ty) ) {
1971
- Ok ( ty_layout) => {
1972
- writeln ! (
1973
- f,
1974
- "<div class=\" warning\" ><p><strong>Note:</strong> Most layout information is \
1975
- <strong>completely unstable</strong> and may even differ between compilations. \
1976
- The only exception is types with certain <code>repr(...)</code> attributes. \
1977
- Please see the Rust Reference’s \
1978
- <a href=\" https://doc.rust-lang.org/reference/type-layout.html\" >“Type Layout”</a> \
1979
- chapter for details on type layout guarantees.</p></div>"
1980
- ) ?;
1981
- f. write_str ( "<p><strong>Size:</strong> " ) ?;
1982
- write_size_of_layout ( & mut f, & ty_layout. layout . 0 , 0 ) ;
1983
- writeln ! ( f, "</p>" ) ?;
1984
- if let Variants :: Multiple { variants, tag, tag_encoding, .. } =
1985
- & ty_layout. layout . variants ( )
1986
- {
1987
- if !variants. is_empty ( ) {
1988
- f. write_str (
1989
- "<p><strong>Size for each variant:</strong></p>\
1990
- <ul>",
1991
- ) ?;
1992
-
1993
- let Adt ( adt, _) = ty_layout. ty . kind ( ) else {
1994
- span_bug ! ( tcx. def_span( ty_def_id) , "not an adt" )
1995
- } ;
1996
-
1997
- let tag_size = if let TagEncoding :: Niche { .. } = tag_encoding {
1998
- 0
1999
- } else if let Primitive :: Int ( i, _) = tag. primitive ( ) {
2000
- i. size ( ) . bytes ( )
2001
- } else {
2002
- span_bug ! ( tcx. def_span( ty_def_id) , "tag is neither niche nor int" )
2003
- } ;
2004
-
2005
- for ( index, layout) in variants. iter_enumerated ( ) {
2006
- let name = adt. variant ( index) . name ;
2007
- write ! ( & mut f, "<li><code>{name}</code>: " ) ?;
2008
- write_size_of_layout ( & mut f, layout, tag_size) ;
2009
- writeln ! ( & mut f, "</li>" ) ?;
2010
- }
2011
- f. write_str ( "</ul>" ) ?;
2012
- }
2013
- }
2014
- }
2015
- // This kind of layout error can occur with valid code, e.g. if you try to
2016
- // get the layout of a generic type such as `Vec<T>`.
2017
- Err ( LayoutError :: Unknown ( _) ) => {
2018
- writeln ! (
2019
- f,
2020
- "<p><strong>Note:</strong> Unable to compute type layout, \
2021
- possibly due to this type having generic parameters. \
2022
- Layout can only be computed for concrete, fully-instantiated types.</p>"
2023
- ) ?;
2024
- }
2025
- // This kind of error probably can't happen with valid code, but we don't
2026
- // want to panic and prevent the docs from building, so we just let the
2027
- // user know that we couldn't compute the layout.
2028
- Err ( LayoutError :: SizeOverflow ( _) ) => {
2029
- writeln ! (
2030
- f,
2031
- "<p><strong>Note:</strong> Encountered an error during type layout; \
2032
- the type was too big.</p>"
2033
- ) ?;
2034
- }
2035
- Err ( LayoutError :: NormalizationFailure ( _, _) ) => {
2036
- writeln ! (
2037
- f,
2038
- "<p><strong>Note:</strong> Encountered an error during type layout; \
2039
- the type failed to be normalized.</p>"
2040
- ) ?;
2041
- }
2042
- }
2043
-
2044
- writeln ! ( f, "</div>" )
2045
- } )
2046
- }
2047
-
2048
1934
fn pluralize ( count : usize ) -> & ' static str {
2049
1935
if count > 1 { "s" } else { "" }
2050
1936
}
0 commit comments