From 8dfc9c03678bf0f1c888fc791db5dc7dfb90dea7 Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Tue, 8 Nov 2022 13:44:07 -0600 Subject: [PATCH] Inline BadType::BadTypeArguments in canonicalization errors --- crates/compiler/can/src/annotation.rs | 16 ++--- crates/compiler/problem/src/can.rs | 10 ++- crates/reporting/src/error/canonicalize.rs | 79 ++++++++++------------ 3 files changed, 51 insertions(+), 54 deletions(-) diff --git a/crates/compiler/can/src/annotation.rs b/crates/compiler/can/src/annotation.rs index ef59cb49cf..c8e2394379 100644 --- a/crates/compiler/can/src/annotation.rs +++ b/crates/compiler/can/src/annotation.rs @@ -625,15 +625,13 @@ fn can_annotation_help( // use a known alias if alias.type_variables.len() != args.len() { - env.problem(roc_problem::can::Problem::BadType( - Problem::BadTypeArguments { - symbol, - region, - alias_needs: alias.type_variables.len() as u8, - type_got: args.len() as u8, - alias_kind: alias.kind, - }, - )); + env.problem(roc_problem::can::Problem::BadTypeArguments { + symbol, + region, + alias_needs: alias.type_variables.len() as u8, + type_got: args.len() as u8, + alias_kind: alias.kind, + }); return Type::Erroneous; } diff --git a/crates/compiler/problem/src/can.rs b/crates/compiler/problem/src/can.rs index bf7a11df54..c49efaab6e 100644 --- a/crates/compiler/problem/src/can.rs +++ b/crates/compiler/problem/src/can.rs @@ -188,7 +188,13 @@ pub enum Problem { MultipleListRestPattern { region: Region, }, - BadType(roc_types::types::Problem), + BadTypeArguments { + symbol: Symbol, + region: Region, + alias_needs: u8, + type_got: u8, + alias_kind: AliasKind, + }, } impl Problem { @@ -318,6 +324,7 @@ impl Problem { .. } | Problem::MultipleListRestPattern { region } + | Problem::BadTypeArguments { region, .. } | Problem::UnnecessaryOutputWildcard { region } => Some(*region), Problem::RuntimeError(RuntimeError::CircularDef(cycle_entries)) | Problem::BadRecursion(cycle_entries) => { @@ -331,7 +338,6 @@ impl Problem { | Problem::RuntimeError(RuntimeError::ExposedButNotDefined(_)) | Problem::RuntimeError(RuntimeError::NoImplementationNamed { .. }) | Problem::ExposedButNotDefined(_) => None, - Problem::BadType(..) => None, } } } diff --git a/crates/reporting/src/error/canonicalize.rs b/crates/reporting/src/error/canonicalize.rs index 8479a05d25..3188c21738 100644 --- a/crates/reporting/src/error/canonicalize.rs +++ b/crates/reporting/src/error/canonicalize.rs @@ -1037,53 +1037,46 @@ pub fn can_problem<'b>( title = "MULTIPLE LIST REST PATTERNS".to_string(); severity = Severity::RuntimeError; } - Problem::BadType(type_problem) => { - use roc_types::types::Problem::*; - match type_problem { - BadTypeArguments { - symbol, - region, - type_got, - alias_needs, - alias_kind, - } => { - let needed_arguments = if alias_needs == 1 { - alloc.reflow("1 type argument") - } else { - alloc - .text(alias_needs.to_string()) - .append(alloc.reflow(" type arguments")) - }; + Problem::BadTypeArguments { + symbol, + region, + type_got, + alias_needs, + alias_kind, + } => { + let needed_arguments = if alias_needs == 1 { + alloc.reflow("1 type argument") + } else { + alloc + .text(alias_needs.to_string()) + .append(alloc.reflow(" type arguments")) + }; - let found_arguments = alloc.text(type_got.to_string()); + let found_arguments = alloc.text(type_got.to_string()); - doc = alloc.stack([ - alloc.concat([ - alloc.reflow("The "), - alloc.symbol_unqualified(symbol), - alloc.reflow(" "), - alloc.reflow(alias_kind.as_str()), - alloc.reflow(" expects "), - needed_arguments, - alloc.reflow(", but it got "), - found_arguments, - alloc.reflow(" instead:"), - ]), - alloc.region(lines.convert_region(region)), - alloc.reflow("Are there missing parentheses?"), - ]); + doc = alloc.stack([ + alloc.concat([ + alloc.reflow("The "), + alloc.symbol_unqualified(symbol), + alloc.reflow(" "), + alloc.reflow(alias_kind.as_str()), + alloc.reflow(" expects "), + needed_arguments, + alloc.reflow(", but it got "), + found_arguments, + alloc.reflow(" instead:"), + ]), + alloc.region(lines.convert_region(region)), + alloc.reflow("Are there missing parentheses?"), + ]); - title = if type_got > alias_needs { - "TOO MANY TYPE ARGUMENTS".to_string() - } else { - "TOO FEW TYPE ARGUMENTS".to_string() - }; + title = if type_got > alias_needs { + "TOO MANY TYPE ARGUMENTS".to_string() + } else { + "TOO FEW TYPE ARGUMENTS".to_string() + }; - severity = Severity::RuntimeError; - } - - other => panic!("unhandled bad type: {:?}", other), - } + severity = Severity::RuntimeError; } };