Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c6cd0b4

Browse files
committedMar 24, 2025
progress
1 parent 1da5731 commit c6cd0b4

File tree

2 files changed

+49
-18
lines changed

2 files changed

+49
-18
lines changed
 

‎src/ir/lowering/lower.rs

+40-15
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ use crate::{
1111
};
1212

1313
use super::{
14-
IRBuilderContext, Symbol,
15-
adts::{lower_enum, lower_struct},
16-
constants::lower_constant,
17-
functions::{lower_func, lower_func_decl},
18-
ir::{IR, ModuleIndex, Type},
19-
traits::{TraitDatabase, TraitImpl},
20-
types::lower_type,
14+
adts::{lower_enum, lower_struct}, constants::lower_constant, functions::{lower_func, lower_func_decl}, ir::{ModuleIndex, Type, IR}, traits::{TraitDatabase, TraitGeneric, TraitImpl}, types::lower_type, IRBuilderContext, Symbol
2115
};
2216

2317
/// Lowers the ast compile units, the last should be the "main" unit whose unit tests are saved.
@@ -632,16 +626,47 @@ pub fn lower_module(
632626
path: builder.get_current_module().file_path.clone(),
633627
})?;
634628

629+
let mut trait_generics = Vec::new();
630+
631+
for g in &impl_trait.generic_params {
632+
if let Some(ty) = builder.context.generics_mapping.get(&g.name.name) {
633+
trait_generics.push(TraitGeneric::Type(*ty));
634+
} else {
635+
trait_generics.push(TraitGeneric::Generic);
636+
}
637+
}
638+
639+
let target_ty = if !impl_trait.generic_params.is_empty() {
640+
let adt_symbol = Symbol {
641+
name: impl_trait.target.get_name().unwrap(),
642+
method_of: None,
643+
generics: Vec::new(),
644+
};
645+
646+
let id = *builder
647+
.symbols
648+
.get(&builder.get_current_module_idx())
649+
.unwrap()
650+
.aggregates
651+
.get(&adt_symbol)
652+
.unwrap();
653+
654+
let type_id = *builder.adt_to_type_idx.get(&id).unwrap();
655+
type_id
656+
} else {
657+
lower_type(builder, &impl_trait.target)?
658+
};
659+
660+
builder.trait_db.add_trait_impl(
661+
trait_id,
662+
TraitImpl {
663+
implementor: target_ty,
664+
generics: trait_generics,
665+
},
666+
);
667+
635668
if impl_trait.generic_params.is_empty() {
636-
let target_ty = lower_type(builder, &impl_trait.target)?;
637669
builder.context.self_ty = Some(target_ty);
638-
builder.trait_db.add_trait_impl(
639-
trait_id,
640-
TraitImpl {
641-
implementor: target_ty,
642-
generics: Vec::new(),
643-
},
644-
);
645670
for method in &impl_trait.methods {
646671
if method.decl.generic_params.is_empty() {
647672
lower_func(builder, method, Some(target_ty))?;

‎src/ir/lowering/traits.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use typed_generational_arena::{StandardSlab, StandardSlabIndex};
44

55
use crate::{
66
ast::traits::TraitDecl,
7-
ir::{FnIndex, ModuleIndex, TypeIndex},
7+
ir::{ModuleIndex, TypeIndex},
88
};
99

1010
pub type TraitIdx = StandardSlabIndex<Arc<TraitDecl>>;
@@ -26,7 +26,13 @@ pub struct TraitDatabase {
2626
#[derive(Debug, Clone)]
2727
pub struct TraitImpl {
2828
pub implementor: TypeIndex,
29-
pub generics: Vec<TypeIndex>,
29+
pub generics: Vec<TraitGeneric>,
30+
}
31+
32+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
33+
pub enum TraitGeneric {
34+
Generic,
35+
Type(TypeIndex),
3036
}
3137

3238
impl Default for TraitDatabase {
@@ -72,7 +78,7 @@ impl TraitDatabase {
7278
&self,
7379
ty: TypeIndex,
7480
check_trait: TraitIdx,
75-
generics: &[TypeIndex],
81+
generics: &[TraitGeneric],
7682
) -> bool {
7783
if let Some(t) = self.implementors.get(&check_trait.to_idx()) {
7884
if let Some(impls) = t.get(&ty.to_idx()) {

0 commit comments

Comments
 (0)
Please sign in to comment.