Report errors in the CLI

This commit is contained in:
Richard Feldman 2020-04-03 21:29:34 -04:00
parent 463eb6a1af
commit d28f8449d0
5 changed files with 52 additions and 13 deletions

1
Cargo.lock generated
View file

@ -870,6 +870,7 @@ dependencies = [
"roc_parse", "roc_parse",
"roc_problem", "roc_problem",
"roc_region", "roc_region",
"roc_reporting",
"roc_solve", "roc_solve",
"roc_types", "roc_types",
"roc_unify", "roc_unify",

View file

@ -32,6 +32,7 @@ roc_unify = { path = "../compiler/unify" }
roc_solve = { path = "../compiler/solve" } roc_solve = { path = "../compiler/solve" }
roc_mono = { path = "../compiler/mono" } roc_mono = { path = "../compiler/mono" }
roc_gen = { path = "../compiler/gen", version = "0.1.0" } roc_gen = { path = "../compiler/gen", version = "0.1.0" }
roc_reporting = { path = "../compiler/reporting", version = "0.1.0" }
im = "14" # im and im-rc should always have the same version! im = "14" # im and im-rc should always have the same version!
im-rc = "14" # im and im-rc should always have the same version! im-rc = "14" # im and im-rc should always have the same version!
bumpalo = { version = "3.2", features = ["collections"] } bumpalo = { version = "3.2", features = ["collections"] }

View file

@ -1,4 +1,5 @@
extern crate roc_gen; extern crate roc_gen;
extern crate roc_reporting;
use crate::helpers::{infer_expr, uniq_expr_with}; use crate::helpers::{infer_expr, uniq_expr_with};
use bumpalo::Bump; use bumpalo::Bump;
@ -22,7 +23,7 @@ use inkwell::targets::{
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
use std::path::Path; use std::path::{Path, PathBuf};
use target_lexicon::{Architecture, OperatingSystem, Triple, Vendor}; use target_lexicon::{Architecture, OperatingSystem, Triple, Vendor};
pub mod helpers; pub mod helpers;
@ -40,7 +41,12 @@ fn main() -> io::Result<()> {
let dest_filename = Path::new(filename).with_extension("o"); let dest_filename = Path::new(filename).with_extension("o");
gen(contents.as_str(), Triple::host(), &dest_filename); gen(
Path::new(filename).to_path_buf(),
contents.as_str(),
Triple::host(),
&dest_filename,
);
let end_time = now.elapsed().unwrap(); let end_time = now.elapsed().unwrap();
@ -56,15 +62,46 @@ fn main() -> io::Result<()> {
} }
} }
fn gen(src: &str, target: Triple, dest_filename: &Path) { fn gen(filename: PathBuf, src: &str, target: Triple, dest_filename: &Path) {
use roc_reporting::report::{can_problem, DEFAULT_PALETTE};
use roc_reporting::type_error::type_problem;
// Build the expr // Build the expr
let arena = Bump::new(); let arena = Bump::new();
let (loc_expr, _output, _problems, subs, var, constraint, home, interns) = let (loc_expr, _output, can_problems, subs, var, constraint, home, interns) =
uniq_expr_with(&arena, src, &ImMap::default()); uniq_expr_with(&arena, src, &ImMap::default());
let mut unify_problems = Vec::new(); let mut type_problems = Vec::new();
let (content, mut subs) = infer_expr(subs, &mut unify_problems, &constraint, var); let (content, mut subs) = infer_expr(subs, &mut type_problems, &constraint, var);
let src_lines: Vec<&str> = src.split('\n').collect();
let palette = DEFAULT_PALETTE;
// Report parsing and canonicalization problems
for problem in can_problems.into_iter() {
let report = can_problem(filename.clone(), problem);
let mut buf = String::new();
report
.text
.render_color_terminal(&mut buf, &mut subs, home, &src_lines, &interns, &palette);
println!("\n{}\n", buf);
}
for problem in type_problems.into_iter() {
let report = type_problem(filename.clone(), problem);
let mut buf = String::new();
report
.text
.render_color_terminal(&mut buf, &mut subs, home, &src_lines, &interns, &palette);
println!("\n{}\n", buf);
}
// Generate the binary
let context = Context::create(); let context = Context::create();
let module = module_from_builtins(&context, "app"); let module = module_from_builtins(&context, "app");
@ -178,7 +215,7 @@ fn gen(src: &str, target: Triple, dest_filename: &Path) {
// Verify the module // Verify the module
if let Err(errors) = env.module.verify() { if let Err(errors) = env.module.verify() {
panic!("Errors defining module: {:?}", errors); panic!("😱 LLVM errors when defining module: {:?}", errors);
} }
// Uncomment this to see the module's optimized LLVM instruction output: // Uncomment this to see the module's optimized LLVM instruction output:

View file

@ -30,7 +30,7 @@ pub struct Palette<'a> {
pub binop: &'a str, pub binop: &'a str,
} }
pub const TEST_PALETTE: Palette = Palette { pub const DEFAULT_PALETTE: Palette = Palette {
primary: WHITE_CODE, primary: WHITE_CODE,
code_block: WHITE_CODE, code_block: WHITE_CODE,
variable: BLUE_CODE, variable: BLUE_CODE,

View file

@ -13,8 +13,8 @@ mod test_reporting {
use roc_module::symbol::{Interns, ModuleId}; use roc_module::symbol::{Interns, ModuleId};
use roc_reporting::report::{ use roc_reporting::report::{
can_problem, em_text, plain_text, url, Report, ReportText, BLUE_CODE, BOLD_CODE, CYAN_CODE, can_problem, em_text, plain_text, url, Report, ReportText, BLUE_CODE, BOLD_CODE, CYAN_CODE,
GREEN_CODE, MAGENTA_CODE, RED_CODE, RESET_CODE, TEST_PALETTE, UNDERLINE_CODE, WHITE_CODE, DEFAULT_PALETTE, GREEN_CODE, MAGENTA_CODE, RED_CODE, RESET_CODE, UNDERLINE_CODE,
YELLOW_CODE, WHITE_CODE, YELLOW_CODE,
}; };
use roc_reporting::type_error::type_problem; use roc_reporting::type_error::type_problem;
use roc_types::pretty_print::name_all_type_vars; use roc_types::pretty_print::name_all_type_vars;
@ -147,7 +147,7 @@ mod test_reporting {
home, home,
&src_lines, &src_lines,
&interns, &interns,
&TEST_PALETTE, &DEFAULT_PALETTE,
); );
assert_eq!(human_readable(&buf), expected_rendering); assert_eq!(human_readable(&buf), expected_rendering);
@ -492,7 +492,7 @@ mod test_reporting {
home, home,
&src_lines, &src_lines,
&interns, &interns,
&TEST_PALETTE, &DEFAULT_PALETTE,
); );
assert_eq!(human_readable(&buf), "<blue>activityIndicatorLarge<reset>"); assert_eq!(human_readable(&buf), "<blue>activityIndicatorLarge<reset>");
@ -523,7 +523,7 @@ mod test_reporting {
home, home,
&src_lines, &src_lines,
&interns, &interns,
&TEST_PALETTE, &DEFAULT_PALETTE,
); );
assert_eq!(human_readable(&buf), "<green>Util.Int<reset>"); assert_eq!(human_readable(&buf), "<green>Util.Int<reset>");