Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check using the type annotation for default parameter value and try-catch variable #112

Closed
kaleidawave opened this issue Jan 30, 2024 · 2 comments
Assignees
Labels
checking Issues around checking enhancement New feature or request good-first-issue PRs welcome 🙏 subtyping Related to comparing two types

Comments

@kaleidawave
Copy link
Owner

putting these together because they are pretty much the same implementation

Default parameter value

currently no exception is raised here

function x(a: number = "hi") {}

it should raise an exception for the default parameter value, as the RHS is not subtype-able under number

the code is here, using type_is_subtype with parameter_constraint against value (default_value) and sending the result to checking_data.diagnostics_container with the position using A::expression_position

parameter_constraint: TypeId,
environment: &mut Environment,
checking_data: &mut CheckingData<T, A>,
expression: &'a A::Expression<'a>,
) -> TypeId {
let (value, out, ..) = environment.new_lexical_environment_fold_into_parent(
Scope::DefaultFunctionParameter {},
checking_data,
|environment, checking_data| {
A::synthesise_expression(expression, parameter_constraint, environment, checking_data)
},
);

Catch variable

currently no exception is raised here

try {
	throw 5
} catch (e: string) {
	
}

it should raise an exception here 5 is RHS is not subtype-able under number

the code should go here

// TODO subtype thrown here with catch_variable_type

The shape of code

Here you can see how it is done for x satisfies Type. You simply want to import the type_is_subtype function and give the type from the annotation as the LHS type/first argument, then follow the rest.

let mut basic_equality = BasicEquality {
add_property_restrictions: false,
position: source_map::Nullable::NULL,
};
let result = type_is_subtype(to_satisfy, expr_ty, &mut basic_equality, environment, types);

and emitting the error diagnostic

ezno/checker/src/lib.rs

Lines 382 to 400 in b6ead3e

if !check_satisfies(expr_ty, to_satisfy, &self.types, environment) {
let expected = diagnostics::TypeStringRepresentation::from_type_id(
to_satisfy,
&environment.as_general_context(),
&self.types,
false,
);
let found = diagnostics::TypeStringRepresentation::from_type_id(
expr_ty,
&environment.as_general_context(),
&self.types,
false,
);
self.diagnostics_container.add_error(TypeCheckError::NotSatisfied {
at,
expected,
found,
});
}


You want to create custom TypeCheckErrors for both these scenarios, to annotate that it was a InvalidDefaultValue or ThrowDoesNotMatchCatchVariableAnnotation

You also want to add tests to specification.md for the cases above.

As with any issue, if you want more help or questions feel free to ask

@kaleidawave kaleidawave added enhancement New feature or request checking Issues around checking subtyping Related to comparing two types good-first-issue PRs welcome 🙏 labels Jan 30, 2024
@n-keerthi-gayathri
Copy link

@kaleidawave please assign me this issue

@CharlesTaylor7
Copy link
Contributor

@kaleidawave @n-keerthi-gayathri
Hey, I'm interested in taking on this task as my first contribution. I'll be following the detailed steps in the issue description, but I'll post again if I run into any issues.

kaleidawave pushed a commit that referenced this issue Apr 14, 2024
* Check default parameter value type (fixes #112)
@kaleidawave kaleidawave unpinned this issue Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
checking Issues around checking enhancement New feature or request good-first-issue PRs welcome 🙏 subtyping Related to comparing two types
Projects
None yet
Development

No branches or pull requests

3 participants