diff --git a/Cargo.lock b/Cargo.lock index b22c838709..a1b75ae205 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3813,12 +3813,14 @@ dependencies = [ "roc_can", "roc_derive", "roc_load", + "roc_module", "roc_parse", "roc_problem", "roc_region", "roc_reporting", "roc_solve", "roc_target", + "roc_types", "test_solve_helpers", ] diff --git a/crates/test_compile/Cargo.toml b/crates/test_compile/Cargo.toml index 8cbb13ad46..977a607814 100644 --- a/crates/test_compile/Cargo.toml +++ b/crates/test_compile/Cargo.toml @@ -16,6 +16,8 @@ roc_region = { path = "../compiler/region" } roc_load = { path = "../compiler/load" } roc_parse = { path = "../compiler/parse" } roc_can = { path = "../compiler/can" } +roc_module = { path = "../compiler/module" } +roc_types = { path = "../compiler/types" } roc_problem = { path = "../compiler/problem" } roc_reporting = { path = "../reporting" } roc_target = { path = "../compiler/roc_target" } diff --git a/crates/test_compile/src/deindent.rs b/crates/test_compile/src/deindent.rs index 501a770e80..434d63e413 100644 --- a/crates/test_compile/src/deindent.rs +++ b/crates/test_compile/src/deindent.rs @@ -1,13 +1,13 @@ use bumpalo::Bump; -use std::borrow::Cow; /// The purpose of this function is to let us run tests like this: /// -/// run_some_test(r#" -/// x = 1 +/// ```rust,ignore +/// run_some_test(r#" +/// x = 1 /// -/// x -/// ") +/// x +/// "#) /// /// ...without needing to call a macro like `indoc!` to deal with the fact that /// multiline Rust string literals preserve all the indented spaces. diff --git a/crates/test_compile/src/help_can.rs b/crates/test_compile/src/help_can.rs index a9336445b4..a36ee48d36 100644 --- a/crates/test_compile/src/help_can.rs +++ b/crates/test_compile/src/help_can.rs @@ -1,6 +1,31 @@ +use std::path::Path; + use crate::help_parse::ParseExpr; use bumpalo::Bump; -use roc_can::expr::Expr; +use roc_can::{ + desugar, + env::Env, + expr::{canonicalize_expr, Expr, Output}, + scope::Scope, +}; +use roc_module::symbol::{IdentIds, Interns, ModuleId, ModuleIds, PackageModuleIds, Symbol}; +use roc_problem::can::Problem; +use roc_region::all::{Loc, Region}; +use roc_types::{ + subs::{VarStore, Variable}, + types::{AliasVar, Type}, +}; + +#[derive(Debug)] +pub struct CanExprOut { + pub loc_expr: Loc, + pub output: Output, + pub problems: Vec, + pub home: ModuleId, + pub interns: Interns, + pub var_store: VarStore, + pub var: Variable, +} pub struct CanExpr { parse_expr: ParseExpr, @@ -14,20 +39,18 @@ impl Default for CanExpr { } } -impl CanExpr { - pub fn can_expr<'a>(&'a self, input: &'a str) -> Result { - match self.parse_expr.parse_expr(input) { - Ok(ast) => { - // todo canonicalize AST and return that result. - let loc_expr = roc_parse::test_helpers::parse_loc_with(arena, expr_str).unwrap_or_else(|e| { - panic!( - "can_expr_with() got a parse error when attempting to canonicalize:\n\n{expr_str:?} {e:?}" - ) - }); +fn test_home() -> ModuleId { + ModuleIds::default().get_or_insert(&"Test".into()) +} +impl CanExpr { + pub fn can_expr<'a>(&'a self, input: &'a str) -> CanExprOut { + match self.parse_expr.parse_loc_expr(input) { + Ok(loc_expr) => { let mut var_store = VarStore::default(); let var = var_store.fresh(); let qualified_module_ids = PackageModuleIds::default(); + let home = test_home(); let mut scope = Scope::new( home, @@ -38,8 +61,8 @@ impl CanExpr { let dep_idents = IdentIds::exposed_builtins(0); let mut env = Env::new( - arena, - expr_str, + &self.arena(), + input, home, Path::new("Test.roc"), &dep_idents, @@ -95,7 +118,7 @@ impl CanExpr { } } Err(syntax_error) => { - // todo panic due to unexpected syntax error + panic!("Unexpected syntax error: {:?}", syntax_error); } } } @@ -103,4 +126,8 @@ impl CanExpr { pub fn into_arena(self) -> Bump { self.parse_expr.into_arena() } + + pub fn arena(&self) -> &Bump { + &self.parse_expr.arena() + } } diff --git a/crates/test_compile/src/help_parse.rs b/crates/test_compile/src/help_parse.rs index a7cb083370..081807b217 100644 --- a/crates/test_compile/src/help_parse.rs +++ b/crates/test_compile/src/help_parse.rs @@ -57,4 +57,8 @@ impl ParseExpr { pub fn into_arena(self) -> Bump { self.arena } + + pub fn arena(&self) -> &Bump { + &self.arena + } }