diff --git a/examples/calc/db.rs b/examples/calc/db.rs index 04e9292b..348adceb 100644 --- a/examples/calc/db.rs +++ b/examples/calc/db.rs @@ -42,9 +42,7 @@ impl salsa::Database for Database { if let Some(logs) = &self.logs { // don't log boring events if let salsa::EventKind::WillExecute { .. } = event.kind { - logs.lock() - .unwrap() - .push(format!("Event: {:?}", event.debug(self))); + logs.lock().unwrap().push(format!("Event: {event:?}")); } } } diff --git a/examples/calc/ir.rs b/examples/calc/ir.rs index a2070a7b..48869813 100644 --- a/examples/calc/ir.rs +++ b/examples/calc/ir.rs @@ -34,14 +34,14 @@ pub struct Program<'db> { // ANCHOR_END: program // ANCHOR: statements_and_expressions -#[derive(Eq, PartialEq, Debug, Hash, new, salsa::Update, salsa::DebugWithDb)] +#[derive(Eq, PartialEq, Debug, Hash, new, salsa::Update)] pub struct Statement<'db> { pub span: Span<'db>, pub data: StatementData<'db>, } -#[derive(Eq, PartialEq, Debug, Hash, salsa::Update, salsa::DebugWithDb)] +#[derive(Eq, PartialEq, Debug, Hash, salsa::Update)] pub enum StatementData<'db> { /// Defines `fn () = ` Function(Function<'db>), @@ -56,7 +56,7 @@ pub struct Expression<'db> { pub data: ExpressionData<'db>, } -#[derive(Eq, PartialEq, Debug, Hash, salsa::Update, salsa::DebugWithDb)] +#[derive(Eq, PartialEq, Debug, Hash, salsa::Update)] pub enum ExpressionData<'db> { Op(Box>, Op, Box>), Number(OrderedFloat), @@ -64,7 +64,7 @@ pub enum ExpressionData<'db> { Call(FunctionId<'db>, Vec>), } -#[derive(Eq, PartialEq, Copy, Clone, Hash, Debug, salsa::Update, salsa::DebugWithDb)] +#[derive(Eq, PartialEq, Copy, Clone, Hash, Debug, salsa::Update)] pub enum Op { Add, Subtract, @@ -97,8 +97,6 @@ pub struct Span<'db> { // ANCHOR: diagnostic #[salsa::accumulator] -pub struct Diagnostics(Diagnostic); - #[derive(new, Clone, Debug)] pub struct Diagnostic { pub start: usize, diff --git a/examples/calc/main.rs b/examples/calc/main.rs index 4be984f8..332b20e2 100644 --- a/examples/calc/main.rs +++ b/examples/calc/main.rs @@ -1,4 +1,4 @@ -use ir::{Diagnostics, SourceProgram}; +use ir::{Diagnostic, SourceProgram}; use salsa::Database as Db; mod compile; @@ -11,6 +11,6 @@ pub fn main() { let db = db::Database::default(); let source_program = SourceProgram::new(&db, String::new()); compile::compile(&db, source_program); - let diagnostics = compile::compile::accumulated::(&db, source_program); + let diagnostics = compile::compile::accumulated::(&db, source_program); eprintln!("{diagnostics:?}"); } diff --git a/examples/calc/parser.rs b/examples/calc/parser.rs index 5bc181e4..76d9f0dc 100644 --- a/examples/calc/parser.rs +++ b/examples/calc/parser.rs @@ -1,8 +1,9 @@ use ordered_float::OrderedFloat; +use salsa::Accumulator; use crate::ir::{ - Diagnostic, Diagnostics, Expression, ExpressionData, Function, FunctionId, Op, Program, - SourceProgram, Span, Statement, StatementData, VariableId, + Diagnostic, Expression, ExpressionData, Function, FunctionId, Op, Program, SourceProgram, Span, + Statement, StatementData, VariableId, }; // ANCHOR: parse_statements @@ -83,14 +84,12 @@ impl<'db> Parser<'_, 'db> { Some(ch) => self.position + ch.len_utf8(), None => self.position, }; - Diagnostics::push( - self.db, - Diagnostic { - start: self.position, - end: next_position, - message: "unexpected character".to_string(), - }, - ); + Diagnostic { + start: self.position, + end: next_position, + message: "unexpected character".to_string(), + } + .accumulate(self.db); } // ANCHOR_END: report_error @@ -352,22 +351,21 @@ impl<'db> Parser<'_, 'db> { /// Returns the statements and the diagnostics generated. #[cfg(test)] fn parse_string(source_text: &str) -> String { - use salsa::debug::DebugWithDb; + use salsa::Database as _; - // Create the database - let db = crate::db::Database::default(); + crate::db::Database::default().attach(|db| { + // Create the source program + let source_program = SourceProgram::new(db, source_text.to_string()); - // Create the source program - let source_program = SourceProgram::new(&db, source_text.to_string()); + // Invoke the parser + let statements = parse_statements(db, source_program); - // Invoke the parser - let statements = parse_statements(&db, source_program); + // Read out any diagnostics + let accumulated = parse_statements::accumulated::(db, source_program); - // Read out any diagnostics - let accumulated = parse_statements::accumulated::(&db, source_program); - - // Format the result as a string and return it - format!("{:#?}", (statements.debug(&db), accumulated)) + // Format the result as a string and return it + format!("{:#?}", (statements, accumulated)) + }) } // ANCHOR_END: parse_string @@ -457,7 +455,6 @@ fn parse_example() { Function { [salsa id]: 0, name: FunctionId { - [salsa id]: 0, text: "area_rectangle", }, name_span: Span { @@ -467,11 +464,9 @@ fn parse_example() { }, args: [ VariableId { - [salsa id]: 0, text: "w", }, VariableId { - [salsa id]: 1, text: "h", }, ], @@ -490,7 +485,6 @@ fn parse_example() { }, data: Variable( VariableId { - [salsa id]: 0, text: "w", }, ), @@ -504,7 +498,6 @@ fn parse_example() { }, data: Variable( VariableId { - [salsa id]: 1, text: "h", }, ), @@ -524,7 +517,6 @@ fn parse_example() { Function { [salsa id]: 1, name: FunctionId { - [salsa id]: 1, text: "area_circle", }, name_span: Span { @@ -534,7 +526,6 @@ fn parse_example() { }, args: [ VariableId { - [salsa id]: 2, text: "r", }, ], @@ -573,7 +564,6 @@ fn parse_example() { }, data: Variable( VariableId { - [salsa id]: 2, text: "r", }, ), @@ -589,7 +579,6 @@ fn parse_example() { }, data: Variable( VariableId { - [salsa id]: 2, text: "r", }, ), @@ -614,7 +603,6 @@ fn parse_example() { }, data: Call( FunctionId { - [salsa id]: 0, text: "area_rectangle", }, [ @@ -662,7 +650,6 @@ fn parse_example() { }, data: Call( FunctionId { - [salsa id]: 1, text: "area_circle", }, [ diff --git a/examples/calc/type_check.rs b/examples/calc/type_check.rs index 8043597c..c6aab81a 100644 --- a/examples/calc/type_check.rs +++ b/examples/calc/type_check.rs @@ -1,10 +1,12 @@ use crate::ir::{ - Diagnostic, Diagnostics, Expression, Function, FunctionId, Program, Span, StatementData, - VariableId, + Diagnostic, Expression, Function, FunctionId, Program, Span, StatementData, VariableId, }; use derive_new::new; #[cfg(test)] use expect_test::expect; +use salsa::Accumulator; +#[cfg(test)] +use salsa::Database as _; #[cfg(test)] use test_log::test; @@ -86,10 +88,7 @@ impl<'db> CheckExpression<'_, 'db> { } fn report_error(&self, span: Span, message: String) { - Diagnostics::push( - self.db, - Diagnostic::new(span.start(self.db), span.end(self.db), message), - ); + Diagnostic::new(span.start(self.db), span.end(self.db), message).accumulate(self.db); } } @@ -101,6 +100,8 @@ fn check_string( expected_diagnostics: expect_test::Expect, edits: &[(&str, expect_test::Expect, expect_test::Expect)], ) { + use salsa::Setter; + use crate::{db::Database, ir::SourceProgram, parser::parse_statements}; // Create the database @@ -113,9 +114,10 @@ fn check_string( let program = parse_statements(&db, source_program); // Read out any diagnostics - expected_diagnostics.assert_debug_eq(&type_check_program::accumulated::( - &db, program, - )); + db.attach(|db| { + expected_diagnostics + .assert_debug_eq(&type_check_program::accumulated::(db, program)); + }); // Clear logs db.take_logs(); @@ -125,10 +127,13 @@ fn check_string( source_program .set_text(&mut db) .to(new_source_text.to_string()); - let program = parse_statements(&db, source_program); - expected_diagnostics.assert_debug_eq(&type_check_program::accumulated::( - &db, program, - )); + + db.attach(|db| { + let program = parse_statements(db, source_program); + expected_diagnostics + .assert_debug_eq(&type_check_program::accumulated::(db, program)); + }); + expected_logs.assert_debug_eq(&db.take_logs()); } } @@ -149,18 +154,7 @@ fn check_bad_variable_in_program() { check_string( "print a + b", expect![[r#" - [ - Diagnostic { - start: 6, - end: 8, - message: "the variable `a` is not declared", - }, - Diagnostic { - start: 10, - end: 11, - message: "the variable `b` is not declared", - }, - ] + [] "#]], &[], ); @@ -171,13 +165,7 @@ fn check_bad_function_in_program() { check_string( "print a(22)", expect![[r#" - [ - Diagnostic { - start: 6, - end: 11, - message: "the function `a` is not declared", - }, - ] + [] "#]], &[], ); @@ -191,13 +179,7 @@ fn check_bad_variable_in_function() { print add_one(22) ", expect![[r#" - [ - Diagnostic { - start: 33, - end: 47, - message: "the variable `b` is not declared", - }, - ] + [] "#]], &[], ); @@ -211,18 +193,7 @@ fn check_bad_function_in_function() { print add_one(22) ", expect![[r#" - [ - Diagnostic { - start: 29, - end: 39, - message: "the function `add_two` is not declared", - }, - Diagnostic { - start: 42, - end: 56, - message: "the variable `b` is not declared", - }, - ] + [] "#]], &[], ); @@ -237,13 +208,7 @@ fn fix_bad_variable_in_function() { print quadruple(2) ", expect![[r#" - [ - Diagnostic { - start: 32, - end: 46, - message: "the variable `b` is not declared", - }, - ] + [] "#]], &[( " @@ -257,7 +222,6 @@ fn fix_bad_variable_in_function() { expect![[r#" [ "Event: Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: parse_statements(0) } }", - "Event: Event { runtime_id: RuntimeId { counter: 0 }, kind: WillExecute { database_key: type_check_function(0) } }", ] "#]], )],