1use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
2
3use ecow::EcoString;
4
5use crate::ast::{BindingId as AstBindingId, Pattern, RestPattern, Span};
6use crate::types::Symbol;
7
8use super::{Definition, File, ModuleInfo};
9
10#[derive(Debug, Clone, Default)]
11pub struct UnusedInfo {
12 bindings: HashSet<Span>,
13 definitions: HashSet<Span>,
14 pub imports_by_module: HashMap<EcoString, HashSet<EcoString>>,
15}
16
17impl UnusedInfo {
18 pub fn mark_binding_unused(&mut self, span: Span) {
19 self.bindings.insert(span);
20 }
21
22 pub fn is_unused_binding(&self, pattern: &Pattern) -> bool {
23 match pattern {
24 Pattern::Identifier { span, .. } => self.bindings.contains(span),
25 Pattern::AsBinding { span, name, .. } => {
26 let name_span = Span::new(
27 span.file_id,
28 span.byte_offset + span.byte_length - name.len() as u32,
29 name.len() as u32,
30 );
31 self.bindings.contains(&name_span)
32 }
33 _ => false,
34 }
35 }
36
37 pub fn is_unused_rest_binding(&self, rest: &RestPattern) -> bool {
38 match rest {
39 RestPattern::Bind { span, .. } => self.bindings.contains(span),
40 _ => false,
41 }
42 }
43
44 pub fn mark_definition_unused(&mut self, span: Span) {
45 self.definitions.insert(span);
46 }
47
48 pub fn is_unused_definition(&self, span: &Span) -> bool {
49 self.definitions.contains(span)
50 }
51
52 pub fn merge(&mut self, other: UnusedInfo) {
53 let UnusedInfo {
54 bindings,
55 definitions,
56 imports_by_module,
57 } = other;
58 self.bindings.extend(bindings);
59 self.definitions.extend(definitions);
60 for (module, imports) in imports_by_module {
61 self.imports_by_module
62 .entry(module)
63 .or_default()
64 .extend(imports);
65 }
66 }
67}
68
69#[derive(Debug, Clone)]
70pub struct TestFunction {
71 pub module_id: String,
72 pub qualified_name: String,
73 pub title: Option<String>,
74 pub doc: Option<String>,
75 pub span: Span,
76}
77
78#[derive(Debug, Clone, Default)]
79pub struct TestIndex {
80 tests: Vec<TestFunction>,
81}
82
83impl TestIndex {
84 pub fn push(&mut self, test: TestFunction) {
85 self.tests.push(test);
86 }
87
88 pub fn tests(&self) -> &[TestFunction] {
89 &self.tests
90 }
91
92 pub fn contains_qualified(&self, qualified_name: &str) -> bool {
93 self.tests
94 .iter()
95 .any(|t| t.qualified_name == qualified_name)
96 }
97
98 pub fn len(&self) -> usize {
99 self.tests.len()
100 }
101
102 pub fn is_empty(&self) -> bool {
103 self.tests.is_empty()
104 }
105}
106
107#[derive(Debug, Clone, Default)]
108pub struct EqualityIndex {
109 by_id: HashMap<String, EqualityInfo>,
110}
111
112#[derive(Debug, Clone)]
113enum EqualityInfo {
114 Method {
115 private_to_module: Option<String>,
116 synthesized: bool,
117 },
118 UfcsLowered {
119 private_to_module: Option<String>,
120 },
121}
122
123fn visible_from(private_to_module: &Option<String>, current_module: &str) -> bool {
124 match private_to_module {
125 None => true,
126 Some(module) => module == current_module,
127 }
128}
129
130impl EqualityIndex {
131 pub fn insert_method(
132 &mut self,
133 id: String,
134 private_to_module: Option<String>,
135 synthesized: bool,
136 ) {
137 self.by_id.insert(
138 id,
139 EqualityInfo::Method {
140 private_to_module,
141 synthesized,
142 },
143 );
144 }
145
146 pub fn insert_ufcs_lowered(&mut self, id: String, private_to_module: Option<String>) {
147 self.by_id
148 .insert(id, EqualityInfo::UfcsLowered { private_to_module });
149 }
150
151 pub fn usable_from(&self, id: &str, current_module: &str) -> bool {
152 matches!(
153 self.by_id.get(id),
154 Some(EqualityInfo::Method { private_to_module, .. })
155 if visible_from(private_to_module, current_module)
156 )
157 }
158
159 pub fn is_ufcs_lowered_from(&self, id: &str, current_module: &str) -> bool {
160 matches!(
161 self.by_id.get(id),
162 Some(EqualityInfo::UfcsLowered { private_to_module })
163 if visible_from(private_to_module, current_module)
164 )
165 }
166
167 pub fn is_synthesized(&self, id: &str) -> bool {
168 matches!(
169 self.by_id.get(id),
170 Some(EqualityInfo::Method {
171 synthesized: true,
172 ..
173 })
174 )
175 }
176}
177
178#[derive(Debug, Clone, Default)]
179pub struct MutationInfo {
180 bindings: HashSet<AstBindingId>,
181 alias_bindings: HashSet<AstBindingId>,
182}
183
184impl MutationInfo {
185 pub fn mark_binding_mutated(&mut self, id: AstBindingId) {
186 self.bindings.insert(id);
187 }
188
189 pub fn mark_binding_alias_mutated(&mut self, id: AstBindingId) {
191 self.bindings.insert(id);
192 self.alias_bindings.insert(id);
193 }
194
195 pub fn is_mutated(&self, id: AstBindingId) -> bool {
196 self.bindings.contains(&id)
197 }
198
199 pub fn is_alias_mutated(&self, id: AstBindingId) -> bool {
200 self.alias_bindings.contains(&id)
201 }
202}
203
204pub struct EmitInput {
205 pub files: HashMap<u32, File>,
206 pub definitions: HashMap<Symbol, Definition>,
207 pub const_names: HashSet<Symbol>,
208 pub modules: HashMap<String, ModuleInfo>,
209 pub entry_module_id: String,
210 pub unused: UnusedInfo,
211 pub mutations: MutationInfo,
212 pub cached_modules: HashSet<String>,
213 pub ufcs_methods: HashSet<(String, String)>,
214 pub equality_index: EqualityIndex,
215 pub test_index: TestIndex,
216 pub go_package_names: HashMap<String, String>,
217 pub go_module_ids: HashSet<String>,
218 pub bound_types: HashMap<crate::ast::Span, crate::types::Type>,
219 pub resolved_definitions: super::ResolvedDefinitions,
220}
221
222#[cfg(test)]
223mod tests {
224 use super::*;
225
226 fn span(offset: u32) -> Span {
227 Span::new(0, offset, 1)
228 }
229
230 #[test]
231 fn merge_extends_bindings_definitions_and_imports() {
232 let mut a = UnusedInfo::default();
233 a.mark_binding_unused(span(0));
234 a.mark_definition_unused(span(1));
235 a.imports_by_module
236 .insert("m1".into(), HashSet::from_iter(["x".into()]));
237
238 let mut b = UnusedInfo::default();
239 b.mark_binding_unused(span(2));
240 b.mark_definition_unused(span(3));
241 b.imports_by_module
242 .insert("m1".into(), HashSet::from_iter(["y".into()]));
243 b.imports_by_module
244 .insert("m2".into(), HashSet::from_iter(["z".into()]));
245
246 a.merge(b);
247
248 assert_eq!(a.bindings.len(), 2);
249 assert_eq!(a.definitions.len(), 2);
250 assert_eq!(a.imports_by_module["m1"].len(), 2);
251 assert_eq!(a.imports_by_module["m2"].len(), 1);
252 }
253}