@@ -2437,12 +2437,41 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24372437 }
24382438 }
24392439
2440- /// Returns the `DefPath` of an item. Note that if `id` is not
2441- /// local to this crate -- or is inlined into this crate -- the
2442- /// result will be a non-local `DefPath`.
2440+ /// Convert a `DefId` into its fully expanded `DefPath` (every
2441+ /// `DefId` is really just an interned def-path).
2442+ ///
2443+ /// Note that if `id` is not local to this crate -- or is
2444+ /// inlined into this crate -- the result will be a non-local
2445+ /// `DefPath`.
2446+ ///
2447+ /// This function is only safe to use when you are sure that the
2448+ /// full def-path is accessible. Examples that are known to be
2449+ /// safe are local def-ids or items; see `opt_def_path` for more
2450+ /// details.
24432451 pub fn def_path ( self , id : DefId ) -> ast_map:: DefPath {
2452+ self . opt_def_path ( id) . unwrap_or_else ( || {
2453+ bug ! ( "could not load def-path for {:?}" , id)
2454+ } )
2455+ }
2456+
2457+ /// Convert a `DefId` into its fully expanded `DefPath` (every
2458+ /// `DefId` is really just an interned def-path).
2459+ ///
2460+ /// When going across crates, we do not save the full info for
2461+ /// every cross-crate def-id, and hence we may not always be able
2462+ /// to create a def-path. Therefore, this returns
2463+ /// `Option<DefPath>` to cover that possibility. It will always
2464+ /// return `Some` for local def-ids, however, as well as for
2465+ /// items. The problems arise with "minor" def-ids like those
2466+ /// associated with a pattern, `impl Trait`, or other internal
2467+ /// detail to a fn.
2468+ ///
2469+ /// Note that if `id` is not local to this crate -- or is
2470+ /// inlined into this crate -- the result will be a non-local
2471+ /// `DefPath`.
2472+ pub fn opt_def_path ( self , id : DefId ) -> Option < ast_map:: DefPath > {
24442473 if id. is_local ( ) {
2445- self . map . def_path ( id)
2474+ Some ( self . map . def_path ( id) )
24462475 } else {
24472476 self . sess . cstore . relative_def_path ( id)
24482477 }
0 commit comments