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 len(&self) -> usize {
93 self.tests.len()
94 }
95
96 pub fn is_empty(&self) -> bool {
97 self.tests.is_empty()
98 }
99}
100
101#[derive(Debug, Clone, Default)]
102pub struct EqualityIndex {
103 by_id: HashMap<String, EqualityInfo>,
104}
105
106#[derive(Debug, Clone)]
107pub enum EqualityInfo {
108 Method {
109 private_to_module: Option<String>,
110 synthesized: bool,
111 },
112 Unusable {
113 reason: EqualityUnusableReason,
114 private_to_module: Option<String>,
115 },
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq)]
119pub enum EqualityUnusableReason {
120 UfcsLowered,
121 BoundMismatch,
122}
123
124fn visible_from(private_to_module: &Option<String>, current_module: &str) -> bool {
125 match private_to_module {
126 None => true,
127 Some(module) => module == current_module,
128 }
129}
130
131impl EqualityIndex {
132 pub fn insert_method(
133 &mut self,
134 id: String,
135 private_to_module: Option<String>,
136 synthesized: bool,
137 ) {
138 self.by_id.insert(
139 id,
140 EqualityInfo::Method {
141 private_to_module,
142 synthesized,
143 },
144 );
145 }
146
147 pub fn insert_unusable(
148 &mut self,
149 id: String,
150 reason: EqualityUnusableReason,
151 private_to_module: Option<String>,
152 ) {
153 self.by_id.insert(
154 id,
155 EqualityInfo::Unusable {
156 reason,
157 private_to_module,
158 },
159 );
160 }
161
162 pub fn usable_from(&self, id: &str, current_module: &str) -> bool {
163 matches!(
164 self.by_id.get(id),
165 Some(EqualityInfo::Method { private_to_module, .. })
166 if visible_from(private_to_module, current_module)
167 )
168 }
169
170 pub fn unusable_reason_from(
171 &self,
172 id: &str,
173 current_module: &str,
174 ) -> Option<EqualityUnusableReason> {
175 match self.by_id.get(id) {
176 Some(EqualityInfo::Unusable {
177 reason,
178 private_to_module,
179 }) if visible_from(private_to_module, current_module) => Some(*reason),
180 _ => None,
181 }
182 }
183
184 pub fn is_synthesized(&self, id: &str) -> bool {
185 matches!(
186 self.by_id.get(id),
187 Some(EqualityInfo::Method {
188 synthesized: true,
189 ..
190 })
191 )
192 }
193}
194
195#[derive(Debug, Clone, Default)]
196pub struct MutationInfo {
197 bindings: HashSet<AstBindingId>,
198}
199
200impl MutationInfo {
201 pub fn mark_binding_mutated(&mut self, id: AstBindingId) {
202 self.bindings.insert(id);
203 }
204
205 pub fn is_mutated(&self, id: AstBindingId) -> bool {
206 self.bindings.contains(&id)
207 }
208}
209
210pub struct EmitInput {
211 pub files: HashMap<u32, File>,
212 pub definitions: HashMap<Symbol, Definition>,
213 pub modules: HashMap<String, ModuleInfo>,
214 pub entry_module_id: String,
215 pub unused: UnusedInfo,
216 pub mutations: MutationInfo,
217 pub cached_modules: HashSet<String>,
218 pub ufcs_methods: HashSet<(String, String)>,
219 pub equality_index: EqualityIndex,
220 pub test_index: TestIndex,
221 pub go_package_names: HashMap<String, String>,
222 pub go_module_ids: HashSet<String>,
223}
224
225#[cfg(test)]
226mod tests {
227 use super::*;
228
229 fn span(offset: u32) -> Span {
230 Span::new(0, offset, 1)
231 }
232
233 #[test]
234 fn merge_extends_bindings_definitions_and_imports() {
235 let mut a = UnusedInfo::default();
236 a.mark_binding_unused(span(0));
237 a.mark_definition_unused(span(1));
238 a.imports_by_module
239 .insert("m1".into(), HashSet::from_iter(["x".into()]));
240
241 let mut b = UnusedInfo::default();
242 b.mark_binding_unused(span(2));
243 b.mark_definition_unused(span(3));
244 b.imports_by_module
245 .insert("m1".into(), HashSet::from_iter(["y".into()]));
246 b.imports_by_module
247 .insert("m2".into(), HashSet::from_iter(["z".into()]));
248
249 a.merge(b);
250
251 assert_eq!(a.bindings.len(), 2);
252 assert_eq!(a.definitions.len(), 2);
253 assert_eq!(a.imports_by_module["m1"].len(), 2);
254 assert_eq!(a.imports_by_module["m2"].len(), 1);
255 }
256}