Skip to content

Commit

Permalink
Finding @T impls near T definition.
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi committed Jan 8, 2025
1 parent 536872b commit b0b026e
Show file tree
Hide file tree
Showing 29 changed files with 236 additions and 244 deletions.
2 changes: 1 addition & 1 deletion corelib/src/ops.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pub use index::{Index, IndexView};
mod arith;
pub use arith::{AddAssign, DivAssign, MulAssign, RemAssign, SubAssign};
mod deref;
pub use deref::Deref;
#[feature("deref_mut")]
pub use deref::DerefMut;
pub use deref::{Deref, SnapshotDeref};
mod range;
// `RangeOp` is used internally by the compiler.
#[allow(unused_imports)]
Expand Down
19 changes: 0 additions & 19 deletions corelib/src/ops/deref.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,6 @@ pub trait DerefMut<T> {
fn deref_mut(ref self: T) -> Self::Target;
}

/// A helper trait for dereferencing a snapshot of a type. Should not be implemented for copyable
/// types.
pub trait SnapshotDeref<T> {
/// The type of the dereferenced value.
type Target;
/// Returns the dereferenced value.
fn snapshot_deref(self: @T) -> Self::Target;
}

/// Implementation of `Deref` for snapshots that implement `SnapshotDeref`.
// TODO(Gil): Add a negative impl for Copy types, it is not working right now when T is a generic
// type.
impl SnapshotDerefHelper<T, +SnapshotDeref<T>> of Deref<@T> {
type Target = SnapshotDeref::<T>::Target;
fn deref(self: @T) -> Self::Target {
self.snapshot_deref()
}
}

/// Implementation of `Deref` for copyable snapshots.
// TODO(Gil): This should not use the `*` operator as the `*` operator will later be calling
// `Deref`.
Expand Down
43 changes: 27 additions & 16 deletions crates/cairo-lang-semantic/src/expr/inference/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,35 @@ pub fn enrich_lookup_context(
// Add the defining module of the generic args to the lookup.
for generic_arg in &generic_args {
if let GenericArgumentId::Type(ty) = generic_arg {
match ty.lookup_intern(db) {
TypeLongId::Concrete(concrete) => {
lookup_context
.insert_module(concrete.generic_type(db).module_file_id(db.upcast()).0);
}
TypeLongId::Coupon(function_id) => {
if let Some(module_file_id) =
function_id.get_concrete(db).generic_function.module_file_id(db)
{
lookup_context.insert_module(module_file_id.0);
}
}
TypeLongId::ImplType(impl_type_id) => {
lookup_context.insert_impl(impl_type_id.impl_id());
}
_ => (),
enrich_lookup_context_with_ty(db, *ty, lookup_context);
}
}
}

/// Adds the defining module of the type to the lookup context.
fn enrich_lookup_context_with_ty(
db: &dyn SemanticGroup,
ty: TypeId,
lookup_context: &mut ImplLookupContext,
) {
match ty.lookup_intern(db) {
TypeLongId::Concrete(concrete) => {
lookup_context.insert_module(concrete.generic_type(db).module_file_id(db.upcast()).0);
}
TypeLongId::Coupon(function_id) => {
if let Some(module_file_id) =
function_id.get_concrete(db).generic_function.module_file_id(db)
{
lookup_context.insert_module(module_file_id.0);
}
}
TypeLongId::ImplType(impl_type_id) => {
lookup_context.insert_impl(impl_type_id.impl_id());
}
TypeLongId::Snapshot(ty) => {
enrich_lookup_context_with_ty(db, ty, lookup_context);
}
_ => (),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -906,9 +906,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down Expand Up @@ -271,9 +271,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down Expand Up @@ -455,9 +455,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down Expand Up @@ -635,9 +635,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down Expand Up @@ -815,9 +815,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down Expand Up @@ -999,9 +999,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down Expand Up @@ -1182,9 +1182,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down Expand Up @@ -1358,9 +1358,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down Expand Up @@ -1546,9 +1546,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down Expand Up @@ -1779,9 +1779,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down Expand Up @@ -1963,9 +1963,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,9 @@ pub struct ComponentState<TContractState> {

impl ComponentStateDrop<TContractState> of Drop<ComponentState<TContractState>> {}

impl ComponentStateDeref<TContractState> of core::ops::SnapshotDeref<ComponentState<TContractState>> {
impl ComponentStateDeref<TContractState> of core::ops::Deref<@ComponentState<TContractState>> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ComponentState<TContractState>) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ pub struct ContractState {

impl ContractStateDrop of Drop<ContractState> {}

impl ContractStateDeref of core::ops::SnapshotDeref<ContractState> {
impl ContractStateDeref of core::ops::Deref<@ContractState> {
type Target = starknet::storage::FlattenedStorage<Storage>;
fn snapshot_deref(self: @ContractState) -> starknet::storage::FlattenedStorage<Storage> {
fn deref(self: @ContractState) -> starknet::storage::FlattenedStorage<Storage> {
starknet::storage::FlattenedStorage {}
}
}
Expand Down
Loading

0 comments on commit b0b026e

Please sign in to comment.