Skip to content

Commit

Permalink
Move all diagnostics into one type
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Dec 15, 2020
1 parent f2ad2ea commit b018b74
Show file tree
Hide file tree
Showing 40 changed files with 255 additions and 347 deletions.
49 changes: 20 additions & 29 deletions crates/rune-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,38 +424,33 @@ fn load_path(
None => {
log::trace!("building file: {}", path.display());

let mut errors = rune::Errors::new();
let mut warnings = rune::Warnings::new();
let mut diagnostics = if shared.warnings {
rune::Diagnostics::new()
} else {
rune::Diagnostics::without_warnings()
};

let test_finder = Rc::new(tests::TestVisitor::default());
let source_loader = Rc::new(rune::FileSourceLoader::new());

let unit = match rune::load_sources_with_visitor(
let result = rune::load_sources_with_visitor(
&context,
&options,
&mut sources,
&mut errors,
&mut warnings,
&mut diagnostics,
test_finder.clone(),
source_loader.clone(),
) {
Ok(unit) => unit,
Err(err @ rune::LoadSourcesError) => {
errors.emit_diagnostics(out, &sources)?;
return Err(err.into());
}
};
);

diagnostics.emit_diagnostics(out, &sources)?;
let unit = result?;

if options.bytecode {
log::trace!("serializing cache: {}", bytecode_path.display());
let f = fs::File::create(&bytecode_path)?;
bincode::serialize_into(f, &unit)?;
}

if shared.warnings && !warnings.is_empty() {
warnings.emit_diagnostics(out, &sources)?;
}

let test_finder = match Rc::try_unwrap(test_finder) {
Ok(test_finder) => test_finder,
Err(..) => panic!("test finder should be uniquely held"),
Expand Down Expand Up @@ -499,8 +494,11 @@ async fn run_path(args: &Args, options: &rune::Options, path: &Path) -> Result<E

sources.insert(source);

let mut errors = rune::Errors::new();
let mut warnings = rune::Warnings::new();
let mut diagnostics = if checkargs.shared.warnings || checkargs.warnings_are_errors {
rune::Diagnostics::new()
} else {
rune::Diagnostics::without_warnings()
};

let test_finder = Rc::new(tests::TestVisitor::default());
let source_loader = Rc::new(rune::FileSourceLoader::new());
Expand All @@ -509,23 +507,16 @@ async fn run_path(args: &Args, options: &rune::Options, path: &Path) -> Result<E
&context,
&options,
&mut sources,
&mut errors,
&mut warnings,
&mut diagnostics,
test_finder.clone(),
source_loader.clone(),
);

if !errors.is_empty() {
errors.emit_diagnostics(&mut out, &sources).unwrap();
}

if checkargs.shared.warnings && !warnings.is_empty() {
warnings.emit_diagnostics(&mut out, &sources).unwrap();
}
diagnostics.emit_diagnostics(&mut out, &sources).unwrap();

if !errors.is_empty() {
if !diagnostics.errors().is_empty() {
Ok(ExitCode::Failure)
} else if checkargs.warnings_are_errors && !warnings.is_empty() {
} else if checkargs.warnings_are_errors && !diagnostics.warnings().is_empty() {
Ok(ExitCode::Failure)
} else {
Ok(ExitCode::Success)
Expand Down
10 changes: 4 additions & 6 deletions crates/rune-languageserver/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,20 @@ impl State {

sources.insert(input);

let mut errors = rune::Errors::new();
let mut warnings = rune::Warnings::new();
let mut diagnostics = rune::Diagnostics::new();
let visitor = Rc::new(Visitor::new(Index::default()));

let result = rune::load_sources_with_visitor(
&self.inner.context,
&self.inner.options,
&mut sources,
&mut errors,
&mut warnings,
&mut diagnostics,
visitor.clone(),
source_loader.clone(),
);

if let Err(rune::LoadSourcesError) = result {
for error in errors {
for error in diagnostics.errors() {
let source_id = error.source_id();

match error.kind() {
Expand Down Expand Up @@ -211,7 +209,7 @@ impl State {
}
}

for warning in &warnings {
for warning in diagnostics.warnings() {
report(
&sources,
&mut by_url,
Expand Down
16 changes: 6 additions & 10 deletions crates/rune-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,12 @@ async fn inner_compile(input: String, config: JsValue) -> Result<CompileResult,
options.parse_option(option)?;
}

let mut errors = rune::Errors::new();
let mut warnings = rune::Warnings::new();

let mut d = rune::Diagnostics::new();
let mut diagnostics = Vec::new();

let result = rune::load_sources(&context, &options, &mut sources, &mut errors, &mut warnings);
let result = rune::load_sources(&context, &options, &mut sources, &mut d);

for warning in &warnings {
for warning in d.warnings() {
let span = warning.span();

if let Some(source) = sources.get(warning.source_id) {
Expand All @@ -223,15 +221,14 @@ async fn inner_compile(input: String, config: JsValue) -> Result<CompileResult,
let mut writer = rune::termcolor::Buffer::no_color();

if !config.suppress_text_warnings {
warnings
.emit_diagnostics(&mut writer, &sources)
d.emit_diagnostics(&mut writer, &sources)
.context("emitting to buffer should never fail")?;
}

let unit = match result {
Ok(unit) => Arc::new(unit),
Err(error) => {
for error in &errors {
for error in d.errors() {
if let Some(source) = sources.get(error.source_id()) {
match error.kind() {
rune::ErrorKind::ParseError(error) => {
Expand Down Expand Up @@ -311,8 +308,7 @@ async fn inner_compile(input: String, config: JsValue) -> Result<CompileResult,
}
}

errors
.emit_diagnostics(&mut writer, &sources)
d.emit_diagnostics(&mut writer, &sources)
.expect("emitting to buffer should never fail");

return Ok(CompileResult::from_error(
Expand Down
45 changes: 21 additions & 24 deletions crates/rune/src/compiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::load::{FileSourceLoader, SourceLoader, Sources};
use crate::query::{Build, BuildEntry, Query};
use crate::shared::{Consts, Gen};
use crate::worker::{LoadFileKind, Task, Worker};
use crate::{Error, Errors, Options, Spanned as _, Storage, Warnings};
use crate::{Diagnostics, Error, Options, Spanned as _, Storage};
use runestick::{Context, Location, Source, Span};
use std::rc::Rc;
use std::sync::Arc;
Expand All @@ -26,8 +26,7 @@ pub fn compile(
context: &Context,
sources: &mut Sources,
unit: &UnitBuilder,
errors: &mut Errors,
warnings: &mut Warnings,
diagnostics: &mut Diagnostics,
) -> Result<(), ()> {
let visitor = Rc::new(NoopCompileVisitor::new());
let source_loader = Rc::new(FileSourceLoader::new());
Expand All @@ -36,8 +35,7 @@ pub fn compile(
context,
sources,
unit,
errors,
warnings,
diagnostics,
&Default::default(),
visitor,
source_loader,
Expand All @@ -51,8 +49,7 @@ pub fn compile_with_options(
context: &Context,
sources: &mut Sources,
unit: &UnitBuilder,
errors: &mut Errors,
warnings: &mut Warnings,
diagnostics: &mut Diagnostics,
options: &Options,
visitor: Rc<dyn CompileVisitor>,
source_loader: Rc<dyn SourceLoader>,
Expand All @@ -71,8 +68,7 @@ pub fn compile_with_options(
options,
unit.clone(),
consts,
errors,
warnings,
diagnostics,
visitor.clone(),
source_loader,
storage.clone(),
Expand All @@ -84,7 +80,7 @@ pub fn compile_with_options(
let mod_item = match worker.query.insert_root_mod(source_id, Span::empty()) {
Ok(result) => result,
Err(error) => {
errors.push(Error::new(source_id, error));
diagnostics.error(Error::new(source_id, error));
return Err(());
}
};
Expand All @@ -98,7 +94,7 @@ pub fn compile_with_options(

worker.run();

if !worker.errors.is_empty() {
if !worker.diagnostics.errors().is_empty() {
return Err(());
}

Expand All @@ -112,13 +108,13 @@ pub fn compile_with_options(
options,
storage: &storage,
unit,
warnings: worker.warnings,
diagnostics: worker.diagnostics,
consts: &worker.consts,
query: &mut worker.query,
};

if let Err(error) = task.compile(entry) {
worker.errors.push(Error::new(source_id, error));
worker.diagnostics.error(Error::new(source_id, error));
}
}

Expand All @@ -127,13 +123,13 @@ pub fn compile_with_options(
Ok(false) => break,
Err((source_id, error)) => {
worker
.errors
.push(Error::new(source_id, CompileError::from(error)));
.diagnostics
.error(Error::new(source_id, CompileError::from(error)));
}
}
}

if !worker.errors.is_empty() {
if !worker.diagnostics.errors().is_empty() {
return Err(());
}

Expand All @@ -146,7 +142,7 @@ struct CompileBuildEntry<'a> {
options: &'a Options,
storage: &'a Storage,
unit: &'a UnitBuilder,
warnings: &'a mut Warnings,
diagnostics: &'a mut Diagnostics,
consts: &'a Consts,
query: &'a mut Query,
}
Expand All @@ -173,7 +169,7 @@ impl CompileBuildEntry<'_> {
contexts: vec![span],
loops: self::v1::Loops::new(),
options: self.options,
warnings: self.warnings,
diagnostics: self.diagnostics,
}
}

Expand Down Expand Up @@ -201,7 +197,7 @@ impl CompileBuildEntry<'_> {
f.ast.assemble_fn(&mut c, false)?;

if used.is_unused() {
self.warnings.not_used(location.source_id, span, None);
self.diagnostics.not_used(location.source_id, span, None);
} else {
self.unit.new_function(
location,
Expand Down Expand Up @@ -232,7 +228,7 @@ impl CompileBuildEntry<'_> {
f.ast.assemble_fn(&mut c, true)?;

if used.is_unused() {
c.warnings.not_used(location.source_id, span, None);
c.diagnostics.not_used(location.source_id, span, None);
} else {
self.unit.new_instance_function(
location,
Expand All @@ -257,7 +253,8 @@ impl CompileBuildEntry<'_> {
closure.ast.assemble_closure(&mut c, &closure.captures)?;

if used.is_unused() {
c.warnings.not_used(location.source_id, location.span, None);
c.diagnostics
.not_used(location.source_id, location.span, None);
} else {
self.unit.new_function(
location,
Expand All @@ -279,7 +276,7 @@ impl CompileBuildEntry<'_> {
b.ast.assemble_closure(&mut c, &b.captures)?;

if used.is_unused() {
self.warnings
self.diagnostics
.not_used(location.source_id, location.span, None);
} else {
self.unit.new_function(
Expand All @@ -293,7 +290,7 @@ impl CompileBuildEntry<'_> {
}
}
Build::Unused => {
self.warnings
self.diagnostics
.not_used(location.source_id, location.span, None);
}
Build::Import(import) => {
Expand All @@ -303,7 +300,7 @@ impl CompileBuildEntry<'_> {
.import(location.span, &item.module, &item.item, used)?;

if used.is_unused() {
self.warnings
self.diagnostics
.not_used(location.source_id, location.span, None);
}

Expand Down
6 changes: 3 additions & 3 deletions crates/rune/src/compiling/unit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::collections::HashMap;
use crate::compiling::{Assembly, AssemblyInst};
use crate::{CompileError, CompileErrorKind, Error, Errors};
use crate::{CompileError, CompileErrorKind, Diagnostics, Error};
use runestick::debug::{DebugArgs, DebugSignature};
use runestick::{
Call, CompileMeta, CompileMetaKind, ConstValue, Context, DebugInfo, DebugInst, Hash, Inst,
Expand Down Expand Up @@ -583,12 +583,12 @@ impl UnitBuilder {
/// functions are provided.
///
/// This can prevent a number of runtime errors, like missing functions.
pub(crate) fn link(&self, context: &Context, errors: &mut Errors) {
pub(crate) fn link(&self, context: &Context, diagnostics: &mut Diagnostics) {
let inner = self.inner.borrow();

for (hash, spans) in &inner.required_functions {
if inner.functions.get(hash).is_none() && context.lookup(*hash).is_none() {
errors.push(Error::new(
diagnostics.error(Error::new(
0,
LinkerError::MissingFunction {
hash: *hash,
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/compiling/v1/assemble/builtin_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Assemble for BuiltInTemplate {
}

if self.from_literal && expansions == 0 {
c.warnings
c.diagnostics
.template_without_expansions(c.source_id, span, c.context());
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/compiling/v1/assemble/const_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ impl AssembleConst for ConstValue {
use num::ToPrimitive as _;

if !needs.value() {
c.warnings.not_used(c.source_id, span, c.context());
c.diagnostics.not_used(c.source_id, span, c.context());
return Ok(());
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/compiling/v1/assemble/expr_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl Assemble for ast::ExprCall {

if tuple.args == 0 {
let tuple = path.span();
c.warnings
c.diagnostics
.remove_tuple_call_parens(c.source_id, span, tuple, c.context());
}
}
Expand Down
Loading

0 comments on commit b018b74

Please sign in to comment.