Remove param arguments from TOO MANY ARGS error

This commit is contained in:
Agus Zubiaga 2024-08-27 15:57:47 -03:00
parent 8b9cb22f49
commit 658c6963c5
No known key found for this signature in database
7 changed files with 173 additions and 8 deletions

View file

@ -4627,6 +4627,7 @@ struct SolveResult {
exposed_vars_by_symbol: Vec<(Symbol, Variable)>,
problems: Vec<TypeError>,
abilities_store: AbilitiesStore,
imported_modules_with_params: Vec<ModuleId>,
#[cfg(debug_assertions)]
checkmate: Option<roc_checkmate::Collector>,
@ -4672,6 +4673,11 @@ fn run_solve_solve(
&mut imported_flex_vars,
);
let imported_modules_with_params = imported_param_vars
.keys()
.copied()
.collect::<Vec<ModuleId>>();
let actual_constraint = constraints.let_import_constraint(
imported_rigid_vars,
imported_flex_vars,
@ -4754,6 +4760,7 @@ fn run_solve_solve(
exposed_vars_by_symbol,
problems: errors,
abilities_store: resolved_abilities_store,
imported_modules_with_params,
#[cfg(debug_assertions)]
checkmate,
@ -4786,6 +4793,7 @@ fn run_solve<'a>(
let aliases = module.aliases.clone();
let opt_params_var = module.module_params.as_ref().map(|params| params.whole_var);
let home_has_params = opt_params_var.is_some();
let mut module = module;
let loc_expects = std::mem::take(&mut module.loc_expects);
@ -4820,6 +4828,8 @@ fn run_solve<'a>(
exposed_vars_by_symbol,
problems: vec![],
abilities_store: abilities,
// todo: agus
imported_modules_with_params: vec![],
#[cfg(debug_assertions)]
checkmate: None,
@ -4847,8 +4857,9 @@ fn run_solve<'a>(
solved: mut solved_subs,
solved_implementations,
exposed_vars_by_symbol,
problems,
mut problems,
abilities_store,
imported_modules_with_params,
#[cfg(debug_assertions)]
checkmate,
@ -4863,6 +4874,14 @@ fn run_solve<'a>(
&abilities_store,
);
// todo: check exec mode
roc_lower_params::type_error::remove_module_param_arguments(
&mut problems,
home_has_params,
&decls.symbols,
imported_modules_with_params,
);
let solved_module = SolvedModule {
exposed_vars_by_symbol,
problems,

View file

@ -13,5 +13,6 @@ roc_module = { path = "../module" }
roc_region = { path = "../region" }
roc_types = { path = "../types" }
roc_collections = { path = "../collections" }
roc_solve_problem = { path = "../solve_problem" }
bumpalo.workspace = true

View file

@ -1 +1,2 @@
pub mod lower;
pub mod type_error;

View file

@ -0,0 +1,105 @@
use roc_can::expected::Expected;
use roc_module::symbol::{ModuleId, Symbol};
use roc_region::all::Loc;
use roc_solve_problem::TypeError;
use roc_types::types::{ErrorType, Reason};
/// Removes the arguments added as a result of lowering params
pub fn remove_module_param_arguments(
errors: &mut Vec<TypeError>,
home_has_params: bool,
home_top_level_symbols: &[Loc<Symbol>],
imported_modules_with_params: Vec<ModuleId>,
) {
if errors.is_empty() || (!home_has_params && imported_modules_with_params.is_empty()) {
return;
}
let home_top_level_symbols = if home_has_params {
home_top_level_symbols
.iter()
.map(|loc| loc.value)
.collect::<Vec<_>>()
} else {
vec![]
};
let is_extended = |symbol: &Symbol| {
imported_modules_with_params.contains(&symbol.module_id())
|| home_top_level_symbols.contains(symbol)
};
for error in errors {
match error {
TypeError::BadExpr(
_,
_,
found,
Expected::ForReason(
Reason::FnCall {
name: Some(name),
arity,
called_via: _,
},
err_type,
_,
),
) if is_extended(name) => {
*arity -= 1;
drop_last_argument(found);
drop_last_argument(err_type);
}
TypeError::BadExpr(_, _, _, _) => todo!(),
// Irrelevant
TypeError::BadPattern(_, _, _, _)
| TypeError::CircularType(_, _, _)
| TypeError::CircularDef(_)
| TypeError::UnexposedLookup(_, _)
| TypeError::UnfulfilledAbility(_)
| TypeError::BadExprMissingAbility(_, _, _, _)
| TypeError::BadPatternMissingAbility(_, _, _, _)
| TypeError::Exhaustive(_)
| TypeError::StructuralSpecialization {
region: _,
typ: _,
ability: _,
member: _,
}
| TypeError::WrongSpecialization {
region: _,
ability_member: _,
expected_opaque: _,
found_opaque: _,
}
| TypeError::IngestedFileBadUtf8(_, _)
| TypeError::IngestedFileUnsupportedType(_, _)
| TypeError::UnexpectedModuleParams(_, _)
| TypeError::MissingModuleParams(_, _, _)
| TypeError::ModuleParamsMismatch(_, _, _, _) => {}
}
}
}
fn drop_last_argument(err_type: &mut ErrorType) {
match err_type {
ErrorType::Function(arguments, _, _) => {
arguments.pop();
}
// Irrelevant
ErrorType::Infinite
| ErrorType::Type(_, _)
| ErrorType::FlexVar(_)
| ErrorType::RigidVar(_)
| ErrorType::FlexAbleVar(_, _)
| ErrorType::RigidAbleVar(_, _)
| ErrorType::Record(_, _)
| ErrorType::Tuple(_, _)
| ErrorType::TagUnion(_, _, _)
| ErrorType::RecursiveTagUnion(_, _, _, _)
| ErrorType::Alias(_, _, _, _)
| ErrorType::Range(_)
| ErrorType::Error => {}
}
}