rubydex 0.4.1-aarch64-linux → 0.4.3-aarch64-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +11 -0
- data/THIRD_PARTY_LICENSES.html +14 -427
- data/ext/rubydex/extconf.rb +1 -0
- data/lib/rubydex/cli/command/dead_code.rb +59 -0
- data/lib/rubydex/librubydex_sys.so +0 -0
- data/lib/rubydex/mcp_server/tools/codebase_stats_tool.rb +1 -1
- data/lib/rubydex/mcp_server/tools/find_constant_references_tool.rb +4 -4
- data/lib/rubydex/mcp_server/tools/find_dead_code_candidates_tool.rb +33 -0
- data/lib/rubydex/mcp_server/tools/get_declaration_tool.rb +2 -2
- data/lib/rubydex/mcp_server/tools/get_descendants_tool.rb +4 -4
- data/lib/rubydex/mcp_server/tools/get_file_declarations_tool.rb +2 -2
- data/lib/rubydex/mcp_server/tools/search_declarations_tool.rb +6 -6
- data/lib/rubydex/mcp_server.rb +24 -12
- data/lib/rubydex/version.rb +1 -1
- data/rust/Cargo.lock +17 -11
- data/rust/Cargo.toml +3 -0
- data/rust/rubydex/Cargo.toml +2 -2
- data/rust/rubydex/src/compile_assertions.rs +2 -0
- data/rust/rubydex/src/config.rs +1 -1
- data/rust/rubydex/src/indexing/rbs_indexer.rs +4 -4
- data/rust/rubydex/src/indexing/ruby_indexer.rs +26 -18
- data/rust/rubydex/src/indexing/ruby_indexer_tests.rs +42 -2
- data/rust/rubydex/src/indexing.rs +6 -6
- data/rust/rubydex/src/listing.rs +12 -12
- data/rust/rubydex/src/model/built_in.rs +1 -1
- data/rust/rubydex/src/model/comment.rs +4 -4
- data/rust/rubydex/src/model/declaration.rs +25 -17
- data/rust/rubydex/src/model/document.rs +11 -11
- data/rust/rubydex/src/model/graph.rs +3 -3
- data/rust/rubydex/src/offset.rs +1 -13
- data/rust/rubydex/src/operation/ruby_builder.rs +29 -19
- data/rust/rubydex/src/query.rs +3 -2
- data/rust/rubydex/src/test_utils/graph_test.rs +2 -2
- data/rust/rubydex/src/test_utils/local_graph_test.rs +13 -6
- data/rust/rubydex-sys/Cargo.toml +6 -3
- data/rust/rubydex-sys/build.rs +1 -2
- data/rust/rubydex-sys/src/definition_api.rs +1 -1
- data/rust/rubydex-sys/src/graph_api.rs +96 -49
- data/rust/rubydex-sys/src/name_api.rs +193 -58
- metadata +199 -198
|
@@ -173,11 +173,11 @@ pub unsafe extern "C" fn rdx_graph_resolve_constant(
|
|
|
173
173
|
let nesting: Vec<String> = unsafe { utils::convert_double_pointer_to_vec(nesting, count).unwrap() };
|
|
174
174
|
let const_name: String = unsafe { utils::convert_char_ptr_to_string(const_name).unwrap() };
|
|
175
175
|
|
|
176
|
-
let Some(
|
|
176
|
+
let Some(tracked_name) = name_api::name_from_nesting_names(graph, &const_name, nesting) else {
|
|
177
177
|
return ptr::null();
|
|
178
178
|
};
|
|
179
179
|
|
|
180
|
-
resolve_constant_name(
|
|
180
|
+
resolve_constant_name(tracked_name)
|
|
181
181
|
})
|
|
182
182
|
}
|
|
183
183
|
|
|
@@ -225,15 +225,14 @@ pub unsafe extern "C" fn rdx_graph_resolve_constant_from_definition(
|
|
|
225
225
|
}
|
|
226
226
|
};
|
|
227
227
|
|
|
228
|
-
let Some(
|
|
229
|
-
else {
|
|
228
|
+
let Some(tracked_name) = name_api::name_from_lexical_nesting(graph, &const_name, Some(nesting)) else {
|
|
230
229
|
return CDefinitionConstantResolutionResult {
|
|
231
230
|
status: CDefinitionConstantResolution::NotFound,
|
|
232
231
|
declaration: ptr::null(),
|
|
233
232
|
};
|
|
234
233
|
};
|
|
235
234
|
|
|
236
|
-
let declaration = resolve_constant_name(
|
|
235
|
+
let declaration = resolve_constant_name(tracked_name);
|
|
237
236
|
let status = if declaration.is_null() {
|
|
238
237
|
CDefinitionConstantResolution::NotFound
|
|
239
238
|
} else {
|
|
@@ -260,18 +259,13 @@ fn class_or_module_definition_name_id(
|
|
|
260
259
|
}
|
|
261
260
|
}
|
|
262
261
|
|
|
263
|
-
fn resolve_constant_name(
|
|
264
|
-
let
|
|
265
|
-
let
|
|
266
|
-
|
|
262
|
+
fn resolve_constant_name(mut tracked_name: name_api::TrackedName<'_>) -> *const CDeclaration {
|
|
263
|
+
let name_id = tracked_name.name_id();
|
|
264
|
+
let declaration_id = Resolver::new(tracked_name.graph_mut()).resolve_constant(name_id);
|
|
265
|
+
declaration_id.map_or(ptr::null(), |id| {
|
|
266
|
+
let declaration = tracked_name.graph().declarations().get(&id).unwrap();
|
|
267
267
|
Box::into_raw(Box::new(CDeclaration::from_declaration(id, declaration))).cast_const()
|
|
268
|
-
})
|
|
269
|
-
|
|
270
|
-
for name_id in names_to_untrack {
|
|
271
|
-
graph.untrack_name(name_id);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
declaration
|
|
268
|
+
})
|
|
275
269
|
}
|
|
276
270
|
|
|
277
271
|
/// Adds glob patterns to exclude from file discovery during indexing.
|
|
@@ -745,7 +739,7 @@ pub unsafe extern "C" fn rdx_index_source(
|
|
|
745
739
|
};
|
|
746
740
|
|
|
747
741
|
with_mut_graph(pointer, |graph| {
|
|
748
|
-
indexing::index_source(graph,
|
|
742
|
+
indexing::index_source(graph, uri_str.into(), source_str, &language);
|
|
749
743
|
IndexSourceResult::Success
|
|
750
744
|
})
|
|
751
745
|
}
|
|
@@ -815,19 +809,19 @@ pub unsafe extern "C" fn rdx_completion_candidates_free(ptr: *mut CompletionCand
|
|
|
815
809
|
}
|
|
816
810
|
}
|
|
817
811
|
|
|
818
|
-
/// Converts the nesting
|
|
819
|
-
/// The last
|
|
812
|
+
/// Converts the nesting names into a tracked name.
|
|
813
|
+
/// The last nesting name is treated as the self type; if the array is empty, `"Object"` is used.
|
|
820
814
|
///
|
|
821
|
-
/// Returns `
|
|
815
|
+
/// Returns `None` if the nesting array contains invalid UTF-8.
|
|
822
816
|
///
|
|
823
817
|
/// # Safety
|
|
824
818
|
///
|
|
825
819
|
/// `nesting` must point to `nesting_count` valid, null-terminated UTF-8 strings.
|
|
826
|
-
unsafe fn
|
|
820
|
+
unsafe fn completion_nesting_name(
|
|
827
821
|
graph: &mut Graph,
|
|
828
822
|
nesting: *const *const c_char,
|
|
829
823
|
nesting_count: usize,
|
|
830
|
-
) -> Option<
|
|
824
|
+
) -> Option<name_api::TrackedName<'_>> {
|
|
831
825
|
let mut nesting: Vec<String> = unsafe { utils::convert_double_pointer_to_vec(nesting, nesting_count).ok()? };
|
|
832
826
|
|
|
833
827
|
// When serving completion in a bare script, the self (top level) context is Object
|
|
@@ -837,7 +831,7 @@ unsafe fn completion_nesting_name_id(
|
|
|
837
831
|
nesting.pop().unwrap()
|
|
838
832
|
};
|
|
839
833
|
|
|
840
|
-
name_api::
|
|
834
|
+
name_api::name_from_nesting_names(graph, &self_name, nesting)
|
|
841
835
|
}
|
|
842
836
|
|
|
843
837
|
/// The result of a completion operation, carrying either a candidate array or an error message.
|
|
@@ -866,19 +860,10 @@ impl CompletionResult {
|
|
|
866
860
|
}
|
|
867
861
|
|
|
868
862
|
/// Runs completion for the given receiver and returns a structured result with candidates or an error message
|
|
869
|
-
fn run_and_finalize_completion(
|
|
870
|
-
graph: &mut Graph,
|
|
871
|
-
receiver: CompletionReceiver,
|
|
872
|
-
names_to_untrack: Vec<NameId>,
|
|
873
|
-
) -> CompletionResult {
|
|
863
|
+
fn run_and_finalize_completion(graph: &Graph, receiver: CompletionReceiver) -> CompletionResult {
|
|
874
864
|
let candidates = match query::completion_candidates(graph, CompletionContext::new(receiver)) {
|
|
875
865
|
Ok(candidates) => candidates,
|
|
876
|
-
Err(e) =>
|
|
877
|
-
for name_id in names_to_untrack {
|
|
878
|
-
graph.untrack_name(name_id);
|
|
879
|
-
}
|
|
880
|
-
return CompletionResult::error(&e.to_string());
|
|
881
|
-
}
|
|
866
|
+
Err(e) => return CompletionResult::error(&e.to_string()),
|
|
882
867
|
};
|
|
883
868
|
|
|
884
869
|
let entries: Vec<CCompletionCandidate> = candidates
|
|
@@ -926,10 +911,6 @@ fn run_and_finalize_completion(
|
|
|
926
911
|
})
|
|
927
912
|
.collect();
|
|
928
913
|
|
|
929
|
-
for name_id in names_to_untrack {
|
|
930
|
-
graph.untrack_name(name_id);
|
|
931
|
-
}
|
|
932
|
-
|
|
933
914
|
CompletionResult::success(CompletionCandidateArray::from_vec(entries))
|
|
934
915
|
}
|
|
935
916
|
|
|
@@ -950,20 +931,19 @@ pub unsafe extern "C" fn rdx_graph_complete_expression(
|
|
|
950
931
|
self_receiver: *const c_char,
|
|
951
932
|
) -> CompletionResult {
|
|
952
933
|
with_mut_graph(pointer, |graph| {
|
|
953
|
-
let Some(
|
|
954
|
-
else {
|
|
934
|
+
let Some(tracked_name) = (unsafe { completion_nesting_name(graph, nesting, nesting_count) }) else {
|
|
955
935
|
return CompletionResult::success(ptr::null_mut());
|
|
956
936
|
};
|
|
937
|
+
let name_id = tracked_name.name_id();
|
|
957
938
|
|
|
958
939
|
let self_decl_id = unsafe { decl_id_from_char_ptr(self_receiver) };
|
|
959
940
|
|
|
960
941
|
run_and_finalize_completion(
|
|
961
|
-
graph,
|
|
942
|
+
tracked_name.graph(),
|
|
962
943
|
CompletionReceiver::Expression {
|
|
963
944
|
self_decl_id,
|
|
964
945
|
nesting_name_id: name_id,
|
|
965
946
|
},
|
|
966
|
-
names_to_untrack,
|
|
967
947
|
)
|
|
968
948
|
})
|
|
969
949
|
}
|
|
@@ -997,7 +977,6 @@ pub unsafe extern "C" fn rdx_graph_complete_namespace_access(
|
|
|
997
977
|
self_decl_id,
|
|
998
978
|
namespace_decl_id: declaration_id_from_lookup_name(&name_str),
|
|
999
979
|
},
|
|
1000
|
-
Vec::new(),
|
|
1001
980
|
)
|
|
1002
981
|
})
|
|
1003
982
|
}
|
|
@@ -1031,7 +1010,6 @@ pub unsafe extern "C" fn rdx_graph_complete_method_call(
|
|
|
1031
1010
|
self_decl_id,
|
|
1032
1011
|
receiver_decl_id: declaration_id_from_lookup_name(&name_str),
|
|
1033
1012
|
},
|
|
1034
|
-
Vec::new(),
|
|
1035
1013
|
)
|
|
1036
1014
|
})
|
|
1037
1015
|
}
|
|
@@ -1060,22 +1038,20 @@ pub unsafe extern "C" fn rdx_graph_complete_method_argument(
|
|
|
1060
1038
|
};
|
|
1061
1039
|
|
|
1062
1040
|
with_mut_graph(pointer, |graph| {
|
|
1063
|
-
let Some((
|
|
1064
|
-
(unsafe { completion_nesting_name_id(graph, nesting, nesting_count) })
|
|
1065
|
-
else {
|
|
1041
|
+
let Some(tracked_name) = (unsafe { completion_nesting_name(graph, nesting, nesting_count) }) else {
|
|
1066
1042
|
return CompletionResult::success(ptr::null_mut());
|
|
1067
1043
|
};
|
|
1044
|
+
let nesting_name_id = tracked_name.name_id();
|
|
1068
1045
|
|
|
1069
1046
|
let self_decl_id = unsafe { decl_id_from_char_ptr(self_receiver) };
|
|
1070
1047
|
|
|
1071
1048
|
run_and_finalize_completion(
|
|
1072
|
-
graph,
|
|
1049
|
+
tracked_name.graph(),
|
|
1073
1050
|
CompletionReceiver::MethodArgument {
|
|
1074
1051
|
self_decl_id,
|
|
1075
1052
|
nesting_name_id,
|
|
1076
1053
|
method_decl_id: declaration_id_from_lookup_name(&name_str),
|
|
1077
1054
|
},
|
|
1078
|
-
names_to_untrack,
|
|
1079
1055
|
)
|
|
1080
1056
|
})
|
|
1081
1057
|
}
|
|
@@ -1196,6 +1172,7 @@ pub unsafe extern "C" fn rdx_keyword_free(ptr: *const CKeyword) {
|
|
|
1196
1172
|
|
|
1197
1173
|
#[cfg(test)]
|
|
1198
1174
|
mod tests {
|
|
1175
|
+
use crate::declaration_api;
|
|
1199
1176
|
use rubydex::indexing::ruby_indexer::RubyIndexer;
|
|
1200
1177
|
|
|
1201
1178
|
use super::*;
|
|
@@ -1246,7 +1223,9 @@ mod tests {
|
|
|
1246
1223
|
nesting_ptrs.as_ptr(),
|
|
1247
1224
|
nesting_ptrs.len(),
|
|
1248
1225
|
);
|
|
1226
|
+
assert!(!decl.is_null());
|
|
1249
1227
|
assert_eq!((*decl).id(), *DeclarationId::from("Foo::BAR"));
|
|
1228
|
+
declaration_api::free_c_declaration(decl);
|
|
1250
1229
|
};
|
|
1251
1230
|
|
|
1252
1231
|
let graph = unsafe { Box::from_raw(graph_ptr.cast::<RwLock<Graph>>()) };
|
|
@@ -1268,4 +1247,72 @@ mod tests {
|
|
|
1268
1247
|
.ref_count()
|
|
1269
1248
|
);
|
|
1270
1249
|
}
|
|
1250
|
+
|
|
1251
|
+
#[test]
|
|
1252
|
+
fn names_are_untracked_after_completion_error() {
|
|
1253
|
+
let mut indexer = RubyIndexer::new(
|
|
1254
|
+
"file:///foo.rb".into(),
|
|
1255
|
+
"
|
|
1256
|
+
class Foo
|
|
1257
|
+
end
|
|
1258
|
+
",
|
|
1259
|
+
);
|
|
1260
|
+
indexer.index();
|
|
1261
|
+
|
|
1262
|
+
let mut graph = Graph::new();
|
|
1263
|
+
graph.consume_document_changes(indexer.local_graph());
|
|
1264
|
+
let mut resolver = Resolver::new(&mut graph);
|
|
1265
|
+
resolver.resolve();
|
|
1266
|
+
|
|
1267
|
+
let (foo_name_id, foo_string_id, name_ref_count, string_ref_count) = graph
|
|
1268
|
+
.names()
|
|
1269
|
+
.iter()
|
|
1270
|
+
.find_map(|(name_id, name)| {
|
|
1271
|
+
let string_id = *name.str();
|
|
1272
|
+
if graph.strings().get(&string_id).unwrap().as_str() == "Foo"
|
|
1273
|
+
&& name.parent_scope().is_none()
|
|
1274
|
+
&& name.nesting().is_none()
|
|
1275
|
+
{
|
|
1276
|
+
Some((
|
|
1277
|
+
*name_id,
|
|
1278
|
+
string_id,
|
|
1279
|
+
name.ref_count(),
|
|
1280
|
+
graph.strings().get(&string_id).unwrap().ref_count(),
|
|
1281
|
+
))
|
|
1282
|
+
} else {
|
|
1283
|
+
None
|
|
1284
|
+
}
|
|
1285
|
+
})
|
|
1286
|
+
.unwrap();
|
|
1287
|
+
let name_count = graph.names().len();
|
|
1288
|
+
let string_count = graph.strings().len();
|
|
1289
|
+
|
|
1290
|
+
let graph_ptr = Box::into_raw(Box::new(RwLock::new(graph))) as GraphPointer;
|
|
1291
|
+
let nesting_strings = [CString::new("Foo").unwrap()];
|
|
1292
|
+
let nesting_ptrs: Vec<*const c_char> = nesting_strings.iter().map(|s| s.as_ptr()).collect();
|
|
1293
|
+
let self_receiver = CString::new("Nonexistent").unwrap();
|
|
1294
|
+
|
|
1295
|
+
let result = unsafe {
|
|
1296
|
+
rdx_graph_complete_expression(
|
|
1297
|
+
graph_ptr,
|
|
1298
|
+
nesting_ptrs.as_ptr(),
|
|
1299
|
+
nesting_ptrs.len(),
|
|
1300
|
+
self_receiver.as_ptr(),
|
|
1301
|
+
)
|
|
1302
|
+
};
|
|
1303
|
+
assert!(result.candidates.is_null());
|
|
1304
|
+
assert!(!result.error.is_null());
|
|
1305
|
+
utils::free_c_string(result.error);
|
|
1306
|
+
|
|
1307
|
+
let graph = unsafe { Box::from_raw(graph_ptr.cast::<RwLock<Graph>>()) };
|
|
1308
|
+
let graph = graph.read().unwrap();
|
|
1309
|
+
|
|
1310
|
+
assert_eq!(name_count, graph.names().len());
|
|
1311
|
+
assert_eq!(string_count, graph.strings().len());
|
|
1312
|
+
assert_eq!(name_ref_count, graph.names().get(&foo_name_id).unwrap().ref_count());
|
|
1313
|
+
assert_eq!(
|
|
1314
|
+
string_ref_count,
|
|
1315
|
+
graph.strings().get(&foo_string_id).unwrap().ref_count()
|
|
1316
|
+
);
|
|
1317
|
+
}
|
|
1271
1318
|
}
|
|
@@ -1,63 +1,105 @@
|
|
|
1
1
|
use rubydex::model::{graph::Graph, ids::NameId, name::ParentScope};
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
struct NameTracker<'graph> {
|
|
4
|
+
graph: &'graph mut Graph,
|
|
5
|
+
tracked_name_ids: Vec<NameId>,
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
impl<'graph> NameTracker<'graph> {
|
|
9
|
+
fn new(graph: &'graph mut Graph) -> Self {
|
|
10
|
+
Self {
|
|
11
|
+
graph,
|
|
12
|
+
tracked_name_ids: Vec::new(),
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
fn track_name(&mut self, string: &str, parent_scope: ParentScope, nesting: Option<NameId>) -> NameId {
|
|
17
|
+
let str_id = self.graph.intern_string(string.to_owned());
|
|
18
|
+
let name_id = self.graph.add_name(str_id, parent_scope, nesting);
|
|
19
|
+
self.tracked_name_ids.push(name_id);
|
|
20
|
+
name_id
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
impl Drop for NameTracker<'_> {
|
|
25
|
+
fn drop(&mut self) {
|
|
26
|
+
for name_id in self.tracked_name_ids.drain(..) {
|
|
27
|
+
self.graph.untrack_name(name_id);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/// A name temporarily tracked in a graph. Dropping this guard undoes every name and string reference-count increment
|
|
33
|
+
/// made while building the name.
|
|
34
|
+
pub struct TrackedName<'graph> {
|
|
35
|
+
tracker: NameTracker<'graph>,
|
|
36
|
+
name_id: NameId,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
impl<'graph> TrackedName<'graph> {
|
|
40
|
+
fn new(tracker: NameTracker<'graph>, name_id: NameId) -> Self {
|
|
41
|
+
TrackedName { tracker, name_id }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#[must_use]
|
|
45
|
+
pub fn name_id(&self) -> NameId {
|
|
46
|
+
self.name_id
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
#[must_use]
|
|
50
|
+
pub fn graph(&self) -> &Graph {
|
|
51
|
+
&*self.tracker.graph
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
pub fn graph_mut(&mut self) -> &mut Graph {
|
|
55
|
+
&mut *self.tracker.graph
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/// Takes a constant name and nesting names (e.g.: `["Foo", "Bar::Baz", "Qux"]`) and transforms them into a `NameId`,
|
|
60
|
+
/// registering each required part in the graph. The names are untracked when the returned guard is dropped.
|
|
61
|
+
/// Returns `None` if the constant name contains no valid identifier parts (e.g.: `""`, `"::"`, `"Foo::"`).
|
|
62
|
+
pub fn name_from_nesting_names<'graph>(
|
|
63
|
+
graph: &'graph mut Graph,
|
|
8
64
|
const_name: &str,
|
|
9
65
|
nesting: Vec<String>,
|
|
10
|
-
) -> Option<
|
|
66
|
+
) -> Option<TrackedName<'graph>> {
|
|
11
67
|
let mut current_nesting = None;
|
|
12
68
|
let mut current_name = ParentScope::None;
|
|
13
|
-
let mut
|
|
69
|
+
let mut tracker = NameTracker::new(graph);
|
|
14
70
|
|
|
15
71
|
for entry in nesting {
|
|
16
|
-
process_qualified_name(
|
|
72
|
+
process_qualified_name(&mut tracker, &entry, current_nesting, &mut current_name);
|
|
17
73
|
current_nesting = current_name.as_ref().copied();
|
|
18
74
|
current_name = ParentScope::None;
|
|
19
75
|
}
|
|
20
76
|
|
|
21
|
-
process_qualified_name(
|
|
22
|
-
graph,
|
|
23
|
-
const_name,
|
|
24
|
-
current_nesting,
|
|
25
|
-
&mut current_name,
|
|
26
|
-
&mut names_to_untrack,
|
|
27
|
-
);
|
|
77
|
+
process_qualified_name(&mut tracker, const_name, current_nesting, &mut current_name);
|
|
28
78
|
|
|
29
79
|
let (ParentScope::Some(name_id) | ParentScope::Attached(name_id)) = current_name else {
|
|
30
|
-
// Invalid names may leave temporary parts in the graph (e.g. `Foo` from `Foo::`).
|
|
31
|
-
for name_id in names_to_untrack {
|
|
32
|
-
graph.untrack_name(name_id);
|
|
33
|
-
}
|
|
34
80
|
return None;
|
|
35
81
|
};
|
|
36
82
|
|
|
37
|
-
Some((
|
|
83
|
+
Some(TrackedName::new(tracker, name_id))
|
|
38
84
|
}
|
|
39
85
|
|
|
40
|
-
/// Takes a constant name and an existing lexical nesting and transforms
|
|
41
|
-
/// part in the graph.
|
|
42
|
-
pub fn
|
|
43
|
-
graph: &mut Graph,
|
|
86
|
+
/// Takes a constant name and an existing lexical nesting and transforms them into a `NameId`, registering each required
|
|
87
|
+
/// part in the graph. The names are untracked when the returned guard is dropped.
|
|
88
|
+
pub fn name_from_lexical_nesting<'graph>(
|
|
89
|
+
graph: &'graph mut Graph,
|
|
44
90
|
const_name: &str,
|
|
45
91
|
nesting: Option<NameId>,
|
|
46
|
-
) -> Option<
|
|
92
|
+
) -> Option<TrackedName<'graph>> {
|
|
47
93
|
let mut current_name = ParentScope::None;
|
|
48
|
-
let mut
|
|
94
|
+
let mut tracker = NameTracker::new(graph);
|
|
49
95
|
|
|
50
|
-
process_qualified_name(
|
|
96
|
+
process_qualified_name(&mut tracker, const_name, nesting, &mut current_name);
|
|
51
97
|
|
|
52
98
|
let (ParentScope::Some(name_id) | ParentScope::Attached(name_id)) = current_name else {
|
|
53
|
-
// Invalid names may leave temporary parts in the graph (e.g. `Foo` from `Foo::`).
|
|
54
|
-
for name_id in names_to_untrack {
|
|
55
|
-
graph.untrack_name(name_id);
|
|
56
|
-
}
|
|
57
99
|
return None;
|
|
58
100
|
};
|
|
59
101
|
|
|
60
|
-
Some((
|
|
102
|
+
Some(TrackedName::new(tracker, name_id))
|
|
61
103
|
}
|
|
62
104
|
|
|
63
105
|
/// Processes a qualified name (e.g., `"Foo::Bar"` or `"<Foo>"`) by splitting on `"::"` and registering each part in the
|
|
@@ -66,11 +108,10 @@ pub fn name_in_nesting_to_name_id(
|
|
|
66
108
|
/// When a singleton is the first part (i.e., `current_name` has no parent), `current_nesting` is used as the attachment
|
|
67
109
|
/// point.
|
|
68
110
|
fn process_qualified_name(
|
|
69
|
-
|
|
111
|
+
tracker: &mut NameTracker<'_>,
|
|
70
112
|
qualified_name: &str,
|
|
71
113
|
current_nesting: Option<NameId>,
|
|
72
114
|
current_name: &mut ParentScope,
|
|
73
|
-
names_to_untrack: &mut Vec<NameId>,
|
|
74
115
|
) {
|
|
75
116
|
for part in qualified_name.split("::") {
|
|
76
117
|
if part.is_empty() {
|
|
@@ -90,9 +131,7 @@ fn process_qualified_name(
|
|
|
90
131
|
(*current_name, current_nesting)
|
|
91
132
|
};
|
|
92
133
|
|
|
93
|
-
let
|
|
94
|
-
let name_id = graph.add_name(str_id, parent_scope, nesting_for_part);
|
|
95
|
-
names_to_untrack.push(name_id);
|
|
134
|
+
let name_id = tracker.track_name(part, parent_scope, nesting_for_part);
|
|
96
135
|
*current_name = ParentScope::Some(name_id);
|
|
97
136
|
}
|
|
98
137
|
}
|
|
@@ -107,38 +146,41 @@ mod tests {
|
|
|
107
146
|
fn nesting_is_converted_to_name_id() {
|
|
108
147
|
let mut graph = Graph::new();
|
|
109
148
|
|
|
110
|
-
let
|
|
149
|
+
let tracked_name = name_from_nesting_names(
|
|
111
150
|
&mut graph,
|
|
112
151
|
"Some::CONST",
|
|
113
152
|
vec!["Foo".into(), "Bar::Zip".into(), "Qux".into()],
|
|
114
153
|
)
|
|
115
154
|
.unwrap();
|
|
155
|
+
let name_id = tracked_name.name_id();
|
|
116
156
|
|
|
117
|
-
let const_name = graph.names().get(&name_id).unwrap();
|
|
157
|
+
let const_name = tracked_name.graph().names().get(&name_id).unwrap();
|
|
118
158
|
assert_eq!(StringId::from("CONST"), *const_name.str());
|
|
119
159
|
|
|
120
|
-
let some_name =
|
|
160
|
+
let some_name = tracked_name
|
|
161
|
+
.graph()
|
|
121
162
|
.names()
|
|
122
163
|
.get(&const_name.parent_scope().expect("Parent scope should exist"))
|
|
123
164
|
.unwrap();
|
|
124
165
|
assert_eq!(StringId::from("Some"), *some_name.str());
|
|
125
166
|
assert_eq!(const_name.nesting(), some_name.nesting());
|
|
126
167
|
|
|
127
|
-
let qux_name = graph.names().get(&some_name.nesting().unwrap()).unwrap();
|
|
168
|
+
let qux_name = tracked_name.graph().names().get(&some_name.nesting().unwrap()).unwrap();
|
|
128
169
|
assert_eq!(StringId::from("Qux"), *qux_name.str());
|
|
129
170
|
assert!(qux_name.parent_scope().is_none());
|
|
130
171
|
|
|
131
|
-
let zip_name = graph.names().get(&qux_name.nesting().unwrap()).unwrap();
|
|
172
|
+
let zip_name = tracked_name.graph().names().get(&qux_name.nesting().unwrap()).unwrap();
|
|
132
173
|
assert_eq!(StringId::from("Zip"), *zip_name.str());
|
|
133
174
|
|
|
134
|
-
let bar_name =
|
|
175
|
+
let bar_name = tracked_name
|
|
176
|
+
.graph()
|
|
135
177
|
.names()
|
|
136
178
|
.get(&zip_name.parent_scope().expect("Parent scope should exist"))
|
|
137
179
|
.unwrap();
|
|
138
180
|
assert_eq!(StringId::from("Bar"), *bar_name.str());
|
|
139
181
|
assert_eq!(zip_name.nesting(), bar_name.nesting());
|
|
140
182
|
|
|
141
|
-
let foo_name = graph.names().get(&bar_name.nesting().unwrap()).unwrap();
|
|
183
|
+
let foo_name = tracked_name.graph().names().get(&bar_name.nesting().unwrap()).unwrap();
|
|
142
184
|
assert_eq!(StringId::from("Foo"), *foo_name.str());
|
|
143
185
|
assert!(foo_name.parent_scope().is_none());
|
|
144
186
|
assert!(foo_name.nesting().is_none());
|
|
@@ -148,13 +190,18 @@ mod tests {
|
|
|
148
190
|
fn top_level_reference_is_converted_to_name_id() {
|
|
149
191
|
let mut graph = Graph::new();
|
|
150
192
|
|
|
151
|
-
let
|
|
193
|
+
let tracked_name = name_from_nesting_names(&mut graph, "::CONST", vec!["Foo".into()]).unwrap();
|
|
194
|
+
let name_id = tracked_name.name_id();
|
|
152
195
|
|
|
153
|
-
let const_name = graph.names().get(&name_id).unwrap();
|
|
196
|
+
let const_name = tracked_name.graph().names().get(&name_id).unwrap();
|
|
154
197
|
assert_eq!(StringId::from("CONST"), *const_name.str());
|
|
155
198
|
assert!(const_name.parent_scope().is_top_level());
|
|
156
199
|
|
|
157
|
-
let foo_name =
|
|
200
|
+
let foo_name = tracked_name
|
|
201
|
+
.graph()
|
|
202
|
+
.names()
|
|
203
|
+
.get(&const_name.nesting().unwrap())
|
|
204
|
+
.unwrap();
|
|
158
205
|
assert_eq!(StringId::from("Foo"), *foo_name.str());
|
|
159
206
|
assert!(foo_name.nesting().is_none());
|
|
160
207
|
assert!(foo_name.parent_scope().is_none());
|
|
@@ -164,13 +211,18 @@ mod tests {
|
|
|
164
211
|
fn singleton_class_names_use_attached_parent_scope() {
|
|
165
212
|
let mut graph = Graph::new();
|
|
166
213
|
|
|
167
|
-
let
|
|
214
|
+
let tracked_name = name_from_nesting_names(&mut graph, "CONST", vec!["Foo".into(), "<Foo>".into()]).unwrap();
|
|
215
|
+
let name_id = tracked_name.name_id();
|
|
168
216
|
|
|
169
|
-
let const_name = graph.names().get(&name_id).unwrap();
|
|
217
|
+
let const_name = tracked_name.graph().names().get(&name_id).unwrap();
|
|
170
218
|
assert_eq!(StringId::from("CONST"), *const_name.str());
|
|
171
219
|
|
|
172
220
|
// The nesting should be <Foo> with an Attached parent scope
|
|
173
|
-
let singleton_name =
|
|
221
|
+
let singleton_name = tracked_name
|
|
222
|
+
.graph()
|
|
223
|
+
.names()
|
|
224
|
+
.get(&const_name.nesting().unwrap())
|
|
225
|
+
.unwrap();
|
|
174
226
|
assert_eq!(StringId::from("<Foo>"), *singleton_name.str());
|
|
175
227
|
assert!(
|
|
176
228
|
matches!(singleton_name.parent_scope(), ParentScope::Attached(_)),
|
|
@@ -180,7 +232,7 @@ mod tests {
|
|
|
180
232
|
|
|
181
233
|
// The attached parent should be Foo
|
|
182
234
|
let foo_id = singleton_name.parent_scope().expect("Attached should have an id");
|
|
183
|
-
let foo_name = graph.names().get(&foo_id).unwrap();
|
|
235
|
+
let foo_name = tracked_name.graph().names().get(&foo_id).unwrap();
|
|
184
236
|
assert_eq!(StringId::from("Foo"), *foo_name.str());
|
|
185
237
|
}
|
|
186
238
|
|
|
@@ -188,33 +240,116 @@ mod tests {
|
|
|
188
240
|
fn top_level_nesting_is_converted_to_name_id() {
|
|
189
241
|
let mut graph = Graph::new();
|
|
190
242
|
|
|
191
|
-
let
|
|
243
|
+
let tracked_name = name_from_nesting_names(&mut graph, "CONST", vec!["Foo".into(), "::Bar".into()]).unwrap();
|
|
244
|
+
let name_id = tracked_name.name_id();
|
|
192
245
|
|
|
193
|
-
let const_name = graph.names().get(&name_id).unwrap();
|
|
246
|
+
let const_name = tracked_name.graph().names().get(&name_id).unwrap();
|
|
194
247
|
assert_eq!(StringId::from("CONST"), *const_name.str());
|
|
195
248
|
assert!(const_name.parent_scope().is_none());
|
|
196
249
|
|
|
197
|
-
let bar_name =
|
|
250
|
+
let bar_name = tracked_name
|
|
251
|
+
.graph()
|
|
252
|
+
.names()
|
|
253
|
+
.get(&const_name.nesting().unwrap())
|
|
254
|
+
.unwrap();
|
|
198
255
|
assert_eq!(StringId::from("Bar"), *bar_name.str());
|
|
199
256
|
assert!(bar_name.parent_scope().is_top_level());
|
|
200
257
|
|
|
201
|
-
let foo_name = graph.names().get(&bar_name.nesting().unwrap()).unwrap();
|
|
258
|
+
let foo_name = tracked_name.graph().names().get(&bar_name.nesting().unwrap()).unwrap();
|
|
202
259
|
assert_eq!(StringId::from("Foo"), *foo_name.str());
|
|
203
260
|
assert!(foo_name.parent_scope().is_none());
|
|
204
261
|
assert!(foo_name.nesting().is_none());
|
|
205
262
|
}
|
|
206
263
|
|
|
207
264
|
#[test]
|
|
208
|
-
fn
|
|
265
|
+
fn invalid_names_restore_name_and_string_counts() {
|
|
266
|
+
let mut graph = Graph::new();
|
|
267
|
+
let name_count = graph.names().len();
|
|
268
|
+
let string_count = graph.strings().len();
|
|
269
|
+
|
|
270
|
+
assert!(name_from_nesting_names(&mut graph, "", vec!["Foo".into()]).is_none());
|
|
271
|
+
assert_eq!(name_count, graph.names().len());
|
|
272
|
+
assert_eq!(string_count, graph.strings().len());
|
|
273
|
+
|
|
274
|
+
assert!(name_from_nesting_names(&mut graph, "Foo::", vec!["Bar".into()]).is_none());
|
|
275
|
+
assert_eq!(name_count, graph.names().len());
|
|
276
|
+
assert_eq!(string_count, graph.strings().len());
|
|
277
|
+
|
|
278
|
+
assert!(name_from_lexical_nesting(&mut graph, "Foo::", None).is_none());
|
|
279
|
+
assert_eq!(name_count, graph.names().len());
|
|
280
|
+
assert_eq!(string_count, graph.strings().len());
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
#[test]
|
|
284
|
+
fn invalid_name_restores_existing_name_reference_counts() {
|
|
285
|
+
let mut graph = Graph::new();
|
|
286
|
+
let string_id = graph.intern_string("Foo".to_owned());
|
|
287
|
+
let name_id = graph.add_name(string_id, ParentScope::None, None);
|
|
288
|
+
let name_ref_count = graph.names().get(&name_id).unwrap().ref_count();
|
|
289
|
+
let string_ref_count = graph.strings().get(&string_id).unwrap().ref_count();
|
|
290
|
+
|
|
291
|
+
assert!(name_from_nesting_names(&mut graph, "", vec!["Foo".into()]).is_none());
|
|
292
|
+
|
|
293
|
+
assert_eq!(name_ref_count, graph.names().get(&name_id).unwrap().ref_count());
|
|
294
|
+
assert_eq!(string_ref_count, graph.strings().get(&string_id).unwrap().ref_count());
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
#[test]
|
|
298
|
+
fn tracker_preserves_duplicate_name_ids() {
|
|
209
299
|
let mut graph = Graph::new();
|
|
210
300
|
let name_count = graph.names().len();
|
|
211
301
|
let string_count = graph.strings().len();
|
|
212
302
|
|
|
213
|
-
|
|
303
|
+
{
|
|
304
|
+
let mut tracker = NameTracker::new(&mut graph);
|
|
305
|
+
let first_id = tracker.track_name("Foo", ParentScope::None, None);
|
|
306
|
+
let second_id = tracker.track_name("Foo", ParentScope::None, None);
|
|
307
|
+
|
|
308
|
+
assert_eq!(first_id, second_id);
|
|
309
|
+
assert_eq!(2, tracker.graph.names().get(&first_id).unwrap().ref_count());
|
|
310
|
+
}
|
|
311
|
+
|
|
214
312
|
assert_eq!(name_count, graph.names().len());
|
|
215
313
|
assert_eq!(string_count, graph.strings().len());
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
#[test]
|
|
317
|
+
fn tracked_name_is_untracked_when_scope_ends() {
|
|
318
|
+
let mut graph = Graph::new();
|
|
319
|
+
let name_count = graph.names().len();
|
|
320
|
+
let string_count = graph.strings().len();
|
|
321
|
+
|
|
322
|
+
{
|
|
323
|
+
let tracked_name = name_from_nesting_names(&mut graph, "Bar::CONST", vec!["Foo".into()]).unwrap();
|
|
324
|
+
let name_id = tracked_name.name_id();
|
|
325
|
+
let const_name = tracked_name.graph().names().get(&name_id).unwrap();
|
|
326
|
+
|
|
327
|
+
assert_eq!(
|
|
328
|
+
"CONST",
|
|
329
|
+
tracked_name.graph().strings().get(const_name.str()).unwrap().as_str()
|
|
330
|
+
);
|
|
331
|
+
assert_eq!(name_count + 3, tracked_name.graph().names().len());
|
|
332
|
+
assert_eq!(string_count + 3, tracked_name.graph().strings().len());
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
assert_eq!(name_count, graph.names().len());
|
|
336
|
+
assert_eq!(string_count, graph.strings().len());
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
#[test]
|
|
340
|
+
fn tracked_name_is_untracked_during_unwind() {
|
|
341
|
+
let mut graph = Graph::new();
|
|
342
|
+
let name_count = graph.names().len();
|
|
343
|
+
let string_count = graph.strings().len();
|
|
344
|
+
|
|
345
|
+
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
|
346
|
+
let tracked_name = name_from_nesting_names(&mut graph, "Bar::CONST", vec!["Foo".into()]).unwrap();
|
|
347
|
+
|
|
348
|
+
assert_eq!(name_count + 3, tracked_name.graph().names().len());
|
|
349
|
+
panic!("unwind tracked name");
|
|
350
|
+
}));
|
|
216
351
|
|
|
217
|
-
assert!(
|
|
352
|
+
assert!(result.is_err());
|
|
218
353
|
assert_eq!(name_count, graph.names().len());
|
|
219
354
|
assert_eq!(string_count, graph.strings().len());
|
|
220
355
|
}
|