Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding @T impls near T definition. #7024

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading