Skip to content

Commit 22fd6e5

Browse files
authored
Remove Upcast trait and using DebugDbUpcast only where explicitly required. (#8505)
1 parent dc0d867 commit 22fd6e5

File tree

20 files changed

+53
-134
lines changed

20 files changed

+53
-134
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cairo-lang-compiler/src/db.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use cairo_lang_semantic::inline_macros::get_default_plugin_suite;
1717
use cairo_lang_semantic::plugin::PluginSuite;
1818
use cairo_lang_sierra_generator::db::init_sierra_gen_group;
1919
use cairo_lang_sierra_generator::program_generator::get_dummy_program_for_size_estimation;
20-
use cairo_lang_utils::Upcast;
2120
use salsa::Database;
2221

2322
use crate::InliningStrategy;
@@ -242,9 +241,3 @@ pub fn validate_corelib(db: &(dyn salsa::Database + 'static)) -> Result<()> {
242241
};
243242
bail!("Corelib version mismatch: expected `{expected}`, found `{found}`{path_part}.");
244243
}
245-
246-
impl<'db> Upcast<'db, dyn Database> for RootDatabase {
247-
fn upcast(&self) -> &dyn Database {
248-
self
249-
}
250-
}

crates/cairo-lang-debug/src/debug.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::hash::Hash;
88
use std::rc::Rc;
99
use std::sync::Arc;
1010

11-
use cairo_lang_utils::Upcast;
1211
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
1312
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
1413

@@ -33,6 +32,16 @@ pub trait DebugWithDb<'db> {
3332
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &'db Self::Db) -> std::fmt::Result;
3433
}
3534

35+
/// A trait that allows upcasting the database to the type T.
36+
pub trait DebugDbUpcast<'db, T: ?Sized> {
37+
fn debug_db_upcast(&'db self) -> &'db T;
38+
}
39+
impl<'db, T: ?Sized> DebugDbUpcast<'db, T> for T {
40+
fn debug_db_upcast(&'db self) -> &'db T {
41+
self
42+
}
43+
}
44+
3645
pub struct DebugWith<'me, 'db, Db: ?Sized> {
3746
value: BoxRef<'me, dyn DebugWithDb<'db, Db = Db> + 'me>,
3847
db: &'db Db,
@@ -192,11 +201,14 @@ impl<'db, A, B> DebugWithDb<'db> for (A, B)
192201
where
193202
A: DebugWithDb<'db>,
194203
B: DebugWithDb<'db> + 'db,
195-
A::Db: Upcast<'db, B::Db>,
204+
A::Db: DebugDbUpcast<'db, B::Db>,
196205
{
197206
type Db = A::Db;
198207
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &'db Self::Db) -> std::fmt::Result {
199-
f.debug_tuple("").field(&self.0.debug(db)).field(&self.1.debug(db.upcast())).finish()
208+
f.debug_tuple("")
209+
.field(&self.0.debug(db))
210+
.field(&self.1.debug(db.debug_db_upcast()))
211+
.finish()
200212
}
201213
}
202214

@@ -205,15 +217,15 @@ where
205217
A: DebugWithDb<'db>,
206218
B: DebugWithDb<'db> + 'db,
207219
C: DebugWithDb<'db> + 'db,
208-
A::Db: Upcast<'db, B::Db>,
209-
A::Db: Upcast<'db, C::Db>,
220+
A::Db: DebugDbUpcast<'db, B::Db>,
221+
A::Db: DebugDbUpcast<'db, C::Db>,
210222
{
211223
type Db = A::Db;
212224
fn fmt(&self, f: &mut std::fmt::Formatter<'_>, db: &'db Self::Db) -> std::fmt::Result {
213225
f.debug_tuple("")
214226
.field(&self.0.debug(db))
215-
.field(&self.1.debug(db.upcast()))
216-
.field(&self.2.debug(db.upcast()))
227+
.field(&self.1.debug(db.debug_db_upcast()))
228+
.field(&self.2.debug(db.debug_db_upcast()))
217229
.finish()
218230
}
219231
}
@@ -278,9 +290,7 @@ pub mod helper {
278290
use std::fmt;
279291
use std::marker::PhantomData;
280292

281-
use cairo_lang_utils::Upcast;
282-
283-
use super::{DebugWith, DebugWithDb};
293+
use super::{DebugDbUpcast, DebugWith, DebugWithDb};
284294

285295
pub trait Fallback<'db, T: fmt::Debug, Db: ?Sized> {
286296
fn helper_debug(a: &'db T, _db: &'db Db) -> &'db dyn fmt::Debug {
@@ -290,10 +300,10 @@ pub mod helper {
290300

291301
pub struct HelperDebug<T, Db: ?Sized>(PhantomData<T>, PhantomData<Db>);
292302

293-
impl<'db, T: DebugWithDb<'db>, Db: ?Sized + Upcast<'db, T::Db>> HelperDebug<T, Db> {
303+
impl<'db, T: DebugWithDb<'db>, Db: ?Sized + DebugDbUpcast<'db, T::Db>> HelperDebug<T, Db> {
294304
#[allow(dead_code)]
295305
pub fn helper_debug<'me>(a: &'me T, db: &'db Db) -> DebugWith<'me, 'db, T::Db> {
296-
a.debug(db.upcast())
306+
a.debug(db.debug_db_upcast())
297307
}
298308
}
299309

crates/cairo-lang-debug/src/debug_test.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use cairo_lang_proc_macros::DebugWithDb;
22
use cairo_lang_test_utils::test;
3-
use cairo_lang_utils::Upcast;
43
use salsa::{Database, Storage};
54

65
use crate::debug::DebugWithDb;
@@ -33,24 +32,6 @@ impl<'db> DebugWithDb<'db> for Dummy<'db> {
3332
}
3433
}
3534

36-
impl<'db> Upcast<'db, dyn TestGroup> for DummyDb {
37-
fn upcast(&'db self) -> &'db dyn TestGroup {
38-
self
39-
}
40-
}
41-
42-
impl<'db> Upcast<'db, ()> for dyn TestGroup {
43-
fn upcast(&'db self) -> &'db () {
44-
&()
45-
}
46-
}
47-
48-
impl<'db> Upcast<'db, dyn Database> for dyn TestGroup {
49-
fn upcast(&'db self) -> &'db dyn Database {
50-
self
51-
}
52-
}
53-
5435
#[derive(DebugWithDb)]
5536
#[debug_db(dyn Database)]
5637
struct ComplexStruct<'db> {

crates/cairo-lang-defs/src/test.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use cairo_lang_syntax::node::kind::SyntaxKind;
1111
use cairo_lang_syntax::node::{SyntaxNode, Terminal, TypedSyntaxNode, ast};
1212
use cairo_lang_test_utils::parse_test_file::TestRunnerResult;
1313
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
14-
use cairo_lang_utils::{Intern, Upcast, extract_matches, try_extract_matches};
14+
use cairo_lang_utils::{Intern, extract_matches, try_extract_matches};
1515
use indoc::indoc;
1616
use salsa::{Database, Setter};
1717

@@ -46,11 +46,6 @@ impl Default for DatabaseForTesting {
4646
res
4747
}
4848
}
49-
impl<'db> Upcast<'db, dyn Database> for DatabaseForTesting {
50-
fn upcast(&'db self) -> &'db dyn Database {
51-
self
52-
}
53-
}
5449

5550
cairo_lang_test_utils::test_file_test!(
5651
defs,

crates/cairo-lang-doc/src/tests/test_utils.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use cairo_lang_filesystem::{override_file_content, set_crate_config};
1010
use cairo_lang_parser::db::ParserGroup;
1111
use cairo_lang_semantic::db::{PluginSuiteInput, init_semantic_group};
1212
use cairo_lang_semantic::plugin::PluginSuite;
13-
use cairo_lang_utils::{Intern, Upcast};
13+
use cairo_lang_utils::Intern;
1414
use salsa::Database;
1515

1616
#[salsa::db]
@@ -45,12 +45,6 @@ impl TestDatabase {
4545
}
4646
}
4747

48-
impl<'db> Upcast<'db, dyn Database> for TestDatabase {
49-
fn upcast(&self) -> &dyn Database {
50-
self
51-
}
52-
}
53-
5448
pub fn setup_test_module(db: &mut dyn Database, content: &str) {
5549
let crate_id = test_crate_id(db);
5650
let directory = Directory::Real("src".into());

crates/cairo-lang-lowering/src/lower/block_builder_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use cairo_lang_semantic::usage::MemberPath;
66
use cairo_lang_semantic::{self as semantic, Expr, Statement, StatementId};
77
use cairo_lang_syntax::node::TypedStablePtr;
88
use cairo_lang_test_utils::parse_test_file::TestRunnerResult;
9+
use cairo_lang_utils::extract_matches;
910
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
1011
use cairo_lang_utils::unordered_hash_set::UnorderedHashSet;
11-
use cairo_lang_utils::{Upcast, extract_matches};
1212
use itertools::Itertools;
1313

1414
use super::block_builder::{BlockBuilder, merge_block_builders};
@@ -80,7 +80,7 @@ fn test_merge_block_builders(
8080
.map(|_| ctx.new_var(VarRequest { ty: unit_ty(ctx.db), location: dummy_location }))
8181
.collect();
8282

83-
let expr_formatter = ExprFormatter { db: db.upcast(), function_id: test_function.function_id };
83+
let expr_formatter = ExprFormatter { db: &db, function_id: test_function.function_id };
8484

8585
let input_blocks = create_block_builders(&mut ctx, &test_function, &lowering_vars);
8686
let input_blocks_str =
@@ -89,7 +89,7 @@ fn test_merge_block_builders(
8989
// Invoke [merge_block_builders] on the input blocks.
9090
let merged_block = merge_block_builders(&mut ctx, input_blocks, dummy_location);
9191

92-
let lowered_formatter = LoweredFormatter::new(db.upcast(), &ctx.variables.variables);
92+
let lowered_formatter = LoweredFormatter::new(&db, &ctx.variables.variables);
9393
let lowered_blocks = ctx.blocks.build().unwrap();
9494
let lowered_str = lowered_blocks
9595
.iter()

crates/cairo-lang-lowering/src/test_utils.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use cairo_lang_filesystem::db::{init_dev_corelib, init_files_group};
66
use cairo_lang_filesystem::detect::detect_corelib;
77
use cairo_lang_semantic::db::{PluginSuiteInput, init_semantic_group};
88
use cairo_lang_semantic::inline_macros::get_default_plugin_suite;
9-
use cairo_lang_utils::Upcast;
109
use salsa::Database;
1110

1211
use crate::Lowered;
@@ -52,12 +51,6 @@ impl Default for LoweringDatabaseForTesting {
5251
}
5352
}
5453

55-
impl<'db> Upcast<'db, dyn Database> for LoweringDatabaseForTesting {
56-
fn upcast(&self) -> &dyn Database {
57-
self
58-
}
59-
}
60-
6154
/// Helper for formatting a lowered representation for tests.
6255
pub fn formatted_lowered(db: &dyn Database, lowered: Option<&Lowered<'_>>) -> String {
6356
match lowered {

crates/cairo-lang-parser/src/utils.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use cairo_lang_filesystem::span::{TextOffset, TextWidth};
77
use cairo_lang_primitive_token::{PrimitiveToken, ToPrimitiveTokenStream};
88
use cairo_lang_syntax::node::ast::SyntaxFile;
99
use cairo_lang_syntax::node::{SyntaxNode, TypedSyntaxNode};
10-
use cairo_lang_utils::{Intern, Upcast};
10+
use cairo_lang_utils::Intern;
1111
use itertools::chain;
1212

1313
use crate::ParserDiagnostic;
@@ -29,12 +29,6 @@ impl Default for SimpleParserDatabase {
2929
}
3030
}
3131

32-
impl<'db> Upcast<'db, dyn salsa::Database> for SimpleParserDatabase {
33-
fn upcast(&'db self) -> &'db dyn salsa::Database {
34-
self
35-
}
36-
}
37-
3832
impl SimpleParserDatabase {
3933
/// Parses new file and returns its syntax root.
4034
///

crates/cairo-lang-plugins/src/test.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use cairo_lang_syntax::node::helpers::QueryAttrs;
1919
use cairo_lang_syntax::node::{TypedSyntaxNode, ast};
2020
use cairo_lang_test_utils::parse_test_file::TestRunnerResult;
2121
use cairo_lang_test_utils::verify_diagnostics_expectation;
22+
use cairo_lang_utils::Intern;
2223
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
23-
use cairo_lang_utils::{Intern, Upcast};
2424
use itertools::{Itertools, chain};
2525
use salsa::{AsDynDatabase, Database, Setter};
2626

@@ -70,11 +70,6 @@ impl Default for DatabaseForTesting {
7070
res
7171
}
7272
}
73-
impl<'db> Upcast<'db, dyn Database> for DatabaseForTesting {
74-
fn upcast(&'db self) -> &'db dyn Database {
75-
self
76-
}
77-
}
7873

7974
/// Tests expansion of given code, with only the default plugins.
8075
pub fn test_expand_plugin(

0 commit comments

Comments
 (0)