Merge branch 'main' into cleanup_unused

This commit is contained in:
Sam Mohr 2024-12-08 10:14:47 -08:00
commit 01f4d4f3da
No known key found for this signature in database
GPG key ID: EA41D161A3C1BC99
514 changed files with 4950 additions and 1811 deletions

View file

@ -18,14 +18,16 @@ use roc_module::called_via::CalledVia;
use roc_module::ident::{ForeignSymbol, Lowercase, TagName};
use roc_module::low_level::LowLevel;
use roc_module::symbol::{IdentId, ModuleId, Symbol};
use roc_parse::ast::{self, Defs, PrecedenceConflict, StrLiteral};
use roc_parse::ast::{self, Defs, PrecedenceConflict, ResultTryKind, StrLiteral};
use roc_parse::ident::Accessor;
use roc_parse::pattern::PatternType::*;
use roc_problem::can::{PrecedenceProblem, Problem, RuntimeError};
use roc_region::all::{Loc, Region};
use roc_types::num::SingleQuoteBound;
use roc_types::subs::{ExhaustiveMark, IllegalCycleMark, RedundantMark, VarStore, Variable};
use roc_types::types::{Alias, Category, IndexOrField, LambdaSet, OptAbleVar, Type};
use roc_types::types::{
Alias, Category, EarlyReturnKind, IndexOrField, LambdaSet, OptAbleVar, Type,
};
use soa::Index;
use std::fmt::{Debug, Display};
use std::path::PathBuf;
@ -327,6 +329,16 @@ pub enum Expr {
symbol: Symbol,
},
Try {
result_expr: Box<Loc<Expr>>,
result_var: Variable,
return_var: Variable,
ok_payload_var: Variable,
err_payload_var: Variable,
err_ext_var: Variable,
kind: TryKind,
},
Return {
return_value: Box<Loc<Expr>>,
return_var: Variable,
@ -336,6 +348,12 @@ pub enum Expr {
RuntimeError(RuntimeError),
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TryKind {
KeywordPrefix,
OperatorSuffix,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ExpectLookup {
pub symbol: Symbol,
@ -394,9 +412,10 @@ impl Expr {
}
Self::Expect { .. } => Category::Expect,
Self::Crash { .. } => Category::Crash,
Self::Return { .. } => Category::Return,
Self::Return { .. } => Category::Return(EarlyReturnKind::Return),
Self::Dbg { .. } => Category::Expect,
Self::Try { .. } => Category::TrySuccess,
// these nodes place no constraints on the expression's type
Self::RuntimeError(..) => Category::Unknown,
@ -420,7 +439,7 @@ impl Expr {
| Self::ZeroArgumentTag { .. }
| Self::OpaqueWrapFunction(_)
| Self::RuntimeError(..) => false,
Self::Return { .. } => true,
Self::Return { .. } | Self::Try { .. } => true,
Self::List { loc_elems, .. } => loc_elems
.iter()
.any(|elem| elem.value.contains_any_early_returns()),
@ -536,7 +555,7 @@ pub struct ClosureData {
pub closure_type: Variable,
pub return_type: Variable,
pub fx_type: Variable,
pub early_returns: Vec<(Variable, Region)>,
pub early_returns: Vec<(Variable, Region, EarlyReturnKind)>,
pub name: Symbol,
pub captured_symbols: Vec<(Symbol, Variable)>,
pub recursive: Recursive,
@ -1346,6 +1365,32 @@ pub fn canonicalize_expr<'a>(
output,
)
}
ast::Expr::LowLevelTry(loc_expr, kind) => {
let (loc_result_expr, output) =
canonicalize_expr(env, var_store, scope, loc_expr.region, &loc_expr.value);
let return_var = var_store.fresh();
scope
.early_returns
.push((return_var, loc_expr.region, EarlyReturnKind::Try));
(
Try {
result_expr: Box::new(loc_result_expr),
result_var: var_store.fresh(),
return_var,
ok_payload_var: var_store.fresh(),
err_payload_var: var_store.fresh(),
err_ext_var: var_store.fresh(),
kind: match kind {
ResultTryKind::KeywordPrefix => TryKind::KeywordPrefix,
ResultTryKind::OperatorSuffix => TryKind::OperatorSuffix,
},
},
output,
)
}
ast::Expr::Return(return_expr, after_return) => {
let mut output = Output::default();
@ -1370,7 +1415,9 @@ pub fn canonicalize_expr<'a>(
let return_var = var_store.fresh();
scope.early_returns.push((return_var, return_expr.region));
scope
.early_returns
.push((return_var, return_expr.region, EarlyReturnKind::Return));
(
Return {
@ -2166,6 +2213,7 @@ pub fn is_valid_interpolation(expr: &ast::Expr<'_>) -> bool {
| ast::Expr::MalformedIdent(_, _)
| ast::Expr::Tag(_)
| ast::Expr::OpaqueRef(_) => true,
ast::Expr::LowLevelTry(loc_expr, _) => is_valid_interpolation(&loc_expr.value),
// Newlines are disallowed inside interpolation, and these all require newlines
ast::Expr::DbgStmt { .. }
| ast::Expr::LowLevelDbg(_, _, _)
@ -2922,7 +2970,7 @@ pub struct FunctionDef {
pub closure_type: Variable,
pub return_type: Variable,
pub fx_type: Variable,
pub early_returns: Vec<(Variable, Region)>,
pub early_returns: Vec<(Variable, Region, EarlyReturnKind)>,
pub captured_symbols: Vec<(Symbol, Variable)>,
pub arguments: Vec<(Variable, AnnotatedMark, Loc<Pattern>)>,
}
@ -3064,6 +3112,9 @@ pub(crate) fn get_lookup_symbols(expr: &Expr) -> Vec<ExpectLookup> {
// Intentionally ignore the lookups in the nested `expect` condition itself,
// because they couldn't possibly influence the outcome of this `expect`!
}
Expr::Try { result_expr, .. } => {
stack.push(&result_expr.value);
}
Expr::Return { return_value, .. } => {
stack.push(&return_value.value);
}