mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
Constrain + solve crash
This commit is contained in:
parent
9dc489c2b0
commit
e2b30e5301
13 changed files with 194 additions and 60 deletions
|
@ -376,7 +376,10 @@ fn deep_copy_expr_help<C: CopyEnv>(env: &mut C, copied: &mut Vec<Variable>, expr
|
|||
*called_via,
|
||||
)
|
||||
}
|
||||
Crash => Crash,
|
||||
Crash { msg, ret_var } => Crash {
|
||||
msg: Box::new(msg.map(|m| go_help!(m))),
|
||||
ret_var: sub!(*ret_var),
|
||||
},
|
||||
RunLowLevel { op, args, ret_var } => RunLowLevel {
|
||||
op: *op,
|
||||
args: args
|
||||
|
|
|
@ -167,7 +167,10 @@ pub enum Expr {
|
|||
EmptyRecord,
|
||||
|
||||
/// The "crash" keyword
|
||||
Crash,
|
||||
Crash {
|
||||
msg: Box<Loc<Expr>>,
|
||||
ret_var: Variable,
|
||||
},
|
||||
|
||||
/// Look up exactly one field on a record, e.g. (expr).foo.
|
||||
Access {
|
||||
|
@ -312,7 +315,7 @@ impl Expr {
|
|||
}
|
||||
Self::Expect { .. } => Category::Expect,
|
||||
Self::ExpectFx { .. } => Category::Expect,
|
||||
Self::Crash => Category::Crash,
|
||||
Self::Crash { .. } => Category::Crash,
|
||||
|
||||
Self::Dbg { .. } => Category::Expect,
|
||||
|
||||
|
@ -788,6 +791,47 @@ pub fn canonicalize_expr<'a>(
|
|||
}
|
||||
}
|
||||
}
|
||||
} else if let ast::Expr::Crash = loc_fn.value {
|
||||
// We treat crash specially, since crashing must be applied with one argument.
|
||||
|
||||
debug_assert!(!args.is_empty());
|
||||
|
||||
let mut args = Vec::new();
|
||||
let mut output = Output::default();
|
||||
|
||||
for loc_arg in loc_args.iter() {
|
||||
let (arg_expr, arg_out) =
|
||||
canonicalize_expr(env, var_store, scope, loc_arg.region, &loc_arg.value);
|
||||
|
||||
args.push(arg_expr);
|
||||
output.references.union_mut(&arg_out.references);
|
||||
}
|
||||
|
||||
let crash = if args.len() > 1 {
|
||||
let args_region = Region::span_across(
|
||||
&loc_args.first().unwrap().region,
|
||||
&loc_args.last().unwrap().region,
|
||||
);
|
||||
env.problem(Problem::OverAppliedCrash {
|
||||
region: args_region,
|
||||
});
|
||||
// Still crash, just with our own message, and drop the references.
|
||||
Crash {
|
||||
msg: Box::new(Loc::at(
|
||||
region,
|
||||
Expr::Str(String::from("hit a crash!").into_boxed_str()),
|
||||
)),
|
||||
ret_var: var_store.fresh(),
|
||||
}
|
||||
} else {
|
||||
let msg = args.pop().unwrap();
|
||||
Crash {
|
||||
msg: Box::new(msg),
|
||||
ret_var: var_store.fresh(),
|
||||
}
|
||||
};
|
||||
|
||||
(crash, output)
|
||||
} else {
|
||||
// Canonicalize the function expression and its arguments
|
||||
let (fn_expr, fn_expr_output) =
|
||||
|
@ -878,7 +922,22 @@ pub fn canonicalize_expr<'a>(
|
|||
|
||||
(RuntimeError(problem), Output::default())
|
||||
}
|
||||
ast::Expr::Crash => (Crash, Output::default()),
|
||||
ast::Expr::Crash => {
|
||||
// Naked crashes aren't allowed; we'll admit this with our own message, but yield an
|
||||
// error.
|
||||
env.problem(Problem::UnappliedCrash { region });
|
||||
|
||||
(
|
||||
Crash {
|
||||
msg: Box::new(Loc::at(
|
||||
region,
|
||||
Expr::Str(String::from("hit a crash!").into_boxed_str()),
|
||||
)),
|
||||
ret_var: var_store.fresh(),
|
||||
},
|
||||
Output::default(),
|
||||
)
|
||||
}
|
||||
ast::Expr::Defs(loc_defs, loc_ret) => {
|
||||
// The body expression gets a new scope for canonicalization,
|
||||
scope.inner_scope(|inner_scope| {
|
||||
|
@ -1726,7 +1785,7 @@ pub fn inline_calls(var_store: &mut VarStore, expr: Expr) -> Expr {
|
|||
| other @ TypedHole { .. }
|
||||
| other @ ForeignCall { .. }
|
||||
| other @ OpaqueWrapFunction(_)
|
||||
| other @ Crash => other,
|
||||
| other @ Crash { .. } => other,
|
||||
|
||||
List {
|
||||
elem_var,
|
||||
|
@ -2834,8 +2893,7 @@ fn get_lookup_symbols(expr: &Expr) -> Vec<ExpectLookup> {
|
|||
| Expr::EmptyRecord
|
||||
| Expr::TypedHole(_)
|
||||
| Expr::RuntimeError(_)
|
||||
| Expr::OpaqueWrapFunction(_)
|
||||
| Expr::Crash => {}
|
||||
| Expr::OpaqueWrapFunction(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -981,6 +981,14 @@ fn fix_values_captured_in_closure_expr(
|
|||
);
|
||||
}
|
||||
|
||||
Crash { msg, ret_var: _ } => {
|
||||
fix_values_captured_in_closure_expr(
|
||||
&mut msg.value,
|
||||
no_capture_symbols,
|
||||
closure_captures,
|
||||
);
|
||||
}
|
||||
|
||||
Closure(ClosureData {
|
||||
captured_symbols,
|
||||
name,
|
||||
|
@ -1056,8 +1064,7 @@ fn fix_values_captured_in_closure_expr(
|
|||
| TypedHole { .. }
|
||||
| RuntimeError(_)
|
||||
| ZeroArgumentTag { .. }
|
||||
| Accessor { .. }
|
||||
| Crash => {}
|
||||
| Accessor { .. } => {}
|
||||
|
||||
List { loc_elems, .. } => {
|
||||
for elem in loc_elems.iter_mut() {
|
||||
|
|
|
@ -185,7 +185,6 @@ pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &Expr, var: Variable) {
|
|||
}
|
||||
Expr::Var(..) => { /* terminal */ }
|
||||
Expr::AbilityMember(..) => { /* terminal */ }
|
||||
Expr::Crash => { /* terminal */ }
|
||||
Expr::If {
|
||||
cond_var,
|
||||
branches,
|
||||
|
@ -204,6 +203,9 @@ pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &Expr, var: Variable) {
|
|||
let (fn_var, loc_fn, _closure_var, _ret_var) = &**f;
|
||||
walk_call(visitor, *fn_var, loc_fn, args);
|
||||
}
|
||||
Expr::Crash { msg, .. } => {
|
||||
visitor.visit_expr(&msg.value, msg.region, Variable::STR);
|
||||
}
|
||||
Expr::RunLowLevel {
|
||||
op: _,
|
||||
args,
|
||||
|
|
|
@ -482,11 +482,17 @@ pub fn constrain_expr(
|
|||
let and_constraint = constraints.and_constraint(and_cons);
|
||||
constraints.exists(vars, and_constraint)
|
||||
}
|
||||
Expr::Crash => {
|
||||
let crash_type_index = constraints.push_type(Type::Crash);
|
||||
let expected_index = constraints.push_expected_type(expected);
|
||||
Expr::Crash { msg, ret_var } => {
|
||||
let expected_msg = Expected::ForReason(Reason::CrashArg, str_type(), msg.region);
|
||||
let expected_ret = constraints.push_expected_type(expected);
|
||||
|
||||
constraints.equal_types(crash_type_index, expected_index, Category::Crash, region)
|
||||
let msg_is_str = constrain_expr(constraints, env, msg.region, &msg.value, expected_msg);
|
||||
let magic =
|
||||
constraints.equal_types_var(*ret_var, expected_ret, Category::Crash, region);
|
||||
|
||||
let and = constraints.and_constraint([msg_is_str, magic]);
|
||||
|
||||
constraints.exists([*ret_var], and)
|
||||
}
|
||||
Var(symbol, variable) => {
|
||||
// Save the expectation in the variable, then lookup the symbol's type in the environment
|
||||
|
|
|
@ -5562,7 +5562,7 @@ pub fn with_hole<'a>(
|
|||
}
|
||||
TypedHole(_) => Stmt::RuntimeError("Hit a blank"),
|
||||
RuntimeError(e) => Stmt::RuntimeError(env.arena.alloc(e.runtime_message())),
|
||||
Crash => todo!(),
|
||||
Crash { .. } => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -195,6 +195,12 @@ pub enum Problem {
|
|||
type_got: u8,
|
||||
alias_kind: AliasKind,
|
||||
},
|
||||
UnappliedCrash {
|
||||
region: Region,
|
||||
},
|
||||
OverAppliedCrash {
|
||||
region: Region,
|
||||
},
|
||||
}
|
||||
|
||||
impl Problem {
|
||||
|
@ -325,7 +331,9 @@ impl Problem {
|
|||
}
|
||||
| Problem::MultipleListRestPattern { region }
|
||||
| Problem::BadTypeArguments { region, .. }
|
||||
| Problem::UnnecessaryOutputWildcard { region } => Some(*region),
|
||||
| Problem::UnnecessaryOutputWildcard { region }
|
||||
| Problem::OverAppliedCrash { region }
|
||||
| Problem::UnappliedCrash { region } => Some(*region),
|
||||
Problem::RuntimeError(RuntimeError::CircularDef(cycle_entries))
|
||||
| Problem::BadRecursion(cycle_entries) => {
|
||||
cycle_entries.first().map(|entry| entry.expr_region)
|
||||
|
|
|
@ -2961,27 +2961,6 @@ fn type_to_variable<'a>(
|
|||
|
||||
register_with_known_var(subs, destination, rank, pools, content)
|
||||
}
|
||||
Crash => {
|
||||
let magic_return = subs.fresh(Descriptor {
|
||||
content: Content::FlexVar(None),
|
||||
rank,
|
||||
mark: Mark::NONE,
|
||||
copy: OptVariable::NONE,
|
||||
});
|
||||
let magic_lambda_set = subs.fresh(Descriptor {
|
||||
content: Content::FlexVar(None),
|
||||
rank,
|
||||
mark: Mark::NONE,
|
||||
copy: OptVariable::NONE,
|
||||
});
|
||||
let magic_crash = Content::Structure(FlatType::Func(
|
||||
Subs::STR_SLICE,
|
||||
magic_lambda_set,
|
||||
magic_return,
|
||||
));
|
||||
|
||||
register_with_known_var(subs, destination, rank, pools, magic_crash)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -279,7 +279,7 @@ fn expr<'a>(c: &Ctx, p: EPrec, f: &'a Arena<'a>, e: &'a Expr) -> DocBuilder<'a,
|
|||
)
|
||||
.group()
|
||||
),
|
||||
Crash => f.text("crash"),
|
||||
Crash { .. } => todo!(),
|
||||
ZeroArgumentTag { .. } => todo!(),
|
||||
OpaqueRef { .. } => todo!(),
|
||||
Dbg { .. } => todo!(),
|
||||
|
|
|
@ -994,7 +994,6 @@ impl Types {
|
|||
self.set_type_tag(index, TypeTag::RangedNumber(*range), Slice::default())
|
||||
}
|
||||
Type::Error => self.set_type_tag(index, TypeTag::Error, Slice::default()),
|
||||
Type::Crash => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1669,7 +1668,6 @@ pub enum Type {
|
|||
Apply(Symbol, Vec<Loc<Type>>, Region),
|
||||
Variable(Variable),
|
||||
RangedNumber(NumericRange),
|
||||
Crash, // Str -> a
|
||||
/// A type error, which will code gen to a runtime error
|
||||
Error,
|
||||
}
|
||||
|
@ -1712,7 +1710,6 @@ impl Clone for Type {
|
|||
match self {
|
||||
Self::EmptyRec => Self::EmptyRec,
|
||||
Self::EmptyTagUnion => Self::EmptyTagUnion,
|
||||
Self::Crash => Self::Crash,
|
||||
Self::Function(arg0, arg1, arg2) => {
|
||||
Self::Function(arg0.clone(), arg1.clone(), arg2.clone())
|
||||
}
|
||||
|
@ -2081,7 +2078,6 @@ impl fmt::Debug for Type {
|
|||
Type::RangedNumber(range_vars) => {
|
||||
write!(f, "Ranged({:?})", range_vars)
|
||||
}
|
||||
Type::Crash => write!(f, "Crash"),
|
||||
Type::UnspecializedLambdaSet { unspecialized } => {
|
||||
write!(f, "{:?}", unspecialized)
|
||||
}
|
||||
|
@ -2245,7 +2241,7 @@ impl Type {
|
|||
);
|
||||
}
|
||||
|
||||
EmptyRec | EmptyTagUnion | Crash | Error => {}
|
||||
EmptyRec | EmptyTagUnion | Error => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2375,7 +2371,7 @@ impl Type {
|
|||
);
|
||||
}
|
||||
|
||||
EmptyRec | EmptyTagUnion | Crash | Error => {}
|
||||
EmptyRec | EmptyTagUnion | Error => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2478,7 +2474,7 @@ impl Type {
|
|||
}
|
||||
RangedNumber(_) => Ok(()),
|
||||
UnspecializedLambdaSet { .. } => Ok(()),
|
||||
EmptyRec | EmptyTagUnion | ClosureTag { .. } | Error | Variable(_) | Crash => Ok(()),
|
||||
EmptyRec | EmptyTagUnion | ClosureTag { .. } | Error | Variable(_) => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2540,7 +2536,7 @@ impl Type {
|
|||
UnspecializedLambdaSet {
|
||||
unspecialized: Uls(_, sym, _),
|
||||
} => *sym == rep_symbol,
|
||||
EmptyRec | EmptyTagUnion | ClosureTag { .. } | Error | Variable(_) | Crash => false,
|
||||
EmptyRec | EmptyTagUnion | ClosureTag { .. } | Error | Variable(_) => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2596,7 +2592,7 @@ impl Type {
|
|||
.iter()
|
||||
.any(|arg| arg.value.contains_variable(rep_variable)),
|
||||
RangedNumber(_) => false,
|
||||
EmptyRec | EmptyTagUnion | Error | Crash => false,
|
||||
EmptyRec | EmptyTagUnion | Error => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2970,7 +2966,7 @@ impl Type {
|
|||
}
|
||||
RangedNumber(_) => {}
|
||||
UnspecializedLambdaSet { .. } => {}
|
||||
EmptyRec | EmptyTagUnion | ClosureTag { .. } | Error | Variable(_) | Crash => {}
|
||||
EmptyRec | EmptyTagUnion | ClosureTag { .. } | Error | Variable(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3106,7 +3102,7 @@ fn symbols_help(initial: &Type) -> Vec<Symbol> {
|
|||
} => {
|
||||
// ignore the member symbol because unspecialized lambda sets are internal-only
|
||||
}
|
||||
EmptyRec | EmptyTagUnion | ClosureTag { .. } | Error | Variable(_) | Crash => {}
|
||||
EmptyRec | EmptyTagUnion | ClosureTag { .. } | Error | Variable(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3222,7 +3218,7 @@ fn variables_help(tipe: &Type, accum: &mut ImSet<Variable>) {
|
|||
}
|
||||
variables_help(actual, accum);
|
||||
}
|
||||
RangedNumber(_) | Crash => {}
|
||||
RangedNumber(_) => {}
|
||||
Apply(_, args, _) => {
|
||||
for x in args {
|
||||
variables_help(&x.value, accum);
|
||||
|
@ -3250,7 +3246,7 @@ fn variables_help_detailed(tipe: &Type, accum: &mut VariableDetail) {
|
|||
use Type::*;
|
||||
|
||||
match tipe {
|
||||
EmptyRec | EmptyTagUnion | Error | Crash => (),
|
||||
EmptyRec | EmptyTagUnion | Error => (),
|
||||
|
||||
Variable(v) => {
|
||||
accum.type_variables.insert(*v);
|
||||
|
@ -3490,6 +3486,7 @@ pub enum Reason {
|
|||
member_name: Symbol,
|
||||
def_region: Region,
|
||||
},
|
||||
CrashArg,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Clone)]
|
||||
|
@ -4377,7 +4374,6 @@ fn instantiate_lambda_sets_as_unspecialized(
|
|||
match typ {
|
||||
Type::EmptyRec => {}
|
||||
Type::EmptyTagUnion => {}
|
||||
Type::Crash => {}
|
||||
Type::Function(args, lambda_set, ret) => {
|
||||
debug_assert!(
|
||||
matches!(**lambda_set, Type::Variable(..)),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue