Skip to content

Commit

Permalink
Small subtyping update
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleidawave committed Feb 21, 2024
1 parent 4143369 commit 9b13ef0
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 68 deletions.
Binary file modified checker/definitions/internal.ts.d.bin
Binary file not shown.
6 changes: 5 additions & 1 deletion checker/src/features/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ pub fn evaluate_equality_inequality_operation(
match attempt_constant_equality(lhs, rhs, types) {
Ok(ty) => Ok(ty),
Err(()) => {
unreachable!("should have been caught by above")
unreachable!(
"should have been caught `is_dependent` above, {:?} === {:?}",
types.get_type_by_id(lhs),
types.get_type_by_id(rhs)
)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion checker/src/synthesis/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,17 @@ pub(super) fn type_definition_file<T: crate::ReadFromFS>(
base.constant_function,
);

// TODO not sure
let open = checking_data.types.new_open_type(base);

let _context = decorators_to_context(&func.decorators);

env.register_variable_handle_error(
func.name.as_str(),
VariableRegisterArguments {
constant: true,
space: None,
initial_value: Some(base),
initial_value: Some(open),
},
func.get_position().with_source(source),
&mut checking_data.diagnostics_container,
Expand Down
6 changes: 4 additions & 2 deletions checker/src/synthesis/type_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub(super) fn synthesise_type_annotation<T: crate::ReadFromFS>(
checking_data.options.debug_types,
),
position: position.with_source(environment.get_source()),
}
},
);
return TypeId::ERROR_TYPE;
}
Expand Down Expand Up @@ -225,7 +225,9 @@ pub(super) fn synthesise_type_annotation<T: crate::ReadFromFS>(
&checking_data.types,
checking_data.options.debug_types,
),
position: argument_type_annotation.get_position().with_source(environment.get_source()),
position: argument_type_annotation
.get_position()
.with_source(environment.get_source()),
};

checking_data.diagnostics_container.add_error(error);
Expand Down
23 changes: 13 additions & 10 deletions checker/src/types/calling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ fn get_logical_callable_from_type(
return Some(Logical::Error);
}

let ty1 = types.get_type_by_id(ty);
crate::utils::notify!("ty1={:?}", ty1);
match ty1 {
let le_ty = types.get_type_by_id(ty);
crate::utils::notify!("ty1={:?} ({:?})", le_ty, ty);
match le_ty {
Type::And(_, _) => todo!(),
Type::Or(left, right) => {
if let (Some(left), Some(right)) = (
Expand Down Expand Up @@ -475,11 +475,11 @@ fn call_logical<E: CallCheckingBehavior>(

// is poly
if let Some(on) = function.from {
// TODO think all constant calls return constants
let has_known_behavior =
!function_type.effects.is_empty() || result.result_was_const_computation;
// TODO this is a hack
let has_events = !function_type.effects.is_empty();
let has_known_side_effects = has_events || result.result_was_const_computation;

if !has_known_behavior {
if !has_known_side_effects {
find_possible_mutations(&arguments, types, top_environment);

let with = arguments.clone().into_boxed_slice();
Expand Down Expand Up @@ -689,9 +689,12 @@ impl FunctionType {
| "bind" | "create_proxy"
);

// TODO just for debugging. These have their constant things called everytime AND queue an event
let is_independent = const_fn_ident.ends_with("independent");

// Most of the time don't call using constant function if an argument is dependent.
// But sometimes do for the cases above
if call_anyway || !has_dependent_argument {
if call_anyway || is_independent || !has_dependent_argument {
// TODO event
let result = call_constant_function(
const_fn_ident,
Expand All @@ -715,7 +718,7 @@ impl FunctionType {
returned_type: value,
warnings: Default::default(),
called: None,
result_was_const_computation: true,
result_was_const_computation: !is_independent,
special,
});
}
Expand All @@ -725,7 +728,7 @@ impl FunctionType {
returned_type: TypeId::UNDEFINED_TYPE,
warnings: vec![InfoDiagnostic(diagnostic)],
called: None,
result_was_const_computation: true,
result_was_const_computation: !is_independent,
// TODO!!
special: Some(SpecialExpressions::Marker),
});
Expand Down
31 changes: 26 additions & 5 deletions checker/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ use crate::{
pub use self::functions::*;

use self::{
poly_types::generic_type_arguments::StructureGenericArguments, properties::PropertyKey,
poly_types::generic_type_arguments::StructureGenericArguments,
properties::PropertyKey,
subtyping::{AlreadyChecked, SubTypingMode},
};

pub type ExplicitTypeArgument = (TypeId, SpanWithSource);
Expand Down Expand Up @@ -345,27 +347,32 @@ pub struct BasicEquality {
}

/// For subtyping

pub trait SubTypeBehavior {
/// If false will error out if T >: U
const INFER_GENERICS: bool;

// TODO better return type
/// TODO better return type
/// TODO not sure about `AlreadyChecked`
fn set_type_argument(
&mut self,
on: TypeId,
argument: TypeId,
depth: u8,
environment: &Environment,
types: &TypeStore,
already_checked: &mut AlreadyChecked,
) -> SubTypeResult;

/// TODO not sure about `AlreadyChecked`
fn try_set_contravariant(
&mut self,
on: TypeId,
restriction: TypeId,
position: SpanWithSource,
environment: &Environment,
types: &TypeStore,
already_checked: &mut AlreadyChecked,
) -> SubTypeResult;

fn add_object_mutation_constraint(&mut self, on: TypeId, constraint: TypeId);
Expand Down Expand Up @@ -396,11 +403,24 @@ impl SubTypeBehavior for BasicEquality {
_depth: u8,
environment: &Environment,
types: &TypeStore,
already_checked: &mut AlreadyChecked,
) -> SubTypeResult {
// TODO shouldn't be here
crate::utils::notify!(
"Shouldn't be doing 'contributing like' subtyping here for BasicEquality"
);

let constraint = get_constraint(on, types).unwrap();
subtyping::type_is_subtype(constraint, argument, self, environment, types)
// type_is_subtype(on, argument, self, environment, types)
subtyping::type_is_subtype_with_generics(
constraint,
None,
argument,
None,
self,
environment,
types,
SubTypingMode::default(),
already_checked,
)
}

fn try_set_contravariant(
Expand All @@ -410,6 +430,7 @@ impl SubTypeBehavior for BasicEquality {
_position: SpanWithSource,
_environment: &Environment,
_types: &TypeStore,
_already_checked: &mut AlreadyChecked,
) -> SubTypeResult {
crate::utils::notify!("Here!");
SubTypeResult::IsSubType
Expand Down
80 changes: 65 additions & 15 deletions checker/src/types/poly_types/contributions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use source_map::SpanWithSource;

use crate::{
subtyping::{type_is_subtype, SubTypeBehavior, SubTypeResult},
subtyping::{type_is_subtype_with_generics, AlreadyChecked, SubTypeBehavior, SubTypeResult},
types::{LookUpGeneric, TypeRestrictions, TypeStore},
Environment, TypeId,
};
Expand Down Expand Up @@ -53,18 +53,37 @@ impl<'a> Contributions<'a> {
environment: &Environment,
types: &TypeStore,
lookup_properties: bool,
already_checked: &mut AlreadyChecked,
) -> SubTypeResult {
// TODO staging_contravariant
if let Some(constraint) = self.get_standard_restriction(under) {
type_is_subtype(constraint, argument, self, environment, types)
type_is_subtype_with_generics(
constraint,
None,
argument,
None,
self,
environment,
types,
Default::default(),
already_checked,
)
} else if let Some(lookup_restriction) =
lookup_properties.then(|| self.get_lookup(under)).flatten()
{
// TODO don't create vec
for constraint in lookup_restriction.calculate_lookup(environment) {
if let e @ SubTypeResult::IsNotSubType(_) =
type_is_subtype(constraint, argument, self, environment, types)
{
if let e @ SubTypeResult::IsNotSubType(_) = type_is_subtype_with_generics(
constraint,
None,
argument,
None,
self,
environment,
types,
Default::default(),
already_checked,
) {
return e;
}
}
Expand All @@ -74,7 +93,17 @@ impl<'a> Contributions<'a> {
// TODO not sure
let constraint = crate::types::get_constraint(under, types).unwrap();
crate::utils::notify!("Here, constraint={:?}", constraint);
type_is_subtype(constraint, argument, self, environment, types)
type_is_subtype_with_generics(
constraint,
None,
argument,
None,
self,
environment,
types,
Default::default(),
already_checked,
)
}
}

Expand All @@ -85,14 +114,22 @@ impl<'a> Contributions<'a> {
depth: u8,
environment: &Environment,
types: &TypeStore,
already_checked: &mut AlreadyChecked,
) -> SubTypeResult {
let lhs = crate::types::printing::print_type(on, types, environment, true);
let rhs = crate::types::printing::print_type(argument, types, environment, true);
crate::utils::notify!("Here on=({}) :< arg=({})", lhs, rhs);

if let e @ SubTypeResult::IsNotSubType(_) =
self.passes_under_current_contravariant(on, argument, environment, types, false)
{
let lhs = crate::types::printing::print_type(on, types, environment, true);
let rhs = crate::types::printing::print_type(argument, types, environment, true);
crate::utils::notify!("Here on=({}) :< arg=({})", lhs, rhs);
}

if let e @ SubTypeResult::IsNotSubType(_) = self.passes_under_current_contravariant(
on,
argument,
environment,
types,
false,
already_checked,
) {
// TODO more detailed error
return e;
}
Expand All @@ -109,12 +146,23 @@ impl<'a> Contributions<'a> {
_position: SpanWithSource,
environment: &Environment,
types: &TypeStore,
already_checked: &mut AlreadyChecked,
) -> SubTypeResult {
crate::utils::notify!("TODO assert it meets existing_covariant and staging_covariant");
crate::utils::notify!("TODO add to staging_covariant");

if let Some(under) = self.get_standard_restriction(on) {
type_is_subtype(under, restriction, self, environment, types)
type_is_subtype_with_generics(
under,
None,
restriction,
None,
self,
environment,
types,
Default::default(),
already_checked,
)
} else {
SubTypeResult::IsSubType
}
Expand Down Expand Up @@ -144,8 +192,9 @@ impl<'a> SubTypeBehavior for Contributions<'a> {
depth: u8,
environment: &Environment,
types: &TypeStore,
already_checked: &mut AlreadyChecked,
) -> SubTypeResult {
self.try_set_covariant(on, argument, depth, environment, types)
self.try_set_covariant(on, argument, depth, environment, types, already_checked)
}

fn try_set_contravariant(
Expand All @@ -155,7 +204,8 @@ impl<'a> SubTypeBehavior for Contributions<'a> {
position: SpanWithSource,
environment: &Environment,
types: &TypeStore,
already_checked: &mut AlreadyChecked,
) -> SubTypeResult {
self.try_set_contravariant(on, restriction, position, environment, types)
self.try_set_contravariant(on, restriction, position, environment, types, already_checked)
}
}
5 changes: 2 additions & 3 deletions checker/src/types/printing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ fn print_type_into_buf<C: InformationChain>(
{
print_type_into_buf(*on, buf, cycles, args, types, info_chain, debug);
buf.push('<');
for (not_at_end, (arg, _)) in
arguments.type_restrictions.values().nendiate()
{
let nendiate = arguments.type_restrictions.values().nendiate();
for (not_at_end, (arg, _)) in nendiate {
print_type_into_buf(*arg, buf, cycles, args, types, info_chain, debug);
if not_at_end {
buf.push_str(", ");
Expand Down
5 changes: 5 additions & 0 deletions checker/src/types/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,4 +602,9 @@ impl TypeStore {
}));
self.register_type(ty)
}

/// TODO WIP
pub fn new_open_type(&mut self, base: TypeId) -> TypeId {
self.register_type(Type::RootPolyType(PolyNature::Open(base)))
}
}

0 comments on commit 9b13ef0

Please sign in to comment.