]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | //! Validates all used crates and extern libraries and loads their metadata |
223e47cc | 2 | |
f2b60f7d | 3 | use crate::errors::{ |
487cf647 FG |
4 | AllocFuncRequired, ConflictingAllocErrorHandler, ConflictingGlobalAlloc, CrateNotPanicRuntime, |
5 | GlobalAllocRequired, MissingAllocErrorHandler, NoMultipleAllocErrorHandler, | |
6 | NoMultipleGlobalAlloc, NoPanicStrategy, NoTransitiveNeedsDep, NotProfilerRuntime, | |
7 | ProfilerBuiltinsNeedsCore, | |
f2b60f7d | 8 | }; |
3dfed10e | 9 | use crate::locator::{CrateError, CrateLocator, CratePaths}; |
dfeec247 | 10 | use crate::rmeta::{CrateDep, CrateMetadata, CrateNumMap, CrateRoot, MetadataBlob}; |
92a42be0 | 11 | |
3dfed10e XL |
12 | use rustc_ast::expand::allocator::AllocatorKind; |
13 | use rustc_ast::{self as ast, *}; | |
6a06907d | 14 | use rustc_data_structures::fx::{FxHashMap, FxHashSet}; |
b7449926 | 15 | use rustc_data_structures::svh::Svh; |
60c5eb7d | 16 | use rustc_data_structures::sync::Lrc; |
dfeec247 | 17 | use rustc_expand::base::SyntaxExtension; |
6a06907d | 18 | use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE}; |
ba9703b0 | 19 | use rustc_hir::definitions::Definitions; |
60c5eb7d | 20 | use rustc_index::vec::IndexVec; |
ba9703b0 | 21 | use rustc_middle::ty::TyCtxt; |
f035d41b | 22 | use rustc_session::config::{self, CrateType, ExternLocation}; |
c295e0f8 XL |
23 | use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate}; |
24 | use rustc_session::cstore::{ExternCrateSource, MetadataLoaderDyn}; | |
04454e1e | 25 | use rustc_session::lint; |
ba9703b0 XL |
26 | use rustc_session::output::validate_crate_name; |
27 | use rustc_session::search_paths::PathKind; | |
136023e0 | 28 | use rustc_session::Session; |
dfeec247 XL |
29 | use rustc_span::edition::Edition; |
30 | use rustc_span::symbol::{sym, Symbol}; | |
31 | use rustc_span::{Span, DUMMY_SP}; | |
83c7162d | 32 | use rustc_target::spec::{PanicStrategy, TargetTriple}; |
dfeec247 | 33 | |
e1599b0c | 34 | use proc_macro::bridge::client::ProcMacro; |
3c0e092e | 35 | use std::ops::Fn; |
dfeec247 | 36 | use std::path::Path; |
5869c6ff | 37 | use std::{cmp, env}; |
60c5eb7d XL |
38 | |
39 | #[derive(Clone)] | |
40 | pub struct CStore { | |
41 | metas: IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>, | |
42 | injected_panic_runtime: Option<CrateNum>, | |
43 | /// This crate needs an allocator and either provides it itself, or finds it in a dependency. | |
44 | /// If the above is true, then this field denotes the kind of the found allocator. | |
45 | allocator_kind: Option<AllocatorKind>, | |
487cf647 FG |
46 | /// This crate needs an allocation error handler and either provides it itself, or finds it in a dependency. |
47 | /// If the above is true, then this field denotes the kind of the found allocator. | |
48 | alloc_error_handler_kind: Option<AllocatorKind>, | |
60c5eb7d XL |
49 | /// This crate has a `#[global_allocator]` item. |
50 | has_global_allocator: bool, | |
487cf647 FG |
51 | /// This crate has a `#[alloc_error_handler]` item. |
52 | has_alloc_error_handler: bool, | |
6a06907d XL |
53 | |
54 | /// This map is used to verify we get no hash conflicts between | |
55 | /// `StableCrateId` values. | |
c295e0f8 | 56 | pub(crate) stable_crate_ids: FxHashMap<StableCrateId, CrateNum>, |
cdc7bbd5 XL |
57 | |
58 | /// Unused externs of the crate | |
59 | unused_externs: Vec<Symbol>, | |
e9174d1e SL |
60 | } |
61 | ||
136023e0 XL |
62 | impl std::fmt::Debug for CStore { |
63 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
64 | f.debug_struct("CStore").finish_non_exhaustive() | |
65 | } | |
66 | } | |
67 | ||
c30ab7b3 | 68 | pub struct CrateLoader<'a> { |
e74abb32 XL |
69 | // Immutable configuration. |
70 | sess: &'a Session, | |
17df50a5 | 71 | metadata_loader: Box<MetadataLoaderDyn>, |
476ff2be | 72 | local_crate_name: Symbol, |
e74abb32 XL |
73 | // Mutable output. |
74 | cstore: CStore, | |
f9f354fc | 75 | used_extern_options: FxHashSet<Symbol>, |
223e47cc LB |
76 | } |
77 | ||
60c5eb7d XL |
78 | pub enum LoadedMacro { |
79 | MacroDef(ast::Item, Edition), | |
80 | ProcMacro(SyntaxExtension), | |
81 | } | |
82 | ||
923072b8 | 83 | pub(crate) struct Library { |
60c5eb7d XL |
84 | pub source: CrateSource, |
85 | pub metadata: MetadataBlob, | |
1a4d82fc | 86 | } |
e9174d1e | 87 | |
a7813a04 | 88 | enum LoadResult { |
9e0c209e | 89 | Previous(CrateNum), |
c30ab7b3 | 90 | Loaded(Library), |
a7813a04 XL |
91 | } |
92 | ||
74b04a01 XL |
93 | /// A reference to `CrateMetadata` that can also give access to whole crate store when necessary. |
94 | #[derive(Clone, Copy)] | |
923072b8 | 95 | pub(crate) struct CrateMetadataRef<'a> { |
74b04a01 XL |
96 | pub cdata: &'a CrateMetadata, |
97 | pub cstore: &'a CStore, | |
98 | } | |
99 | ||
100 | impl std::ops::Deref for CrateMetadataRef<'_> { | |
101 | type Target = CrateMetadata; | |
102 | ||
103 | fn deref(&self) -> &Self::Target { | |
104 | self.cdata | |
105 | } | |
106 | } | |
107 | ||
3dfed10e XL |
108 | struct CrateDump<'a>(&'a CStore); |
109 | ||
110 | impl<'a> std::fmt::Debug for CrateDump<'a> { | |
111 | fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
112 | writeln!(fmt, "resolved crates:")?; | |
a2a8927a XL |
113 | for (cnum, data) in self.0.iter_crate_data() { |
114 | writeln!(fmt, " name: {}", data.name())?; | |
115 | writeln!(fmt, " cnum: {}", cnum)?; | |
116 | writeln!(fmt, " hash: {}", data.hash())?; | |
117 | writeln!(fmt, " reqd: {:?}", data.dep_kind())?; | |
118 | let CrateSource { dylib, rlib, rmeta } = data.source(); | |
119 | if let Some(dylib) = dylib { | |
120 | writeln!(fmt, " dylib: {}", dylib.0.display())?; | |
121 | } | |
122 | if let Some(rlib) = rlib { | |
123 | writeln!(fmt, " rlib: {}", rlib.0.display())?; | |
124 | } | |
125 | if let Some(rmeta) = rmeta { | |
126 | writeln!(fmt, " rmeta: {}", rmeta.0.display())?; | |
127 | } | |
128 | } | |
129 | Ok(()) | |
3dfed10e | 130 | } |
60c5eb7d XL |
131 | } |
132 | ||
133 | impl CStore { | |
136023e0 XL |
134 | pub fn from_tcx(tcx: TyCtxt<'_>) -> &CStore { |
135 | tcx.cstore_untracked() | |
136 | .as_any() | |
137 | .downcast_ref::<CStore>() | |
138 | .expect("`tcx.cstore` is not a `CStore`") | |
60c5eb7d XL |
139 | } |
140 | ||
141 | fn alloc_new_crate_num(&mut self) -> CrateNum { | |
142 | self.metas.push(None); | |
143 | CrateNum::new(self.metas.len() - 1) | |
144 | } | |
145 | ||
923072b8 FG |
146 | pub fn has_crate_data(&self, cnum: CrateNum) -> bool { |
147 | self.metas[cnum].is_some() | |
148 | } | |
149 | ||
150 | pub(crate) fn get_crate_data(&self, cnum: CrateNum) -> CrateMetadataRef<'_> { | |
74b04a01 | 151 | let cdata = self.metas[cnum] |
dfeec247 | 152 | .as_ref() |
74b04a01 XL |
153 | .unwrap_or_else(|| panic!("Failed to get crate data for {:?}", cnum)); |
154 | CrateMetadataRef { cdata, cstore: self } | |
60c5eb7d XL |
155 | } |
156 | ||
157 | fn set_crate_data(&mut self, cnum: CrateNum, data: CrateMetadata) { | |
158 | assert!(self.metas[cnum].is_none(), "Overwriting crate metadata entry"); | |
159 | self.metas[cnum] = Some(Lrc::new(data)); | |
160 | } | |
161 | ||
923072b8 | 162 | pub(crate) fn iter_crate_data(&self) -> impl Iterator<Item = (CrateNum, &CrateMetadata)> { |
a2a8927a XL |
163 | self.metas |
164 | .iter_enumerated() | |
487cf647 | 165 | .filter_map(|(cnum, data)| data.as_deref().map(|data| (cnum, data))) |
60c5eb7d XL |
166 | } |
167 | ||
168 | fn push_dependencies_in_postorder(&self, deps: &mut Vec<CrateNum>, cnum: CrateNum) { | |
169 | if !deps.contains(&cnum) { | |
170 | let data = self.get_crate_data(cnum); | |
171 | for &dep in data.dependencies().iter() { | |
172 | if dep != cnum { | |
173 | self.push_dependencies_in_postorder(deps, dep); | |
174 | } | |
175 | } | |
176 | ||
177 | deps.push(cnum); | |
178 | } | |
179 | } | |
180 | ||
923072b8 | 181 | pub(crate) fn crate_dependencies_in_postorder(&self, cnum: CrateNum) -> Vec<CrateNum> { |
60c5eb7d XL |
182 | let mut deps = Vec::new(); |
183 | if cnum == LOCAL_CRATE { | |
a2a8927a XL |
184 | for (cnum, _) in self.iter_crate_data() { |
185 | self.push_dependencies_in_postorder(&mut deps, cnum); | |
186 | } | |
60c5eb7d XL |
187 | } else { |
188 | self.push_dependencies_in_postorder(&mut deps, cnum); | |
189 | } | |
190 | deps | |
191 | } | |
192 | ||
193 | fn crate_dependencies_in_reverse_postorder(&self, cnum: CrateNum) -> Vec<CrateNum> { | |
194 | let mut deps = self.crate_dependencies_in_postorder(cnum); | |
195 | deps.reverse(); | |
196 | deps | |
197 | } | |
198 | ||
923072b8 | 199 | pub(crate) fn injected_panic_runtime(&self) -> Option<CrateNum> { |
60c5eb7d XL |
200 | self.injected_panic_runtime |
201 | } | |
202 | ||
923072b8 | 203 | pub(crate) fn allocator_kind(&self) -> Option<AllocatorKind> { |
60c5eb7d XL |
204 | self.allocator_kind |
205 | } | |
206 | ||
487cf647 FG |
207 | pub(crate) fn alloc_error_handler_kind(&self) -> Option<AllocatorKind> { |
208 | self.alloc_error_handler_kind | |
209 | } | |
210 | ||
923072b8 | 211 | pub(crate) fn has_global_allocator(&self) -> bool { |
60c5eb7d XL |
212 | self.has_global_allocator |
213 | } | |
cdc7bbd5 | 214 | |
487cf647 FG |
215 | pub(crate) fn has_alloc_error_handler(&self) -> bool { |
216 | self.has_alloc_error_handler | |
217 | } | |
218 | ||
cdc7bbd5 | 219 | pub fn report_unused_deps(&self, tcx: TyCtxt<'_>) { |
04454e1e FG |
220 | let json_unused_externs = tcx.sess.opts.json_unused_externs; |
221 | ||
cdc7bbd5 XL |
222 | // We put the check for the option before the lint_level_at_node call |
223 | // because the call mutates internal state and introducing it | |
224 | // leads to some ui tests failing. | |
04454e1e | 225 | if !json_unused_externs.is_enabled() { |
cdc7bbd5 XL |
226 | return; |
227 | } | |
228 | let level = tcx | |
229 | .lint_level_at_node(lint::builtin::UNUSED_CRATE_DEPENDENCIES, rustc_hir::CRATE_HIR_ID) | |
230 | .0; | |
231 | if level != lint::Level::Allow { | |
232 | let unused_externs = | |
233 | self.unused_externs.iter().map(|ident| ident.to_ident_string()).collect::<Vec<_>>(); | |
234 | let unused_externs = unused_externs.iter().map(String::as_str).collect::<Vec<&str>>(); | |
04454e1e FG |
235 | tcx.sess.parse_sess.span_diagnostic.emit_unused_externs( |
236 | level, | |
237 | json_unused_externs.is_loud(), | |
238 | &unused_externs, | |
239 | ); | |
cdc7bbd5 XL |
240 | } |
241 | } | |
60c5eb7d XL |
242 | } |
243 | ||
c30ab7b3 | 244 | impl<'a> CrateLoader<'a> { |
e74abb32 XL |
245 | pub fn new( |
246 | sess: &'a Session, | |
17df50a5 | 247 | metadata_loader: Box<MetadataLoaderDyn>, |
487cf647 | 248 | local_crate_name: Symbol, |
e74abb32 | 249 | ) -> Self { |
6a06907d | 250 | let mut stable_crate_ids = FxHashMap::default(); |
136023e0 | 251 | stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE); |
6a06907d | 252 | |
c30ab7b3 | 253 | CrateLoader { |
3b2f2976 | 254 | sess, |
e74abb32 | 255 | metadata_loader, |
487cf647 | 256 | local_crate_name, |
60c5eb7d XL |
257 | cstore: CStore { |
258 | // We add an empty entry for LOCAL_CRATE (which maps to zero) in | |
259 | // order to make array indices in `metas` match with the | |
260 | // corresponding `CrateNum`. This first entry will always remain | |
261 | // `None`. | |
262 | metas: IndexVec::from_elem_n(None, 1), | |
263 | injected_panic_runtime: None, | |
264 | allocator_kind: None, | |
487cf647 | 265 | alloc_error_handler_kind: None, |
60c5eb7d | 266 | has_global_allocator: false, |
487cf647 | 267 | has_alloc_error_handler: false, |
6a06907d | 268 | stable_crate_ids, |
cdc7bbd5 | 269 | unused_externs: Vec::new(), |
dfeec247 | 270 | }, |
f9f354fc | 271 | used_extern_options: Default::default(), |
223e47cc | 272 | } |
223e47cc | 273 | } |
223e47cc | 274 | |
e74abb32 XL |
275 | pub fn cstore(&self) -> &CStore { |
276 | &self.cstore | |
277 | } | |
278 | ||
279 | pub fn into_cstore(self) -> CStore { | |
280 | self.cstore | |
281 | } | |
282 | ||
60c5eb7d | 283 | fn existing_match(&self, name: Symbol, hash: Option<Svh>, kind: PathKind) -> Option<CrateNum> { |
a2a8927a | 284 | for (cnum, data) in self.cstore.iter_crate_data() { |
dfeec247 | 285 | if data.name() != name { |
f2b60f7d | 286 | trace!("{} did not match {}", data.name(), name); |
a2a8927a | 287 | continue; |
dfeec247 | 288 | } |
223e47cc | 289 | |
1a4d82fc | 290 | match hash { |
a2a8927a | 291 | Some(hash) if hash == data.hash() => return Some(cnum), |
1b1a35ee XL |
292 | Some(hash) => { |
293 | debug!("actual hash {} did not match expected {}", hash, data.hash()); | |
a2a8927a | 294 | continue; |
1b1a35ee | 295 | } |
1a4d82fc JJ |
296 | None => {} |
297 | } | |
223e47cc | 298 | |
85aaf69f SL |
299 | // When the hash is None we're dealing with a top-level dependency |
300 | // in which case we may have a specification on the command line for | |
301 | // this library. Even though an upstream library may have loaded | |
302 | // something of the same name, we have to make sure it was loaded | |
303 | // from the exact same location as well. | |
1a4d82fc JJ |
304 | // |
305 | // We're also sure to compare *paths*, not actual byte slices. The | |
306 | // `source` stores paths which are normalized which may be different | |
307 | // from the strings on the command line. | |
74b04a01 | 308 | let source = self.cstore.get_crate_data(cnum).cdata.source(); |
a2a8927a | 309 | if let Some(entry) = self.sess.opts.externs.get(name.as_str()) { |
b7449926 | 310 | // Only use `--extern crate_name=path` here, not `--extern crate_name`. |
60c5eb7d XL |
311 | if let Some(mut files) = entry.files() { |
312 | if files.any(|l| { | |
5869c6ff XL |
313 | let l = l.canonicalized(); |
314 | source.dylib.as_ref().map(|(p, _)| p) == Some(l) | |
315 | || source.rlib.as_ref().map(|(p, _)| p) == Some(l) | |
316 | || source.rmeta.as_ref().map(|(p, _)| p) == Some(l) | |
60c5eb7d | 317 | }) { |
a2a8927a | 318 | return Some(cnum); |
60c5eb7d | 319 | } |
1a4d82fc | 320 | } |
a2a8927a | 321 | continue; |
85aaf69f SL |
322 | } |
323 | ||
324 | // Alright, so we've gotten this far which means that `data` has the | |
325 | // right name, we don't have a hash, and we don't have a --extern | |
326 | // pointing for ourselves. We're still not quite yet done because we | |
327 | // have to make sure that this crate was found in the crate lookup | |
328 | // path (this is a top-level dependency) as we don't want to | |
329 | // implicitly load anything inside the dependency lookup path. | |
dfeec247 XL |
330 | let prev_kind = source |
331 | .dylib | |
332 | .as_ref() | |
333 | .or(source.rlib.as_ref()) | |
334 | .or(source.rmeta.as_ref()) | |
335 | .expect("No sources for crate") | |
336 | .1; | |
60c5eb7d | 337 | if kind.matches(prev_kind) { |
a2a8927a | 338 | return Some(cnum); |
1b1a35ee XL |
339 | } else { |
340 | debug!( | |
341 | "failed to load existing crate {}; kind {:?} did not match prev_kind {:?}", | |
342 | name, kind, prev_kind | |
343 | ); | |
1a4d82fc | 344 | } |
a2a8927a XL |
345 | } |
346 | ||
347 | None | |
1a4d82fc | 348 | } |
223e47cc | 349 | |
923072b8 | 350 | fn verify_no_symbol_conflicts(&self, root: &CrateRoot) -> Result<(), CrateError> { |
54a0048b | 351 | // Check for (potential) conflicts with the local crate |
136023e0 | 352 | if self.sess.local_stable_crate_id() == root.stable_crate_id() { |
3dfed10e | 353 | return Err(CrateError::SymbolConflictsCurrent(root.name())); |
54a0048b SL |
354 | } |
355 | ||
54a0048b | 356 | // Check for conflicts with any crate loaded so far |
a2a8927a XL |
357 | for (_, other) in self.cstore.iter_crate_data() { |
358 | // Same stable crate id but different SVH | |
359 | if other.stable_crate_id() == root.stable_crate_id() && other.hash() != root.hash() { | |
360 | return Err(CrateError::SymbolConflictsOthers(root.name())); | |
54a0048b | 361 | } |
a2a8927a | 362 | } |
3dfed10e | 363 | |
a2a8927a | 364 | Ok(()) |
54a0048b SL |
365 | } |
366 | ||
6a06907d XL |
367 | fn verify_no_stable_crate_id_hash_conflicts( |
368 | &mut self, | |
923072b8 | 369 | root: &CrateRoot, |
6a06907d XL |
370 | cnum: CrateNum, |
371 | ) -> Result<(), CrateError> { | |
372 | if let Some(existing) = self.cstore.stable_crate_ids.insert(root.stable_crate_id(), cnum) { | |
373 | let crate_name0 = root.name(); | |
374 | let crate_name1 = self.cstore.get_crate_data(existing).name(); | |
375 | return Err(CrateError::StableCrateIdCollision(crate_name0, crate_name1)); | |
376 | } | |
377 | ||
378 | Ok(()) | |
379 | } | |
380 | ||
532ac7d7 XL |
381 | fn register_crate( |
382 | &mut self, | |
383 | host_lib: Option<Library>, | |
e74abb32 | 384 | root: Option<&CratePaths>, |
532ac7d7 | 385 | lib: Library, |
3dfed10e | 386 | dep_kind: CrateDepKind, |
dfeec247 | 387 | name: Symbol, |
3dfed10e | 388 | ) -> Result<CrateNum, CrateError> { |
e74abb32 XL |
389 | let _prof_timer = self.sess.prof.generic_activity("metadata_register_crate"); |
390 | ||
391 | let Library { source, metadata } = lib; | |
392 | let crate_root = metadata.get_root(); | |
60c5eb7d | 393 | let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash()); |
b039eaaf | 394 | |
dfeec247 | 395 | let private_dep = |
a2a8927a | 396 | self.sess.opts.externs.get(name.as_str()).map_or(false, |e| e.is_private_dep); |
48663c56 | 397 | |
223e47cc | 398 | // Claim this crate number and cache it |
94b46f34 | 399 | let cnum = self.cstore.alloc_new_crate_num(); |
1a4d82fc | 400 | |
3dfed10e XL |
401 | info!( |
402 | "register crate `{}` (cnum = {}. private_dep = {})", | |
403 | crate_root.name(), | |
404 | cnum, | |
405 | private_dep | |
406 | ); | |
407 | ||
e74abb32 | 408 | // Maintain a reference to the top most crate. |
1a4d82fc | 409 | // Stash paths for top-most crate locally if necessary. |
e74abb32 XL |
410 | let crate_paths; |
411 | let root = if let Some(root) = root { | |
412 | root | |
1a4d82fc | 413 | } else { |
60c5eb7d | 414 | crate_paths = CratePaths::new(crate_root.name(), source.clone()); |
e74abb32 | 415 | &crate_paths |
1a4d82fc | 416 | }; |
1a4d82fc | 417 | |
3dfed10e | 418 | let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, dep_kind)?; |
1a4d82fc | 419 | |
60c5eb7d | 420 | let raw_proc_macros = if crate_root.is_proc_macro_crate() { |
e74abb32 XL |
421 | let temp_root; |
422 | let (dlsym_source, dlsym_root) = match &host_lib { | |
dfeec247 XL |
423 | Some(host_lib) => (&host_lib.source, { |
424 | temp_root = host_lib.metadata.get_root(); | |
425 | &temp_root | |
426 | }), | |
e74abb32 XL |
427 | None => (&source, &crate_root), |
428 | }; | |
429 | let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate"); | |
136023e0 | 430 | Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.stable_crate_id())?) |
60c5eb7d XL |
431 | } else { |
432 | None | |
433 | }; | |
e1599b0c | 434 | |
cdc7bbd5 XL |
435 | // Perform some verification *after* resolve_crate_deps() above is |
436 | // known to have been successful. It seems that - in error cases - the | |
437 | // cstore can be in a temporarily invalid state between cnum allocation | |
438 | // and dependency resolution and the verification code would produce | |
439 | // ICEs in that case (see #83045). | |
440 | self.verify_no_symbol_conflicts(&crate_root)?; | |
441 | self.verify_no_stable_crate_id_hash_conflicts(&crate_root, cnum)?; | |
442 | ||
3dfed10e XL |
443 | let crate_metadata = CrateMetadata::new( |
444 | self.sess, | |
04454e1e | 445 | &self.cstore, |
3dfed10e XL |
446 | metadata, |
447 | crate_root, | |
448 | raw_proc_macros, | |
3b2f2976 | 449 | cnum, |
3dfed10e XL |
450 | cnum_map, |
451 | dep_kind, | |
452 | source, | |
453 | private_dep, | |
454 | host_hash, | |
dfeec247 | 455 | ); |
1a4d82fc | 456 | |
3dfed10e XL |
457 | self.cstore.set_crate_data(cnum, crate_metadata); |
458 | ||
459 | Ok(cnum) | |
1a4d82fc JJ |
460 | } |
461 | ||
dc9dc135 | 462 | fn load_proc_macro<'b>( |
e74abb32 | 463 | &self, |
60c5eb7d | 464 | locator: &mut CrateLocator<'b>, |
532ac7d7 | 465 | path_kind: PathKind, |
c295e0f8 | 466 | host_hash: Option<Svh>, |
3dfed10e | 467 | ) -> Result<Option<(LoadResult, Option<Library>)>, CrateError> |
532ac7d7 | 468 | where |
dc9dc135 | 469 | 'a: 'b, |
532ac7d7 | 470 | { |
60c5eb7d | 471 | // Use a new crate locator so trying to load a proc macro doesn't affect the error |
532ac7d7 | 472 | // message we emit |
60c5eb7d | 473 | let mut proc_macro_locator = locator.clone(); |
532ac7d7 XL |
474 | |
475 | // Try to load a proc macro | |
c295e0f8 | 476 | proc_macro_locator.is_proc_macro = true; |
532ac7d7 XL |
477 | |
478 | // Load the proc macro crate for the target | |
064997fb | 479 | let (locator, target_result) = if self.sess.opts.unstable_opts.dual_proc_macros { |
532ac7d7 XL |
480 | proc_macro_locator.reset(); |
481 | let result = match self.load(&mut proc_macro_locator)? { | |
3dfed10e XL |
482 | Some(LoadResult::Previous(cnum)) => { |
483 | return Ok(Some((LoadResult::Previous(cnum), None))); | |
484 | } | |
485 | Some(LoadResult::Loaded(library)) => Some(LoadResult::Loaded(library)), | |
486 | None => return Ok(None), | |
532ac7d7 | 487 | }; |
c295e0f8 | 488 | locator.hash = host_hash; |
60c5eb7d | 489 | // Use the locator when looking for the host proc macro crate, as that is required |
532ac7d7 | 490 | // so we want it to affect the error message |
60c5eb7d | 491 | (locator, result) |
532ac7d7 XL |
492 | } else { |
493 | (&mut proc_macro_locator, None) | |
494 | }; | |
495 | ||
496 | // Load the proc macro crate for the host | |
497 | ||
498 | locator.reset(); | |
c295e0f8 | 499 | locator.is_proc_macro = true; |
532ac7d7 XL |
500 | locator.target = &self.sess.host; |
501 | locator.triple = TargetTriple::from_triple(config::host_triple()); | |
502 | locator.filesearch = self.sess.host_filesearch(path_kind); | |
503 | ||
5e7ed085 FG |
504 | let Some(host_result) = self.load(locator)? else { |
505 | return Ok(None); | |
3dfed10e | 506 | }; |
532ac7d7 | 507 | |
064997fb | 508 | Ok(Some(if self.sess.opts.unstable_opts.dual_proc_macros { |
532ac7d7 XL |
509 | let host_result = match host_result { |
510 | LoadResult::Previous(..) => { | |
511 | panic!("host and target proc macros must be loaded in lock-step") | |
512 | } | |
dfeec247 | 513 | LoadResult::Loaded(library) => library, |
532ac7d7 XL |
514 | }; |
515 | (target_result.unwrap(), Some(host_result)) | |
516 | } else { | |
517 | (host_result, None) | |
3dfed10e | 518 | })) |
532ac7d7 XL |
519 | } |
520 | ||
b7449926 XL |
521 | fn resolve_crate<'b>( |
522 | &'b mut self, | |
b7449926 | 523 | name: Symbol, |
b7449926 | 524 | span: Span, |
3dfed10e | 525 | dep_kind: CrateDepKind, |
a2a8927a | 526 | ) -> Option<CrateNum> { |
c295e0f8 | 527 | self.used_extern_options.insert(name); |
a2a8927a XL |
528 | match self.maybe_resolve_crate(name, dep_kind, None) { |
529 | Ok(cnum) => Some(cnum), | |
530 | Err(err) => { | |
531 | let missing_core = | |
532 | self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err(); | |
533 | err.report(&self.sess, span, missing_core); | |
534 | None | |
535 | } | |
536 | } | |
e74abb32 XL |
537 | } |
538 | ||
539 | fn maybe_resolve_crate<'b>( | |
540 | &'b mut self, | |
541 | name: Symbol, | |
3dfed10e | 542 | mut dep_kind: CrateDepKind, |
e74abb32 | 543 | dep: Option<(&'b CratePaths, &'b CrateDep)>, |
3dfed10e | 544 | ) -> Result<CrateNum, CrateError> { |
e74abb32 | 545 | info!("resolving crate `{}`", name); |
3dfed10e XL |
546 | if !name.as_str().is_ascii() { |
547 | return Err(CrateError::NonAsciiName(name)); | |
548 | } | |
e74abb32 XL |
549 | let (root, hash, host_hash, extra_filename, path_kind) = match dep { |
550 | Some((root, dep)) => ( | |
551 | Some(root), | |
60c5eb7d XL |
552 | Some(dep.hash), |
553 | dep.host_hash, | |
e74abb32 | 554 | Some(&dep.extra_filename[..]), |
60c5eb7d | 555 | PathKind::Dependency, |
e74abb32 XL |
556 | ), |
557 | None => (None, None, None, None, PathKind::Crate), | |
558 | }; | |
476ff2be | 559 | let result = if let Some(cnum) = self.existing_match(name, hash, path_kind) { |
532ac7d7 | 560 | (LoadResult::Previous(cnum), None) |
476ff2be SL |
561 | } else { |
562 | info!("falling back to a load"); | |
60c5eb7d XL |
563 | let mut locator = CrateLocator::new( |
564 | self.sess, | |
17df50a5 | 565 | &*self.metadata_loader, |
60c5eb7d | 566 | name, |
48663c56 XL |
567 | hash, |
568 | extra_filename, | |
60c5eb7d XL |
569 | false, // is_host |
570 | path_kind, | |
60c5eb7d | 571 | ); |
476ff2be | 572 | |
3dfed10e XL |
573 | match self.load(&mut locator)? { |
574 | Some(res) => (res, None), | |
575 | None => { | |
576 | dep_kind = CrateDepKind::MacrosOnly; | |
c295e0f8 | 577 | match self.load_proc_macro(&mut locator, path_kind, host_hash)? { |
3dfed10e | 578 | Some(res) => res, |
c295e0f8 | 579 | None => return Err(locator.into_error(root.cloned())), |
3dfed10e XL |
580 | } |
581 | } | |
582 | } | |
92a42be0 SL |
583 | }; |
584 | ||
585 | match result { | |
532ac7d7 | 586 | (LoadResult::Previous(cnum), None) => { |
92a42be0 | 587 | let data = self.cstore.get_crate_data(cnum); |
60c5eb7d | 588 | if data.is_proc_macro_crate() { |
3dfed10e | 589 | dep_kind = CrateDepKind::MacrosOnly; |
e9174d1e | 590 | } |
60c5eb7d | 591 | data.update_dep_kind(|data_dep_kind| cmp::max(data_dep_kind, dep_kind)); |
e74abb32 | 592 | Ok(cnum) |
92a42be0 | 593 | } |
532ac7d7 | 594 | (LoadResult::Loaded(library), host_library) => { |
3dfed10e | 595 | self.register_crate(host_library, root, library, dep_kind, name) |
1a4d82fc | 596 | } |
dfeec247 | 597 | _ => panic!(), |
1a4d82fc JJ |
598 | } |
599 | } | |
600 | ||
3dfed10e | 601 | fn load(&self, locator: &mut CrateLocator<'_>) -> Result<Option<LoadResult>, CrateError> { |
5e7ed085 FG |
602 | let Some(library) = locator.maybe_load_library_crate()? else { |
603 | return Ok(None); | |
3dfed10e | 604 | }; |
a7813a04 XL |
605 | |
606 | // In the case that we're loading a crate, but not matching | |
607 | // against a hash, we could load a crate which has the same hash | |
608 | // as an already loaded crate. If this is the case prevent | |
609 | // duplicates by just using the first crate. | |
610 | // | |
611 | // Note that we only do this for target triple crates, though, as we | |
612 | // don't want to match a host crate against an equivalent target one | |
613 | // already loaded. | |
9e0c209e | 614 | let root = library.metadata.get_root(); |
136023e0 XL |
615 | // FIXME: why is this condition necessary? It was adding in #33625 but I |
616 | // don't know why and the original author doesn't remember ... | |
617 | let can_reuse_cratenum = | |
c295e0f8 | 618 | locator.triple == self.sess.opts.target_triple || locator.is_proc_macro; |
136023e0 | 619 | Ok(Some(if can_reuse_cratenum { |
a7813a04 | 620 | let mut result = LoadResult::Loaded(library); |
a2a8927a | 621 | for (cnum, data) in self.cstore.iter_crate_data() { |
60c5eb7d XL |
622 | if data.name() == root.name() && root.hash() == data.hash() { |
623 | assert!(locator.hash.is_none()); | |
9e0c209e | 624 | info!("load success, going to previous cnum: {}", cnum); |
a7813a04 | 625 | result = LoadResult::Previous(cnum); |
a2a8927a | 626 | break; |
a7813a04 | 627 | } |
a2a8927a | 628 | } |
3dfed10e | 629 | result |
a7813a04 | 630 | } else { |
3dfed10e XL |
631 | LoadResult::Loaded(library) |
632 | })) | |
a7813a04 XL |
633 | } |
634 | ||
60c5eb7d | 635 | fn update_extern_crate(&self, cnum: CrateNum, extern_crate: ExternCrate) { |
54a0048b | 636 | let cmeta = self.cstore.get_crate_data(cnum); |
60c5eb7d XL |
637 | if cmeta.update_extern_crate(extern_crate) { |
638 | // Propagate the extern crate info to dependencies if it was updated. | |
639 | let extern_crate = ExternCrate { dependency_of: cnum, ..extern_crate }; | |
640 | for &dep_cnum in cmeta.dependencies().iter() { | |
641 | self.update_extern_crate(dep_cnum, extern_crate); | |
642 | } | |
54a0048b SL |
643 | } |
644 | } | |
645 | ||
1a4d82fc | 646 | // Go through the crate metadata and load any crates that it references |
dfeec247 XL |
647 | fn resolve_crate_deps( |
648 | &mut self, | |
649 | root: &CratePaths, | |
923072b8 | 650 | crate_root: &CrateRoot, |
dfeec247 XL |
651 | metadata: &MetadataBlob, |
652 | krate: CrateNum, | |
3dfed10e XL |
653 | dep_kind: CrateDepKind, |
654 | ) -> Result<CrateNumMap, CrateError> { | |
1a4d82fc | 655 | debug!("resolving deps of external crate"); |
60c5eb7d | 656 | if crate_root.is_proc_macro_crate() { |
3dfed10e | 657 | return Ok(CrateNumMap::new()); |
476ff2be SL |
658 | } |
659 | ||
660 | // The map from crate numbers in the crate we're resolving to local crate numbers. | |
661 | // We map 0 and all other holes in the map to our parent crate. The "additional" | |
3157f602 | 662 | // self-dependencies should be harmless. |
3dfed10e XL |
663 | let deps = crate_root.decode_crate_deps(metadata); |
664 | let mut crate_num_map = CrateNumMap::with_capacity(1 + deps.len()); | |
665 | crate_num_map.push(krate); | |
666 | for dep in deps { | |
667 | info!( | |
668 | "resolving dep crate {} hash: `{}` extra filename: `{}`", | |
669 | dep.name, dep.hash, dep.extra_filename | |
670 | ); | |
671 | let dep_kind = match dep_kind { | |
672 | CrateDepKind::MacrosOnly => CrateDepKind::MacrosOnly, | |
673 | _ => dep.kind, | |
674 | }; | |
675 | let cnum = self.maybe_resolve_crate(dep.name, dep_kind, Some((root, &dep)))?; | |
676 | crate_num_map.push(cnum); | |
677 | } | |
678 | ||
679 | debug!("resolve_crate_deps: cnum_map for {:?} is {:?}", krate, crate_num_map); | |
680 | Ok(crate_num_map) | |
1a4d82fc JJ |
681 | } |
682 | ||
dfeec247 XL |
683 | fn dlsym_proc_macros( |
684 | &self, | |
685 | path: &Path, | |
136023e0 | 686 | stable_crate_id: StableCrateId, |
3dfed10e | 687 | ) -> Result<&'static [ProcMacro], CrateError> { |
c30ab7b3 SL |
688 | // Make sure the path contains a / or the linker will search for it. |
689 | let path = env::current_dir().unwrap().join(path); | |
a2a8927a XL |
690 | let lib = unsafe { libloading::Library::new(path) } |
691 | .map_err(|err| CrateError::DlOpen(err.to_string()))?; | |
c30ab7b3 | 692 | |
a2a8927a XL |
693 | let sym_name = self.sess.generate_proc_macro_decls_symbol(stable_crate_id); |
694 | let sym = unsafe { lib.get::<*const &[ProcMacro]>(sym_name.as_bytes()) } | |
695 | .map_err(|err| CrateError::DlSym(err.to_string()))?; | |
c30ab7b3 | 696 | |
c30ab7b3 SL |
697 | // Intentionally leak the dynamic library. We can't ever unload it |
698 | // since the library can make things that will live arbitrarily long. | |
a2a8927a | 699 | let sym = unsafe { sym.into_raw() }; |
e1599b0c | 700 | std::mem::forget(lib); |
a1dfa0c6 | 701 | |
a2a8927a | 702 | Ok(unsafe { **sym }) |
1a4d82fc JJ |
703 | } |
704 | ||
a7813a04 XL |
705 | fn inject_panic_runtime(&mut self, krate: &ast::Crate) { |
706 | // If we're only compiling an rlib, then there's no need to select a | |
707 | // panic runtime, so we just skip this section entirely. | |
f9f354fc | 708 | let any_non_rlib = self.sess.crate_types().iter().any(|ct| *ct != CrateType::Rlib); |
a7813a04 XL |
709 | if !any_non_rlib { |
710 | info!("panic runtime injection skipped, only generating rlib"); | |
dfeec247 | 711 | return; |
a7813a04 XL |
712 | } |
713 | ||
714 | // If we need a panic runtime, we try to find an existing one here. At | |
715 | // the same time we perform some general validation of the DAG we've got | |
716 | // going such as ensuring everything has a compatible panic strategy. | |
717 | // | |
718 | // The logic for finding the panic runtime here is pretty much the same | |
719 | // as the allocator case with the only addition that the panic strategy | |
720 | // compilation mode also comes into play. | |
c30ab7b3 | 721 | let desired_strategy = self.sess.panic_strategy(); |
a7813a04 | 722 | let mut runtime_found = false; |
3dfed10e XL |
723 | let mut needs_panic_runtime = |
724 | self.sess.contains_name(&krate.attrs, sym::needs_panic_runtime); | |
7cac9316 | 725 | |
a2a8927a | 726 | for (cnum, data) in self.cstore.iter_crate_data() { |
60c5eb7d XL |
727 | needs_panic_runtime = needs_panic_runtime || data.needs_panic_runtime(); |
728 | if data.is_panic_runtime() { | |
a7813a04 XL |
729 | // Inject a dependency from all #![needs_panic_runtime] to this |
730 | // #![panic_runtime] crate. | |
dfeec247 XL |
731 | self.inject_dependency_if(cnum, "a panic runtime", &|data| { |
732 | data.needs_panic_runtime() | |
733 | }); | |
3dfed10e | 734 | runtime_found = runtime_found || data.dep_kind() == CrateDepKind::Explicit; |
a7813a04 | 735 | } |
a2a8927a | 736 | } |
a7813a04 XL |
737 | |
738 | // If an explicitly linked and matching panic runtime was found, or if | |
739 | // we just don't need one at all, then we're done here and there's | |
740 | // nothing else to do. | |
741 | if !needs_panic_runtime || runtime_found { | |
dfeec247 | 742 | return; |
a7813a04 XL |
743 | } |
744 | ||
745 | // By this point we know that we (a) need a panic runtime and (b) no | |
746 | // panic runtime was explicitly linked. Here we just load an appropriate | |
747 | // default runtime for our panic strategy and then inject the | |
748 | // dependencies. | |
749 | // | |
750 | // We may resolve to an already loaded crate (as the crate may not have | |
751 | // been explicitly linked prior to this) and we may re-inject | |
752 | // dependencies again, but both of those situations are fine. | |
753 | // | |
754 | // Also note that we have yet to perform validation of the crate graph | |
755 | // in terms of everyone has a compatible panic runtime format, that's | |
756 | // performed later as part of the `dependency_format` module. | |
757 | let name = match desired_strategy { | |
3dfed10e XL |
758 | PanicStrategy::Unwind => sym::panic_unwind, |
759 | PanicStrategy::Abort => sym::panic_abort, | |
a7813a04 XL |
760 | }; |
761 | info!("panic runtime not found -- loading {}", name); | |
762 | ||
a2a8927a | 763 | let Some(cnum) = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit) else { return; }; |
e74abb32 | 764 | let data = self.cstore.get_crate_data(cnum); |
a7813a04 XL |
765 | |
766 | // Sanity check the loaded crate to ensure it is indeed a panic runtime | |
767 | // and the panic strategy is indeed what we thought it was. | |
60c5eb7d | 768 | if !data.is_panic_runtime() { |
f2b60f7d | 769 | self.sess.emit_err(CrateNotPanicRuntime { crate_name: name }); |
a7813a04 | 770 | } |
064997fb | 771 | if data.required_panic_strategy() != Some(desired_strategy) { |
f2b60f7d | 772 | self.sess.emit_err(NoPanicStrategy { crate_name: name, strategy: desired_strategy }); |
a7813a04 XL |
773 | } |
774 | ||
60c5eb7d | 775 | self.cstore.injected_panic_runtime = Some(cnum); |
dfeec247 | 776 | self.inject_dependency_if(cnum, "a panic runtime", &|data| data.needs_panic_runtime()); |
8bb4bdeb XL |
777 | } |
778 | ||
fc512014 | 779 | fn inject_profiler_runtime(&mut self, krate: &ast::Crate) { |
064997fb | 780 | if self.sess.opts.unstable_opts.no_profiler_runtime |
94222f64 | 781 | || !(self.sess.instrument_coverage() |
064997fb | 782 | || self.sess.opts.unstable_opts.profile |
94222f64 | 783 | || self.sess.opts.cg.profile_generate.enabled()) |
ba9703b0 | 784 | { |
136023e0 XL |
785 | return; |
786 | } | |
041b39d2 | 787 | |
136023e0 | 788 | info!("loading profiler"); |
fc512014 | 789 | |
064997fb | 790 | let name = Symbol::intern(&self.sess.opts.unstable_opts.profiler_runtime); |
136023e0 | 791 | if name == sym::profiler_builtins && self.sess.contains_name(&krate.attrs, sym::no_core) { |
f2b60f7d | 792 | self.sess.emit_err(ProfilerBuiltinsNeedsCore); |
136023e0 | 793 | } |
041b39d2 | 794 | |
a2a8927a | 795 | let Some(cnum) = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit) else { return; }; |
136023e0 XL |
796 | let data = self.cstore.get_crate_data(cnum); |
797 | ||
798 | // Sanity check the loaded crate to ensure it is indeed a profiler runtime | |
799 | if !data.is_profiler_runtime() { | |
f2b60f7d | 800 | self.sess.emit_err(NotProfilerRuntime { crate_name: name }); |
041b39d2 XL |
801 | } |
802 | } | |
803 | ||
60c5eb7d | 804 | fn inject_allocator_crate(&mut self, krate: &ast::Crate) { |
3dfed10e | 805 | self.cstore.has_global_allocator = match &*global_allocator_spans(&self.sess, krate) { |
416331ca | 806 | [span1, span2, ..] => { |
f2b60f7d | 807 | self.sess.emit_err(NoMultipleGlobalAlloc { span2: *span2, span1: *span1 }); |
416331ca XL |
808 | true |
809 | } | |
dfeec247 | 810 | spans => !spans.is_empty(), |
416331ca | 811 | }; |
487cf647 FG |
812 | self.cstore.has_alloc_error_handler = match &*alloc_error_handler_spans(&self.sess, krate) { |
813 | [span1, span2, ..] => { | |
814 | self.sess.emit_err(NoMultipleAllocErrorHandler { span2: *span2, span1: *span1 }); | |
815 | true | |
816 | } | |
817 | spans => !spans.is_empty(), | |
818 | }; | |
041b39d2 XL |
819 | |
820 | // Check to see if we actually need an allocator. This desire comes | |
821 | // about through the `#![needs_allocator]` attribute and is typically | |
822 | // written down in liballoc. | |
a2a8927a XL |
823 | if !self.sess.contains_name(&krate.attrs, sym::needs_allocator) |
824 | && !self.cstore.iter_crate_data().any(|(_, data)| data.needs_allocator()) | |
825 | { | |
dfeec247 | 826 | return; |
041b39d2 | 827 | } |
e9174d1e | 828 | |
041b39d2 XL |
829 | // At this point we've determined that we need an allocator. Let's see |
830 | // if our compilation session actually needs an allocator based on what | |
831 | // we're emitting. | |
29967ef6 | 832 | let all_rlib = self.sess.crate_types().iter().all(|ct| matches!(*ct, CrateType::Rlib)); |
a1dfa0c6 | 833 | if all_rlib { |
dfeec247 | 834 | return; |
041b39d2 | 835 | } |
e9174d1e | 836 | |
041b39d2 XL |
837 | // Ok, we need an allocator. Not only that but we're actually going to |
838 | // create an artifact that needs one linked in. Let's go find the one | |
839 | // that we're going to link in. | |
e9174d1e | 840 | // |
041b39d2 XL |
841 | // First up we check for global allocators. Look at the crate graph here |
842 | // and see what's a global allocator, including if we ourselves are a | |
843 | // global allocator. | |
dfeec247 XL |
844 | let mut global_allocator = |
845 | self.cstore.has_global_allocator.then(|| Symbol::intern("this crate")); | |
a2a8927a XL |
846 | for (_, data) in self.cstore.iter_crate_data() { |
847 | if data.has_global_allocator() { | |
848 | match global_allocator { | |
5e7ed085 | 849 | Some(other_crate) => { |
f2b60f7d FG |
850 | self.sess.emit_err(ConflictingGlobalAlloc { |
851 | crate_name: data.name(), | |
852 | other_crate_name: other_crate, | |
853 | }); | |
5e7ed085 | 854 | } |
a2a8927a | 855 | None => global_allocator = Some(data.name()), |
041b39d2 | 856 | } |
041b39d2 | 857 | } |
a2a8927a | 858 | } |
487cf647 FG |
859 | let mut alloc_error_handler = |
860 | self.cstore.has_alloc_error_handler.then(|| Symbol::intern("this crate")); | |
861 | for (_, data) in self.cstore.iter_crate_data() { | |
862 | if data.has_alloc_error_handler() { | |
863 | match alloc_error_handler { | |
864 | Some(other_crate) => { | |
865 | self.sess.emit_err(ConflictingAllocErrorHandler { | |
866 | crate_name: data.name(), | |
867 | other_crate_name: other_crate, | |
868 | }); | |
869 | } | |
870 | None => alloc_error_handler = Some(data.name()), | |
871 | } | |
872 | } | |
873 | } | |
a2a8927a | 874 | |
041b39d2 | 875 | if global_allocator.is_some() { |
60c5eb7d | 876 | self.cstore.allocator_kind = Some(AllocatorKind::Global); |
487cf647 FG |
877 | } else { |
878 | // Ok we haven't found a global allocator but we still need an | |
879 | // allocator. At this point our allocator request is typically fulfilled | |
880 | // by the standard library, denoted by the `#![default_lib_allocator]` | |
881 | // attribute. | |
882 | if !self.sess.contains_name(&krate.attrs, sym::default_lib_allocator) | |
883 | && !self.cstore.iter_crate_data().any(|(_, data)| data.has_default_lib_allocator()) | |
884 | { | |
885 | self.sess.emit_err(GlobalAllocRequired); | |
886 | } | |
887 | self.cstore.allocator_kind = Some(AllocatorKind::Default); | |
041b39d2 | 888 | } |
e9174d1e | 889 | |
487cf647 FG |
890 | if alloc_error_handler.is_some() { |
891 | self.cstore.alloc_error_handler_kind = Some(AllocatorKind::Global); | |
892 | } else { | |
893 | // The alloc crate provides a default allocation error handler if | |
894 | // one isn't specified. | |
895 | if !self.sess.features_untracked().default_alloc_error_handler { | |
896 | self.sess.emit_err(AllocFuncRequired); | |
897 | self.sess.emit_note(MissingAllocErrorHandler); | |
898 | } | |
899 | self.cstore.alloc_error_handler_kind = Some(AllocatorKind::Default); | |
e9174d1e | 900 | } |
e9174d1e SL |
901 | } |
902 | ||
dfeec247 XL |
903 | fn inject_dependency_if( |
904 | &self, | |
905 | krate: CrateNum, | |
906 | what: &str, | |
907 | needs_dep: &dyn Fn(&CrateMetadata) -> bool, | |
908 | ) { | |
a7813a04 XL |
909 | // don't perform this validation if the session has errors, as one of |
910 | // those errors may indicate a circular dependency which could cause | |
911 | // this to stack overflow. | |
5e7ed085 | 912 | if self.sess.has_errors().is_some() { |
dfeec247 | 913 | return; |
a7813a04 XL |
914 | } |
915 | ||
e9174d1e | 916 | // Before we inject any dependencies, make sure we don't inject a |
a7813a04 XL |
917 | // circular dependency by validating that this crate doesn't |
918 | // transitively depend on any crates satisfying `needs_dep`. | |
60c5eb7d | 919 | for dep in self.cstore.crate_dependencies_in_reverse_postorder(krate) { |
3157f602 XL |
920 | let data = self.cstore.get_crate_data(dep); |
921 | if needs_dep(&data) { | |
f2b60f7d FG |
922 | self.sess.emit_err(NoTransitiveNeedsDep { |
923 | crate_name: self.cstore.get_crate_data(krate).name(), | |
924 | needs_crate_name: what, | |
925 | deps_crate_name: data.name(), | |
926 | }); | |
3157f602 XL |
927 | } |
928 | } | |
a7813a04 XL |
929 | |
930 | // All crates satisfying `needs_dep` do not explicitly depend on the | |
931 | // crate provided for this compile, but in order for this compilation to | |
932 | // be successfully linked we need to inject a dependency (to order the | |
933 | // crates on the command line correctly). | |
a2a8927a XL |
934 | for (cnum, data) in self.cstore.iter_crate_data() { |
935 | if needs_dep(data) { | |
936 | info!("injecting a dep from {} to {}", cnum, krate); | |
937 | data.add_dependency(krate); | |
e9174d1e | 938 | } |
a2a8927a | 939 | } |
e9174d1e | 940 | } |
e9174d1e | 941 | |
f9f354fc XL |
942 | fn report_unused_deps(&mut self, krate: &ast::Crate) { |
943 | // Make a point span rather than covering the whole file | |
5e7ed085 | 944 | let span = krate.spans.inner_span.shrink_to_lo(); |
f9f354fc | 945 | // Complain about anything left over |
f035d41b XL |
946 | for (name, entry) in self.sess.opts.externs.iter() { |
947 | if let ExternLocation::FoundInLibrarySearchDirectories = entry.location { | |
948 | // Don't worry about pathless `--extern foo` sysroot references | |
949 | continue; | |
950 | } | |
04454e1e FG |
951 | if entry.nounused_dep { |
952 | // We're not worried about this one | |
953 | continue; | |
954 | } | |
cdc7bbd5 XL |
955 | let name_interned = Symbol::intern(name); |
956 | if self.used_extern_options.contains(&name_interned) { | |
6a06907d XL |
957 | continue; |
958 | } | |
959 | ||
960 | // Got a real unused --extern | |
04454e1e | 961 | if self.sess.opts.json_unused_externs.is_enabled() { |
cdc7bbd5 XL |
962 | self.cstore.unused_externs.push(name_interned); |
963 | continue; | |
964 | } | |
965 | ||
04454e1e | 966 | self.sess.parse_sess.buffer_lint( |
f9f354fc XL |
967 | lint::builtin::UNUSED_CRATE_DEPENDENCIES, |
968 | span, | |
969 | ast::CRATE_NODE_ID, | |
970 | &format!( | |
971 | "external crate `{}` unused in `{}`: remove the dependency or add `use {} as _;`", | |
972 | name, | |
973 | self.local_crate_name, | |
974 | name), | |
975 | ); | |
f9f354fc XL |
976 | } |
977 | } | |
978 | ||
b7449926 | 979 | pub fn postprocess(&mut self, krate: &ast::Crate) { |
fc512014 | 980 | self.inject_profiler_runtime(krate); |
041b39d2 | 981 | self.inject_allocator_crate(krate); |
c30ab7b3 | 982 | self.inject_panic_runtime(krate); |
c34b1796 | 983 | |
f9f354fc | 984 | self.report_unused_deps(krate); |
cdc7bbd5 XL |
985 | |
986 | info!("{:?}", CrateDump(&self.cstore)); | |
9e0c209e | 987 | } |
c34b1796 | 988 | |
b7449926 | 989 | pub fn process_extern_crate( |
e74abb32 XL |
990 | &mut self, |
991 | item: &ast::Item, | |
992 | definitions: &Definitions, | |
f035d41b | 993 | def_id: LocalDefId, |
a2a8927a | 994 | ) -> Option<CrateNum> { |
e74abb32 | 995 | match item.kind { |
0531ce1d | 996 | ast::ItemKind::ExternCrate(orig_name) => { |
dfeec247 XL |
997 | debug!( |
998 | "resolving extern crate stmt. ident: {} orig_name: {:?}", | |
999 | item.ident, orig_name | |
1000 | ); | |
e74abb32 | 1001 | let name = match orig_name { |
0531ce1d | 1002 | Some(orig_name) => { |
487cf647 | 1003 | validate_crate_name(self.sess, orig_name, Some(item.span)); |
0531ce1d | 1004 | orig_name |
ff7c6d11 XL |
1005 | } |
1006 | None => item.ident.name, | |
1007 | }; | |
3dfed10e XL |
1008 | let dep_kind = if self.sess.contains_name(&item.attrs, sym::no_link) { |
1009 | CrateDepKind::MacrosOnly | |
ff7c6d11 | 1010 | } else { |
3dfed10e | 1011 | CrateDepKind::Explicit |
ff7c6d11 XL |
1012 | }; |
1013 | ||
a2a8927a | 1014 | let cnum = self.resolve_crate(name, item.span, dep_kind)?; |
476ff2be | 1015 | |
ba9703b0 | 1016 | let path_len = definitions.def_path(def_id).data.len(); |
83c7162d XL |
1017 | self.update_extern_crate( |
1018 | cnum, | |
1019 | ExternCrate { | |
ba9703b0 | 1020 | src: ExternCrateSource::Extern(def_id.to_def_id()), |
83c7162d XL |
1021 | span: item.span, |
1022 | path_len, | |
e74abb32 | 1023 | dependency_of: LOCAL_CRATE, |
83c7162d | 1024 | }, |
83c7162d | 1025 | ); |
a2a8927a | 1026 | Some(cnum) |
c30ab7b3 | 1027 | } |
83c7162d | 1028 | _ => bug!(), |
c34b1796 | 1029 | } |
c34b1796 | 1030 | } |
ff7c6d11 | 1031 | |
a2a8927a XL |
1032 | pub fn process_path_extern(&mut self, name: Symbol, span: Span) -> Option<CrateNum> { |
1033 | let cnum = self.resolve_crate(name, span, CrateDepKind::Explicit)?; | |
83c7162d XL |
1034 | |
1035 | self.update_extern_crate( | |
1036 | cnum, | |
1037 | ExternCrate { | |
1038 | src: ExternCrateSource::Path, | |
1039 | span, | |
1040 | // to have the least priority in `update_extern_crate` | |
f035d41b | 1041 | path_len: usize::MAX, |
e74abb32 | 1042 | dependency_of: LOCAL_CRATE, |
83c7162d | 1043 | }, |
83c7162d XL |
1044 | ); |
1045 | ||
a2a8927a | 1046 | Some(cnum) |
83c7162d XL |
1047 | } |
1048 | ||
3dfed10e XL |
1049 | pub fn maybe_process_path_extern(&mut self, name: Symbol) -> Option<CrateNum> { |
1050 | self.maybe_resolve_crate(name, CrateDepKind::Explicit, None).ok() | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | fn global_allocator_spans(sess: &Session, krate: &ast::Crate) -> Vec<Span> { | |
1055 | struct Finder<'a> { | |
1056 | sess: &'a Session, | |
1057 | name: Symbol, | |
1058 | spans: Vec<Span>, | |
1059 | } | |
1060 | impl<'ast, 'a> visit::Visitor<'ast> for Finder<'a> { | |
1061 | fn visit_item(&mut self, item: &'ast ast::Item) { | |
1062 | if item.ident.name == self.name | |
1063 | && self.sess.contains_name(&item.attrs, sym::rustc_std_internal_symbol) | |
1064 | { | |
1065 | self.spans.push(item.span); | |
1066 | } | |
1067 | visit::walk_item(self, item) | |
1068 | } | |
ff7c6d11 | 1069 | } |
3dfed10e XL |
1070 | |
1071 | let name = Symbol::intern(&AllocatorKind::Global.fn_name(sym::alloc)); | |
1072 | let mut f = Finder { sess, name, spans: Vec::new() }; | |
1073 | visit::walk_crate(&mut f, krate); | |
1074 | f.spans | |
c34b1796 | 1075 | } |
487cf647 FG |
1076 | |
1077 | fn alloc_error_handler_spans(sess: &Session, krate: &ast::Crate) -> Vec<Span> { | |
1078 | struct Finder<'a> { | |
1079 | sess: &'a Session, | |
1080 | name: Symbol, | |
1081 | spans: Vec<Span>, | |
1082 | } | |
1083 | impl<'ast, 'a> visit::Visitor<'ast> for Finder<'a> { | |
1084 | fn visit_item(&mut self, item: &'ast ast::Item) { | |
1085 | if item.ident.name == self.name | |
1086 | && self.sess.contains_name(&item.attrs, sym::rustc_std_internal_symbol) | |
1087 | { | |
1088 | self.spans.push(item.span); | |
1089 | } | |
1090 | visit::walk_item(self, item) | |
1091 | } | |
1092 | } | |
1093 | ||
1094 | let name = Symbol::intern(&AllocatorKind::Global.fn_name(sym::oom)); | |
1095 | let mut f = Finder { sess, name, spans: Vec::new() }; | |
1096 | visit::walk_crate(&mut f, krate); | |
1097 | f.spans | |
1098 | } |