Skip to content

Commit b018b74

Browse files
committed
Move all diagnostics into one type
1 parent f2ad2ea commit b018b74

40 files changed

+255
-347
lines changed

crates/rune-cli/src/main.rs

+20-29
Original file line numberDiff line numberDiff line change
@@ -424,38 +424,33 @@ fn load_path(
424424
None => {
425425
log::trace!("building file: {}", path.display());
426426

427-
let mut errors = rune::Errors::new();
428-
let mut warnings = rune::Warnings::new();
427+
let mut diagnostics = if shared.warnings {
428+
rune::Diagnostics::new()
429+
} else {
430+
rune::Diagnostics::without_warnings()
431+
};
429432

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

433-
let unit = match rune::load_sources_with_visitor(
436+
let result = rune::load_sources_with_visitor(
434437
&context,
435438
&options,
436439
&mut sources,
437-
&mut errors,
438-
&mut warnings,
440+
&mut diagnostics,
439441
test_finder.clone(),
440442
source_loader.clone(),
441-
) {
442-
Ok(unit) => unit,
443-
Err(err @ rune::LoadSourcesError) => {
444-
errors.emit_diagnostics(out, &sources)?;
445-
return Err(err.into());
446-
}
447-
};
443+
);
444+
445+
diagnostics.emit_diagnostics(out, &sources)?;
446+
let unit = result?;
448447

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

455-
if shared.warnings && !warnings.is_empty() {
456-
warnings.emit_diagnostics(out, &sources)?;
457-
}
458-
459454
let test_finder = match Rc::try_unwrap(test_finder) {
460455
Ok(test_finder) => test_finder,
461456
Err(..) => panic!("test finder should be uniquely held"),
@@ -499,8 +494,11 @@ async fn run_path(args: &Args, options: &rune::Options, path: &Path) -> Result<E
499494

500495
sources.insert(source);
501496

502-
let mut errors = rune::Errors::new();
503-
let mut warnings = rune::Warnings::new();
497+
let mut diagnostics = if checkargs.shared.warnings || checkargs.warnings_are_errors {
498+
rune::Diagnostics::new()
499+
} else {
500+
rune::Diagnostics::without_warnings()
501+
};
504502

505503
let test_finder = Rc::new(tests::TestVisitor::default());
506504
let source_loader = Rc::new(rune::FileSourceLoader::new());
@@ -509,23 +507,16 @@ async fn run_path(args: &Args, options: &rune::Options, path: &Path) -> Result<E
509507
&context,
510508
&options,
511509
&mut sources,
512-
&mut errors,
513-
&mut warnings,
510+
&mut diagnostics,
514511
test_finder.clone(),
515512
source_loader.clone(),
516513
);
517514

518-
if !errors.is_empty() {
519-
errors.emit_diagnostics(&mut out, &sources).unwrap();
520-
}
521-
522-
if checkargs.shared.warnings && !warnings.is_empty() {
523-
warnings.emit_diagnostics(&mut out, &sources).unwrap();
524-
}
515+
diagnostics.emit_diagnostics(&mut out, &sources).unwrap();
525516

526-
if !errors.is_empty() {
517+
if !diagnostics.errors().is_empty() {
527518
Ok(ExitCode::Failure)
528-
} else if checkargs.warnings_are_errors && !warnings.is_empty() {
519+
} else if checkargs.warnings_are_errors && !diagnostics.warnings().is_empty() {
529520
Ok(ExitCode::Failure)
530521
} else {
531522
Ok(ExitCode::Success)

crates/rune-languageserver/src/state.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -132,22 +132,20 @@ impl State {
132132

133133
sources.insert(input);
134134

135-
let mut errors = rune::Errors::new();
136-
let mut warnings = rune::Warnings::new();
135+
let mut diagnostics = rune::Diagnostics::new();
137136
let visitor = Rc::new(Visitor::new(Index::default()));
138137

139138
let result = rune::load_sources_with_visitor(
140139
&self.inner.context,
141140
&self.inner.options,
142141
&mut sources,
143-
&mut errors,
144-
&mut warnings,
142+
&mut diagnostics,
145143
visitor.clone(),
146144
source_loader.clone(),
147145
);
148146

149147
if let Err(rune::LoadSourcesError) = result {
150-
for error in errors {
148+
for error in diagnostics.errors() {
151149
let source_id = error.source_id();
152150

153151
match error.kind() {
@@ -211,7 +209,7 @@ impl State {
211209
}
212210
}
213211

214-
for warning in &warnings {
212+
for warning in diagnostics.warnings() {
215213
report(
216214
&sources,
217215
&mut by_url,

crates/rune-wasm/src/lib.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,12 @@ async fn inner_compile(input: String, config: JsValue) -> Result<CompileResult,
196196
options.parse_option(option)?;
197197
}
198198

199-
let mut errors = rune::Errors::new();
200-
let mut warnings = rune::Warnings::new();
201-
199+
let mut d = rune::Diagnostics::new();
202200
let mut diagnostics = Vec::new();
203201

204-
let result = rune::load_sources(&context, &options, &mut sources, &mut errors, &mut warnings);
202+
let result = rune::load_sources(&context, &options, &mut sources, &mut d);
205203

206-
for warning in &warnings {
204+
for warning in d.warnings() {
207205
let span = warning.span();
208206

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

225223
if !config.suppress_text_warnings {
226-
warnings
227-
.emit_diagnostics(&mut writer, &sources)
224+
d.emit_diagnostics(&mut writer, &sources)
228225
.context("emitting to buffer should never fail")?;
229226
}
230227

231228
let unit = match result {
232229
Ok(unit) => Arc::new(unit),
233230
Err(error) => {
234-
for error in &errors {
231+
for error in d.errors() {
235232
if let Some(source) = sources.get(error.source_id()) {
236233
match error.kind() {
237234
rune::ErrorKind::ParseError(error) => {
@@ -311,8 +308,7 @@ async fn inner_compile(input: String, config: JsValue) -> Result<CompileResult,
311308
}
312309
}
313310

314-
errors
315-
.emit_diagnostics(&mut writer, &sources)
311+
d.emit_diagnostics(&mut writer, &sources)
316312
.expect("emitting to buffer should never fail");
317313

318314
return Ok(CompileResult::from_error(

crates/rune/src/compiling/mod.rs

+21-24
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::load::{FileSourceLoader, SourceLoader, Sources};
33
use crate::query::{Build, BuildEntry, Query};
44
use crate::shared::{Consts, Gen};
55
use crate::worker::{LoadFileKind, Task, Worker};
6-
use crate::{Error, Errors, Options, Spanned as _, Storage, Warnings};
6+
use crate::{Diagnostics, Error, Options, Spanned as _, Storage};
77
use runestick::{Context, Location, Source, Span};
88
use std::rc::Rc;
99
use std::sync::Arc;
@@ -26,8 +26,7 @@ pub fn compile(
2626
context: &Context,
2727
sources: &mut Sources,
2828
unit: &UnitBuilder,
29-
errors: &mut Errors,
30-
warnings: &mut Warnings,
29+
diagnostics: &mut Diagnostics,
3130
) -> Result<(), ()> {
3231
let visitor = Rc::new(NoopCompileVisitor::new());
3332
let source_loader = Rc::new(FileSourceLoader::new());
@@ -36,8 +35,7 @@ pub fn compile(
3635
context,
3736
sources,
3837
unit,
39-
errors,
40-
warnings,
38+
diagnostics,
4139
&Default::default(),
4240
visitor,
4341
source_loader,
@@ -51,8 +49,7 @@ pub fn compile_with_options(
5149
context: &Context,
5250
sources: &mut Sources,
5351
unit: &UnitBuilder,
54-
errors: &mut Errors,
55-
warnings: &mut Warnings,
52+
diagnostics: &mut Diagnostics,
5653
options: &Options,
5754
visitor: Rc<dyn CompileVisitor>,
5855
source_loader: Rc<dyn SourceLoader>,
@@ -71,8 +68,7 @@ pub fn compile_with_options(
7168
options,
7269
unit.clone(),
7370
consts,
74-
errors,
75-
warnings,
71+
diagnostics,
7672
visitor.clone(),
7773
source_loader,
7874
storage.clone(),
@@ -84,7 +80,7 @@ pub fn compile_with_options(
8480
let mod_item = match worker.query.insert_root_mod(source_id, Span::empty()) {
8581
Ok(result) => result,
8682
Err(error) => {
87-
errors.push(Error::new(source_id, error));
83+
diagnostics.error(Error::new(source_id, error));
8884
return Err(());
8985
}
9086
};
@@ -98,7 +94,7 @@ pub fn compile_with_options(
9894

9995
worker.run();
10096

101-
if !worker.errors.is_empty() {
97+
if !worker.diagnostics.errors().is_empty() {
10298
return Err(());
10399
}
104100

@@ -112,13 +108,13 @@ pub fn compile_with_options(
112108
options,
113109
storage: &storage,
114110
unit,
115-
warnings: worker.warnings,
111+
diagnostics: worker.diagnostics,
116112
consts: &worker.consts,
117113
query: &mut worker.query,
118114
};
119115

120116
if let Err(error) = task.compile(entry) {
121-
worker.errors.push(Error::new(source_id, error));
117+
worker.diagnostics.error(Error::new(source_id, error));
122118
}
123119
}
124120

@@ -127,13 +123,13 @@ pub fn compile_with_options(
127123
Ok(false) => break,
128124
Err((source_id, error)) => {
129125
worker
130-
.errors
131-
.push(Error::new(source_id, CompileError::from(error)));
126+
.diagnostics
127+
.error(Error::new(source_id, CompileError::from(error)));
132128
}
133129
}
134130
}
135131

136-
if !worker.errors.is_empty() {
132+
if !worker.diagnostics.errors().is_empty() {
137133
return Err(());
138134
}
139135

@@ -146,7 +142,7 @@ struct CompileBuildEntry<'a> {
146142
options: &'a Options,
147143
storage: &'a Storage,
148144
unit: &'a UnitBuilder,
149-
warnings: &'a mut Warnings,
145+
diagnostics: &'a mut Diagnostics,
150146
consts: &'a Consts,
151147
query: &'a mut Query,
152148
}
@@ -173,7 +169,7 @@ impl CompileBuildEntry<'_> {
173169
contexts: vec![span],
174170
loops: self::v1::Loops::new(),
175171
options: self.options,
176-
warnings: self.warnings,
172+
diagnostics: self.diagnostics,
177173
}
178174
}
179175

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

203199
if used.is_unused() {
204-
self.warnings.not_used(location.source_id, span, None);
200+
self.diagnostics.not_used(location.source_id, span, None);
205201
} else {
206202
self.unit.new_function(
207203
location,
@@ -232,7 +228,7 @@ impl CompileBuildEntry<'_> {
232228
f.ast.assemble_fn(&mut c, true)?;
233229

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

259255
if used.is_unused() {
260-
c.warnings.not_used(location.source_id, location.span, None);
256+
c.diagnostics
257+
.not_used(location.source_id, location.span, None);
261258
} else {
262259
self.unit.new_function(
263260
location,
@@ -279,7 +276,7 @@ impl CompileBuildEntry<'_> {
279276
b.ast.assemble_closure(&mut c, &b.captures)?;
280277

281278
if used.is_unused() {
282-
self.warnings
279+
self.diagnostics
283280
.not_used(location.source_id, location.span, None);
284281
} else {
285282
self.unit.new_function(
@@ -293,7 +290,7 @@ impl CompileBuildEntry<'_> {
293290
}
294291
}
295292
Build::Unused => {
296-
self.warnings
293+
self.diagnostics
297294
.not_used(location.source_id, location.span, None);
298295
}
299296
Build::Import(import) => {
@@ -303,7 +300,7 @@ impl CompileBuildEntry<'_> {
303300
.import(location.span, &item.module, &item.item, used)?;
304301

305302
if used.is_unused() {
306-
self.warnings
303+
self.diagnostics
307304
.not_used(location.source_id, location.span, None);
308305
}
309306

crates/rune/src/compiling/unit_builder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use crate::collections::HashMap;
77
use crate::compiling::{Assembly, AssemblyInst};
8-
use crate::{CompileError, CompileErrorKind, Error, Errors};
8+
use crate::{CompileError, CompileErrorKind, Diagnostics, Error};
99
use runestick::debug::{DebugArgs, DebugSignature};
1010
use runestick::{
1111
Call, CompileMeta, CompileMetaKind, ConstValue, Context, DebugInfo, DebugInst, Hash, Inst,
@@ -583,12 +583,12 @@ impl UnitBuilder {
583583
/// functions are provided.
584584
///
585585
/// This can prevent a number of runtime errors, like missing functions.
586-
pub(crate) fn link(&self, context: &Context, errors: &mut Errors) {
586+
pub(crate) fn link(&self, context: &Context, diagnostics: &mut Diagnostics) {
587587
let inner = self.inner.borrow();
588588

589589
for (hash, spans) in &inner.required_functions {
590590
if inner.functions.get(hash).is_none() && context.lookup(*hash).is_none() {
591-
errors.push(Error::new(
591+
diagnostics.error(Error::new(
592592
0,
593593
LinkerError::MissingFunction {
594594
hash: *hash,

crates/rune/src/compiling/v1/assemble/builtin_template.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl Assemble for BuiltInTemplate {
3434
}
3535

3636
if self.from_literal && expansions == 0 {
37-
c.warnings
37+
c.diagnostics
3838
.template_without_expansions(c.source_id, span, c.context());
3939
}
4040

crates/rune/src/compiling/v1/assemble/const_value.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ impl AssembleConst for ConstValue {
66
use num::ToPrimitive as _;
77

88
if !needs.value() {
9-
c.warnings.not_used(c.source_id, span, c.context());
9+
c.diagnostics.not_used(c.source_id, span, c.context());
1010
return Ok(());
1111
}
1212

crates/rune/src/compiling/v1/assemble/expr_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl Assemble for ast::ExprCall {
128128

129129
if tuple.args == 0 {
130130
let tuple = path.span();
131-
c.warnings
131+
c.diagnostics
132132
.remove_tuple_call_parens(c.source_id, span, tuple, c.context());
133133
}
134134
}

0 commit comments

Comments
 (0)