Skip to content

Commit

Permalink
Remove all optional positions from the checker events (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickLaflamme authored May 28, 2024
1 parent aa18724 commit e1a7b5e
Show file tree
Hide file tree
Showing 25 changed files with 231 additions and 162 deletions.
Binary file modified checker/definitions/internal.ts.d.bin
Binary file not shown.
32 changes: 17 additions & 15 deletions checker/src/context/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ impl<'a> Environment<'a> {
&with,
rhs,
&mut checking_data.types,
Some(span),
span,
&checking_data.options,
)?
.unwrap_or(rhs)),
Expand Down Expand Up @@ -781,7 +781,12 @@ impl<'a> Environment<'a> {
}

/// TODO decidable & private?
pub fn delete_property(&mut self, on: TypeId, property: &PropertyKey) -> bool {
pub fn delete_property(
&mut self,
on: TypeId,
property: &PropertyKey,
position: SpanWithSource,
) -> bool {
let existing = self.property_in(on, property);

let under = property.into_owned();
Expand All @@ -801,7 +806,7 @@ impl<'a> Environment<'a> {
new: PropertyValue::Deleted,
initialization: false,
publicity: Publicity::Public,
position: None,
position,
});

existing
Expand Down Expand Up @@ -1163,6 +1168,7 @@ impl<'a> Environment<'a> {
let combined_result =
R::combine(condition, truthy_result, falsy_result, &mut checking_data.types);

let position = pos.with_source(self.get_source());
match self.context_type.parent {
GeneralContext::Syntax(syn) => {
merge_info(
Expand All @@ -1172,6 +1178,7 @@ impl<'a> Environment<'a> {
truthy_info,
falsy_info,
&mut checking_data.types,
position,
);
}
GeneralContext::Root(root) => {
Expand All @@ -1182,6 +1189,7 @@ impl<'a> Environment<'a> {
truthy_info,
falsy_info,
&mut checking_data.types,
position,
);
}
}
Expand Down Expand Up @@ -1253,7 +1261,7 @@ impl<'a> Environment<'a> {
&checking_data.types,
checking_data.options.debug_types,
),
annotation_position: Some(position.with_source(self.get_source())),
annotation_position: position.with_source(self.get_source()),
returned_position,
},
);
Expand All @@ -1273,11 +1281,8 @@ impl<'a> Environment<'a> {
) -> Result<(), NotInLoopOrCouldNotFindLabel> {
if let Some(carry) = self.find_label_or_conditional_count(label, true) {
self.info.events.push(
FinalEvent::Continue {
position: Some(position.with_source(self.get_source())),
carry,
}
.into(),
FinalEvent::Continue { position: position.with_source(self.get_source()), carry }
.into(),
);
Ok(())
} else {
Expand All @@ -1295,11 +1300,8 @@ impl<'a> Environment<'a> {
) -> Result<(), NotInLoopOrCouldNotFindLabel> {
if let Some(carry) = self.find_label_or_conditional_count(label, false) {
self.info.events.push(
FinalEvent::Break {
position: Some(position.with_source(self.get_source())),
carry,
}
.into(),
FinalEvent::Break { position: position.with_source(self.get_source()), carry }
.into(),
);
Ok(())
} else {
Expand All @@ -1321,7 +1323,7 @@ impl<'a> Environment<'a> {
under: &PropertyKey,
new: TypeId,
types: &mut TypeStore,
setter_position: Option<SpanWithSource>,
setter_position: SpanWithSource,
options: &TypeCheckOptions,
) -> Result<Option<TypeId>, SetPropertyError> {
crate::types::properties::set_property(
Expand Down
8 changes: 5 additions & 3 deletions checker/src/context/information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl LocalInformation {
under: PropertyKey<'static>,
to: PropertyValue,
register_setter_event: bool,
position: Option<SpanWithSource>,
position: SpanWithSource,
) {
// crate::utilities::notify!("Registering {:?} {:?} {:?}", on, under, to);
self.current_properties.entry(on).or_default().push((publicity, under.clone(), to.clone()));
Expand Down Expand Up @@ -89,6 +89,7 @@ impl LocalInformation {
&mut self,
prototype: Option<TypeId>,
types: &mut crate::types::TypeStore,
position: SpanWithSource,
// TODO if this on environment instead it could be worked out?
is_under_dyn: bool,
is_function_this: bool,
Expand All @@ -111,7 +112,7 @@ impl LocalInformation {
let value = Event::CreateObject {
referenced_in_scope_as: ty,
prototype,
position: None,
position,
is_function_this,
};
self.events.push(value);
Expand Down Expand Up @@ -317,6 +318,7 @@ pub fn merge_info(
truthy: LocalInformation,
mut falsy: Option<LocalInformation>,
types: &mut TypeStore,
position: SpanWithSource,
) {
onto.events.push(Event::Conditionally {
condition,
Expand All @@ -325,7 +327,7 @@ pub fn merge_info(
.as_mut()
.map(|falsy| mem::take(&mut falsy.events).into_boxed_slice())
.unwrap_or_default(),
position: None,
position,
});

// TODO don't need to do above some scope
Expand Down
43 changes: 21 additions & 22 deletions checker/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum Diagnostic {
PositionWithAdditionalLabels {
reason: String,
position: SpanWithSource,
labels: Vec<(String, Option<SpanWithSource>)>,
labels: Vec<(String, SpanWithSource)>,
kind: DiagnosticKind,
},
}
Expand All @@ -64,9 +64,7 @@ impl Diagnostic {
Diagnostic::Global { .. } => Left(Left(iter::empty())),
Diagnostic::Position { position: span, .. } => Left(Right(iter::once(span.source))),
Diagnostic::PositionWithAdditionalLabels { position: pos, labels, .. } => {
Right(iter::once(pos.source).chain(
labels.iter().filter_map(|(_, span)| span.as_ref().map(|span| span.source)),
))
Right(iter::once(pos.source).chain(labels.iter().map(|(_, span)| span.source)))
}
}
}
Expand Down Expand Up @@ -311,7 +309,7 @@ mod defined_errors_and_warnings {
expected_return_type: TypeStringRepresentation,
returned_type: TypeStringRepresentation,
/// Can be `None` if it is inferred parameters
annotation_position: Option<SpanWithSource>,
annotation_position: SpanWithSource,
returned_position: SpanWithSource,
},
// TODO are these the same errors?
Expand Down Expand Up @@ -387,7 +385,8 @@ mod defined_errors_and_warnings {
DoubleDefaultExport(SpanWithSource),
CannotOpenFile {
file: CouldNotOpenFile,
position: Option<SpanWithSource>,
/// `None` if reading it from entry point (aka CLI args)
import_position: Option<SpanWithSource>,
},
VariableNotDefinedInContext {
variable: &'a str,
Expand Down Expand Up @@ -477,7 +476,7 @@ mod defined_errors_and_warnings {
format!(
"{parameter_type} was specialised with type {restriction}"
),
Some(restriction_pos),
restriction_pos,
)],
kind,
}
Expand All @@ -489,7 +488,7 @@ mod defined_errors_and_warnings {
position: argument_position,
labels: vec![(
format!("Parameter has type {parameter_type}"),
Some(parameter_position),
parameter_position,
)],
kind,
}
Expand All @@ -502,7 +501,7 @@ mod defined_errors_and_warnings {
kind,
labels: vec![(
"(non-optional) Parameter declared here".into(),
Some(parameter_position),
parameter_position,
)],
}
}
Expand Down Expand Up @@ -542,30 +541,30 @@ mod defined_errors_and_warnings {
FunctionCallingError::NeedsToBeCalledWithNewKeyword(position) => Diagnostic::Position { reason: "class constructor must be called with new".to_owned(), kind, position },
FunctionCallingError::TDZ { error: TDZ { position, variable_name }, call_site } => Diagnostic::PositionWithAdditionalLabels {
reason: format!("Variable '{variable_name}' used before declaration"),
position: call_site.unwrap(),
position: call_site,
kind,
labels: vec![(
"Variable referenced here".to_owned(),
Some(position),
position,
)],
},
FunctionCallingError::SetPropertyConstraint { property_type, value_type, assignment_position, call_site } => Diagnostic::PositionWithAdditionalLabels {
reason: "Invalid assignment to parameter".to_owned(),
position: call_site.unwrap(),
position: call_site,
kind,
labels: vec![(
format!(
"Type {value_type} does not meet property constraint {property_type}"
),
Some(assignment_position),
assignment_position,
)],
},
FunctionCallingError::UnconditionalThrow { value, call_site } => {
Diagnostic::Position {
reason: format!(
"{value} unconditionally thrown in function"
),
position: call_site.unwrap(),
position: call_site,
kind,
}
}
Expand All @@ -590,7 +589,7 @@ mod defined_errors_and_warnings {
position: value_site,
labels: vec![(
format!("Variable declared with type {variable_type}"),
Some(variable_site),
variable_site,
)],
kind,
},
Expand Down Expand Up @@ -647,10 +646,10 @@ mod defined_errors_and_warnings {
reason: format!(
"Cannot return {returned_type} because the function is expected to return {expected_return_type}",
),
labels: annotation_position.into_iter().map(|annotation_position| (
labels: vec![(
format!("Function annotated to return {expected_return_type} here"),
Some(annotation_position),
)).collect(),
annotation_position,
)],
position: returned_position,
kind,
},
Expand Down Expand Up @@ -779,10 +778,10 @@ mod defined_errors_and_warnings {
kind,
},
TypeCheckError::DoubleDefaultExport(_) => todo!(),
TypeCheckError::CannotOpenFile { file, position } => if let Some(position) = position {
TypeCheckError::CannotOpenFile { file, import_position } => if let Some(import_position) = import_position {
Diagnostic::Position {
reason: "Cannot find file".to_owned(),
position,
position: import_position,
kind,
}
} else {
Expand Down Expand Up @@ -845,7 +844,7 @@ mod defined_errors_and_warnings {
),
labels: vec![(
format!("Function has base type {parameter} here"),
Some(parameter_position),
parameter_position,
)],
position: overloaded_parameter_position,
kind,
Expand All @@ -856,7 +855,7 @@ mod defined_errors_and_warnings {
),
labels: vec![(
format!("Function annotated to return {base} here"),
Some(base_position),
base_position,
)],
position: overload_position,
kind,
Expand Down
Loading

0 comments on commit e1a7b5e

Please sign in to comment.