Skip to content

Commit f2e88c7

Browse files
committed
[IMP] Handle env get item for multiple fn evals
1 parent b7515ee commit f2e88c7

File tree

2 files changed

+58
-63
lines changed

2 files changed

+58
-63
lines changed

server/src/core/evaluation.rs

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ impl Evaluation {
11491149
evals.push(Evaluation::new_unbound(name));
11501150
}
11511151
},
1152-
ExprOrIdent::Expr(Expr::Subscript(sub)) => 'subscript_block: {
1152+
ExprOrIdent::Expr(Expr::Subscript(sub)) => {
11531153
let (eval_left, diags) = Evaluation::eval_from_ast(session, &sub.value, parent.clone(), max_infer, false, required_dependencies);
11541154
diagnostics.extend(diags);
11551155
// TODO handle multiple eval_left
@@ -1161,73 +1161,68 @@ impl Evaluation {
11611161
return AnalyzeAstResult::from_only_diagnostics(diagnostics);
11621162
}
11631163
let bases = Symbol::follow_ref(&base, session, &mut None, false, false, None);
1164-
if bases.len() != 1 {
1165-
return AnalyzeAstResult::from_only_diagnostics(diagnostics);
1166-
}
1167-
let base = &bases[0];
1168-
match base {
1169-
EvaluationSymbolPtr::WEAK(base_sym_weak_eval) if base_sym_weak_eval.instance == Some(false) => {
1170-
if let Some(SymType::CLASS) = base.upgrade_weak().map(|s| s.borrow().typ()) {
1171-
// This is a Generic type (Field[int], or List[int]), for now we just return the main type/Class (Field/List)
1172-
// TODO: handle generic types
1173-
let mut new_base = base.clone();
1174-
if for_annotation {
1175-
new_base.as_mut_weak().instance = Some(true);
1176-
}
1177-
evals.push(Evaluation {
1178-
symbol: EvaluationSymbol {
1179-
sym: new_base,
1180-
get_symbol_hook: None,
1181-
},
1182-
value: None,
1183-
range: Some(sub.range())
1184-
});
1185-
break 'subscript_block;
1186-
}
1187-
}
1188-
_ => {}
1189-
}
11901164
let value = Evaluation::expr_to_str(session, &sub.slice, parent.clone(), max_infer, false, &mut diagnostics);
11911165
diagnostics.extend(value.1);
1192-
if let Some(value) = value.0 {
1193-
if !base.is_weak() {
1194-
return AnalyzeAstResult::from_only_diagnostics(diagnostics);
1166+
for base in bases.iter() {
1167+
match base {
1168+
EvaluationSymbolPtr::WEAK(base_sym_weak_eval) if base_sym_weak_eval.instance == Some(false) => {
1169+
if let Some(SymType::CLASS) = base.upgrade_weak().map(|s| s.borrow().typ()) {
1170+
// This is a Generic type (Field[int], or List[int]), for now we just return the main type/Class (Field/List)
1171+
// TODO: handle generic types
1172+
let mut new_base = base.clone();
1173+
if for_annotation {
1174+
new_base.as_mut_weak().instance = Some(true);
1175+
}
1176+
evals.push(Evaluation {
1177+
symbol: EvaluationSymbol {
1178+
sym: new_base,
1179+
get_symbol_hook: None,
1180+
},
1181+
value: None,
1182+
range: Some(sub.range())
1183+
});
1184+
continue;
1185+
}
1186+
}
1187+
_ => {}
11951188
}
1196-
let parent_file_or_func = parent.clone().borrow().parent_file_or_function().as_ref().unwrap().upgrade().unwrap();
1197-
let is_in_validation = match parent_file_or_func.borrow().typ().clone() {
1198-
SymType::FILE | SymType::PACKAGE(_) | SymType::FUNCTION => {
1199-
parent_file_or_func.borrow().build_status(BuildSteps::VALIDATION) == BuildStatus::IN_PROGRESS
1200-
},
1201-
_ => {false}
1202-
};
1203-
let base = base.upgrade_weak().unwrap();
1204-
let get_item = base.borrow().get_content_symbol("__getitem__", u32::MAX).symbols;
1205-
if get_item.len() == 1 {
1206-
let get_item = &get_item[0];
1207-
let get_item = get_item.borrow();
1208-
if get_item.evaluations().is_some() && get_item.evaluations().unwrap().len() == 1 {
1209-
let get_item_eval = &get_item.evaluations().unwrap()[0];
1210-
if let Some(hook) = get_item_eval.symbol.get_symbol_hook {
1211-
context.as_mut().unwrap().insert(S!("args"), ContextValue::STRING(value));
1212-
let old_range = context.as_mut().unwrap().remove(&S!("range"));
1213-
context.as_mut().unwrap().insert(S!("range"), ContextValue::RANGE(sub.slice.range()));
1214-
context.as_mut().unwrap().insert(S!("is_in_validation"), ContextValue::BOOLEAN(is_in_validation));
1215-
let hook_result = hook(session, &get_item_eval.symbol, context, &mut diagnostics, Some(parent.clone()));
1216-
if let Some(hook_result) = hook_result {
1217-
match hook_result {
1218-
EvaluationSymbolPtr::WEAK(ref weak) => {
1219-
if !weak.weak.is_expired() {
1189+
if base.is_weak() && let Some(value) = &value.0 {
1190+
let base = base.upgrade_weak().unwrap();
1191+
let get_item = base.borrow().get_content_symbol("__getitem__", u32::MAX).symbols;
1192+
if get_item.len() == 1 {
1193+
let get_item = &get_item[0];
1194+
let get_item = get_item.borrow();
1195+
if get_item.evaluations().is_some() && get_item.evaluations().unwrap().len() == 1 {
1196+
let get_item_eval = &get_item.evaluations().unwrap()[0];
1197+
if let Some(hook) = get_item_eval.symbol.get_symbol_hook {
1198+
let parent_file_or_func = parent.clone().borrow().parent_file_or_function().as_ref().unwrap().upgrade().unwrap();
1199+
let is_in_validation = match parent_file_or_func.borrow().typ().clone() {
1200+
SymType::FILE | SymType::PACKAGE(_) | SymType::FUNCTION => {
1201+
parent_file_or_func.borrow().build_status(BuildSteps::VALIDATION) == BuildStatus::IN_PROGRESS
1202+
},
1203+
_ => {false}
1204+
};
1205+
context.as_mut().unwrap().insert(S!("args"), ContextValue::STRING(value.clone()));
1206+
let old_range = context.as_mut().unwrap().remove(&S!("range"));
1207+
context.as_mut().unwrap().insert(S!("range"), ContextValue::RANGE(sub.slice.range()));
1208+
context.as_mut().unwrap().insert(S!("is_in_validation"), ContextValue::BOOLEAN(is_in_validation));
1209+
let hook_result = hook(session, &get_item_eval.symbol, context, &mut diagnostics, Some(parent.clone()));
1210+
if let Some(hook_result) = hook_result {
1211+
match hook_result {
1212+
EvaluationSymbolPtr::WEAK(ref weak) => {
1213+
if !weak.weak.is_expired() {
1214+
evals.push(Evaluation::eval_from_ptr(&hook_result));
1215+
}
1216+
},
1217+
_ => {
12201218
evals.push(Evaluation::eval_from_ptr(&hook_result));
12211219
}
1222-
},
1223-
_ => {
1224-
evals.push(Evaluation::eval_from_ptr(&hook_result));
12251220
}
12261221
}
1222+
context.as_mut().unwrap().remove(&S!("args"));
1223+
context.as_mut().unwrap().remove(&S!("is_in_validation"));
1224+
context.as_mut().unwrap().insert(S!("range"), old_range.unwrap());
12271225
}
1228-
context.as_mut().unwrap().remove(&S!("args"));
1229-
context.as_mut().unwrap().remove(&S!("is_in_validation"));
1230-
context.as_mut().unwrap().insert(S!("range"), old_range.unwrap());
12311226
}
12321227
}
12331228
}
@@ -1846,4 +1841,4 @@ impl EvaluationSymbolPtr {
18461841
_ => panic!("Not an EvaluationSymbolWeak")
18471842
}
18481843
}
1849-
}
1844+
}

server/src/core/python_arch_builder_hooks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static arch_class_hooks: Lazy<Vec<PythonArchClassHook>> = Lazy::new(|| {vec![
8080
],
8181
func: |session: &mut SessionInfo, _entry_point: &Rc<RefCell<EntryPoint>>, symbol: Rc<RefCell<Symbol>>| {
8282
let range = symbol.borrow().range().clone();
83-
// ----------- env.cr ------------
83+
// ----------- global ------------
8484
symbol.borrow_mut().add_new_variable(session, Sy!("global"), &range);
8585
}
8686
},
@@ -240,4 +240,4 @@ impl PythonArchBuilderHooks {
240240
}
241241
}
242242
}
243-
}
243+
}

0 commit comments

Comments
 (0)