Add fx to ErrorType

This commit is contained in:
Agus Zubiaga 2024-10-17 22:59:44 -03:00
parent 8a65617704
commit 6533e9084d
No known key found for this signature in database
6 changed files with 66 additions and 30 deletions

View file

@ -1,7 +1,7 @@
#![deny(unsafe_op_in_unsafe_fn)]
use crate::types::{
name_type_var, AbilitySet, AliasKind, ErrorType, ExtImplicitOpenness, Polarity, RecordField,
RecordFieldsError, TupleElemsError, TypeExt, Uls,
name_type_var, AbilitySet, AliasKind, ErrorFunctionFx, ErrorType, ExtImplicitOpenness,
Polarity, RecordField, RecordFieldsError, TupleElemsError, TypeExt, Uls,
};
use crate::unification_table::{self, UnificationTable};
use roc_collections::all::{FnvMap, ImMap, ImSet, MutSet, SendMap};
@ -4206,7 +4206,7 @@ fn flat_type_to_err_type(
ErrorType::Type(symbol, arg_types)
}
Func(arg_vars, closure_var, ret_var, _fx_var) => {
Func(arg_vars, closure_var, ret_var, fx_var) => {
let args = arg_vars
.into_iter()
.map(|index| {
@ -4217,9 +4217,23 @@ fn flat_type_to_err_type(
let ret = var_to_err_type(subs, state, ret_var, Polarity::Pos);
let closure = var_to_err_type(subs, state, closure_var, pol);
let fx = match subs.get_content_without_compacting(fx_var) {
Content::Pure | Content::FlexVar(_) | Content::Error => ErrorFunctionFx::Pure,
Content::Effectful => ErrorFunctionFx::Effectful,
Content::RigidVar(_)
| Content::FlexAbleVar(_, _)
| Content::RigidAbleVar(_, _)
| Content::RecursionVar { .. }
| Content::LambdaSet(_)
| Content::ErasedLambda
| Content::Structure(_)
| Content::Alias(_, _, _, _)
| Content::RangedNumber(_) => {
internal_error!("Unexpected content in fx var")
}
};
// [purity-inference] TODO: add fx var to the error type
ErrorType::Function(args, Box::new(closure), Box::new(ret))
ErrorType::Function(args, Box::new(closure), fx, Box::new(ret))
}
EmptyRecord => ErrorType::Record(SendMap::default(), TypeExt::Closed),

View file

@ -3680,12 +3680,23 @@ pub enum ErrorType {
TypeExt,
Polarity,
),
Function(Vec<ErrorType>, Box<ErrorType>, Box<ErrorType>),
Function(
Vec<ErrorType>,
Box<ErrorType>,
ErrorFunctionFx,
Box<ErrorType>,
),
Alias(Symbol, Vec<ErrorType>, Box<ErrorType>, AliasKind),
Range(Vec<ErrorType>),
Error,
}
#[derive(PartialEq, Eq, Clone, Hash)]
pub enum ErrorFunctionFx {
Pure,
Effectful,
}
impl std::fmt::Debug for ErrorType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// TODO remove clone
@ -3731,7 +3742,7 @@ impl ErrorType {
.for_each(|(_, ts)| ts.iter().for_each(|t| t.add_names(taken)));
ext.add_names(taken);
}
Function(args, capt, ret) => {
Function(args, capt, _fx, ret) => {
args.iter().for_each(|t| t.add_names(taken));
capt.add_names(taken);
ret.add_names(taken);
@ -3817,7 +3828,7 @@ fn write_error_type_help(
}
}
}
Function(arguments, _closure, result) => {
Function(arguments, _closure, fx, result) => {
let write_parens = parens != Parens::Unnecessary;
if write_parens {
@ -3833,7 +3844,10 @@ fn write_error_type_help(
}
}
buf.push_str(" -> ");
match fx {
ErrorFunctionFx::Pure => buf.push_str(" -> "),
ErrorFunctionFx::Effectful => buf.push_str(" => "),
}
write_error_type_help(interns, *result, buf, Parens::InFn);
@ -3967,7 +3981,7 @@ fn write_debug_error_type_help(error_type: ErrorType, buf: &mut String, parens:
buf.push(')');
}
}
Function(arguments, _closure, result) => {
Function(arguments, _closure, fx, result) => {
let write_parens = parens != Parens::Unnecessary;
if write_parens {
@ -3983,7 +3997,10 @@ fn write_debug_error_type_help(error_type: ErrorType, buf: &mut String, parens:
}
}
buf.push_str(" -> ");
match fx {
ErrorFunctionFx::Pure => buf.push_str(" -> "),
ErrorFunctionFx::Effectful => buf.push_str(" => "),
}
write_debug_error_type_help(*result, buf, Parens::InFn);