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

Made associated items not replaced if they aren't of the Self trait. #7027

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
24 changes: 24 additions & 0 deletions crates/cairo-lang-semantic/src/items/tests/trait_default_fn
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,27 @@ error: Parameter of trait function `MyTrait::foo` can't be defined as mutable.
--> lib.cairo:2:12
fn foo(mut x: u32) -> u32 {
^^^

//! > ==========================================================================

//! > Test default implementation with associated types.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function
fn foo() {}

//! > function_name
foo

//! > module_code
pub trait MyTrait {
type Associated;
fn impl_required() -> Option<Self::Associated>;
fn default_impl() -> bool {
Self::impl_required().is_some()
}
}

//! > expected_diagnostics
58 changes: 31 additions & 27 deletions crates/cairo-lang-semantic/src/substitution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,16 @@ impl SemanticRewriter<TypeLongId, DiagnosticAdded> for SubstitutionRewriter<'_>
}
TypeLongId::TraitType(trait_type_id) => {
if let Some(self_impl) = &self.substitution.self_impl {
assert_eq!(
trait_type_id.trait_id(self.db.upcast()),
self_impl.concrete_trait(self.db)?.trait_id(self.db)
);
let impl_type_id = ImplTypeId::new(*self_impl, *trait_type_id, self.db);
*value =
self.db.impl_type_concrete_implized(impl_type_id)?.lookup_intern(self.db);
return Ok(RewriteResult::Modified);
if trait_type_id.trait_id(self.db.upcast())
== self_impl.concrete_trait(self.db)?.trait_id(self.db)
{
let impl_type_id = ImplTypeId::new(*self_impl, *trait_type_id, self.db);
*value = self
.db
.impl_type_concrete_implized(impl_type_id)?
.lookup_intern(self.db);
return Ok(RewriteResult::Modified);
}
}
}
_ => {}
Expand Down Expand Up @@ -550,18 +552,18 @@ impl SemanticRewriter<ConstValue, DiagnosticAdded> for SubstitutionRewriter<'_>
}
ConstValue::TraitConstant(trait_constant_id) => {
if let Some(self_impl) = &self.substitution.self_impl {
assert_eq!(
trait_constant_id.trait_id(self.db.upcast()),
self_impl.concrete_trait(self.db)?.trait_id(self.db)
);
let impl_const_id =
ImplConstantId::new(*self_impl, *trait_constant_id, self.db);
*value = self
.db
.impl_constant_concrete_implized_value(impl_const_id)?
.lookup_intern(self.db);
if trait_constant_id.trait_id(self.db.upcast())
== self_impl.concrete_trait(self.db)?.trait_id(self.db)
{
let impl_const_id =
ImplConstantId::new(*self_impl, *trait_constant_id, self.db);
*value = self
.db
.impl_constant_concrete_implized_value(impl_const_id)?
.lookup_intern(self.db);

return Ok(RewriteResult::Modified);
return Ok(RewriteResult::Modified);
}
}
}
_ => {}
Expand Down Expand Up @@ -595,15 +597,17 @@ impl SemanticRewriter<ImplLongId, DiagnosticAdded> for SubstitutionRewriter<'_>
}
ImplLongId::TraitImpl(trait_impl_id) => {
if let Some(self_impl) = &self.substitution.self_impl {
assert_eq!(
trait_impl_id.trait_id(self.db.upcast()),
self_impl.concrete_trait(self.db)?.trait_id(self.db)
);
let impl_impl_id = ImplImplId::new(*self_impl, *trait_impl_id, self.db);
*value =
self.db.impl_impl_concrete_implized(impl_impl_id)?.lookup_intern(self.db);
if trait_impl_id.trait_id(self.db.upcast())
== self_impl.concrete_trait(self.db)?.trait_id(self.db)
{
let impl_impl_id = ImplImplId::new(*self_impl, *trait_impl_id, self.db);
*value = self
.db
.impl_impl_concrete_implized(impl_impl_id)?
.lookup_intern(self.db);

return Ok(RewriteResult::Modified);
return Ok(RewriteResult::Modified);
}
}
}
_ => {}
Expand Down
Loading