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
140
141
142
143
144
145
146
147
use rustc::ty::TyCtxt;
use syntax::ast;
use {ModuleSource, ModuleTranslation};
const PARTITION_REUSED: &'static str = "rustc_partition_reused";
const PARTITION_TRANSLATED: &'static str = "rustc_partition_translated";
const MODULE: &'static str = "module";
const CFG: &'static str = "cfg";
#[derive(Debug, PartialEq)]
enum Disposition { Reused, Translated }
pub fn assert_module_sources<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
modules: &[ModuleTranslation]) {
let _ignore = tcx.dep_graph.in_ignore();
if tcx.sess.opts.incremental.is_none() {
return;
}
let ams = AssertModuleSource { tcx: tcx, modules: modules };
for attr in &tcx.hir.krate().attrs {
ams.check_attr(attr);
}
}
struct AssertModuleSource<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
modules: &'a [ModuleTranslation],
}
impl<'a, 'tcx> AssertModuleSource<'a, 'tcx> {
fn check_attr(&self, attr: &ast::Attribute) {
let disposition = if attr.check_name(PARTITION_REUSED) {
Disposition::Reused
} else if attr.check_name(PARTITION_TRANSLATED) {
Disposition::Translated
} else {
return;
};
if !self.check_config(attr) {
debug!("check_attr: config does not match, ignoring attr");
return;
}
let mname = self.field(attr, MODULE);
let mtrans = self.modules.iter().find(|mtrans| *mtrans.name == *mname.as_str());
let mtrans = match mtrans {
Some(m) => m,
None => {
debug!("module name `{}` not found amongst:", mname);
for mtrans in self.modules {
debug!("module named `{}` with disposition {:?}",
mtrans.name,
self.disposition(mtrans));
}
self.tcx.sess.span_err(
attr.span,
&format!("no module named `{}`", mname));
return;
}
};
let mtrans_disposition = self.disposition(mtrans);
if disposition != mtrans_disposition {
self.tcx.sess.span_err(
attr.span,
&format!("expected module named `{}` to be {:?} but is {:?}",
mname,
disposition,
mtrans_disposition));
}
}
fn disposition(&self, mtrans: &ModuleTranslation) -> Disposition {
match mtrans.source {
ModuleSource::Preexisting(_) => Disposition::Reused,
ModuleSource::Translated(_) => Disposition::Translated,
}
}
fn field(&self, attr: &ast::Attribute, name: &str) -> ast::Name {
for item in attr.meta_item_list().unwrap_or(&[]) {
if item.check_name(name) {
if let Some(value) = item.value_str() {
return value;
} else {
self.tcx.sess.span_fatal(
item.span,
&format!("associated value expected for `{}`", name));
}
}
}
self.tcx.sess.span_fatal(
attr.span,
&format!("no field `{}`", name));
}
fn check_config(&self, attr: &ast::Attribute) -> bool {
let config = &self.tcx.sess.parse_sess.config;
let value = self.field(attr, CFG);
debug!("check_config(config={:?}, value={:?})", config, value);
if config.iter().any(|&(name, _)| name == value) {
debug!("check_config: matched");
return true;
}
debug!("check_config: no match found");
return false;
}
}