Skip to content

Commit a9cef49

Browse files
committed
rustc_metadata: Privatize all fields of CrateRoot
All of them are read-only
1 parent 765133a commit a9cef49

File tree

4 files changed

+105
-57
lines changed

4 files changed

+105
-57
lines changed

src/librustc_metadata/creader.rs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ pub struct CrateLoader<'a> {
4747
fn dump_crates(cstore: &CStore) {
4848
info!("resolved crates:");
4949
cstore.iter_crate_data(|cnum, data| {
50-
info!(" name: {}", data.root.name);
50+
info!(" name: {}", data.root.name());
5151
info!(" cnum: {}", cnum);
52-
info!(" hash: {}", data.root.hash);
52+
info!(" hash: {}", data.root.hash());
5353
info!(" reqd: {:?}", data.dep_kind());
5454
let CrateSource { dylib, rlib, rmeta } = data.source();
5555
dylib.as_ref().map(|dl| info!(" dylib: {}", dl.0.display()));
@@ -101,10 +101,10 @@ impl<'a> CrateLoader<'a> {
101101
-> Option<CrateNum> {
102102
let mut ret = None;
103103
self.cstore.iter_crate_data(|cnum, data| {
104-
if data.root.name != name { return }
104+
if data.root.name() != name { return }
105105

106106
match hash {
107-
Some(hash) if *hash == data.root.hash => { ret = Some(cnum); return }
107+
Some(hash) if *hash == data.root.hash() => { ret = Some(cnum); return }
108108
Some(..) => return,
109109
None => {}
110110
}
@@ -152,26 +152,26 @@ impl<'a> CrateLoader<'a> {
152152
span: Span,
153153
root: &CrateRoot<'_>) {
154154
// Check for (potential) conflicts with the local crate
155-
if self.local_crate_name == root.name &&
156-
self.sess.local_crate_disambiguator() == root.disambiguator {
155+
if self.local_crate_name == root.name() &&
156+
self.sess.local_crate_disambiguator() == root.disambiguator() {
157157
span_fatal!(self.sess, span, E0519,
158158
"the current crate is indistinguishable from one of its \
159159
dependencies: it has the same crate-name `{}` and was \
160160
compiled with the same `-C metadata` arguments. This \
161161
will result in symbol conflicts between the two.",
162-
root.name)
162+
root.name())
163163
}
164164

165165
// Check for conflicts with any crate loaded so far
166166
self.cstore.iter_crate_data(|_, other| {
167-
if other.root.name == root.name && // same crate-name
168-
other.root.disambiguator == root.disambiguator && // same crate-disambiguator
169-
other.root.hash != root.hash { // but different SVH
167+
if other.root.name() == root.name() && // same crate-name
168+
other.root.disambiguator() == root.disambiguator() && // same crate-disambiguator
169+
other.root.hash() != root.hash() { // but different SVH
170170
span_fatal!(self.sess, span, E0523,
171171
"found two different crates with name `{}` that are \
172172
not distinguished by differing `-C metadata`. This \
173173
will result in symbol conflicts between the two.",
174-
root.name)
174+
root.name())
175175
}
176176
});
177177
}
@@ -189,14 +189,14 @@ impl<'a> CrateLoader<'a> {
189189

190190
let Library { source, metadata } = lib;
191191
let crate_root = metadata.get_root();
192-
let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash);
192+
let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash());
193193
self.verify_no_symbol_conflicts(span, &crate_root);
194194

195195
let private_dep = self.sess.opts.externs.get(&name.as_str())
196196
.map(|e| e.is_private_dep)
197197
.unwrap_or(false);
198198

199-
info!("register crate `{}` (private_dep = {})", crate_root.name, private_dep);
199+
info!("register crate `{}` (private_dep = {})", crate_root.name(), private_dep);
200200

201201
// Claim this crate number and cache it
202202
let cnum = self.cstore.alloc_new_crate_num();
@@ -207,7 +207,7 @@ impl<'a> CrateLoader<'a> {
207207
let root = if let Some(root) = root {
208208
root
209209
} else {
210-
crate_paths = CratePaths::new(crate_root.name, source.clone());
210+
crate_paths = CratePaths::new(crate_root.name(), source.clone());
211211
&crate_paths
212212
};
213213

@@ -221,7 +221,7 @@ impl<'a> CrateLoader<'a> {
221221
None => (&source, &crate_root),
222222
};
223223
let dlsym_dylib = dlsym_source.dylib.as_ref().expect("no dylib for a proc-macro crate");
224-
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator, span))
224+
Some(self.dlsym_proc_macros(&dlsym_dylib.0, dlsym_root.disambiguator(), span))
225225
} else {
226226
None
227227
};
@@ -378,7 +378,7 @@ impl<'a> CrateLoader<'a> {
378378
if locator.triple == self.sess.opts.target_triple {
379379
let mut result = LoadResult::Loaded(library);
380380
self.cstore.iter_crate_data(|cnum, data| {
381-
if data.root.name == root.name && root.hash == data.root.hash {
381+
if data.root.name() == root.name() && root.hash() == data.root.hash() {
382382
assert!(locator.hash.is_none());
383383
info!("load success, going to previous cnum: {}", cnum);
384384
result = LoadResult::Previous(cnum);
@@ -494,13 +494,12 @@ impl<'a> CrateLoader<'a> {
494494
sym::needs_panic_runtime);
495495

496496
self.cstore.iter_crate_data(|cnum, data| {
497-
needs_panic_runtime = needs_panic_runtime ||
498-
data.root.needs_panic_runtime;
499-
if data.root.panic_runtime {
497+
needs_panic_runtime = needs_panic_runtime || data.needs_panic_runtime();
498+
if data.is_panic_runtime() {
500499
// Inject a dependency from all #![needs_panic_runtime] to this
501500
// #![panic_runtime] crate.
502501
self.inject_dependency_if(cnum, "a panic runtime",
503-
&|data| data.root.needs_panic_runtime);
502+
&|data| data.needs_panic_runtime());
504503
runtime_found = runtime_found || data.dep_kind() == DepKind::Explicit;
505504
}
506505
});
@@ -536,19 +535,19 @@ impl<'a> CrateLoader<'a> {
536535

537536
// Sanity check the loaded crate to ensure it is indeed a panic runtime
538537
// and the panic strategy is indeed what we thought it was.
539-
if !data.root.panic_runtime {
538+
if !data.is_panic_runtime() {
540539
self.sess.err(&format!("the crate `{}` is not a panic runtime",
541540
name));
542541
}
543-
if data.root.panic_strategy != desired_strategy {
542+
if data.panic_strategy() != desired_strategy {
544543
self.sess.err(&format!("the crate `{}` does not have the panic \
545544
strategy `{}`",
546545
name, desired_strategy.desc()));
547546
}
548547

549548
self.cstore.injected_panic_runtime = Some(cnum);
550549
self.inject_dependency_if(cnum, "a panic runtime",
551-
&|data| data.root.needs_panic_runtime);
550+
&|data| data.needs_panic_runtime());
552551
}
553552

554553
fn inject_sanitizer_runtime(&mut self) {
@@ -622,7 +621,7 @@ impl<'a> CrateLoader<'a> {
622621

623622
let mut uses_std = false;
624623
self.cstore.iter_crate_data(|_, data| {
625-
if data.root.name == sym::std {
624+
if data.root.name() == sym::std {
626625
uses_std = true;
627626
}
628627
});
@@ -640,7 +639,7 @@ impl<'a> CrateLoader<'a> {
640639
let data = self.cstore.get_crate_data(cnum);
641640

642641
// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
643-
if !data.root.sanitizer_runtime {
642+
if !data.is_sanitizer_runtime() {
644643
self.sess.err(&format!("the crate `{}` is not a sanitizer runtime",
645644
name));
646645
}
@@ -661,7 +660,7 @@ impl<'a> CrateLoader<'a> {
661660
let data = self.cstore.get_crate_data(cnum);
662661

663662
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
664-
if !data.root.profiler_runtime {
663+
if !data.is_profiler_runtime() {
665664
self.sess.err(&format!("the crate `profiler_builtins` is not \
666665
a profiler runtime"));
667666
}
@@ -687,7 +686,7 @@ impl<'a> CrateLoader<'a> {
687686
let mut needs_allocator = attr::contains_name(&krate.attrs,
688687
sym::needs_allocator);
689688
self.cstore.iter_crate_data(|_, data| {
690-
needs_allocator = needs_allocator || data.root.needs_allocator;
689+
needs_allocator = needs_allocator || data.needs_allocator();
691690
});
692691
if !needs_allocator {
693692
self.cstore.allocator_kind = None;
@@ -723,7 +722,7 @@ impl<'a> CrateLoader<'a> {
723722
None
724723
};
725724
self.cstore.iter_crate_data(|_, data| {
726-
if !data.root.has_global_allocator {
725+
if !data.has_global_allocator() {
727726
return
728727
}
729728
match global_allocator {
@@ -732,14 +731,14 @@ impl<'a> CrateLoader<'a> {
732731
conflicts with this global \
733732
allocator in: {}",
734733
other_crate,
735-
data.root.name));
734+
data.root.name()));
736735
}
737736
Some(None) => {
738737
self.sess.err(&format!("the `#[global_allocator]` in this \
739738
crate conflicts with global \
740-
allocator in: {}", data.root.name));
739+
allocator in: {}", data.root.name()));
741740
}
742-
None => global_allocator = Some(Some(data.root.name)),
741+
None => global_allocator = Some(Some(data.root.name())),
743742
}
744743
});
745744
if global_allocator.is_some() {
@@ -753,7 +752,7 @@ impl<'a> CrateLoader<'a> {
753752
// attribute.
754753
let mut has_default = attr::contains_name(&krate.attrs, sym::default_lib_allocator);
755754
self.cstore.iter_crate_data(|_, data| {
756-
if data.root.has_default_lib_allocator {
755+
if data.has_default_lib_allocator() {
757756
has_default = true;
758757
}
759758
});
@@ -787,9 +786,9 @@ impl<'a> CrateLoader<'a> {
787786
self.sess.err(&format!("the crate `{}` cannot depend \
788787
on a crate that needs {}, but \
789788
it depends on `{}`",
790-
self.cstore.get_crate_data(krate).root.name,
789+
self.cstore.get_crate_data(krate).root.name(),
791790
what,
792-
data.root.name));
791+
data.root.name()));
793792
}
794793
}
795794

src/librustc_metadata/locator.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ impl<'a> CrateLocator<'a> {
597597
"multiple matching crates for `{}`",
598598
self.crate_name);
599599
let candidates = libraries.iter().filter_map(|(_, lib)| {
600-
let crate_name = &lib.metadata.get_root().name.as_str();
600+
let crate_name = &lib.metadata.get_root().name().as_str();
601601
match &(&lib.source.dylib, &lib.source.rlib) {
602602
&(&Some((ref pd, _)), &Some((ref pr, _))) => {
603603
Some(format!("\ncrate `{}`: {}\n{:>padding$}",
@@ -774,35 +774,36 @@ impl<'a> CrateLocator<'a> {
774774
}
775775

776776
if self.exact_paths.is_empty() {
777-
if self.crate_name != root.name {
777+
if self.crate_name != root.name() {
778778
info!("Rejecting via crate name");
779779
return None;
780780
}
781781
}
782782

783-
if root.triple != self.triple {
783+
if root.triple() != &self.triple {
784784
info!("Rejecting via crate triple: expected {} got {}",
785785
self.triple,
786-
root.triple);
786+
root.triple());
787787
self.rejected_via_triple.push(CrateMismatch {
788788
path: libpath.to_path_buf(),
789-
got: root.triple.to_string(),
789+
got: root.triple().to_string(),
790790
});
791791
return None;
792792
}
793793

794-
if let Some(myhash) = self.hash {
795-
if *myhash != root.hash {
796-
info!("Rejecting via hash: expected {} got {}", *myhash, root.hash);
794+
let hash = root.hash();
795+
if let Some(&expected_hash) = self.hash {
796+
if hash != expected_hash {
797+
info!("Rejecting via hash: expected {} got {}", expected_hash, hash);
797798
self.rejected_via_hash.push(CrateMismatch {
798799
path: libpath.to_path_buf(),
799-
got: myhash.to_string(),
800+
got: hash.to_string(),
800801
});
801802
return None;
802803
}
803804
}
804805

805-
Some(root.hash)
806+
Some(hash)
806807
}
807808

808809

@@ -1021,7 +1022,7 @@ pub fn find_plugin_registrar(
10211022

10221023
match library.source.dylib {
10231024
Some(dylib) => {
1024-
Some((dylib.0, library.metadata.get_root().disambiguator))
1025+
Some((dylib.0, library.metadata.get_root().disambiguator()))
10251026
}
10261027
None => {
10271028
span_err!(sess, span, E0457,

src/librustc_metadata/rmeta/decoder.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,22 @@ impl CrateRoot<'_> {
558558
self.proc_macro_data.is_some()
559559
}
560560

561+
crate fn name(&self) -> Symbol {
562+
self.name
563+
}
564+
565+
crate fn disambiguator(&self) -> CrateDisambiguator {
566+
self.disambiguator
567+
}
568+
569+
crate fn hash(&self) -> Svh {
570+
self.hash
571+
}
572+
573+
crate fn triple(&self) -> &TargetTriple {
574+
&self.triple
575+
}
576+
561577
crate fn decode_crate_deps(
562578
&self,
563579
metadata: &'a MetadataBlob,
@@ -1546,6 +1562,38 @@ impl<'a, 'tcx> CrateMetadata {
15461562
crate fn update_dep_kind(&self, f: impl FnOnce(DepKind) -> DepKind) {
15471563
self.dep_kind.with_lock(|dep_kind| *dep_kind = f(*dep_kind))
15481564
}
1565+
1566+
crate fn panic_strategy(&self) -> PanicStrategy {
1567+
self.root.panic_strategy
1568+
}
1569+
1570+
crate fn needs_panic_runtime(&self) -> bool {
1571+
self.root.needs_panic_runtime
1572+
}
1573+
1574+
crate fn is_panic_runtime(&self) -> bool {
1575+
self.root.panic_runtime
1576+
}
1577+
1578+
crate fn is_sanitizer_runtime(&self) -> bool {
1579+
self.root.sanitizer_runtime
1580+
}
1581+
1582+
crate fn is_profiler_runtime(&self) -> bool {
1583+
self.root.profiler_runtime
1584+
}
1585+
1586+
crate fn needs_allocator(&self) -> bool {
1587+
self.root.needs_allocator
1588+
}
1589+
1590+
crate fn has_global_allocator(&self) -> bool {
1591+
self.root.has_global_allocator
1592+
}
1593+
1594+
crate fn has_default_lib_allocator(&self) -> bool {
1595+
self.root.has_default_lib_allocator
1596+
}
15491597
}
15501598

15511599
// Cannot be implemented on 'ProcMacro', as libproc_macro

src/librustc_metadata/rmeta/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ macro_rules! Lazy {
173173

174174
#[derive(RustcEncodable, RustcDecodable)]
175175
crate struct CrateRoot<'tcx> {
176-
pub name: Symbol,
177-
pub triple: TargetTriple,
176+
name: Symbol,
177+
triple: TargetTriple,
178178
extra_filename: String,
179-
pub hash: Svh,
180-
pub disambiguator: CrateDisambiguator,
181-
pub panic_strategy: PanicStrategy,
179+
hash: Svh,
180+
disambiguator: CrateDisambiguator,
181+
panic_strategy: PanicStrategy,
182182
edition: Edition,
183-
pub has_global_allocator: bool,
183+
has_global_allocator: bool,
184184
has_panic_handler: bool,
185-
pub has_default_lib_allocator: bool,
185+
has_default_lib_allocator: bool,
186186
plugin_registrar_fn: Option<DefIndex>,
187187
proc_macro_decls_static: Option<DefIndex>,
188188
proc_macro_stability: Option<attr::Stability>,
@@ -207,12 +207,12 @@ crate struct CrateRoot<'tcx> {
207207
proc_macro_data: Option<Lazy<[DefIndex]>>,
208208

209209
compiler_builtins: bool,
210-
pub needs_allocator: bool,
211-
pub needs_panic_runtime: bool,
210+
needs_allocator: bool,
211+
needs_panic_runtime: bool,
212212
no_builtins: bool,
213-
pub panic_runtime: bool,
214-
pub profiler_runtime: bool,
215-
pub sanitizer_runtime: bool,
213+
panic_runtime: bool,
214+
profiler_runtime: bool,
215+
sanitizer_runtime: bool,
216216
symbol_mangling_version: SymbolManglingVersion,
217217
}
218218

0 commit comments

Comments
 (0)