From 596b3fb749a5b238670840165304659c831278e6 Mon Sep 17 00:00:00 2001 From: y21 <30553356+y21@users.noreply.github.com> Date: Sun, 15 Oct 2023 19:50:55 +0200 Subject: [PATCH] fix incorrect suberror prototypes & unused warnings --- cli/src/cmd/dump.rs | 6 +++--- cli/src/cmd/eval.rs | 2 +- cli/src/cmd/repl.rs | 2 +- cli/src/cmd/run.rs | 4 ++-- crates/dash_middle/src/parser/error.rs | 25 ++----------------------- crates/dash_vm/src/eval.rs | 4 ---- crates/dash_vm/src/lib.rs | 12 ++++++------ crates/dash_vm/src/test/mod.rs | 13 +++++++++++++ 8 files changed, 28 insertions(+), 40 deletions(-) diff --git a/cli/src/cmd/dump.rs b/cli/src/cmd/dump.rs index a4fb11df..e9e62096 100644 --- a/cli/src/cmd/dump.rs +++ b/cli/src/cmd/dump.rs @@ -31,7 +31,7 @@ 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:#?}"); @@ -39,7 +39,7 @@ pub fn dump(arg: &ArgMatches) -> anyhow::Result<()> { 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); @@ -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)?; diff --git a/cli/src/cmd/eval.rs b/cli/src/cmd/eval.rs index dc7a7268..593408ea 100644 --- a/cli/src/cmd/eval.rs +++ b/cli/src/cmd/eval.rs @@ -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(); diff --git a/cli/src/cmd/repl.rs b/cli/src/cmd/repl.rs index b0659ca6..f7704bec 100644 --- a/cli/src/cmd/repl.rs +++ b/cli/src/cmd/repl.rs @@ -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(); diff --git a/cli/src/cmd/run.rs b/cli/src/cmd/run.rs index 21891ce3..b54fa14c 100644 --- a/cli/src/cmd/run.rs +++ b/cli/src/cmd/run.rs @@ -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(()); } }; diff --git a/crates/dash_middle/src/parser/error.rs b/crates/dash_middle/src/parser/error.rs index 3f4e156e..a012fa50 100644 --- a/crates/dash_middle/src/parser/error.rs +++ b/crates/dash_middle/src/parser/error.rs @@ -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; @@ -63,7 +62,6 @@ pub enum Error { pub struct FormattableError<'a, 'buf> { error: &'a Error, - interner: &'a StringInterner, source: &'buf str, color: bool, } @@ -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, @@ -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, @@ -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, @@ -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, } @@ -408,7 +389,6 @@ impl IntoFormattableErrors for [Error] { pub struct FormattableErrors<'a, 'buf> { errors: &'a [Error], - interner: &'a StringInterner, source: &'buf str, colors: bool, } @@ -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, } diff --git a/crates/dash_vm/src/eval.rs b/crates/dash_vm/src/eval.rs index a649c201..f656c680 100644 --- a/crates/dash_vm/src/eval.rs +++ b/crates/dash_vm/src/eval.rs @@ -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; diff --git a/crates/dash_vm/src/lib.rs b/crates/dash_vm/src/lib.rs index 3eb0bd4a..e97824d0 100644 --- a/crates/dash_vm/src/lib.rs +++ b/crates/dash_vm/src/lib.rs @@ -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()), @@ -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()), @@ -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()), @@ -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()), @@ -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()), @@ -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()), diff --git a/crates/dash_vm/src/test/mod.rs b/crates/dash_vm/src/test/mod.rs index 62ef977e..7c04730f 100644 --- a/crates/dash_vm/src/test/mod.rs +++ b/crates/dash_vm/src/test/mod.rs @@ -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() +);