-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Expand file tree
/
Copy pathqueries.rs
More file actions
139 lines (123 loc) · 4.66 KB
/
Copy pathqueries.rs
File metadata and controls
139 lines (123 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use std::any::Any;
use std::sync::Arc;
use rustc_codegen_ssa::CodegenResults;
use rustc_codegen_ssa::traits::CodegenBackend;
use rustc_data_structures::svh::Svh;
use rustc_errors::timings::TimingSection;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
use rustc_metadata::EncodedMetadata;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_session::config::{self, OutputFilenames, OutputType};
use crate::errors::FailedWritingFile;
use crate::passes;
pub struct Linker {
dep_graph: DepGraph,
output_filenames: Arc<OutputFilenames>,
// Only present when incr. comp. is enabled.
crate_hash: Option<Svh>,
metadata: EncodedMetadata,
ongoing_codegen: Box<dyn Any>,
}
impl Linker {
pub fn codegen_and_build_linker(
tcx: TyCtxt<'_>,
codegen_backend: &dyn CodegenBackend,
) -> Linker {
let (ongoing_codegen, metadata) = passes::start_codegen(codegen_backend, tcx);
Linker {
dep_graph: tcx.dep_graph.clone(),
output_filenames: Arc::clone(tcx.output_filenames(())),
crate_hash: if tcx.needs_crate_hash() {
Some(tcx.crate_hash(LOCAL_CRATE))
} else {
None
},
metadata,
ongoing_codegen,
}
}
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) {
let (codegen_results, mut work_products) = sess.time("finish_ongoing_codegen", || {
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.output_filenames)
});
sess.timings.end_section(sess.dcx(), TimingSection::Codegen);
if sess.opts.incremental.is_some()
&& let Some(path) = self.metadata.path()
&& let Some((id, product)) =
rustc_incremental::copy_cgu_workproduct_to_incr_comp_cache_dir(
sess,
"metadata",
&[("rmeta", path)],
&[],
)
{
work_products.insert(id, product);
}
sess.dcx().abort_if_errors();
let _timer = sess.timer("link");
sess.time("serialize_work_products", || {
rustc_incremental::save_work_product_index(sess, &self.dep_graph, work_products)
});
let prof = sess.prof.clone();
prof.generic_activity("drop_dep_graph").run(move || drop(self.dep_graph));
// Now that we won't touch anything in the incremental compilation directory
// any more, we can finalize it (which involves renaming it)
rustc_incremental::finalize_session_directory(sess, self.crate_hash);
if !sess
.opts
.output_types
.keys()
.any(|&i| i == OutputType::Exe || i == OutputType::Metadata)
{
return;
}
if sess.opts.unstable_opts.no_link {
let rlink_file = self.output_filenames.with_extension(config::RLINK_EXT);
CodegenResults::serialize_rlink(
sess,
&rlink_file,
&codegen_results,
&self.metadata,
&*self.output_filenames,
)
.unwrap_or_else(|error| {
sess.dcx().emit_fatal(FailedWritingFile { path: &rlink_file, error })
});
return;
}
let _timer = sess.prof.verbose_generic_activity("link_crate");
let _timing = sess.timings.section_guard(sess.dcx(), TimingSection::Linking);
codegen_backend.link(sess, codegen_results, self.metadata, &self.output_filenames)
}
}
/// Checks if the given `DefId` refers to an item that is unnamable, such as one defined in a const block.
///
/// # Example
///
/// ```
/// pub fn f() {
/// // In here, `X` is not reachable outside of `f`, making it unnamable.
/// pub struct X;
/// }
/// ```
pub(crate) fn is_unnamable(tcx: TyCtxt<'_>, did: DefId) -> bool {
let mut cur_did = did;
while let Some(parent) = tcx.opt_parent(cur_did) {
match tcx.def_kind(parent) {
// items defined in these can be linked to, as long as they are visible
DefKind::Mod | DefKind::ForeignMod => cur_did = parent,
// items in impls can be linked to,
// as long as we can link to the item the impl is on.
// since associated traits are not yet a thing,
// it should not be possible to refer to an impl item if
// the base type is not namable.
DefKind::Impl { .. } => return false,
// everything else does not have docs generated for it
_ => return true,
}
}
return false;
}