Skip to content

Commit

Permalink
First iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
wzwywx committed Oct 21, 2023
1 parent 42a7de7 commit b49d27c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 12 deletions.
4 changes: 2 additions & 2 deletions checker/src/context/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ impl<'a> Environment<'a> {
self.facts.events.push(Event::Throw(value));
}

pub fn return_value(&mut self, returned: TypeId) {
self.facts.events.push(Event::Return { returned })
pub fn return_value(&mut self, returned: TypeId, position: SpanWithSource) {
self.facts.events.push(Event::Return { returned, position })
}

/// Updates **a existing property**
Expand Down
11 changes: 9 additions & 2 deletions checker/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ mod defined_errors_and_warnings {
expected_return_type: TypeStringRepresentation,
returned_type: TypeStringRepresentation,
position: SpanWithSource,
returned_position: SpanWithSource,
},
// TODO are these the same errors?
TypeIsNotIndexable(TypeStringRepresentation),
Expand Down Expand Up @@ -433,12 +434,18 @@ mod defined_errors_and_warnings {
},
TypeCheckError::ReturnedTypeDoesNotMatch {
position,
returned_position,
expected_return_type,
returned_type,
} => Diagnostic::Position {
} => Diagnostic::PositionWithAdditionLabels {
reason: format!(
"Function is expected to return {expected_return_type} but returned {returned_type}",
"Function is expected to return {expected_return_type} but returned {returned_type} {:?}",
returned_position.clone()
),
labels: vec![(
format!("The returned {returned_type} came from here."),
Some(returned_position.clone()),
)],
position,
kind: super::DiagnosticKind::Error,
},
Expand Down
2 changes: 1 addition & 1 deletion checker/src/events/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub(crate) fn apply_event(
});
}
}
Event::Return { returned } => {
Event::Return { returned, position } => {
let substituted_returned = substitute(returned, type_arguments, environment, types);

if substituted_returned != TypeId::ERROR_TYPE {
Expand Down
7 changes: 4 additions & 3 deletions checker/src/events/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) fn get_return_from_events<'a, T: crate::ReadFromFS, M: crate::ASTImpl
) -> ReturnedTypeFromBlock {
while let Some(event) = iter.next() {
match event {
Event::Return { returned } => {
Event::Return { returned, position } => {
if let Some((expected_return_type, annotation_span)) = expected_return_type.clone()
{
let mut behavior = crate::subtyping::BasicEquality {
Expand Down Expand Up @@ -55,8 +55,9 @@ pub(crate) fn get_return_from_events<'a, T: crate::ReadFromFS, M: crate::ASTImpl
&checking_data.types,
false,
),
position: annotation_span,
// TODO event position here #37
position: annotation_span.clone(),
// TODO test with return_position
returned_position: position.clone(), // TODO event position here #37
},
);
}
Expand Down
4 changes: 2 additions & 2 deletions checker/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
behavior::functions::ThisValue,
context::{calling::Target, facts::PublicityKind, get_on_ctx, CallCheckingBehavior},
types::{calling::CalledWithNew, properties::Property},
FunctionId, GeneralContext, VariableId,
FunctionId, GeneralContext, SpanWithSource, VariableId,
};

pub(crate) mod application;
Expand Down Expand Up @@ -83,7 +83,7 @@ pub enum Event {
/// Run events conditionally
Conditionally { condition: TypeId, events_if_truthy: Box<[Event]>, else_events: Box<[Event]> },
/// TODO not sure but whatever
Return { returned: TypeId },
Return { returned: TypeId, position: SpanWithSource },
/// *lil bit magic*, handles:
/// - Creating objects `{}`
/// - Creating objects with prototypes:
Expand Down
4 changes: 3 additions & 1 deletion checker/src/synthesis/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ impl SynthesisableFunctionBody for ExpressionOrBlock {
match self {
ExpressionOrBlock::Expression(expression) => {
let returned = synthesise_expression(expression, environment, checking_data);
environment.return_value(returned);
let position =
expression.get_position().clone().with_source(environment.get_source());
environment.return_value(returned, position);
}
ExpressionOrBlock::Block(block) => {
block.synthesise_function_body(environment, checking_data)
Expand Down
5 changes: 4 additions & 1 deletion checker/src/synthesis/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ pub(super) fn synthesise_statement<T: crate::ReadFromFS>(
} else {
TypeId::UNDEFINED_TYPE
};
environment.return_value(returned);

let position = return_statement.2.clone().with_source(environment.get_source());

environment.return_value(returned, position);
}
Statement::IfStatement(if_statement) => {
let condition =
Expand Down

0 comments on commit b49d27c

Please sign in to comment.