mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-11 05:34:11 +03:00
Merge branch 'trunk' into list-reverse
This commit is contained in:
commit
4cd9b72859
@ -719,6 +719,7 @@ fn pattern_to_vars_by_symbol(
|
||||
| FloatLiteral(_)
|
||||
| StrLiteral(_)
|
||||
| Underscore
|
||||
| MalformedPattern(_, _)
|
||||
| UnsupportedPattern(_) => {}
|
||||
|
||||
Shadowed(_, _) => {}
|
||||
|
@ -5,7 +5,7 @@ use roc_module::ident::{Ident, Lowercase, TagName};
|
||||
use roc_module::symbol::Symbol;
|
||||
use roc_parse::ast;
|
||||
use roc_parse::pattern::PatternType;
|
||||
use roc_problem::can::{Problem, RuntimeError};
|
||||
use roc_problem::can::{MalformedPatternProblem, Problem, RuntimeError};
|
||||
use roc_region::all::{Located, Region};
|
||||
use roc_types::subs::{VarStore, Variable};
|
||||
|
||||
@ -35,6 +35,8 @@ pub enum Pattern {
|
||||
Shadowed(Region, Located<Ident>),
|
||||
// Example: (5 = 1 + 2) is an unsupported pattern in an assignment; Int patterns aren't allowed in assignments!
|
||||
UnsupportedPattern(Region),
|
||||
// parse error patterns
|
||||
MalformedPattern(MalformedPatternProblem, Region),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@ -76,6 +78,7 @@ pub fn symbols_from_pattern_help(pattern: &Pattern, symbols: &mut Vec<Symbol>) {
|
||||
| FloatLiteral(_)
|
||||
| StrLiteral(_)
|
||||
| Underscore
|
||||
| MalformedPattern(_, _)
|
||||
| UnsupportedPattern(_) => {}
|
||||
|
||||
Shadowed(_, _) => {}
|
||||
@ -165,32 +168,30 @@ pub fn canonicalize_pattern<'a>(
|
||||
}
|
||||
|
||||
FloatLiteral(ref string) => match pattern_type {
|
||||
WhenBranch => {
|
||||
let float = finish_parsing_float(string)
|
||||
.unwrap_or_else(|_| panic!("TODO handle malformed float pattern"));
|
||||
|
||||
Pattern::FloatLiteral(float)
|
||||
}
|
||||
ptype @ DefExpr | ptype @ TopLevelDef | ptype @ FunctionArg => {
|
||||
unsupported_pattern(env, ptype, region)
|
||||
}
|
||||
WhenBranch => match finish_parsing_float(string) {
|
||||
Err(_error) => {
|
||||
let problem = MalformedPatternProblem::MalformedFloat;
|
||||
malformed_pattern(env, problem, region)
|
||||
}
|
||||
Ok(float) => Pattern::FloatLiteral(float),
|
||||
},
|
||||
ptype => unsupported_pattern(env, ptype, region),
|
||||
},
|
||||
|
||||
Underscore => match pattern_type {
|
||||
WhenBranch | FunctionArg => Pattern::Underscore,
|
||||
ptype @ DefExpr | ptype @ TopLevelDef => unsupported_pattern(env, ptype, region),
|
||||
ptype => unsupported_pattern(env, ptype, region),
|
||||
},
|
||||
|
||||
NumLiteral(string) => match pattern_type {
|
||||
WhenBranch => {
|
||||
let int = finish_parsing_int(string)
|
||||
.unwrap_or_else(|_| panic!("TODO handle malformed int pattern"));
|
||||
|
||||
Pattern::NumLiteral(var_store.fresh(), int)
|
||||
}
|
||||
ptype @ DefExpr | ptype @ TopLevelDef | ptype @ FunctionArg => {
|
||||
unsupported_pattern(env, ptype, region)
|
||||
}
|
||||
WhenBranch => match finish_parsing_int(string) {
|
||||
Err(_error) => {
|
||||
let problem = MalformedPatternProblem::MalformedInt;
|
||||
malformed_pattern(env, problem, region)
|
||||
}
|
||||
Ok(int) => Pattern::NumLiteral(var_store.fresh(), int),
|
||||
},
|
||||
ptype => unsupported_pattern(env, ptype, region),
|
||||
},
|
||||
|
||||
NonBase10Literal {
|
||||
@ -198,29 +199,33 @@ pub fn canonicalize_pattern<'a>(
|
||||
base,
|
||||
is_negative,
|
||||
} => match pattern_type {
|
||||
WhenBranch => {
|
||||
let int = finish_parsing_base(string, *base)
|
||||
.unwrap_or_else(|_| panic!("TODO handle malformed {:?} pattern", base));
|
||||
|
||||
if *is_negative {
|
||||
Pattern::IntLiteral(-int)
|
||||
} else {
|
||||
Pattern::IntLiteral(int)
|
||||
WhenBranch => match finish_parsing_base(string, *base) {
|
||||
Err(_error) => {
|
||||
let problem = MalformedPatternProblem::MalformedBase(*base);
|
||||
malformed_pattern(env, problem, region)
|
||||
}
|
||||
}
|
||||
ptype @ DefExpr | ptype @ TopLevelDef | ptype @ FunctionArg => {
|
||||
unsupported_pattern(env, ptype, region)
|
||||
}
|
||||
Ok(int) => {
|
||||
if *is_negative {
|
||||
Pattern::IntLiteral(-int)
|
||||
} else {
|
||||
Pattern::IntLiteral(int)
|
||||
}
|
||||
}
|
||||
},
|
||||
ptype => unsupported_pattern(env, ptype, region),
|
||||
},
|
||||
|
||||
StrLiteral(_string) => match pattern_type {
|
||||
StrLiteral(string) => match pattern_type {
|
||||
WhenBranch => {
|
||||
panic!("TODO check whether string pattern is malformed.");
|
||||
// Pattern::StrLiteral((*string).into())
|
||||
}
|
||||
ptype @ DefExpr | ptype @ TopLevelDef | ptype @ FunctionArg => {
|
||||
unsupported_pattern(env, ptype, region)
|
||||
// TODO report whether string was malformed
|
||||
Pattern::StrLiteral((*string).into())
|
||||
}
|
||||
ptype => unsupported_pattern(env, ptype, region),
|
||||
},
|
||||
|
||||
BlockStrLiteral(_lines) => match pattern_type {
|
||||
WhenBranch => todo!("TODO block string literal pattern"),
|
||||
ptype => unsupported_pattern(env, ptype, region),
|
||||
},
|
||||
|
||||
SpaceBefore(sub_pattern, _) | SpaceAfter(sub_pattern, _) | Nested(sub_pattern) => {
|
||||
@ -288,7 +293,7 @@ pub fn canonicalize_pattern<'a>(
|
||||
},
|
||||
});
|
||||
}
|
||||
_ => panic!("invalid pattern in record"),
|
||||
_ => unreachable!("Any other pattern should have given a parse error"),
|
||||
}
|
||||
}
|
||||
|
||||
@ -304,7 +309,15 @@ pub fn canonicalize_pattern<'a>(
|
||||
unreachable!("should have been handled in RecordDestructure");
|
||||
}
|
||||
|
||||
_ => panic!("TODO finish restoring can_pattern branch for {:?}", pattern),
|
||||
Malformed(_str) => {
|
||||
let problem = MalformedPatternProblem::Unknown;
|
||||
malformed_pattern(env, problem, region)
|
||||
}
|
||||
|
||||
QualifiedIdentifier { .. } => {
|
||||
let problem = MalformedPatternProblem::QualifiedIdentifier;
|
||||
malformed_pattern(env, problem, region)
|
||||
}
|
||||
};
|
||||
|
||||
Located {
|
||||
@ -325,6 +338,20 @@ fn unsupported_pattern<'a>(
|
||||
Pattern::UnsupportedPattern(region)
|
||||
}
|
||||
|
||||
/// When we detect a malformed pattern like `3.X` or `0b5`,
|
||||
/// report it to Env and return an UnsupportedPattern runtime error pattern.
|
||||
fn malformed_pattern<'a>(
|
||||
env: &mut Env<'a>,
|
||||
problem: MalformedPatternProblem,
|
||||
region: Region,
|
||||
) -> Pattern {
|
||||
env.problem(Problem::RuntimeError(RuntimeError::MalformedPattern(
|
||||
problem, region,
|
||||
)));
|
||||
|
||||
Pattern::MalformedPattern(problem, region)
|
||||
}
|
||||
|
||||
pub fn bindings_from_patterns<'a, I>(loc_patterns: I, scope: &Scope) -> Vec<(Symbol, Region)>
|
||||
where
|
||||
I: Iterator<Item = &'a Located<Pattern>>,
|
||||
@ -374,6 +401,7 @@ fn add_bindings_from_patterns(
|
||||
| StrLiteral(_)
|
||||
| Underscore
|
||||
| Shadowed(_, _)
|
||||
| MalformedPattern(_, _)
|
||||
| UnsupportedPattern(_) => (),
|
||||
}
|
||||
}
|
||||
|
@ -52,6 +52,7 @@ fn headers_from_annotation_help(
|
||||
}
|
||||
Underscore
|
||||
| Shadowed(_, _)
|
||||
| MalformedPattern(_, _)
|
||||
| UnsupportedPattern(_)
|
||||
| NumLiteral(_, _)
|
||||
| IntLiteral(_)
|
||||
@ -117,7 +118,7 @@ pub fn constrain_pattern(
|
||||
state: &mut PatternState,
|
||||
) {
|
||||
match pattern {
|
||||
Underscore | UnsupportedPattern(_) | Shadowed(_, _) => {
|
||||
Underscore | UnsupportedPattern(_) | MalformedPattern(_, _) | Shadowed(_, _) => {
|
||||
// Neither the _ pattern nor erroneous ones add any constraints.
|
||||
}
|
||||
|
||||
|
@ -342,7 +342,7 @@ fn constrain_pattern(
|
||||
state.constraints.push(tag_con);
|
||||
}
|
||||
|
||||
Underscore | Shadowed(_, _) | UnsupportedPattern(_) => {
|
||||
Underscore | Shadowed(_, _) | MalformedPattern(_, _) | UnsupportedPattern(_) => {
|
||||
// no constraints
|
||||
}
|
||||
}
|
||||
|
@ -446,6 +446,12 @@ fn pattern_to_when<'a>(
|
||||
(env.unique_symbol(), Located::at_zero(RuntimeError(error)))
|
||||
}
|
||||
|
||||
MalformedPattern(problem, region) => {
|
||||
// create the runtime error here, instead of delegating to When.
|
||||
let error = roc_problem::can::RuntimeError::MalformedPattern(*problem, *region);
|
||||
(env.unique_symbol(), Located::at_zero(RuntimeError(error)))
|
||||
}
|
||||
|
||||
AppliedTag { .. } | RecordDestructure { .. } => {
|
||||
let symbol = env.unique_symbol();
|
||||
|
||||
@ -1545,7 +1551,10 @@ fn from_can_pattern<'a>(
|
||||
StrLiteral(v) => Pattern::StrLiteral(v.clone()),
|
||||
Shadowed(region, ident) => Pattern::Shadowed(*region, ident.clone()),
|
||||
UnsupportedPattern(region) => Pattern::UnsupportedPattern(*region),
|
||||
|
||||
MalformedPattern(_problem, region) => {
|
||||
// TODO preserve malformed problem information here?
|
||||
Pattern::UnsupportedPattern(*region)
|
||||
}
|
||||
NumLiteral(var, num) => match num_argument_to_int_or_float(env.subs, *var) {
|
||||
IntOrFloat::IntType => Pattern::IntLiteral(*num),
|
||||
IntOrFloat::FloatType => Pattern::FloatLiteral(*num as u64),
|
||||
|
@ -3,6 +3,7 @@ use roc_collections::all::MutSet;
|
||||
use roc_module::ident::{Ident, Lowercase, TagName};
|
||||
use roc_module::operator::BinOp;
|
||||
use roc_module::symbol::{ModuleId, Symbol};
|
||||
use roc_parse::ast::Base;
|
||||
use roc_parse::pattern::PatternType;
|
||||
use roc_region::all::{Located, Region};
|
||||
|
||||
@ -69,6 +70,8 @@ pub enum RuntimeError {
|
||||
},
|
||||
// Example: (5 = 1 + 2) is an unsupported pattern in an assignment; Int patterns aren't allowed in assignments!
|
||||
UnsupportedPattern(Region),
|
||||
// Example: when 1 is 1.X -> 32
|
||||
MalformedPattern(MalformedPatternProblem, Region),
|
||||
UnrecognizedFunctionName(Located<InlinableString>),
|
||||
LookupNotInScope(Located<InlinableString>, MutSet<Box<str>>),
|
||||
ValueNotExposed {
|
||||
@ -89,9 +92,17 @@ pub enum RuntimeError {
|
||||
InvalidHex(std::num::ParseIntError, Box<str>),
|
||||
InvalidOctal(std::num::ParseIntError, Box<str>),
|
||||
InvalidBinary(std::num::ParseIntError, Box<str>),
|
||||
QualifiedPatternIdent(InlinableString),
|
||||
CircularDef(Vec<Symbol>, Vec<(Region /* pattern */, Region /* expr */)>),
|
||||
|
||||
/// When the author specifies a type annotation but no implementation
|
||||
NoImplementation,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum MalformedPatternProblem {
|
||||
MalformedInt,
|
||||
MalformedFloat,
|
||||
MalformedBase(Base),
|
||||
Unknown,
|
||||
QualifiedIdentifier,
|
||||
}
|
||||
|
@ -338,6 +338,41 @@ fn pretty_runtime_error<'b>(
|
||||
])
|
||||
}
|
||||
}
|
||||
RuntimeError::MalformedPattern(problem, region) => {
|
||||
use roc_parse::ast::Base;
|
||||
use roc_problem::can::MalformedPatternProblem::*;
|
||||
|
||||
let name = match problem {
|
||||
MalformedInt => " integer ",
|
||||
MalformedFloat => " float ",
|
||||
MalformedBase(Base::Hex) => " hex integer ",
|
||||
MalformedBase(Base::Binary) => " binary integer ",
|
||||
MalformedBase(Base::Octal) => " octal integer ",
|
||||
Unknown => " ",
|
||||
QualifiedIdentifier => " qualified ",
|
||||
};
|
||||
|
||||
let hint = match problem {
|
||||
MalformedInt | MalformedFloat | MalformedBase(_) => alloc
|
||||
.hint()
|
||||
.append(alloc.reflow("Learn more about number literals at TODO")),
|
||||
Unknown => alloc.nil(),
|
||||
QualifiedIdentifier => alloc.hint().append(
|
||||
alloc.reflow("In patterns, only private and global tags can be qualified"),
|
||||
),
|
||||
};
|
||||
|
||||
alloc.stack(vec![
|
||||
alloc.concat(vec![
|
||||
alloc.reflow("This"),
|
||||
alloc.text(name),
|
||||
alloc.reflow("pattern is malformed:"),
|
||||
]),
|
||||
alloc.region(region),
|
||||
hint,
|
||||
])
|
||||
}
|
||||
|
||||
other => {
|
||||
// // Example: (5 = 1 + 2) is an unsupported pattern in an assignment; Int patterns aren't allowed in assignments!
|
||||
// UnsupportedPattern(Region),
|
||||
|
@ -1468,6 +1468,131 @@ mod test_reporting {
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_int_pattern() {
|
||||
report_problem_as(
|
||||
indoc!(
|
||||
r#"
|
||||
when 1 is
|
||||
100A -> 3
|
||||
_ -> 4
|
||||
"#
|
||||
),
|
||||
indoc!(
|
||||
r#"
|
||||
-- SYNTAX PROBLEM --------------------------------------------------------------
|
||||
|
||||
This integer pattern is malformed:
|
||||
|
||||
2 ┆ 100A -> 3
|
||||
┆ ^^^^
|
||||
|
||||
Hint: Learn more about number literals at TODO
|
||||
"#
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_float_pattern() {
|
||||
report_problem_as(
|
||||
indoc!(
|
||||
r#"
|
||||
when 1 is
|
||||
2.X -> 3
|
||||
_ -> 4
|
||||
"#
|
||||
),
|
||||
indoc!(
|
||||
r#"
|
||||
-- SYNTAX PROBLEM --------------------------------------------------------------
|
||||
|
||||
This float pattern is malformed:
|
||||
|
||||
2 ┆ 2.X -> 3
|
||||
┆ ^^^
|
||||
|
||||
Hint: Learn more about number literals at TODO
|
||||
"#
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_hex_pattern() {
|
||||
report_problem_as(
|
||||
indoc!(
|
||||
r#"
|
||||
when 1 is
|
||||
0xZ -> 3
|
||||
_ -> 4
|
||||
"#
|
||||
),
|
||||
indoc!(
|
||||
r#"
|
||||
-- SYNTAX PROBLEM --------------------------------------------------------------
|
||||
|
||||
This hex integer pattern is malformed:
|
||||
|
||||
2 ┆ 0xZ -> 3
|
||||
┆ ^^^
|
||||
|
||||
Hint: Learn more about number literals at TODO
|
||||
"#
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_oct_pattern() {
|
||||
report_problem_as(
|
||||
indoc!(
|
||||
r#"
|
||||
when 1 is
|
||||
0o9 -> 3
|
||||
_ -> 4
|
||||
"#
|
||||
),
|
||||
indoc!(
|
||||
r#"
|
||||
-- SYNTAX PROBLEM --------------------------------------------------------------
|
||||
|
||||
This octal integer pattern is malformed:
|
||||
|
||||
2 ┆ 0o9 -> 3
|
||||
┆ ^^^
|
||||
|
||||
Hint: Learn more about number literals at TODO
|
||||
"#
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_bin_pattern() {
|
||||
report_problem_as(
|
||||
indoc!(
|
||||
r#"
|
||||
when 1 is
|
||||
0b4 -> 3
|
||||
_ -> 4
|
||||
"#
|
||||
),
|
||||
indoc!(
|
||||
r#"
|
||||
-- SYNTAX PROBLEM --------------------------------------------------------------
|
||||
|
||||
This binary integer pattern is malformed:
|
||||
|
||||
2 ┆ 0b4 -> 3
|
||||
┆ ^^^
|
||||
|
||||
Hint: Learn more about number literals at TODO
|
||||
"#
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_fields() {
|
||||
report_problem_as(
|
||||
|
Loading…
Reference in New Issue
Block a user