From ca64d564b62a83a84215299581fae45d62ae10a2 Mon Sep 17 00:00:00 2001 From: VictorHugoPilled Date: Mon, 29 Jul 2024 18:28:19 +0000 Subject: [PATCH] Ran cargo fmt --- checker/src/context/environment.rs | 27 +++++-- checker/src/context/mod.rs | 44 +++++------ checker/src/diagnostics.rs | 79 ++++++++++--------- checker/src/features/modules.rs | 13 +++- checker/src/features/variables.rs | 15 +++- checker/src/lib.rs | 33 ++++---- checker/src/synthesis/type_annotations.rs | 8 +- checker/src/synthesis/variables.rs | 26 +++++-- checker/src/types/properties.rs | 92 ++++++++++++----------- checker/tests/partial_source.rs | 5 +- 10 files changed, 204 insertions(+), 138 deletions(-) diff --git a/checker/src/context/environment.rs b/checker/src/context/environment.rs index d0b18dcc..b2a5743e 100644 --- a/checker/src/context/environment.rs +++ b/checker/src/context/environment.rs @@ -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, @@ -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::>() + site: position, + possibles: get_property_key_names_on_a_single_type( + rhs, + &mut checking_data.types, + self, + ) + .iter() + .map(AsRef::as_ref) + .collect::>(), }, ); @@ -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::>() + site, + possibles: get_property_key_names_on_a_single_type( + on, + &mut checking_data.types, + self, + ) + .iter() + .map(AsRef::as_ref) + .collect::>(), }); Err(()) } diff --git a/checker/src/context/mod.rs b/checker/src/context/mod.rs index 1704dfd9..1f809017 100644 --- a/checker/src/context/mod.rs +++ b/checker/src/context/mod.rs @@ -460,22 +460,22 @@ impl Context { 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::>()) - .flatten() - .map(AsRef::as_ref) - .collect::>() - } - - #[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::>()) - .flatten() - .map(AsRef::as_ref) - .collect::>() + #[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::>()) + .flatten() + .map(AsRef::as_ref) + .collect::>() + } + + #[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::>()) + .flatten() + .map(AsRef::as_ref) + .collect::>() } pub(crate) fn get_variable_name(&self, id: VariableId) -> &str { @@ -716,7 +716,6 @@ impl Context { } }) } - pub fn new_explicit_type_parameter( &mut self, @@ -747,17 +746,18 @@ impl Context { pub fn get_type_by_name_handle_errors( &self, - name: &str, + name: &str, pos: SpanWithSource, checking_data: &mut CheckingData, ) -> 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 } diff --git a/checker/src/diagnostics.rs b/checker/src/diagnostics.rs index 069f6a32..9b7fe104 100644 --- a/checker/src/diagnostics.rs +++ b/checker/src/diagnostics.rs @@ -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::{ @@ -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), @@ -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), @@ -383,9 +382,9 @@ pub(crate) enum TypeCheckError<'a> { DoubleDefaultExport(SpanWithSource), CannotOpenFile { file: CouldNotOpenFile, - import_position: Option, - possibles: Vec<&'a str>, - partial_import_path: &'a str, + import_position: Option, + possibles: Vec<&'a str>, + partial_import_path: &'a str, }, VariableNotDefinedInContext { variable: &'a str, @@ -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, @@ -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::>(); - 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::>(); - - get_possibles_message(candidates.iter().map(AsRef::as_ref).collect::>(), reference) - + get_possibles_message(candidates.iter().map(AsRef::as_ref).collect::>(), 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> for Diagnostic { diff --git a/checker/src/features/modules.rs b/checker/src/features/modules.rs index ade63863..6ef716bb 100644 --- a/checker/src/features/modules.rs +++ b/checker/src/features/modules.rs @@ -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, }, ); } diff --git a/checker/src/features/variables.rs b/checker/src/features/variables.rs index 87939649..14afb899 100644 --- a/checker/src/features/variables.rs +++ b/checker/src/features/variables.rs @@ -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, @@ -167,8 +169,15 @@ pub fn get_new_register_argument_under>() + site: position, + possibles: get_property_key_names_on_a_single_type( + space, + &mut checking_data.types, + environment, + ) + .iter() + .map(AsRef::as_ref) + .collect::>(), }); TypeId::ERROR_TYPE } diff --git a/checker/src/lib.rs b/checker/src/lib.rs index 25cd3e44..79873ece 100644 --- a/checker/src/lib.rs +++ b/checker/src/lib.rs @@ -527,10 +527,15 @@ pub fn check_project( } 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; } @@ -810,13 +815,15 @@ impl std::iter::Extend<(K, V)> for Map { } } -pub fn get_closest<'a, 'b>(items: impl Iterator, closest_one: &'b str) -> Option> -{ - const MIN_DISTANCE: usize = 2; - let candidates = items.filter(|item| levenshtein(closest_one, item) <= MIN_DISTANCE).collect::>(); - match candidates.len() { - 0 => None, - 1.. => Some(candidates) - } - +pub fn get_closest<'a, 'b>( + items: impl Iterator, + closest_one: &'b str, +) -> Option> { + const MIN_DISTANCE: usize = 2; + let candidates = + items.filter(|item| levenshtein(closest_one, item) <= MIN_DISTANCE).collect::>(); + match candidates.len() { + 0 => None, + 1.. => Some(candidates), + } } diff --git a/checker/src/synthesis/type_annotations.rs b/checker/src/synthesis/type_annotations.rs index 30bca8dc..affb7079 100644 --- a/checker/src/synthesis/type_annotations.rs +++ b/checker/src/synthesis/type_annotations.rs @@ -99,8 +99,8 @@ pub(super) fn synthesise_type_annotation( } } 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 @@ -166,8 +166,8 @@ pub(super) fn synthesise_type_annotation( 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; diff --git a/checker/src/synthesis/variables.rs b/checker/src/synthesis/variables.rs index 2c9eaae2..85ff2810 100644 --- a/checker/src/synthesis/variables.rs +++ b/checker/src/synthesis/variables.rs @@ -13,7 +13,7 @@ use crate::{ synthesis::parser_property_key_to_checker_property_key, types::{ printing, - properties::{PropertyKey, Publicity, get_property_key_names_on_a_single_type}, + properties::{get_property_key_names_on_a_single_type, PropertyKey, Publicity}, }, CheckingData, Environment, TypeId, }; @@ -345,9 +345,15 @@ fn assign_initial_to_fields( &checking_data.types, false, ), - site: position, - possibles: get_property_key_names_on_a_single_type(value, &mut checking_data.types, environment) - .iter().map(AsRef::as_ref).collect::>() + site: position, + possibles: get_property_key_names_on_a_single_type( + value, + &mut checking_data.types, + environment, + ) + .iter() + .map(AsRef::as_ref) + .collect::>(), }, ); @@ -420,9 +426,15 @@ fn assign_initial_to_fields( &checking_data.types, false, ), - site: position.with_source(environment.get_source()), - possibles: get_property_key_names_on_a_single_type(value, &mut checking_data.types, environment).iter() - .map(AsRef::as_ref).collect::>() + site: position.with_source(environment.get_source()), + possibles: get_property_key_names_on_a_single_type( + value, + &mut checking_data.types, + environment, + ) + .iter() + .map(AsRef::as_ref) + .collect::>(), }, ); diff --git a/checker/src/types/properties.rs b/checker/src/types/properties.rs index 45ac27c1..ad8fb85c 100644 --- a/checker/src/types/properties.rs +++ b/checker/src/types/properties.rs @@ -5,7 +5,7 @@ use crate::{ information::InformationChain, CallCheckingBehavior, Logical, PossibleLogical, SetPropertyError, }, - diagnostics::TypeStringRepresentation, + diagnostics::TypeStringRepresentation, events::Event, features::{ functions::{FunctionBehavior, ThisValue}, @@ -15,8 +15,8 @@ use crate::{ types::{ calling::{self, FunctionCallingError}, generics::generic_type_arguments::GenericArguments, - get_constraint, get_larger_type, substitute, FunctionType, GenericChain, GenericChainLink, printing, - ObjectNature, PartiallyAppliedGenerics, PolyNature, SynthesisedArgument, + get_constraint, get_larger_type, printing, substitute, FunctionType, GenericChain, + GenericChainLink, ObjectNature, PartiallyAppliedGenerics, PolyNature, SynthesisedArgument, }, Constant, Environment, LocalInformation, TypeId, }; @@ -839,11 +839,21 @@ pub(crate) fn set_property( ); } - if let Ok(fact) = current_property { match fact { - Logical::Pure(og) => { let result = - run_setter_on_object( og, behavior, environment, on, - publicity, under, new, types, setter_position, ); if - let Err(result) = result { + if let Ok(fact) = current_property { + match fact { + Logical::Pure(og) => { + let result = run_setter_on_object( + og, + behavior, + environment, + on, + publicity, + under, + new, + types, + setter_position, + ); + if let Err(result) = result { // TODO temp for error in result { match error { @@ -1567,51 +1577,43 @@ pub fn get_properties_on_single_type( } } - -pub fn get_property_as_string(property: &PropertyKey, types: &mut TypeStore, environment: &mut Environment) -> String { - - match property { - PropertyKey::String(s) => s.to_string(), - PropertyKey::Type(t) => printing::print_type( - *t, - types, - environment, - false, - ), - } +pub fn get_property_as_string( + property: &PropertyKey, + types: &mut TypeStore, + environment: &mut Environment, +) -> String { + match property { + PropertyKey::String(s) => s.to_string(), + PropertyKey::Type(t) => printing::print_type(*t, types, environment, false), + } } pub fn special_type(base: TypeId, types: &mut TypeStore) -> bool { - - matches!(types.get_type_by_id(base), - Type::SpecialObject(_) - | Type::Constructor(_) - | Type::RootPolyType(_) - | Type::Or(..) - | Type::PartiallyAppliedGenerics(_) - | Type::Constant(_) - | Type::AliasTo { .. } - | Type::FunctionReference(_) - | Type::And(_, _)) - - - - - + matches!( + types.get_type_by_id(base), + Type::SpecialObject(_) + | Type::Constructor(_) + | Type::RootPolyType(_) + | Type::Or(..) + | Type::PartiallyAppliedGenerics(_) + | Type::Constant(_) + | Type::AliasTo { .. } + | Type::FunctionReference(_) + | Type::And(_, _) + ) } pub fn get_property_key_names_on_a_single_type( base: TypeId, types: &mut TypeStore, - environment: &mut Environment + environment: &mut Environment, ) -> Vec { + if special_type(base, types) { + return vec![]; + } - if special_type(base, types){ - return vec![]; - } - - get_properties_on_single_type(base, types, environment).into_iter().map(|property| { - get_property_as_string(&property.1, types, environment) - } - ).collect() + get_properties_on_single_type(base, types, environment) + .into_iter() + .map(|property| get_property_as_string(&property.1, types, environment)) + .collect() } diff --git a/checker/tests/partial_source.rs b/checker/tests/partial_source.rs index cc4693d2..c1c48082 100644 --- a/checker/tests/partial_source.rs +++ b/checker/tests/partial_source.rs @@ -31,5 +31,8 @@ fn type_mappings() { let diagnostics: Vec<_> = result.diagnostics.into_iter().collect(); assert_eq!(diagnostics.len(), 1); - assert_eq!(diagnostics.first().unwrap().reason(), "Could not find variable 'b' in scope. Did you mean x, y or z?"); + assert_eq!( + diagnostics.first().unwrap().reason(), + "Could not find variable 'b' in scope. Did you mean x, y or z?" + ); }