mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-03 19:58:18 +00:00
Get severity from type problem variants
This commit is contained in:
parent
5414b4b60f
commit
39f89e3d65
5 changed files with 99 additions and 37 deletions
|
@ -11,3 +11,4 @@ roc_collections = { path = "../collections" }
|
|||
roc_region = { path = "../region" }
|
||||
roc_module = { path = "../module" }
|
||||
roc_error_macros = { path = "../../error_macros" }
|
||||
roc_problem = { path = "../problem" }
|
||||
|
|
|
@ -7,6 +7,7 @@ use roc_module::{
|
|||
ident::{Lowercase, TagIdIntType, TagName},
|
||||
symbol::Symbol,
|
||||
};
|
||||
use roc_problem::Severity;
|
||||
use roc_region::all::Region;
|
||||
|
||||
use self::Pattern::*;
|
||||
|
@ -149,6 +150,17 @@ pub enum Error {
|
|||
},
|
||||
}
|
||||
|
||||
impl Error {
|
||||
pub fn severity(&self) -> Severity {
|
||||
use Severity::*;
|
||||
match self {
|
||||
Error::Incomplete(..) => RuntimeError,
|
||||
Error::Redundant { .. } => Warning,
|
||||
Error::Unmatchable { .. } => Warning,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Context {
|
||||
BadArg,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//! Provides types to describe problems that can occur during solving.
|
||||
use roc_can::expected::{Expected, PExpected};
|
||||
use roc_module::{ident::Lowercase, symbol::Symbol};
|
||||
use roc_problem::can::CycleEntry;
|
||||
use roc_problem::{can::CycleEntry, Severity};
|
||||
use roc_region::all::Region;
|
||||
|
||||
use roc_types::types::{Category, ErrorType, PatternCategory};
|
||||
|
@ -31,6 +31,27 @@ pub enum TypeError {
|
|||
},
|
||||
}
|
||||
|
||||
impl TypeError {
|
||||
pub fn severity(&self) -> Severity {
|
||||
use Severity::*;
|
||||
match self {
|
||||
TypeError::BadExpr(..) => RuntimeError,
|
||||
TypeError::BadPattern(..) => RuntimeError,
|
||||
TypeError::CircularType(..) => RuntimeError,
|
||||
TypeError::CircularDef(_) => RuntimeError,
|
||||
TypeError::UnexposedLookup(_) => RuntimeError,
|
||||
TypeError::UnfulfilledAbility(_) => RuntimeError,
|
||||
TypeError::BadExprMissingAbility(_, _, _, _) => RuntimeError,
|
||||
TypeError::BadPatternMissingAbility(_, _, _, _) => RuntimeError,
|
||||
// NB: if bidirectional exhaustiveness checking is implemented, the other direction
|
||||
// is also not a runtime error.
|
||||
TypeError::Exhaustive(exhtv) => exhtv.severity(),
|
||||
TypeError::StructuralSpecialization { .. } => RuntimeError,
|
||||
TypeError::WrongSpecialization { .. } => RuntimeError,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
pub enum Unfulfilled {
|
||||
/// No claimed implementation of an ability for an opaque type.
|
||||
|
|
|
@ -39,26 +39,30 @@ pub fn type_problem<'b>(
|
|||
) -> Option<Report<'b>> {
|
||||
use TypeError::*;
|
||||
|
||||
fn report(title: String, doc: RocDocBuilder<'_>, filename: PathBuf) -> Option<Report<'_>> {
|
||||
Some(Report {
|
||||
title,
|
||||
filename,
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
})
|
||||
}
|
||||
let severity = problem.severity();
|
||||
|
||||
let report =
|
||||
move |title: String, doc: RocDocBuilder<'b>, filename: PathBuf| -> Option<Report<'b>> {
|
||||
Some(Report {
|
||||
title,
|
||||
filename,
|
||||
doc,
|
||||
severity,
|
||||
})
|
||||
};
|
||||
|
||||
match problem {
|
||||
BadExpr(region, category, found, expected) => Some(to_expr_report(
|
||||
alloc, lines, filename, region, category, found, expected,
|
||||
alloc, lines, filename, severity, region, category, found, expected,
|
||||
)),
|
||||
BadPattern(region, category, found, expected) => Some(to_pattern_report(
|
||||
alloc, lines, filename, region, category, found, expected,
|
||||
alloc, lines, filename, severity, region, category, found, expected,
|
||||
)),
|
||||
CircularType(region, symbol, overall_type) => Some(to_circular_report(
|
||||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
region,
|
||||
symbol,
|
||||
overall_type,
|
||||
|
@ -104,7 +108,7 @@ pub fn type_problem<'b>(
|
|||
title: "TYPE MISMATCH".to_string(),
|
||||
filename,
|
||||
doc: alloc.stack(stack),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
};
|
||||
Some(report)
|
||||
}
|
||||
|
@ -126,7 +130,7 @@ pub fn type_problem<'b>(
|
|||
title: "TYPE MISMATCH".to_string(),
|
||||
filename,
|
||||
doc: alloc.stack(stack),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
};
|
||||
Some(report)
|
||||
}
|
||||
|
@ -134,7 +138,6 @@ pub fn type_problem<'b>(
|
|||
CircularDef(entries) => {
|
||||
let doc = to_circular_def_doc(alloc, lines, &entries);
|
||||
let title = CIRCULAR_DEF.to_string();
|
||||
let severity = Severity::RuntimeError;
|
||||
|
||||
Some(Report {
|
||||
title,
|
||||
|
@ -170,7 +173,7 @@ pub fn type_problem<'b>(
|
|||
title: "ILLEGAL SPECIALIZATION".to_string(),
|
||||
filename,
|
||||
doc: alloc.stack(stack),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
})
|
||||
}
|
||||
WrongSpecialization {
|
||||
|
@ -199,7 +202,7 @@ pub fn type_problem<'b>(
|
|||
title: "WRONG SPECIALIZATION TYPE".to_string(),
|
||||
filename,
|
||||
doc: alloc.stack(stack),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -457,6 +460,7 @@ fn report_mismatch<'b>(
|
|||
alloc: &'b RocDocAllocator<'b>,
|
||||
lines: &LineInfo,
|
||||
filename: PathBuf,
|
||||
severity: Severity,
|
||||
category: &Category,
|
||||
found: ErrorType,
|
||||
expected_type: ErrorType,
|
||||
|
@ -493,7 +497,7 @@ fn report_mismatch<'b>(
|
|||
title: "TYPE MISMATCH".to_string(),
|
||||
filename,
|
||||
doc: alloc.stack(lines),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -502,6 +506,7 @@ fn report_bad_type<'b>(
|
|||
alloc: &'b RocDocAllocator<'b>,
|
||||
lines: &LineInfo,
|
||||
filename: PathBuf,
|
||||
severity: Severity,
|
||||
category: &Category,
|
||||
found: ErrorType,
|
||||
expected_type: ErrorType,
|
||||
|
@ -536,7 +541,7 @@ fn report_bad_type<'b>(
|
|||
title: "TYPE MISMATCH".to_string(),
|
||||
filename,
|
||||
doc: alloc.stack(lines),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -564,6 +569,7 @@ fn to_expr_report<'b>(
|
|||
alloc: &'b RocDocAllocator<'b>,
|
||||
lines: &LineInfo,
|
||||
filename: PathBuf,
|
||||
severity: Severity,
|
||||
expr_region: roc_region::all::Region,
|
||||
category: Category,
|
||||
found: ErrorType,
|
||||
|
@ -591,6 +597,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
opt_sym,
|
||||
".",
|
||||
field,
|
||||
|
@ -621,7 +628,7 @@ fn to_expr_report<'b>(
|
|||
alloc.region(lines.convert_region(expr_region)),
|
||||
comparison,
|
||||
]),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
Expected::FromAnnotation(name, _arity, annotation_source, expected_type) => {
|
||||
|
@ -744,7 +751,7 @@ fn to_expr_report<'b>(
|
|||
},
|
||||
comparison,
|
||||
]),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
Expected::ForReason(reason, expected_type, region) => match reason {
|
||||
|
@ -761,6 +768,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -800,6 +808,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -838,6 +847,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -866,6 +876,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -900,6 +911,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -925,6 +937,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -964,6 +977,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -979,6 +993,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -1014,6 +1029,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -1032,6 +1048,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
Some(symbol),
|
||||
"",
|
||||
field,
|
||||
|
@ -1046,6 +1063,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -1088,7 +1106,7 @@ fn to_expr_report<'b>(
|
|||
filename,
|
||||
title: "TOO MANY ARGS".to_string(),
|
||||
doc: alloc.stack(lines),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
n => {
|
||||
|
@ -1123,7 +1141,7 @@ fn to_expr_report<'b>(
|
|||
filename,
|
||||
title: "TOO MANY ARGS".to_string(),
|
||||
doc: alloc.stack(lines),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
} else {
|
||||
let lines = vec![
|
||||
|
@ -1150,7 +1168,7 @@ fn to_expr_report<'b>(
|
|||
filename,
|
||||
title: "TOO FEW ARGS".to_string(),
|
||||
doc: alloc.stack(lines),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1167,6 +1185,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -1191,6 +1210,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -1242,6 +1262,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -1282,6 +1303,7 @@ fn to_expr_report<'b>(
|
|||
alloc,
|
||||
lines,
|
||||
filename,
|
||||
severity,
|
||||
&category,
|
||||
found,
|
||||
expected_type,
|
||||
|
@ -1335,7 +1357,7 @@ fn to_expr_report<'b>(
|
|||
title: "TYPE MISMATCH".to_string(),
|
||||
filename,
|
||||
doc: alloc.stack(lines),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1372,7 +1394,7 @@ fn to_expr_report<'b>(
|
|||
filename,
|
||||
title: "TYPE MISMATCH".to_string(),
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1408,7 +1430,7 @@ fn to_expr_report<'b>(
|
|||
filename,
|
||||
title: "TYPE MISMATCH".to_string(),
|
||||
doc: alloc.stack(lines),
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1757,6 +1779,7 @@ fn to_pattern_report<'b>(
|
|||
alloc: &'b RocDocAllocator<'b>,
|
||||
lines: &LineInfo,
|
||||
filename: PathBuf,
|
||||
severity: Severity,
|
||||
expr_region: roc_region::all::Region,
|
||||
category: PatternCategory,
|
||||
found: ErrorType,
|
||||
|
@ -1783,7 +1806,7 @@ fn to_pattern_report<'b>(
|
|||
filename,
|
||||
title: "TYPE MISMATCH".to_string(),
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1826,7 +1849,7 @@ fn to_pattern_report<'b>(
|
|||
filename,
|
||||
title: "TYPE MISMATCH".to_string(),
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
PReason::WhenMatch { index, sub_pattern } => {
|
||||
|
@ -1906,7 +1929,7 @@ fn to_pattern_report<'b>(
|
|||
filename,
|
||||
title: "TYPE MISMATCH".to_string(),
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
PReason::ListElem => {
|
||||
|
@ -1933,7 +1956,7 @@ fn to_pattern_report<'b>(
|
|||
filename,
|
||||
title: "TYPE MISMATCH".to_string(),
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
PReason::TagArg { .. } | PReason::PatternGuard => {
|
||||
|
@ -2008,6 +2031,7 @@ fn to_circular_report<'b>(
|
|||
alloc: &'b RocDocAllocator<'b>,
|
||||
lines: &LineInfo,
|
||||
filename: PathBuf,
|
||||
severity: Severity,
|
||||
region: roc_region::all::Region,
|
||||
symbol: Symbol,
|
||||
overall_type: ErrorType,
|
||||
|
@ -2032,7 +2056,7 @@ fn to_circular_report<'b>(
|
|||
]),
|
||||
])
|
||||
},
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4083,6 +4107,7 @@ fn report_record_field_typo<'b>(
|
|||
alloc: &'b RocDocAllocator<'b>,
|
||||
lines: &LineInfo,
|
||||
filename: PathBuf,
|
||||
severity: Severity,
|
||||
opt_sym: Option<Symbol>,
|
||||
field_prefix: &str,
|
||||
field: &Lowercase,
|
||||
|
@ -4164,7 +4189,7 @@ fn report_record_field_typo<'b>(
|
|||
filename,
|
||||
title: "TYPE MISMATCH".to_string(),
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4177,6 +4202,8 @@ fn exhaustive_problem<'a>(
|
|||
use roc_exhaustive::Context::*;
|
||||
use roc_exhaustive::Error::*;
|
||||
|
||||
let severity = problem.severity();
|
||||
|
||||
match problem {
|
||||
Incomplete(region, context, missing) => match context {
|
||||
BadArg => {
|
||||
|
@ -4199,7 +4226,7 @@ fn exhaustive_problem<'a>(
|
|||
filename,
|
||||
title: "UNSAFE PATTERN".to_string(),
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
BadDestruct => {
|
||||
|
@ -4223,7 +4250,7 @@ fn exhaustive_problem<'a>(
|
|||
filename,
|
||||
title: "UNSAFE PATTERN".to_string(),
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
BadCase => {
|
||||
|
@ -4247,7 +4274,7 @@ fn exhaustive_problem<'a>(
|
|||
filename,
|
||||
title: "UNSAFE PATTERN".to_string(),
|
||||
doc,
|
||||
severity: Severity::RuntimeError,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -4276,7 +4303,7 @@ fn exhaustive_problem<'a>(
|
|||
filename,
|
||||
title: "REDUNDANT PATTERN".to_string(),
|
||||
doc,
|
||||
severity: Severity::Warning,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
Unmatchable {
|
||||
|
@ -4304,7 +4331,7 @@ fn exhaustive_problem<'a>(
|
|||
filename,
|
||||
title: "UNMATCHABLE PATTERN".to_string(),
|
||||
doc,
|
||||
severity: Severity::Warning,
|
||||
severity,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue