1use rustc_hash::{FxHashMap, FxHashSet};
2
3use crate::ast::{BindingId as AstBindingId, Span};
4use crate::types::Symbol;
5
6use super::{Definition, EqualityRecipe, File, FileImport, GoMethodPolicy};
7
8#[derive(Debug, Clone, Default)]
10pub struct UnusedImports {
11 keys: FxHashSet<ImportKey>,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub struct ImportKey(Span);
16
17impl FileImport {
18 pub fn key(&self) -> ImportKey {
19 ImportKey(self.name_span)
20 }
21}
22
23impl UnusedImports {
24 pub fn mark_unused_import(&mut self, import: &FileImport) {
25 self.keys.insert(import.key());
26 }
27
28 pub fn is_unused_import(&self, key: ImportKey) -> bool {
29 self.keys.contains(&key)
30 }
31
32 pub fn merge(&mut self, other: UnusedImports) {
33 self.keys.extend(other.keys);
34 }
35}
36
37#[derive(Debug, Clone, Default)]
38pub struct EmitBindings {
39 mutations: FxHashMap<AstBindingId, BindingMutation>,
40 hidden: FxHashMap<AstBindingId, AstBindingId>,
41}
42
43impl EmitBindings {
44 pub fn record(
45 &mut self,
46 id: AstBindingId,
47 mutation: Option<BindingMutation>,
48 hides: Option<AstBindingId>,
49 ) {
50 if let Some(mutation) = mutation {
51 self.mutations.insert(id, mutation);
52 }
53 if let Some(hidden) = hides {
54 self.hidden.insert(id, hidden);
55 }
56 }
57
58 pub fn hidden_binding(&self, id: AstBindingId) -> Option<AstBindingId> {
59 self.hidden.get(&id).copied()
60 }
61
62 pub fn is_mutated(&self, id: AstBindingId) -> bool {
63 self.mutations
64 .get(&id)
65 .is_some_and(|mutation| *mutation != BindingMutation::ThroughReference)
66 }
67
68 pub fn is_alias_mutated(&self, id: AstBindingId) -> bool {
69 self.mutations.get(&id) == Some(&BindingMutation::ThroughAlias)
70 }
71}
72
73#[derive(Debug, Clone)]
74pub struct TestFunction {
75 qualified_name: Symbol,
76 pub title: Option<String>,
77 pub doc: Option<String>,
78 pub span: Span,
79}
80
81impl TestFunction {
82 pub fn new(
83 package_id: &str,
84 name: &str,
85 title: Option<String>,
86 doc: Option<String>,
87 span: Span,
88 ) -> Self {
89 Self {
90 qualified_name: Symbol::from_parts(package_id, name),
91 title,
92 doc,
93 span,
94 }
95 }
96
97 pub fn package_id(&self) -> &str {
98 self.qualified_name
99 .without_last_segment()
100 .expect("test names are constructed with a package")
101 }
102
103 pub fn qualified_name(&self) -> &str {
104 self.qualified_name.as_str()
105 }
106
107 pub fn name(&self) -> &str {
108 self.qualified_name.last_segment()
109 }
110}
111
112#[derive(Debug, Clone, Default)]
113pub struct TestIndex {
114 tests: Vec<TestFunction>,
115}
116
117impl TestIndex {
118 pub fn push(&mut self, test: TestFunction) {
119 self.tests.push(test);
120 }
121
122 pub fn tests(&self) -> &[TestFunction] {
123 &self.tests
124 }
125
126 pub fn contains_qualified(&self, qualified_name: &str) -> bool {
127 self.tests
128 .iter()
129 .any(|test| test.qualified_name == qualified_name)
130 }
131}
132
133#[derive(Debug, Clone, Default)]
134pub struct EqualityIndex {
135 by_id: FxHashMap<String, EqualityInfo>,
136 synthesized_fields: FxHashMap<String, Vec<EqualityRecipe>>,
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
140enum EqualityKind {
141 DeclaredMethod,
142 SynthesizedMethod,
143 UfcsLowered,
144}
145
146#[derive(Debug, Clone)]
147struct EqualityInfo {
148 kind: EqualityKind,
149 private_to_package: Option<String>,
150}
151
152fn visible_from(private_to_package: &Option<String>, current_package: &str) -> bool {
153 match private_to_package {
154 None => true,
155 Some(package) => package == current_package,
156 }
157}
158
159impl EqualityIndex {
160 pub fn insert_declared_method(&mut self, id: String, private_to_package: Option<String>) {
161 self.by_id.insert(
162 id,
163 EqualityInfo {
164 kind: EqualityKind::DeclaredMethod,
165 private_to_package,
166 },
167 );
168 }
169
170 pub fn insert_synthesized_method(&mut self, id: String, private_to_package: Option<String>) {
171 self.by_id.insert(
172 id,
173 EqualityInfo {
174 kind: EqualityKind::SynthesizedMethod,
175 private_to_package,
176 },
177 );
178 }
179
180 pub fn insert_ufcs_lowered(&mut self, id: String, private_to_package: Option<String>) {
181 self.by_id.insert(
182 id,
183 EqualityInfo {
184 kind: EqualityKind::UfcsLowered,
185 private_to_package,
186 },
187 );
188 }
189
190 pub fn usable_from(&self, id: &str, current_package: &str) -> bool {
191 matches!(
192 self.by_id.get(id),
193 Some(EqualityInfo {
194 kind: EqualityKind::DeclaredMethod | EqualityKind::SynthesizedMethod,
195 private_to_package,
196 }) if visible_from(private_to_package, current_package)
197 )
198 }
199
200 pub fn is_ufcs_lowered_from(&self, id: &str, current_package: &str) -> bool {
201 matches!(
202 self.by_id.get(id),
203 Some(EqualityInfo {
204 kind: EqualityKind::UfcsLowered,
205 private_to_package,
206 }) if visible_from(private_to_package, current_package)
207 )
208 }
209
210 pub fn insert_synthesized_fields(&mut self, id: String, fields: Vec<EqualityRecipe>) {
212 self.synthesized_fields.insert(id, fields);
213 }
214
215 pub fn synthesized_fields(&self, id: &str) -> &[EqualityRecipe] {
217 self.synthesized_fields
218 .get(id)
219 .map(Vec::as_slice)
220 .unwrap_or_default()
221 }
222
223 pub fn is_synthesized(&self, id: &str) -> bool {
224 matches!(
225 self.by_id.get(id),
226 Some(EqualityInfo {
227 kind: EqualityKind::SynthesizedMethod,
228 ..
229 })
230 )
231 }
232}
233
234#[derive(Debug, Clone, Copy, PartialEq, Eq)]
235pub enum BindingMutation {
236 ThroughReference,
237 Direct,
238 ThroughAlias,
239}
240
241impl BindingMutation {
242 pub fn merged_with(self, other: Self) -> Self {
243 match (self, other) {
244 (Self::ThroughAlias, _) | (_, Self::ThroughAlias) => Self::ThroughAlias,
245 (Self::Direct, _) | (_, Self::Direct) => Self::Direct,
246 (Self::ThroughReference, Self::ThroughReference) => Self::ThroughReference,
247 }
248 }
249}
250
251#[derive(Default)]
252pub struct EmitInput {
253 pub checked_bodies: super::CheckedBodies,
254 pub files: FxHashMap<u32, File>,
255 pub definitions: FxHashMap<Symbol, Definition>,
256 pub entry_package_id: String,
257 pub unused_imports: UnusedImports,
258 pub bindings: EmitBindings,
259 pub cached_packages: FxHashSet<String>,
260 pub equality_index: EqualityIndex,
261 pub test_index: TestIndex,
262 pub go_package_names: FxHashMap<String, String>,
263 pub go_package_ids: FxHashSet<String>,
264 pub files_with_unspellable_types: FxHashSet<u32>,
265 pub pattern_positions: super::PatternPositions,
266 pub go_method_policy: GoMethodPolicy,
267}
268
269#[cfg(test)]
270mod tests {
271 use super::*;
272
273 fn span(offset: u32) -> Span {
274 Span::new(0, offset, 1)
275 }
276
277 fn import(offset: u32) -> FileImport {
278 FileImport {
279 name: "go:fmt".into(),
280 name_span: span(offset),
281 alias: None,
282 span: span(offset),
283 }
284 }
285
286 #[test]
287 fn merge_extends_imports() {
288 let mut a = UnusedImports::default();
289 a.mark_unused_import(&import(1));
290
291 let mut b = UnusedImports::default();
292 b.mark_unused_import(&import(3));
293
294 a.merge(b);
295
296 assert!(a.is_unused_import(import(1).key()));
297 assert!(a.is_unused_import(import(3).key()));
298 assert!(!a.is_unused_import(import(2).key()));
299 }
300}