Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions rib-core/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use crate::rib_source_span::SourceSpan;
use crate::rib_type_error::RibTypeErrorInternal;
use crate::{
from_string, text, type_checker, type_inference, ComponentDependencies, ComponentDependencyKey,
CustomInstanceSpec, DynamicParsedFunctionName, ExprVisitor, GlobalVariableTypeSpec,
InferredType, InstanceIdentifier, VariableId,
CustomInstanceSpec, DynamicParsedFunctionName, GlobalVariableTypeSpec, InferredType,
InstanceIdentifier, VariableId,
};
use crate::{IntoValueAndType, ValueAndType};
use bigdecimal::{BigDecimal, ToPrimitive};
Expand Down Expand Up @@ -1187,15 +1187,13 @@ impl Expr {
}

pub fn set_origin(&mut self) {
let mut visitor = ExprVisitor::bottom_up(self);

while let Some(expr) = visitor.pop_front() {
type_inference::visit_post_order_mut(self, &mut |expr| {
let source_location = expr.source_span();
let origin = TypeOrigin::OriginatedAt(source_location.clone());
let inferred_type = expr.inferred_type();
let origin = inferred_type.add_origin(origin);
expr.with_inferred_type_mut(origin);
}
});
}

// An inference is a single cycle of to-and-fro scanning of Rib expression, that it takes part in fix point of inference.
Expand Down Expand Up @@ -1780,7 +1778,7 @@ impl Expr {
}

pub fn visit_expr_nodes_lazy<'a>(&'a mut self, queue: &mut VecDeque<&'a mut Expr>) {
type_inference::visit_expr_nodes_lazy(self, queue);
type_inference::collect_children_mut(self, queue);
}

pub fn number_inferred(
Expand Down Expand Up @@ -2112,16 +2110,16 @@ impl Serialize for Expr {

fn find_expr(expr: &mut Expr, source_span: &SourceSpan) -> Option<Expr> {
let mut expr = expr.clone();
let mut found = None;

let mut visitor = ExprVisitor::bottom_up(&mut expr);

while let Some(current) = visitor.pop_back() {
let span = current.source_span();

if source_span.eq(&span) {
return Some(current.clone());
type_inference::visit_post_order_rev_mut(&mut expr, &mut |current| {
if found.is_none() {
let span = current.source_span();
if source_span.eq(&span) {
found = Some(current.clone());
}
}
}
});

None
found
}
11 changes: 4 additions & 7 deletions rib-core/src/type_checker/exhaustive_pattern_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::rib_source_span::SourceSpan;
use crate::{ArmPattern, ComponentDependencies, Expr, ExprVisitor};
use crate::{try_visit_post_order_rev_mut, ArmPattern, ComponentDependencies, Expr};

// When checking exhaustive pattern match, there is no need to ensure
// if the pattern aligns with conditions because those checks are done
Expand All @@ -23,19 +23,16 @@ pub fn check_exhaustive_pattern_match(
expr: &mut Expr,
component_dependency: &ComponentDependencies,
) -> Result<(), ExhaustivePatternMatchError> {
let mut visitor = ExprVisitor::bottom_up(expr);

while let Some(expr) = visitor.pop_back() {
try_visit_post_order_rev_mut(expr, &mut |expr| {
if let Expr::PatternMatch { match_arms, .. } = expr {
let match_arm = match_arms
.iter()
.map(|p| p.arm_pattern.clone())
.collect::<Vec<_>>();
internal::check_exhaustive_pattern_match(expr, &match_arm, component_dependency)?;
}
}

Ok(())
Ok(())
})
}

#[derive(Debug, Clone)]
Expand Down
13 changes: 5 additions & 8 deletions rib-core/src/type_checker/invalid_function_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
use crate::analysis::AnalysedType;
use crate::call_type::CallType;
use crate::type_checker::missing_fields::find_missing_fields_in_record;
use crate::{try_visit_post_order_mut, Expr, FunctionCallError};
use crate::{type_checker, ComponentDependencies, FunctionName};
use crate::{Expr, ExprVisitor, FunctionCallError};

// While we have a dedicated generic phases (refer submodules) within type_checker module,
// we have this special phase to grab errors in the context function calls.
Expand All @@ -27,21 +27,18 @@ pub fn check_invalid_function_args(
expr: &mut Expr,
component_dependency: &ComponentDependencies,
) -> Result<(), FunctionCallError> {
let mut visitor = ExprVisitor::bottom_up(expr);

while let Some(expr) = visitor.pop_front() {
try_visit_post_order_mut(expr, &mut |expr| {
if let Expr::Call {
call_type, args, ..
} = &expr
} = &*expr
{
match call_type {
CallType::InstanceCreation(_) => {}
call_type => get_missing_record_keys(call_type, args, component_dependency, expr)?,
}
}
}

Ok(())
Ok(())
})
}

#[allow(clippy::result_large_err)]
Expand Down
13 changes: 5 additions & 8 deletions rib-core/src/type_checker/invalid_function_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
// limitations under the License.

use crate::call_type::CallType;
use crate::{Expr, ExprVisitor, FunctionCallError};
use crate::{try_visit_post_order_mut, Expr, FunctionCallError};

#[allow(clippy::result_large_err)]
pub fn check_invalid_function_calls(expr: &mut Expr) -> Result<(), FunctionCallError> {
let mut visitor = ExprVisitor::bottom_up(expr);

while let Some(expr) = visitor.pop_front() {
try_visit_post_order_mut(expr, &mut |expr| {
if let Expr::Call {
call_type:
CallType::Function {
Expand All @@ -28,7 +26,7 @@ pub fn check_invalid_function_calls(expr: &mut Expr) -> Result<(), FunctionCallE
..
},
..
} = &expr
} = &*expr
{
if component_info.is_none() {
return Err(FunctionCallError::InvalidFunctionCall {
Expand All @@ -38,7 +36,6 @@ pub fn check_invalid_function_calls(expr: &mut Expr) -> Result<(), FunctionCallE
});
}
}
}

Ok(())
Ok(())
})
}
11 changes: 4 additions & 7 deletions rib-core/src/type_checker/invalid_worker_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
// limitations under the License.

use crate::call_type::{CallType, InstanceCreationType};
use crate::{Expr, ExprVisitor, InvalidWorkerName};
use crate::{try_visit_post_order_rev_mut, Expr, InvalidWorkerName};

// Capture all worker name and see if they are resolved to a string type
pub fn check_invalid_worker_name(expr: &mut Expr) -> Result<(), InvalidWorkerName> {
let mut visitor = ExprVisitor::bottom_up(expr);

while let Some(expr) = visitor.pop_back() {
try_visit_post_order_rev_mut(expr, &mut |expr| {
if let Expr::Call { call_type, .. } = expr {
match call_type {
CallType::InstanceCreation(InstanceCreationType::WitWorker {
Expand All @@ -46,9 +44,8 @@ pub fn check_invalid_worker_name(expr: &mut Expr) -> Result<(), InvalidWorkerNam
}
}
}
}

Ok(())
Ok(())
})
}

mod internal {
Expand Down
14 changes: 5 additions & 9 deletions rib-core/src/type_inference/enum_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ pub fn infer_enums(expr: &mut Expr, component_dependencies: &ComponentDependenci
mod internal {
use crate::analysis::AnalysedType;
use crate::call_type::CallType;
use crate::{ComponentDependencies, Expr, ExprVisitor};
use crate::{visit_post_order_rev_mut, ComponentDependencies, Expr};

pub(crate) fn convert_identifiers_to_enum_function_calls(
expr: &mut Expr,
enum_info: &EnumInfo,
) {
let enum_cases = enum_info.clone();

let mut visitor = ExprVisitor::bottom_up(expr);

while let Some(expr) = visitor.pop_back() {
visit_post_order_rev_mut(expr, &mut |expr| {
if let Expr::Identifier {
variable_id,
inferred_type,
Expand All @@ -52,17 +50,15 @@ mod internal {
};
}
}
}
});
}

pub(crate) fn get_enum_info(
expr: &mut Expr,
component_dependency: &ComponentDependencies,
) -> EnumInfo {
let mut enum_cases = vec![];
let mut visitor = ExprVisitor::bottom_up(expr);

while let Some(expr) = visitor.pop_back() {
visit_post_order_rev_mut(expr, &mut |expr| {
if let Expr::Identifier {
variable_id,
inferred_type,
Expand All @@ -84,7 +80,7 @@ mod internal {
}
}
}
}
});

EnumInfo { cases: enum_cases }
}
Expand Down
Loading
Loading