Skip to content

Commit

Permalink
fix incorrect suberror prototypes & unused warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Oct 15, 2023
1 parent abcf758 commit 596b3fb
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 40 deletions.
6 changes: 3 additions & 3 deletions cli/src/cmd/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ pub fn dump(arg: &ArgMatches) -> anyhow::Result<()> {

let tokens = dash_lexer::Lexer::new(interner, &source)
.scan_all()
.map_err(|e| anyhow!("{}", e.formattable(interner, &source, true)))?;
.map_err(|e| anyhow!("{}", e.formattable(&source, true)))?;

if dump_tokens {
println!("{tokens:#?}");
}

let (mut ast, counter) = dash_parser::Parser::new(interner, &source, tokens)
.parse_all()
.map_err(|err| anyhow!("{}", err.formattable(interner, &source, true)))?;
.map_err(|err| anyhow!("{}", err.formattable(&source, true)))?;

transformations::ast_patch_implicit_return(&mut ast);

Expand Down Expand Up @@ -76,7 +76,7 @@ pub fn dump(arg: &ArgMatches) -> anyhow::Result<()> {

let bytecode = dash_compiler::FunctionCompiler::new(opt, tcx, interner)
.compile_ast(ast, true)
.map_err(|err| anyhow!("{}", [err].formattable(interner, &source, true)))?;
.map_err(|err| anyhow!("{}", [err].formattable(&source, true)))?;

if dump_bytecode {
let buffer = dash_middle::compiler::format::serialize(bytecode)?;
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cmd/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn eval(args: &ArgMatches) -> anyhow::Result<()> {
match scope.eval(source, opt) {
Ok(value) => util::print_value(value.root(&mut scope), &mut scope).unwrap(),
Err((EvalError::Exception(value), _)) => util::print_value(value.root(&mut scope), &mut scope).unwrap(),
Err((EvalError::Middle(errs), interner)) => println!("{}", errs.formattable(&interner, source, true)),
Err((EvalError::Middle(errs), _)) => println!("{}", errs.formattable(source, true)),
};

scope.process_async_tasks();
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cmd/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn repl() -> anyhow::Result<()> {
match scope.eval(&input, OptLevel::Aggressive) {
Ok(value) => util::print_value(value.root(&mut scope), &mut scope).unwrap(),
Err((EvalError::Exception(value), _)) => util::print_value(value.root(&mut scope), &mut scope).unwrap(),
Err((EvalError::Middle(errs), interner)) => println!("{}", errs.formattable(&interner, &input, true)),
Err((EvalError::Middle(errs), _)) => println!("{}", errs.formattable(&input, true)),
}

scope.process_async_tasks();
Expand Down
4 changes: 2 additions & 2 deletions cli/src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async fn inner(source: String, opt: OptLevel, quiet: bool, initial_gc_threshold:
let value = match scope.eval(&source, opt) {
Ok(val) => val.root(&mut scope),
Err((EvalError::Exception(val), _)) => val.root(&mut scope),
Err((EvalError::Middle(errs), interner)) => {
println!("{}", errs.formattable(&interner, &source, true));
Err((EvalError::Middle(errs), _)) => {
println!("{}", errs.formattable(&source, true));
return Ok(());
}
};
Expand Down
25 changes: 2 additions & 23 deletions crates/dash_middle/src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use memchr::memchr;
use memchr::memmem::rfind;
use owo_colors::OwoColorize;

use crate::interner::StringInterner;
use crate::lexer::token::Token;
use crate::lexer::token::TokenType;
use crate::sourcemap::Span;
Expand Down Expand Up @@ -63,7 +62,6 @@ pub enum Error {

pub struct FormattableError<'a, 'buf> {
error: &'a Error,
interner: &'a StringInterner,
source: &'buf str,
color: bool,
}
Expand Down Expand Up @@ -160,8 +158,6 @@ impl<'f, 'a, 'buf> fmt::Display for DiagnosticBuilder<'f, 'a, 'buf> {
Some(span) => {
assert!(span.is_user_span(), "compiler-generated span in diagnostic");
let LineData {
start_index: _,
end_index: _,
relative_span_lo,
relative_span_hi,
line,
Expand Down Expand Up @@ -214,8 +210,6 @@ impl<'f, 'a, 'buf> fmt::Display for DiagnosticBuilder<'f, 'a, 'buf> {
}

struct LineData<'a> {
start_index: usize,
end_index: usize,
relative_span_lo: usize,
relative_span_hi: usize,
line: &'a str,
Expand All @@ -235,8 +229,6 @@ fn line_data(source: &str, span: Span) -> LineData<'_> {

let line = &source[start_index..end_index];
LineData {
start_index,
end_index,
relative_span_lo,
relative_span_hi,
line,
Expand Down Expand Up @@ -382,24 +374,13 @@ impl<'a, 'buf> fmt::Display for FormattableError<'a, 'buf> {
}

pub trait IntoFormattableErrors {
fn formattable<'a, 'buf>(
&'a self,
interner: &'a StringInterner,
source: &'buf str,
colors: bool,
) -> FormattableErrors<'a, 'buf>;
fn formattable<'a, 'buf>(&'a self, source: &'buf str, colors: bool) -> FormattableErrors<'a, 'buf>;
}

impl IntoFormattableErrors for [Error] {
fn formattable<'a, 'buf>(
&'a self,
interner: &'a StringInterner,
source: &'buf str,
colors: bool,
) -> FormattableErrors<'a, 'buf> {
fn formattable<'a, 'buf>(&'a self, source: &'buf str, colors: bool) -> FormattableErrors<'a, 'buf> {
FormattableErrors {
errors: self,
interner,
source,
colors,
}
Expand All @@ -408,7 +389,6 @@ impl IntoFormattableErrors for [Error] {

pub struct FormattableErrors<'a, 'buf> {
errors: &'a [Error],
interner: &'a StringInterner,
source: &'buf str,
colors: bool,
}
Expand All @@ -418,7 +398,6 @@ impl<'a, 'buf> fmt::Display for FormattableErrors<'a, 'buf> {
for error in self.errors {
FormattableError {
color: self.colors,
interner: self.interner,
source: self.source,
error,
}
Expand Down
4 changes: 0 additions & 4 deletions crates/dash_vm/src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use std::fmt;

use dash_compiler::FunctionCompiler;
use dash_lexer::Lexer;
use dash_middle::compiler::StaticImportKind;
use dash_middle::interner::StringInterner;
use dash_middle::parser::error::FormattableErrors;
use dash_middle::parser::error::IntoFormattableErrors;
use dash_optimizer::type_infer::TypeInferCtx;
use dash_optimizer::OptLevel;
use dash_parser::Parser;
Expand Down
12 changes: 6 additions & 6 deletions crates/dash_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ impl Vm {

register(
scope.statics.eval_error_prototype.clone(),
object_proto.clone(),
scope.statics.error_prototype.clone(),
eval_error_ctor.clone(),
[
("toString", scope.statics.error_to_string.clone()),
Expand All @@ -913,7 +913,7 @@ impl Vm {

register(
scope.statics.range_error_prototype.clone(),
object_proto.clone(),
scope.statics.error_prototype.clone(),
range_error_ctor.clone(),
[
("toString", scope.statics.error_to_string.clone()),
Expand All @@ -937,7 +937,7 @@ impl Vm {

register(
scope.statics.reference_error_prototype.clone(),
object_proto.clone(),
scope.statics.error_prototype.clone(),
reference_error_ctor.clone(),
[
("toString", scope.statics.error_to_string.clone()),
Expand All @@ -961,7 +961,7 @@ impl Vm {

register(
scope.statics.syntax_error_prototype.clone(),
object_proto.clone(),
scope.statics.error_prototype.clone(),
syntax_error_ctor.clone(),
[
("toString", scope.statics.error_to_string.clone()),
Expand All @@ -985,7 +985,7 @@ impl Vm {

register(
scope.statics.type_error_prototype.clone(),
object_proto.clone(),
scope.statics.error_prototype.clone(),
type_error_ctor.clone(),
[
("toString", scope.statics.error_to_string.clone()),
Expand All @@ -1009,7 +1009,7 @@ impl Vm {

register(
scope.statics.uri_error_prototype.clone(),
object_proto.clone(),
scope.statics.error_prototype.clone(),
uri_error_ctor.clone(),
[
("toString", scope.statics.error_to_string.clone()),
Expand Down
13 changes: 13 additions & 0 deletions crates/dash_vm/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,16 @@ function x(...v) {
x(0,...[1,2],3,4,...[5,6],7,8,9,10,...[11]);",
Value::undefined()
);

simple_test!(
error_structure,
r#"
assert(new ReferenceError().constructor === ReferenceError);
assert(new ReferenceError().__proto__ === ReferenceError.prototype);
assert(new ReferenceError().__proto__.__proto__ === Error.prototype);
assert(new Error("foo").message === "foo");
assert(new ReferenceError("foo").toString().startsWith("ReferenceError: foo"));
assert(new Error("foo").toString().startsWith("Error: foo"));
"#,
Value::undefined()
);

0 comments on commit 596b3fb

Please sign in to comment.