@@ -5,7 +5,8 @@ use rustc_codegen_ssa::CodegenResults;
55use rustc_codegen_ssa:: traits:: CodegenBackend ;
66use rustc_data_structures:: svh:: Svh ;
77use rustc_errors:: timings:: TimingSection ;
8- use rustc_hir:: def_id:: LOCAL_CRATE ;
8+ use rustc_hir:: def:: DefKind ;
9+ use rustc_hir:: def_id:: { DefId , LOCAL_CRATE } ;
910use rustc_metadata:: EncodedMetadata ;
1011use rustc_middle:: dep_graph:: DepGraph ;
1112use rustc_middle:: ty:: TyCtxt ;
@@ -107,3 +108,32 @@ impl Linker {
107108 codegen_backend. link ( sess, codegen_results, self . metadata , & self . output_filenames )
108109 }
109110}
111+
112+ /// Checks if the given `DefId` refers to an item that is unnamable, such as one defined in a const block.
113+ ///
114+ /// # Example
115+ ///
116+ /// ```
117+ /// pub fn f() {
118+ /// // In here, `X` is not reachable outside of `f`, making it unnamable.
119+ /// pub struct X;
120+ /// }
121+ /// ```
122+ pub ( crate ) fn is_unnamable ( tcx : TyCtxt < ' _ > , did : DefId ) -> bool {
123+ let mut cur_did = did;
124+ while let Some ( parent) = tcx. opt_parent ( cur_did) {
125+ match tcx. def_kind ( parent) {
126+ // items defined in these can be linked to, as long as they are visible
127+ DefKind :: Mod | DefKind :: ForeignMod => cur_did = parent,
128+ // items in impls can be linked to,
129+ // as long as we can link to the item the impl is on.
130+ // since associated traits are not yet a thing,
131+ // it should not be possible to refer to an impl item if
132+ // the base type is not namable.
133+ DefKind :: Impl { .. } => return false ,
134+ // everything else does not have docs generated for it
135+ _ => return true ,
136+ }
137+ }
138+ return false ;
139+ }
0 commit comments