Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect infinite dependencies #279

Merged
merged 11 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions core_lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ pub enum CompileError<'sc> {
call_chain: String, // Pretty list of symbols, e.g., "a, b and c".
span: Span<'sc>,
},
#[error("File {file_path} generates an infinite depedency cycle.")]
InfiniteDependencies { file_path: String, span: Span<'sc> },
emilyaherbert marked this conversation as resolved.
Show resolved Hide resolved
}

impl<'sc> std::convert::From<TypeError<'sc>> for CompileError<'sc> {
Expand Down Expand Up @@ -857,6 +859,7 @@ impl<'sc> CompileError<'sc> {
ArgumentParameterTypeMismatch { span, .. } => span,
RecursiveCall { span, .. } => span,
RecursiveCallChain { span, .. } => span,
InfiniteDependencies { span, .. } => span,
}
}

Expand Down
20 changes: 19 additions & 1 deletion core_lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ pub(crate) struct InnerDependencyCompileResult<'sc> {
/// different types of compilation and stuff. After we get to a good state with the MVP,
/// clean up the types here with the power of hindsight
pub(crate) fn compile_inner_dependency<'sc>(
file_path: String,
emilyaherbert marked this conversation as resolved.
Show resolved Hide resolved
input: &'sc str,
initial_namespace: &Namespace<'sc>,
build_config: BuildConfig,
dead_code_graph: &mut ControlFlowGraph<'sc>,
dependency_graph: &mut HashMap<String, Vec<String>>,
) -> CompileResult<'sc, InnerDependencyCompileResult<'sc>> {
let mut warnings = Vec::new();
let mut errors = Vec::new();
Expand Down Expand Up @@ -227,11 +229,13 @@ pub(crate) fn compile_inner_dependency<'sc>(
.into_iter()
.filter_map(|(name, tree)| {
TypedParseTree::type_check(
file_path.clone(),
emilyaherbert marked this conversation as resolved.
Show resolved Hide resolved
tree,
initial_namespace.clone(),
TreeType::Library,
&build_config.clone(),
dead_code_graph,
dependency_graph,
)
.ok(&mut warnings, &mut errors)
.map(|value| (name, value))
Expand Down Expand Up @@ -274,9 +278,11 @@ pub(crate) fn compile_inner_dependency<'sc>(
}

pub fn compile_to_asm<'sc>(
file_path: String,
input: &'sc str,
initial_namespace: &Namespace<'sc>,
build_config: BuildConfig,
dependency_graph: &mut HashMap<String, Vec<String>>,
) -> CompilationResult<'sc> {
let mut warnings = Vec::new();
let mut errors = Vec::new();
Expand All @@ -295,11 +301,13 @@ pub fn compile_to_asm<'sc>(
let mut type_check_ast = |ast: Option<_>, tree_type| {
ast.map(|tree| {
TypedParseTree::type_check(
file_path.clone(),
tree,
initial_namespace.clone(),
tree_type,
&build_config.clone(),
&mut dead_code_graph,
dependency_graph,
)
.ok(&mut warnings, &mut errors)
})
Expand All @@ -316,11 +324,13 @@ pub fn compile_to_asm<'sc>(
.into_iter()
.filter_map(|(name, tree)| {
TypedParseTree::type_check(
file_path.clone(),
tree,
initial_namespace.clone(),
TreeType::Library,
&build_config.clone(),
&mut dead_code_graph,
dependency_graph,
)
.ok(&mut warnings, &mut errors)
.map(|value| (name, value))
Expand Down Expand Up @@ -447,11 +457,19 @@ pub fn compile_to_asm<'sc>(
}
}
pub fn compile_to_bytecode<'sc>(
file_path: String,
input: &'sc str,
initial_namespace: &Namespace<'sc>,
build_config: BuildConfig,
dependency_graph: &mut HashMap<String, Vec<String>>,
) -> BytecodeCompilationResult<'sc> {
match compile_to_asm(input, initial_namespace, build_config) {
match compile_to_asm(
file_path,
input,
initial_namespace,
build_config,
dependency_graph,
) {
CompilationResult::Success {
mut asm,
mut warnings,
Expand Down
4 changes: 4 additions & 0 deletions core_lang/src/semantic_analysis/ast_node/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl<'sc> TypedCodeBlock<'sc> {
self.clone()
}
pub(crate) fn type_check(
file_path: String,
other: CodeBlock<'sc>,
namespace: &Namespace<'sc>,
// this is for the return or implicit return
Expand All @@ -24,6 +25,7 @@ impl<'sc> TypedCodeBlock<'sc> {
self_type: &MaybeResolvedType<'sc>,
build_config: &BuildConfig,
dead_code_graph: &mut ControlFlowGraph<'sc>,
dependency_graph: &mut HashMap<String, Vec<String>>,
) -> CompileResult<'sc, (Self, Option<MaybeResolvedType<'sc>>)> {
let mut warnings = Vec::new();
let mut errors = Vec::new();
Expand All @@ -36,13 +38,15 @@ impl<'sc> TypedCodeBlock<'sc> {
.iter()
.filter_map(|node| {
TypedAstNode::type_check(
file_path.clone(),
node.clone(),
&mut local_namespace,
type_annotation.clone(),
help_text.clone(),
self_type,
build_config,
dead_code_graph,
dependency_graph,
)
.ok(&mut warnings, &mut errors)
})
Expand Down
7 changes: 6 additions & 1 deletion core_lang/src/semantic_analysis/ast_node/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
};
use crate::{control_flow_analysis::ControlFlowGraph, types::TypeInfo};
use sha2::{Digest, Sha256};
use std::collections::HashMap;

#[derive(Clone, Debug)]
pub enum TypedDeclaration<'sc> {
Expand Down Expand Up @@ -498,6 +499,7 @@ pub struct TypedReassignment<'sc> {

impl<'sc> TypedFunctionDeclaration<'sc> {
pub fn type_check(
file_path: String,
fn_decl: FunctionDeclaration<'sc>,
namespace: &Namespace<'sc>,
_return_type_annotation: Option<MaybeResolvedType<'sc>>,
Expand All @@ -508,6 +510,7 @@ impl<'sc> TypedFunctionDeclaration<'sc> {
build_config: &BuildConfig,
dead_code_graph: &mut ControlFlowGraph<'sc>,
mode: Mode,
dependency_graph: &mut HashMap<String, Vec<String>>,
) -> CompileResult<'sc, TypedFunctionDeclaration<'sc>> {
let mut warnings = Vec::new();
let mut errors = Vec::new();
Expand Down Expand Up @@ -549,13 +552,15 @@ impl<'sc> TypedFunctionDeclaration<'sc> {
// stifle the errors. If there _are_ implicit block returns, we want to type_check them.
let (body, _implicit_block_return) = check!(
TypedCodeBlock::type_check(
file_path,
body.clone(),
&namespace,
Some(return_type.clone()),
"Function body's return type does not match up with its return type annotation.",
self_type,
build_config,
dead_code_graph
dead_code_graph,
dependency_graph
),
(
TypedCodeBlock {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::types::ResolvedType;
/// Given an enum declaration and the instantiation expression/type arguments, construct a valid
/// [TypedExpression].
pub(crate) fn instantiate_enum<'sc>(
file_path: String,
enum_decl: TypedEnumDeclaration<'sc>,
enum_field_name: Ident<'sc>,
args: Vec<Expression<'sc>>,
Expand All @@ -15,6 +16,7 @@ pub(crate) fn instantiate_enum<'sc>(
self_type: &MaybeResolvedType<'sc>,
build_config: &BuildConfig,
dead_code_graph: &mut ControlFlowGraph<'sc>,
dependency_graph: &mut HashMap<String, Vec<String>>,
) -> CompileResult<'sc, TypedExpression<'sc>> {
let mut warnings = vec![];
let mut errors = vec![];
Expand Down Expand Up @@ -62,13 +64,15 @@ pub(crate) fn instantiate_enum<'sc>(
([single_expr], r#type) => {
let typed_expr = check!(
TypedExpression::type_check(
file_path,
single_expr.clone(),
namespace,
Some(MaybeResolvedType::Resolved(r#type.clone())),
"Enum instantiator must match its declared variant type.",
self_type,
build_config,
dead_code_graph,
dependency_graph
),
return err(warnings, errors),
warnings,
Expand Down
Loading