Skip to content

Commit

Permalink
Moved impl rewrite out of conform. (#5659)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi committed May 28, 2024
1 parent 098c821 commit a93f3df
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 38 deletions.
45 changes: 37 additions & 8 deletions crates/cairo-lang-semantic/src/expr/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,14 +1034,43 @@ impl<'a> SemanticRewriter<TypeLongId, NoError> for Inference<'a> {
}
TypeLongId::ImplType(impl_type_id) => {
let impl_type_id_rewrite_result = self.internal_rewrite(impl_type_id)?;
return Ok(
if let Ok(Some(ty)) = self.db.impl_type_concrete_implized(*impl_type_id) {
*value = ty.lookup_intern(self.db);
RewriteResult::Modified
} else {
impl_type_id_rewrite_result
},
);
let impl_id = impl_type_id.impl_id();
let trait_ty = impl_type_id.ty();
return Ok(match impl_id {
ImplId::GenericParameter(_) => impl_type_id_rewrite_result,
ImplId::Concrete(_) => {
if let Ok(Some(ty)) = self.db.impl_type_concrete_implized(ImplTypeId::new(
impl_id, trait_ty, self.db,
)) {
*value = self.rewrite(ty).no_err().lookup_intern(self.db);
RewriteResult::Modified
} else {
impl_type_id_rewrite_result
}
}
ImplId::ImplVar(var) => {
let var_id = var.id(self.db);
if let Some(ty) = self
.data
.impl_vars_trait_types
.get(&var_id)
.and_then(|mapping| mapping.get(&trait_ty))
{
*value = self.rewrite(*ty).no_err().lookup_intern(self.db);
} else {
let ty = self.new_type_var(
self.data.stable_ptrs.get(&InferenceVar::Impl(var_id)).cloned(),
);
self.data
.impl_vars_trait_types
.entry(var_id)
.or_default()
.insert(trait_ty, ty);
*value = ty.lookup_intern(self.db);
}
return Ok(RewriteResult::Modified);
}
});
}
_ => {}
}
Expand Down
15 changes: 2 additions & 13 deletions crates/cairo-lang-semantic/src/expr/inference/conform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@ impl<'db> InferenceConform for Inference<'db> {
}
}
}
TypeLongId::ImplType(impl_type_id) => {
if !matches!(impl_type_id.impl_id(), ImplId::GenericParameter(_)) {
let ty = self.reduce_impl_ty(impl_type_id)?;
return self.conform_ty_ex(ty0, ty, ty0_is_self);
}
}
_ => {}
}
let n_snapshots = 0;
Expand Down Expand Up @@ -168,13 +162,8 @@ impl<'db> InferenceConform for Inference<'db> {
Err(self.set_error(InferenceError::TypeKindMismatch { ty0, ty1 }))
}
TypeLongId::Var(var) => Ok((self.assign_ty(var, ty1)?, n_snapshots)),
TypeLongId::ImplType(impl_type_id) => {
if !matches!(impl_type_id.impl_id(), ImplId::GenericParameter(_)) {
let ty = self.reduce_impl_ty(impl_type_id)?;
self.conform_ty_ex(ty, ty1, ty0_is_self)
} else {
Err(self.set_error(InferenceError::TypeKindMismatch { ty0, ty1 }))
}
TypeLongId::ImplType(_) => {
Err(self.set_error(InferenceError::TypeKindMismatch { ty0, ty1 }))
}
TypeLongId::Missing(_) => Ok((ty0, n_snapshots)),
TypeLongId::Coupon(function_id0) => {
Expand Down
34 changes: 17 additions & 17 deletions crates/cairo-lang-semantic/src/items/tests/trait_type
Original file line number Diff line number Diff line change
Expand Up @@ -1022,22 +1022,22 @@ trait MyTrait {
}

//! > expected_diagnostics
error: Trait has no implementation in context: test::AnotherTrait0.
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:17:31
fn bar1(x: AnotherTrait0::AnotherType);
^*********^

error: Trait has no implementation in context: test::AnotherTrait0.
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:18:33
fn bar2() -> AnotherTrait0::AnotherType;
^*********^

error: Trait `test::AnotherTrait2` has multiple implementations, in: `test::AnotherImpl20`, `test::AnotherImpl21`
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:19:31
fn bar3(x: AnotherTrait2::AnotherType);
^*********^

error: Trait `test::AnotherTrait2` has multiple implementations, in: `test::AnotherImpl20`, `test::AnotherImpl21`
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:20:33
fn bar4() -> AnotherTrait2::AnotherType;
^*********^
Expand Down Expand Up @@ -1192,32 +1192,32 @@ fn bar3() {
}

//! > expected_diagnostics
error: Trait has no implementation in context: test::AnotherTrait.
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:9:32
fn foo1() -> AnotherTrait::AnotherType {
^*********^

error: Return type of impl function `MyImpl::foo1` is incompatible with `MyTrait::foo1`. Expected: `core::integer::u32`, actual: `ImplVar(test::AnotherTrait)::AnotherType`.
error: Return type of impl function `MyImpl::foo1` is incompatible with `MyTrait::foo1`. Expected: `core::integer::u32`, actual: `<missing>`.
--> lib.cairo:9:18
fn foo1() -> AnotherTrait::AnotherType {
^***********************^

error: Trait has no implementation in context: test::AnotherTrait.
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:12:30
fn foo2(x: AnotherTrait::AnotherType) {}
^*********^

error: Parameter type of impl function `MyImpl::foo2` is incompatible with `MyTrait::foo2`. Expected: `core::integer::u32`, actual: `ImplVar(test::AnotherTrait)::AnotherType`.
error: Parameter type of impl function `MyImpl::foo2` is incompatible with `MyTrait::foo2`. Expected: `core::integer::u32`, actual: `<missing>`.
--> lib.cairo:12:16
fn foo2(x: AnotherTrait::AnotherType) {}
^***********************^

error: Trait has no implementation in context: test::AnotherTrait.
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:14:28
fn bar1() -> AnotherTrait::AnotherType {
^*********^

error: Trait has no implementation in context: test::AnotherTrait.
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:17:26
fn bar2(x: AnotherTrait::AnotherType) {}
^*********^
Expand Down Expand Up @@ -1269,32 +1269,32 @@ fn bar3() {
}

//! > expected_diagnostics
error: Trait `test::AnotherTrait` has multiple implementations, in: `test::AnotherImpl1`, `test::AnotherImpl2`
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:15:32
fn foo1() -> AnotherTrait::AnotherType {
^*********^

error: Return type of impl function `MyImpl::foo1` is incompatible with `MyTrait::foo1`. Expected: `core::integer::u32`, actual: `ImplVar(test::AnotherTrait)::AnotherType`.
error: Return type of impl function `MyImpl::foo1` is incompatible with `MyTrait::foo1`. Expected: `core::integer::u32`, actual: `<missing>`.
--> lib.cairo:15:18
fn foo1() -> AnotherTrait::AnotherType {
^***********************^

error: Trait `test::AnotherTrait` has multiple implementations, in: `test::AnotherImpl1`, `test::AnotherImpl2`
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:18:30
fn foo2(x: AnotherTrait::AnotherType) {}
^*********^

error: Parameter type of impl function `MyImpl::foo2` is incompatible with `MyTrait::foo2`. Expected: `core::integer::u32`, actual: `ImplVar(test::AnotherTrait)::AnotherType`.
error: Parameter type of impl function `MyImpl::foo2` is incompatible with `MyTrait::foo2`. Expected: `core::integer::u32`, actual: `<missing>`.
--> lib.cairo:18:16
fn foo2(x: AnotherTrait::AnotherType) {}
^***********************^

error: Trait `test::AnotherTrait` has multiple implementations, in: `test::AnotherImpl1`, `test::AnotherImpl2`
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:20:28
fn bar1() -> AnotherTrait::AnotherType {
^*********^

error: Trait `test::AnotherTrait` has multiple implementations, in: `test::AnotherImpl1`, `test::AnotherImpl2`
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:23:26
fn bar2(x: AnotherTrait::AnotherType) {}
^*********^
Expand Down Expand Up @@ -2184,7 +2184,7 @@ trait Trt {
}

//! > expected_diagnostics
error: Type annotations needed. Failed to infer ImplVar(test::Trt)::T.
error: Type annotations needed. Failed to infer ?0.
--> lib.cairo:7:7
x.t
^
Expand Down

0 comments on commit a93f3df

Please sign in to comment.