Skip to content

Commit

Permalink
Ran cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
vHugoObject committed Jul 29, 2024
1 parent f049a73 commit ca64d56
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 138 deletions.
27 changes: 22 additions & 5 deletions checker/src/context/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use crate::{
subtyping::{type_is_subtype, type_is_subtype_object, State, SubTypeResult, SubTypingOptions},
types::{
printing,
properties::{PropertyKey, PropertyKind, PropertyValue, Publicity, get_property_key_names_on_a_single_type},
properties::{
get_property_key_names_on_a_single_type, PropertyKey, PropertyKind, PropertyValue,
Publicity,
},
PolyNature, Type, TypeStore,
},
CheckingData, Instance, RootContext, TypeCheckOptions, TypeId,
Expand Down Expand Up @@ -535,8 +538,15 @@ impl<'a> Environment<'a> {
&checking_data.types,
false,
),
site: position,
possibles: get_property_key_names_on_a_single_type(rhs, &mut checking_data.types, self).iter().map(AsRef::as_ref).collect::<Vec<&str>>()
site: position,
possibles: get_property_key_names_on_a_single_type(
rhs,
&mut checking_data.types,
self,
)
.iter()
.map(AsRef::as_ref)
.collect::<Vec<&str>>(),
},
);

Expand Down Expand Up @@ -883,8 +893,15 @@ impl<'a> Environment<'a> {
&checking_data.types,
false,
),
site,
possibles: get_property_key_names_on_a_single_type(on, &mut checking_data.types, self).iter().map(AsRef::as_ref).collect::<Vec<&str>>()
site,
possibles: get_property_key_names_on_a_single_type(
on,
&mut checking_data.types,
self,
)
.iter()
.map(AsRef::as_ref)
.collect::<Vec<&str>>(),
});
Err(())
}
Expand Down
44 changes: 22 additions & 22 deletions checker/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,22 +460,22 @@ impl<T: ContextType> Context<T> {
self.parents_iter().find_map(|env| get_on_ctx!(env.named_types.get(name))).copied()
}

#[allow(clippy::map_flatten)]
pub fn get_all_variable_names(&self) -> Vec<&str> {
self.parents_iter()
.map(|env| get_on_ctx!(env.variables.keys()).collect::<Vec<&String>>())
.flatten()
.map(AsRef::as_ref)
.collect::<Vec<&str>>()
}

#[allow(clippy::map_flatten)]
pub fn get_all_named_types(&self) -> Vec<&str> {
self.parents_iter()
.map(|env| get_on_ctx!(env.named_types.keys()).collect::<Vec<&String>>())
.flatten()
.map(AsRef::as_ref)
.collect::<Vec<&str>>()
#[allow(clippy::map_flatten)]
pub fn get_all_variable_names(&self) -> Vec<&str> {
self.parents_iter()
.map(|env| get_on_ctx!(env.variables.keys()).collect::<Vec<&String>>())
.flatten()
.map(AsRef::as_ref)
.collect::<Vec<&str>>()
}

#[allow(clippy::map_flatten)]
pub fn get_all_named_types(&self) -> Vec<&str> {
self.parents_iter()
.map(|env| get_on_ctx!(env.named_types.keys()).collect::<Vec<&String>>())
.flatten()
.map(AsRef::as_ref)
.collect::<Vec<&str>>()
}

pub(crate) fn get_variable_name(&self, id: VariableId) -> &str {
Expand Down Expand Up @@ -716,7 +716,6 @@ impl<T: ContextType> Context<T> {
}
})
}


pub fn new_explicit_type_parameter(
&mut self,
Expand Down Expand Up @@ -747,17 +746,18 @@ impl<T: ContextType> Context<T> {

pub fn get_type_by_name_handle_errors<U, A: crate::ASTImplementation>(
&self,
name: &str,
name: &str,
pos: SpanWithSource,
checking_data: &mut CheckingData<U, A>,
) -> TypeId {
if let Some(val) = self.get_type_from_name(name) {
val
} else {
checking_data
.diagnostics_container
.add_error(TypeCheckError::CouldNotFindType(name,
self.get_all_named_types(), pos));
checking_data.diagnostics_container.add_error(TypeCheckError::CouldNotFindType(
name,
self.get_all_named_types(),
pos,
));

TypeId::ERROR_TYPE
}
Expand Down
79 changes: 45 additions & 34 deletions checker/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use crate::{
context::{environment::Label, information::InformationChain},
diagnostics,
diagnostics, get_closest,
types::{
calling::FunctionCallingError, printing::print_type_with_type_arguments, GenericChain,
GenericChainLink,
},
get_closest
};
use source_map::{SourceId, SpanWithSource};
use std::{
Expand Down Expand Up @@ -287,8 +286,8 @@ pub(crate) enum TypeCheckError<'a> {
PropertyDoesNotExist {
on: TypeStringRepresentation,
property: PropertyRepresentation,
site: SpanWithSource,
possibles: Vec<&'a str>
site: SpanWithSource,
possibles: Vec<&'a str>,
},
NotInLoopOrCouldNotFindLabel(NotInLoopOrCouldNotFindLabel),
RestParameterAnnotationShouldBeArrayType(SpanWithSource),
Expand All @@ -297,7 +296,7 @@ pub(crate) enum TypeCheckError<'a> {
possibles: Vec<&'a str>,
position: SpanWithSource,
},
CouldNotFindType(&'a str, Vec<&'a str>,SpanWithSource),
CouldNotFindType(&'a str, Vec<&'a str>, SpanWithSource),
TypeHasNoGenericParameters(String, SpanWithSource),
AssignmentError(AssignmentError),
InvalidComparison(TypeStringRepresentation, TypeStringRepresentation),
Expand Down Expand Up @@ -383,9 +382,9 @@ pub(crate) enum TypeCheckError<'a> {
DoubleDefaultExport(SpanWithSource),
CannotOpenFile {
file: CouldNotOpenFile,
import_position: Option<SpanWithSource>,
possibles: Vec<&'a str>,
partial_import_path: &'a str,
import_position: Option<SpanWithSource>,
possibles: Vec<&'a str>,
partial_import_path: &'a str,
},
VariableNotDefinedInContext {
variable: &'a str,
Expand All @@ -394,7 +393,7 @@ pub(crate) enum TypeCheckError<'a> {
position: SpanWithSource,
},
TypeNeedsTypeArguments(&'a str, SpanWithSource),
CannotFindType(&'a str, Vec<&'a str>,SpanWithSource),
CannotFindType(&'a str, Vec<&'a str>, SpanWithSource),
TypeAlreadyDeclared {
name: String,
position: SpanWithSource,
Expand Down Expand Up @@ -430,37 +429,49 @@ pub(crate) enum TypeCheckError<'a> {

#[allow(clippy::useless_format)]
pub fn get_possibles_message(possibles: Vec<&str>, reference: &str) -> String {

let mut binding = get_closest(possibles.into_iter(), reference).unwrap_or(vec![]);
let candidates: &mut [&str] = binding.as_mut_slice();
candidates.sort_unstable();
match candidates {
[] => format!(""),
[a] => format!("Did you mean {a}?"),
[a,b] => format!("Did you mean {a} or {b}?"),
[a,b,c] => format!("Did you mean {a}, {b} or {c}?"),
[a @ .., b] => format!("Did you mean {items} or {b}?", items = a.join(", "))
}
let mut binding = get_closest(possibles.into_iter(), reference).unwrap_or(vec![]);
let candidates: &mut [&str] = binding.as_mut_slice();
candidates.sort_unstable();
match candidates {
[] => format!(""),
[a] => format!("Did you mean {a}?"),
[a, b] => format!("Did you mean {a} or {b}?"),
[a, b, c] => format!("Did you mean {a}, {b} or {c}?"),
[a @ .., b] => format!("Did you mean {items} or {b}?", items = a.join(", ")),
}
}

pub fn get_possibles_message_for_imports(possibles: &[&str], reference: &str) -> String {
let candidates = possibles
.iter()
.filter(|file| !file.ends_with(".d.ts"))
.filter_map(|file| file.strip_suffix(".ts"))
.map(|file| {
if file.starts_with("./") || file.starts_with("../") {
file.to_string()
} else {
"./".to_string() + file
}
})
.collect::<Vec<String>>();

let candidates = possibles.iter().filter(|file| !file.ends_with(".d.ts"))
.filter_map(|file| file.strip_suffix(".ts"))
.map(|file| if file.starts_with("./") || file.starts_with("../") {file.to_string()} else {"./".to_string() + file})
.collect::<Vec<String>>();

get_possibles_message(candidates.iter().map(AsRef::as_ref).collect::<Vec<&str>>(), reference)

get_possibles_message(candidates.iter().map(AsRef::as_ref).collect::<Vec<&str>>(), reference)
}

pub fn get_property_does_not_exist_message(property: PropertyRepresentation, on: &TypeStringRepresentation, possibles:Vec<&str>) -> String{

match property {
PropertyRepresentation::Type(ty) => format!("No property of type {ty} on {on}. {}", get_possibles_message(possibles, &ty)),
PropertyRepresentation::StringKey(property) => format!("No property '{property}' on {on}. {}", get_possibles_message(possibles, &property)),
}

pub fn get_property_does_not_exist_message(
property: PropertyRepresentation,
on: &TypeStringRepresentation,
possibles: Vec<&str>,
) -> String {
match property {
PropertyRepresentation::Type(ty) => {
format!("No property of type {ty} on {on}. {}", get_possibles_message(possibles, &ty))
}
PropertyRepresentation::StringKey(property) => format!(
"No property '{property}' on {on}. {}",
get_possibles_message(possibles, &property)
),
}
}

impl From<TypeCheckError<'_>> for Diagnostic {
Expand Down
13 changes: 9 additions & 4 deletions checker/src/features/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,15 @@ pub fn import_items<
checking_data.diagnostics_container.add_error(
crate::diagnostics::TypeCheckError::CannotOpenFile {
file: err.clone(),
import_position: Some(import_position.with_source(environment.get_source())),
possibles: checking_data.modules.files.get_paths().keys()
.filter_map(|path| path.to_str()).collect(),
partial_import_path
import_position: Some(import_position.with_source(environment.get_source())),
possibles: checking_data
.modules
.files
.get_paths()
.keys()
.filter_map(|path| path.to_str())
.collect(),
partial_import_path,
},
);
}
Expand Down
15 changes: 12 additions & 3 deletions checker/src/features/variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use crate::subtyping::{type_is_subtype_object, SubTypeResult};
use crate::{
types::{
printing::print_type,
properties::{get_property_unbound, get_property_key_names_on_a_single_type, PropertyKey, Publicity},
properties::{
get_property_key_names_on_a_single_type, get_property_unbound, PropertyKey, Publicity,
},
TypeId,
},
CheckingData, VariableId,
Expand Down Expand Up @@ -167,8 +169,15 @@ pub fn get_new_register_argument_under<T: crate::ReadFromFS, A: crate::ASTImplem
&checking_data.types,
false,
),
site: position,
possibles: get_property_key_names_on_a_single_type(space, &mut checking_data.types, environment).iter().map(AsRef::as_ref).collect::<Vec<&str>>()
site: position,
possibles: get_property_key_names_on_a_single_type(
space,
&mut checking_data.types,
environment,
)
.iter()
.map(AsRef::as_ref)
.collect::<Vec<&str>>(),
});
TypeId::ERROR_TYPE
}
Expand Down
33 changes: 20 additions & 13 deletions checker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,15 @@ pub fn check_project<T: crate::ReadFromFS, A: crate::ASTImplementation>(
} else {
checking_data.diagnostics_container.add_error(TypeCheckError::CannotOpenFile {
file: CouldNotOpenFile(point.clone()),
import_position: None,
possibles: checking_data.modules.files.get_paths().keys()
.filter_map(|path| path.to_str()).collect(),
partial_import_path: point.to_str().unwrap_or("")
import_position: None,
possibles: checking_data
.modules
.files
.get_paths()
.keys()
.filter_map(|path| path.to_str())
.collect(),
partial_import_path: point.to_str().unwrap_or(""),
});
continue;
}
Expand Down Expand Up @@ -810,13 +815,15 @@ impl<K, V> std::iter::Extend<(K, V)> for Map<K, V> {
}
}

pub fn get_closest<'a, 'b>(items: impl Iterator<Item=&'a str>, closest_one: &'b str) -> Option<Vec<&'a str>>
{
const MIN_DISTANCE: usize = 2;
let candidates = items.filter(|item| levenshtein(closest_one, item) <= MIN_DISTANCE).collect::<Vec<&str>>();
match candidates.len() {
0 => None,
1.. => Some(candidates)
}

pub fn get_closest<'a, 'b>(
items: impl Iterator<Item = &'a str>,
closest_one: &'b str,
) -> Option<Vec<&'a str>> {
const MIN_DISTANCE: usize = 2;
let candidates =
items.filter(|item| levenshtein(closest_one, item) <= MIN_DISTANCE).collect::<Vec<&str>>();
match candidates.len() {
0 => None,
1.. => Some(candidates),
}
}
8 changes: 4 additions & 4 deletions checker/src/synthesis/type_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ pub(super) fn synthesise_type_annotation<T: crate::ReadFromFS>(
}
} else {
checking_data.diagnostics_container.add_error(TypeCheckError::CannotFindType(
name,
environment.get_all_named_types(),
name,
environment.get_all_named_types(),
pos.with_source(environment.get_source()),
));
TypeId::ERROR_TYPE
Expand Down Expand Up @@ -166,8 +166,8 @@ pub(super) fn synthesise_type_annotation<T: crate::ReadFromFS>(

let Some(inner_type_id) = environment.get_type_from_name(name) else {
checking_data.diagnostics_container.add_error(TypeCheckError::CouldNotFindType(
name,
environment.get_all_named_types(),
name,
environment.get_all_named_types(),
position.with_source(environment.get_source()),
));
return TypeId::ERROR_TYPE;
Expand Down
Loading

0 comments on commit ca64d56

Please sign in to comment.