method-ray 0.1.2 → 0.1.4
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/CHANGELOG.md +41 -0
- data/README.md +27 -1
- data/ext/Cargo.toml +1 -1
- data/ext/src/lib.rs +7 -6
- data/lib/methodray/binary_locator.rb +29 -0
- data/lib/methodray/commands.rb +3 -20
- data/lib/methodray/version.rb +1 -1
- data/lib/methodray.rb +1 -1
- data/rust/Cargo.toml +3 -1
- data/rust/src/analyzer/attributes.rs +57 -0
- data/rust/src/analyzer/blocks.rs +175 -0
- data/rust/src/analyzer/calls.rs +7 -4
- data/rust/src/analyzer/conditionals.rs +466 -0
- data/rust/src/analyzer/definitions.rs +280 -13
- data/rust/src/analyzer/dispatch.rs +754 -11
- data/rust/src/analyzer/install.rs +58 -176
- data/rust/src/analyzer/literals.rs +201 -37
- data/rust/src/analyzer/mod.rs +4 -3
- data/rust/src/analyzer/parameters.rs +218 -0
- data/rust/src/analyzer/variables.rs +16 -8
- data/rust/src/cache/rbs_cache.rs +11 -4
- data/rust/src/checker.rs +20 -8
- data/rust/src/env/global_env.rs +42 -2
- data/rust/src/env/method_registry.rs +86 -4
- data/rust/src/env/mod.rs +1 -0
- data/rust/src/env/scope.rs +291 -25
- data/rust/src/graph/box.rs +478 -4
- data/rust/src/graph/change_set.rs +14 -0
- data/rust/src/graph/mod.rs +1 -1
- data/rust/src/lib.rs +2 -1
- data/rust/src/parser.rs +99 -39
- data/rust/src/rbs/converter.rs +16 -11
- data/rust/src/rbs/loader.rs +35 -5
- data/rust/src/rbs/mod.rs +4 -3
- data/rust/src/types.rs +344 -9
- metadata +6 -3
- data/rust/src/analyzer/tests/integration_test.rs +0 -136
- data/rust/src/analyzer/tests/mod.rs +0 -1
data/rust/src/env/scope.rs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
use crate::graph::VertexId;
|
|
2
2
|
use std::collections::HashMap;
|
|
3
3
|
|
|
4
|
-
///
|
|
4
|
+
/// Scope ID
|
|
5
5
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
6
6
|
pub struct ScopeId(pub usize);
|
|
7
7
|
|
|
8
|
-
///
|
|
8
|
+
/// Scope kind
|
|
9
9
|
#[derive(Debug, Clone)]
|
|
10
10
|
#[allow(dead_code)]
|
|
11
11
|
pub enum ScopeKind {
|
|
@@ -19,12 +19,12 @@ pub enum ScopeKind {
|
|
|
19
19
|
},
|
|
20
20
|
Method {
|
|
21
21
|
name: String,
|
|
22
|
-
receiver_type: Option<String>, //
|
|
22
|
+
receiver_type: Option<String>, // Receiver class/module name
|
|
23
23
|
},
|
|
24
24
|
Block,
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
///
|
|
27
|
+
/// Scope information
|
|
28
28
|
#[derive(Debug, Clone)]
|
|
29
29
|
#[allow(dead_code)]
|
|
30
30
|
pub struct Scope {
|
|
@@ -32,13 +32,13 @@ pub struct Scope {
|
|
|
32
32
|
pub kind: ScopeKind,
|
|
33
33
|
pub parent: Option<ScopeId>,
|
|
34
34
|
|
|
35
|
-
///
|
|
35
|
+
/// Local variables
|
|
36
36
|
pub local_vars: HashMap<String, VertexId>,
|
|
37
37
|
|
|
38
|
-
///
|
|
38
|
+
/// Instance variables (class/module scope only)
|
|
39
39
|
pub instance_vars: HashMap<String, VertexId>,
|
|
40
40
|
|
|
41
|
-
///
|
|
41
|
+
/// Class variables (class scope only)
|
|
42
42
|
pub class_vars: HashMap<String, VertexId>,
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -55,28 +55,28 @@ impl Scope {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
///
|
|
58
|
+
/// Add local variable
|
|
59
59
|
pub fn set_local_var(&mut self, name: String, vtx: VertexId) {
|
|
60
60
|
self.local_vars.insert(name, vtx);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
///
|
|
63
|
+
/// Get local variable
|
|
64
64
|
pub fn get_local_var(&self, name: &str) -> Option<VertexId> {
|
|
65
65
|
self.local_vars.get(name).copied()
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
///
|
|
68
|
+
/// Add instance variable
|
|
69
69
|
pub fn set_instance_var(&mut self, name: String, vtx: VertexId) {
|
|
70
70
|
self.instance_vars.insert(name, vtx);
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
///
|
|
73
|
+
/// Get instance variable
|
|
74
74
|
pub fn get_instance_var(&self, name: &str) -> Option<VertexId> {
|
|
75
75
|
self.instance_vars.get(name).copied()
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
///
|
|
79
|
+
/// Scope manager
|
|
80
80
|
#[derive(Debug)]
|
|
81
81
|
pub struct ScopeManager {
|
|
82
82
|
scopes: HashMap<ScopeId, Scope>,
|
|
@@ -99,7 +99,7 @@ impl ScopeManager {
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
///
|
|
102
|
+
/// Create a new scope
|
|
103
103
|
pub fn new_scope(&mut self, kind: ScopeKind) -> ScopeId {
|
|
104
104
|
let id = ScopeId(self.next_id);
|
|
105
105
|
self.next_id += 1;
|
|
@@ -110,12 +110,12 @@ impl ScopeManager {
|
|
|
110
110
|
id
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
///
|
|
113
|
+
/// Enter a scope
|
|
114
114
|
pub fn enter_scope(&mut self, scope_id: ScopeId) {
|
|
115
115
|
self.current_scope = scope_id;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
|
-
///
|
|
118
|
+
/// Exit current scope
|
|
119
119
|
pub fn exit_scope(&mut self) {
|
|
120
120
|
if let Some(scope) = self.scopes.get(&self.current_scope) {
|
|
121
121
|
if let Some(parent) = scope.parent {
|
|
@@ -124,27 +124,27 @@ impl ScopeManager {
|
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
///
|
|
127
|
+
/// Get current scope
|
|
128
128
|
pub fn current_scope(&self) -> &Scope {
|
|
129
129
|
self.scopes.get(&self.current_scope).unwrap()
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
///
|
|
132
|
+
/// Get current scope mutably
|
|
133
133
|
pub fn current_scope_mut(&mut self) -> &mut Scope {
|
|
134
134
|
self.scopes.get_mut(&self.current_scope).unwrap()
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
///
|
|
137
|
+
/// Get scope by ID
|
|
138
138
|
pub fn get_scope(&self, id: ScopeId) -> Option<&Scope> {
|
|
139
139
|
self.scopes.get(&id)
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
///
|
|
142
|
+
/// Get scope by ID mutably
|
|
143
143
|
pub fn get_scope_mut(&mut self, id: ScopeId) -> Option<&mut Scope> {
|
|
144
144
|
self.scopes.get_mut(&id)
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
///
|
|
147
|
+
/// Lookup variable in current scope or parent scopes
|
|
148
148
|
pub fn lookup_var(&self, name: &str) -> Option<VertexId> {
|
|
149
149
|
let mut current = Some(self.current_scope);
|
|
150
150
|
|
|
@@ -162,13 +162,13 @@ impl ScopeManager {
|
|
|
162
162
|
None
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
-
///
|
|
165
|
+
/// Lookup instance variable in enclosing class scope
|
|
166
166
|
pub fn lookup_instance_var(&self, name: &str) -> Option<VertexId> {
|
|
167
167
|
let mut current = Some(self.current_scope);
|
|
168
168
|
|
|
169
169
|
while let Some(scope_id) = current {
|
|
170
170
|
if let Some(scope) = self.scopes.get(&scope_id) {
|
|
171
|
-
//
|
|
171
|
+
// Walk up to class scope
|
|
172
172
|
match &scope.kind {
|
|
173
173
|
ScopeKind::Class { .. } => {
|
|
174
174
|
return scope.get_instance_var(name);
|
|
@@ -185,13 +185,13 @@ impl ScopeManager {
|
|
|
185
185
|
None
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
///
|
|
188
|
+
/// Set instance variable in enclosing class scope
|
|
189
189
|
pub fn set_instance_var_in_class(&mut self, name: String, vtx: VertexId) {
|
|
190
190
|
let mut current = Some(self.current_scope);
|
|
191
191
|
|
|
192
192
|
while let Some(scope_id) = current {
|
|
193
193
|
if let Some(scope) = self.scopes.get(&scope_id) {
|
|
194
|
-
//
|
|
194
|
+
// Find class scope and set variable
|
|
195
195
|
match &scope.kind {
|
|
196
196
|
ScopeKind::Class { .. } => {
|
|
197
197
|
if let Some(class_scope) = self.scopes.get_mut(&scope_id) {
|
|
@@ -209,7 +209,7 @@ impl ScopeManager {
|
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
-
///
|
|
212
|
+
/// Get current class name (simple name, not qualified)
|
|
213
213
|
pub fn current_class_name(&self) -> Option<String> {
|
|
214
214
|
let mut current = Some(self.current_scope);
|
|
215
215
|
|
|
@@ -226,6 +226,133 @@ impl ScopeManager {
|
|
|
226
226
|
|
|
227
227
|
None
|
|
228
228
|
}
|
|
229
|
+
|
|
230
|
+
/// Get current module name (simple name, not qualified)
|
|
231
|
+
pub fn current_module_name(&self) -> Option<String> {
|
|
232
|
+
let mut current = Some(self.current_scope);
|
|
233
|
+
|
|
234
|
+
while let Some(scope_id) = current {
|
|
235
|
+
if let Some(scope) = self.scopes.get(&scope_id) {
|
|
236
|
+
if let ScopeKind::Module { name } = &scope.kind {
|
|
237
|
+
return Some(name.clone());
|
|
238
|
+
}
|
|
239
|
+
current = scope.parent;
|
|
240
|
+
} else {
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
None
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/// Get current fully qualified name by traversing all parent class/module scopes
|
|
249
|
+
///
|
|
250
|
+
/// For example, in:
|
|
251
|
+
/// ```ruby
|
|
252
|
+
/// module Api
|
|
253
|
+
/// module V1
|
|
254
|
+
/// class User
|
|
255
|
+
/// def greet; end
|
|
256
|
+
/// end
|
|
257
|
+
/// end
|
|
258
|
+
/// end
|
|
259
|
+
/// ```
|
|
260
|
+
/// When inside `greet`, this returns `Some("Api::V1::User")`
|
|
261
|
+
pub fn current_qualified_name(&self) -> Option<String> {
|
|
262
|
+
let mut path_segments: Vec<String> = Vec::new();
|
|
263
|
+
let mut current = Some(self.current_scope);
|
|
264
|
+
|
|
265
|
+
// Traverse from current scope up to top-level, collecting class/module names
|
|
266
|
+
while let Some(scope_id) = current {
|
|
267
|
+
if let Some(scope) = self.scopes.get(&scope_id) {
|
|
268
|
+
match &scope.kind {
|
|
269
|
+
ScopeKind::Class { name, .. } => {
|
|
270
|
+
// If the name already contains ::, it's a qualified name from AST
|
|
271
|
+
// (e.g., `class Api::User` defined at top level)
|
|
272
|
+
if name.contains("::") {
|
|
273
|
+
path_segments.push(name.clone());
|
|
274
|
+
} else {
|
|
275
|
+
path_segments.push(name.clone());
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
ScopeKind::Module { name } => {
|
|
279
|
+
if name.contains("::") {
|
|
280
|
+
path_segments.push(name.clone());
|
|
281
|
+
} else {
|
|
282
|
+
path_segments.push(name.clone());
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
_ => {}
|
|
286
|
+
}
|
|
287
|
+
current = scope.parent;
|
|
288
|
+
} else {
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if path_segments.is_empty() {
|
|
294
|
+
return None;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Reverse to get from outermost to innermost
|
|
298
|
+
path_segments.reverse();
|
|
299
|
+
|
|
300
|
+
// Join all segments, handling cases where segments may already contain ::
|
|
301
|
+
let mut result = String::new();
|
|
302
|
+
for segment in path_segments {
|
|
303
|
+
if !result.is_empty() {
|
|
304
|
+
result.push_str("::");
|
|
305
|
+
}
|
|
306
|
+
result.push_str(&segment);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
Some(result)
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/// Lookup instance variable in enclosing module scope
|
|
313
|
+
pub fn lookup_instance_var_in_module(&self, name: &str) -> Option<VertexId> {
|
|
314
|
+
let mut current = Some(self.current_scope);
|
|
315
|
+
|
|
316
|
+
while let Some(scope_id) = current {
|
|
317
|
+
if let Some(scope) = self.scopes.get(&scope_id) {
|
|
318
|
+
match &scope.kind {
|
|
319
|
+
ScopeKind::Module { .. } => {
|
|
320
|
+
return scope.get_instance_var(name);
|
|
321
|
+
}
|
|
322
|
+
_ => {
|
|
323
|
+
current = scope.parent;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
} else {
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
None
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/// Set instance variable in enclosing module scope
|
|
335
|
+
pub fn set_instance_var_in_module(&mut self, name: String, vtx: VertexId) {
|
|
336
|
+
let mut current = Some(self.current_scope);
|
|
337
|
+
|
|
338
|
+
while let Some(scope_id) = current {
|
|
339
|
+
if let Some(scope) = self.scopes.get(&scope_id) {
|
|
340
|
+
match &scope.kind {
|
|
341
|
+
ScopeKind::Module { .. } => {
|
|
342
|
+
if let Some(module_scope) = self.scopes.get_mut(&scope_id) {
|
|
343
|
+
module_scope.set_instance_var(name, vtx);
|
|
344
|
+
}
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
_ => {
|
|
348
|
+
current = scope.parent;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
} else {
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
229
356
|
}
|
|
230
357
|
|
|
231
358
|
#[cfg(test)]
|
|
@@ -327,4 +454,143 @@ mod tests {
|
|
|
327
454
|
// Should still find parent class name
|
|
328
455
|
assert_eq!(sm.current_class_name(), Some("User".to_string()));
|
|
329
456
|
}
|
|
457
|
+
|
|
458
|
+
#[test]
|
|
459
|
+
fn test_scope_manager_module_scope() {
|
|
460
|
+
let mut sm = ScopeManager::new();
|
|
461
|
+
|
|
462
|
+
assert_eq!(sm.current_module_name(), None);
|
|
463
|
+
|
|
464
|
+
let module_id = sm.new_scope(ScopeKind::Module {
|
|
465
|
+
name: "Utils".to_string(),
|
|
466
|
+
});
|
|
467
|
+
sm.enter_scope(module_id);
|
|
468
|
+
|
|
469
|
+
assert_eq!(sm.current_module_name(), Some("Utils".to_string()));
|
|
470
|
+
|
|
471
|
+
// Enter method within module
|
|
472
|
+
let method_id = sm.new_scope(ScopeKind::Method {
|
|
473
|
+
name: "helper".to_string(),
|
|
474
|
+
receiver_type: Some("Utils".to_string()),
|
|
475
|
+
});
|
|
476
|
+
sm.enter_scope(method_id);
|
|
477
|
+
|
|
478
|
+
// Should still find parent module name
|
|
479
|
+
assert_eq!(sm.current_module_name(), Some("Utils".to_string()));
|
|
480
|
+
|
|
481
|
+
sm.exit_scope(); // exit method
|
|
482
|
+
sm.exit_scope(); // exit module
|
|
483
|
+
|
|
484
|
+
assert_eq!(sm.current_module_name(), None);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
#[test]
|
|
488
|
+
fn test_scope_manager_module_instance_var() {
|
|
489
|
+
let mut sm = ScopeManager::new();
|
|
490
|
+
|
|
491
|
+
let module_id = sm.new_scope(ScopeKind::Module {
|
|
492
|
+
name: "Config".to_string(),
|
|
493
|
+
});
|
|
494
|
+
sm.enter_scope(module_id);
|
|
495
|
+
|
|
496
|
+
// Set instance variable in module
|
|
497
|
+
sm.set_instance_var_in_module("@setting".to_string(), VertexId(100));
|
|
498
|
+
|
|
499
|
+
// Enter method within module
|
|
500
|
+
let method_id = sm.new_scope(ScopeKind::Method {
|
|
501
|
+
name: "get_setting".to_string(),
|
|
502
|
+
receiver_type: Some("Config".to_string()),
|
|
503
|
+
});
|
|
504
|
+
sm.enter_scope(method_id);
|
|
505
|
+
|
|
506
|
+
// Should find instance variable from module scope
|
|
507
|
+
assert_eq!(
|
|
508
|
+
sm.lookup_instance_var_in_module("@setting"),
|
|
509
|
+
Some(VertexId(100))
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
#[test]
|
|
514
|
+
fn test_current_qualified_name_simple_class() {
|
|
515
|
+
let mut sm = ScopeManager::new();
|
|
516
|
+
|
|
517
|
+
// module Api; class User; end; end
|
|
518
|
+
let class_id = sm.new_scope(ScopeKind::Class {
|
|
519
|
+
name: "User".to_string(),
|
|
520
|
+
superclass: None,
|
|
521
|
+
});
|
|
522
|
+
sm.enter_scope(class_id);
|
|
523
|
+
|
|
524
|
+
assert_eq!(
|
|
525
|
+
sm.current_qualified_name(),
|
|
526
|
+
Some("User".to_string())
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
#[test]
|
|
531
|
+
fn test_current_qualified_name_nested_module_class() {
|
|
532
|
+
let mut sm = ScopeManager::new();
|
|
533
|
+
|
|
534
|
+
// module Api
|
|
535
|
+
let api_id = sm.new_scope(ScopeKind::Module {
|
|
536
|
+
name: "Api".to_string(),
|
|
537
|
+
});
|
|
538
|
+
sm.enter_scope(api_id);
|
|
539
|
+
|
|
540
|
+
// module V1
|
|
541
|
+
let v1_id = sm.new_scope(ScopeKind::Module {
|
|
542
|
+
name: "V1".to_string(),
|
|
543
|
+
});
|
|
544
|
+
sm.enter_scope(v1_id);
|
|
545
|
+
|
|
546
|
+
// class User
|
|
547
|
+
let user_id = sm.new_scope(ScopeKind::Class {
|
|
548
|
+
name: "User".to_string(),
|
|
549
|
+
superclass: None,
|
|
550
|
+
});
|
|
551
|
+
sm.enter_scope(user_id);
|
|
552
|
+
|
|
553
|
+
assert_eq!(
|
|
554
|
+
sm.current_qualified_name(),
|
|
555
|
+
Some("Api::V1::User".to_string())
|
|
556
|
+
);
|
|
557
|
+
|
|
558
|
+
// def greet
|
|
559
|
+
let method_id = sm.new_scope(ScopeKind::Method {
|
|
560
|
+
name: "greet".to_string(),
|
|
561
|
+
receiver_type: None,
|
|
562
|
+
});
|
|
563
|
+
sm.enter_scope(method_id);
|
|
564
|
+
|
|
565
|
+
// Inside method, should still get the qualified class name
|
|
566
|
+
assert_eq!(
|
|
567
|
+
sm.current_qualified_name(),
|
|
568
|
+
Some("Api::V1::User".to_string())
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
#[test]
|
|
573
|
+
fn test_current_qualified_name_with_inline_qualified_class() {
|
|
574
|
+
let mut sm = ScopeManager::new();
|
|
575
|
+
|
|
576
|
+
// class Api::User (defined at top level with qualified name)
|
|
577
|
+
let class_id = sm.new_scope(ScopeKind::Class {
|
|
578
|
+
name: "Api::User".to_string(),
|
|
579
|
+
superclass: None,
|
|
580
|
+
});
|
|
581
|
+
sm.enter_scope(class_id);
|
|
582
|
+
|
|
583
|
+
assert_eq!(
|
|
584
|
+
sm.current_qualified_name(),
|
|
585
|
+
Some("Api::User".to_string())
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
#[test]
|
|
590
|
+
fn test_current_qualified_name_at_top_level() {
|
|
591
|
+
let sm = ScopeManager::new();
|
|
592
|
+
|
|
593
|
+
// At top level, no class/module
|
|
594
|
+
assert_eq!(sm.current_qualified_name(), None);
|
|
595
|
+
}
|
|
330
596
|
}
|