report malformed type names in annotations

This commit is contained in:
Folkert 2021-03-21 22:52:23 +01:00
parent 22f77ed966
commit b681923270
4 changed files with 129 additions and 50 deletions

View File

@ -65,6 +65,13 @@ impl IntroducedVariables {
}
}
fn malformed(env: &mut Env, region: Region, name: &str) {
use roc_problem::can::RuntimeError::*;
let problem = MalformedTypeName((*name).into(), region);
env.problem(roc_problem::can::Problem::RuntimeError(problem.clone()));
}
pub fn canonicalize_annotation(
env: &mut Env,
scope: &mut Scope,
@ -446,7 +453,16 @@ fn can_annotation_help(
local_aliases,
references,
),
Wildcard | Malformed(_) => {
Wildcard => {
let var = var_store.fresh();
introduced_variables.insert_wildcard(var);
Type::Variable(var)
}
Malformed(string) => {
malformed(env, region, string);
let var = var_store.fresh();
introduced_variables.insert_wildcard(var);
@ -542,8 +558,9 @@ fn can_assigned_fields<'a>(
field = nested;
continue 'inner;
}
Malformed(_) => {
// TODO report this?
Malformed(string) => {
malformed(env, region, string);
// completely skip this element, advance to the next tag
continue 'outer;
}
@ -645,8 +662,9 @@ fn can_tags<'a>(
tag = nested;
continue 'inner;
}
Tag::Malformed(_) => {
// TODO report this?
Tag::Malformed(string) => {
malformed(env, region, string);
// completely skip this element, advance to the next tag
continue 'outer;
}

View File

@ -134,6 +134,7 @@ pub enum RuntimeError {
},
InvalidPrecedence(PrecedenceProblem, Region),
MalformedIdentifier(Box<str>, roc_parse::ident::BadIdent, Region),
MalformedTypeName(Box<str>, Region),
MalformedClosure(Region),
InvalidRecordUpdate {
region: Region,

View File

@ -776,8 +776,20 @@ fn pretty_runtime_error<'b>(
}
RuntimeError::MalformedIdentifier(_box_str, bad_ident, surroundings) => {
to_bad_ident_expr_report(alloc, bad_ident, surroundings)
}
RuntimeError::MalformedTypeName(_box_str, surroundings) => {
alloc.stack(vec![
alloc.reflow(r"I am confused by this type name:"),
alloc.region(surroundings),
alloc.concat(vec![
alloc.reflow("Type names start with an uppercase letter, "),
alloc.reflow("and can optionally be qualified by a module name, like "),
alloc.parser_suggestion("Bool"),
alloc.reflow(" or "),
alloc.parser_suggestion("Http.Request.Request"),
alloc.reflow("."),
]),
])
}
RuntimeError::MalformedClosure(_) => todo!(""),
RuntimeError::InvalidFloat(sign @ FloatErrorKind::PositiveInfinity, region, _raw_str)

View File

@ -3265,20 +3265,20 @@ mod test_reporting {
indoc!(
r#"
TYPE MISMATCH
Something is off with the body of the `x` definition:
4 x : AList I64 I64
5 x = ACons 0 (BCons 1 (ACons "foo" BNil ))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This `ACons` global tag application has the type:
[ ACons Num (Integer Signed64) [ BCons (Num a) [ ACons Str [ BNil
]b ]c ]d, ANil ]
But the type annotation on `x` says it should be:
[ ACons I64 BList I64 I64, ANil ]
"#
),
@ -4155,12 +4155,12 @@ mod test_reporting {
indoc!(
r#"
UNKNOWN OPERATOR
This looks like an operator, but it's not one I recognize!
1 f :: I64
^^
I have no specific suggestion for this operator, see TODO for the full
list of operators in Roc.
"#
@ -4188,12 +4188,12 @@ mod test_reporting {
indoc!(
r#"
TOO MANY ARGS
This value is not a function, but it was given 3 arguments:
3 x == 5
^
Are there any missing commas? Or missing parentheses?
"#
),
@ -4476,12 +4476,12 @@ mod test_reporting {
indoc!(
r#"
TAB CHARACTER
I encountered a tab character
1 # comment with a
1 # comment with a
^
Tab characters are not allowed.
"#
),
@ -4500,9 +4500,9 @@ mod test_reporting {
indoc!(
r#"
BAD TYPE VARIABLE
I am expecting a type variable, but I got stuck here:
1 f : (
^
"#
@ -4545,7 +4545,19 @@ mod test_reporting {
f
"#
),
indoc!(r#""#),
indoc!(
r#"
SYNTAX PROBLEM
I am confused by this type name:
1 f : Foo..Bar
^^^^^^^^
Type names start with an uppercase letter, and can optionally be
qualified by a module name, like Bool or Http.Request.Request.
"#
),
)
// ── DOUBLE DOT ──────────────────────────────────────────────────────────────────
@ -4568,7 +4580,19 @@ mod test_reporting {
f
"#
),
indoc!(r#""#),
indoc!(
r#"
SYNTAX PROBLEM
I am confused by this type name:
1 f : Foo.Bar.
^^^^^^^^
Type names start with an uppercase letter, and can optionally be
qualified by a module name, like Bool or Http.Request.Request.
"#
),
)
// ── TRAILING DOT ────────────────────────────────────────────────────────────────
@ -4619,7 +4643,19 @@ mod test_reporting {
f
"#
),
indoc!(r#""#),
indoc!(
r#"
SYNTAX PROBLEM
I am confused by this type name:
1 f : Foo.1
^^^^^
Type names start with an uppercase letter, and can optionally be
qualified by a module name, like Bool or Http.Request.Request.
"#
),
)
// ── WEIRD QUALIFIED NAME ────────────────────────────────────────────────────────
@ -4643,7 +4679,19 @@ mod test_reporting {
f
"#
),
indoc!(r#""#),
indoc!(
r#"
SYNTAX PROBLEM
I am confused by this type name:
1 f : Foo.foo
^^^^^^^
Type names start with an uppercase letter, and can optionally be
qualified by a module name, like Bool or Http.Request.Request.
"#
),
)
}
@ -4658,19 +4706,19 @@ mod test_reporting {
indoc!(
r#"
MISSING FINAL EXPRESSION
I am partway through parsing a definition's final expression, but I
got stuck here:
1 f : Foo.foo
^
This definition is missing a final expression. A nested definition
must be followed by either another definition, or an expression
x = 4
y = 2
x + y
"#
),
@ -4973,13 +5021,13 @@ mod test_reporting {
indoc!(
r#"
MISSING EXPRESSION
I am partway through parsing a definition, but I got stuck here:
1 when Just 4 is
2 Just when ->
^
I was expecting to see an expression like 42 or "hello".
"#
),
@ -5646,14 +5694,14 @@ mod test_reporting {
indoc!(
r#"
SYNTAX PROBLEM
I cannot find a `bar` value
1 [ "foo", bar("") ]
^^^
these names seem close though:
Nat
Str
U8
@ -5729,16 +5777,16 @@ mod test_reporting {
indoc!(
r#"
UNKNOWN OPERATOR
This looks like an operator, but it's not one I recognize!
1 main =
2 (\x -> x) : I64
^
The has-type operator : can only occur in a definition's type
signature, like
increment : I64 -> I64
increment = \x -> x + 1
"#
@ -5759,19 +5807,19 @@ mod test_reporting {
indoc!(
r#"
MISSING FINAL EXPRESSION
I am partway through parsing a definition's final expression, but I
got stuck here:
1 main = 5 -> 3
^
This definition is missing a final expression. A nested definition
must be followed by either another definition, or an expression
x = 4
y = 2
x + y
"#
),