Skip to content

Commit

Permalink
frontend: Enable test for default map() method
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Feb 6, 2025
1 parent f50b143 commit 3e06096
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 126 deletions.
8 changes: 8 additions & 0 deletions dora-frontend/src/specialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ pub fn specialize_ty_for_call(
trait_ty,
assoc_id,
}
} else if type_param_ty.is_self() {
let caller_fct = caller_element.to_fct().expect("expected function");

assert_eq!(
caller_fct.trait_id(),
assoc.parent.to_trait_id().expect("expected trait")
);
SourceType::Assoc(assoc_id, SourceTypeArray::empty())
} else if let Some(impl_match) = find_impl(
sa,
caller_element,
Expand Down
42 changes: 8 additions & 34 deletions dora-frontend/src/typeck/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,21 +561,9 @@ fn check_expr_call_struct(
}

if struct_.field_name_style.is_named() {
check_expr_call_ctor_with_named_fields(
ck,
struct_,
struct_,
type_params.clone(),
&arguments,
);
check_expr_call_ctor_with_named_fields(ck, struct_, type_params.clone(), &arguments);
} else {
check_expr_call_ctor_with_unnamed_fields(
ck,
struct_,
struct_,
type_params.clone(),
&arguments,
);
check_expr_call_ctor_with_unnamed_fields(ck, struct_, type_params.clone(), &arguments);
}

ck.analysis
Expand All @@ -589,7 +577,6 @@ fn check_expr_call_struct(
fn check_expr_call_ctor_with_named_fields(
ck: &mut TypeCheck,
element_with_fields: &dyn ElementWithFields,
element: &dyn Element,
type_params: SourceTypeArray,
arguments: &CallArguments,
) {
Expand Down Expand Up @@ -636,7 +623,7 @@ fn check_expr_call_ctor_with_named_fields(
for field in element_with_fields.fields() {
if let Some(name) = field.name {
if let Some(arg) = args_by_name.remove(&name) {
let def_ty = specialize_ty_for_call(ck.sa, field.ty, element, &call_data);
let def_ty = specialize_ty_for_call(ck.sa, field.ty, ck.element, &call_data);
let arg_ty = ck.analysis.ty(arg.id);

if !def_ty.allows(ck.sa, arg_ty.clone()) && !arg_ty.is_error() {
Expand Down Expand Up @@ -671,7 +658,6 @@ fn check_expr_call_ctor_with_named_fields(
fn check_expr_call_ctor_with_unnamed_fields(
ck: &mut TypeCheck,
element_with_fields: &dyn ElementWithFields,
element: &dyn Element,
type_params: SourceTypeArray,
arguments: &CallArguments,
) -> bool {
Expand All @@ -681,7 +667,7 @@ fn check_expr_call_ctor_with_unnamed_fields(
};

for (field, argument) in element_with_fields.fields().zip(&arguments.arguments) {
let def_ty = specialize_ty_for_call(ck.sa, field.ty, element, &call_data);
let def_ty = specialize_ty_for_call(ck.sa, field.ty, ck.element, &call_data);
let arg_ty = ck.analysis.ty(argument.id);

if let Some(ref name) = argument.name {
Expand Down Expand Up @@ -767,9 +753,9 @@ fn check_expr_call_class(
}

if cls.field_name_style.is_named() {
check_expr_call_ctor_with_named_fields(ck, cls, cls, type_params.clone(), &arguments);
check_expr_call_ctor_with_named_fields(ck, cls, type_params.clone(), &arguments);
} else {
check_expr_call_ctor_with_unnamed_fields(ck, cls, cls, type_params.clone(), &arguments);
check_expr_call_ctor_with_unnamed_fields(ck, cls, type_params.clone(), &arguments);
}

ck.analysis
Expand Down Expand Up @@ -823,21 +809,9 @@ pub(super) fn check_expr_call_enum_variant(
ck.sa.report(ck.file_id, e.span, msg);
} else {
if variant.field_name_style.is_named() {
check_expr_call_ctor_with_named_fields(
ck,
variant,
enum_,
type_params.clone(),
&arguments,
);
check_expr_call_ctor_with_named_fields(ck, variant, type_params.clone(), &arguments);
} else {
check_expr_call_ctor_with_unnamed_fields(
ck,
variant,
enum_,
type_params.clone(),
&arguments,
);
check_expr_call_ctor_with_unnamed_fields(ck, variant, type_params.clone(), &arguments);
}
}

Expand Down
5 changes: 4 additions & 1 deletion dora-runtime/src/vm/specialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,10 @@ pub fn specialize_ty(
specialize_ty(vm, self_data, impl_alias_ty, &bindings)
}

BytecodeType::This => self_data.expect("unexpected Self").extended_ty.clone(),
BytecodeType::This => {
let ty = self_data.expect("unexpected Self").extended_ty.clone();
specialize_ty(vm, self_data, ty, type_params)
}

BytecodeType::TypeAlias(..) => {
unreachable!()
Expand Down
54 changes: 54 additions & 0 deletions tests/trait/trait-default-map.dora
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
trait MyIterator {
type Item;
fn next(): Option[Self::Item];

fn map[R](f: (Self::Item): R): Map[Self, R] {
Map[Self, R](it = self, fct = f)
}
}

class Range {
value: Int
}

impl MyIterator for Range {
type Item = Int;

fn next(): Option[Int] {
if self.value <= 0 {
None[Int]
} else {
let result = self.value;
self.value -= 1;
Some[Int](result)
}
}
}

class Map[I: MyIterator, R] {
it: I,
fct: (I::Item): R,
}

impl[I: MyIterator, R] MyIterator for Map[I, R] {
type Item = R;

fn next(): Option[R] {
if self.it.next() is Some(value) {
Some[R](self.fct(value))
} else {
None[R]
}
}
}

fn main() {
let r = Range(3).map[Int](|x: Int|: Int {
x * 3
});

assert(Some[Int]((9)) == r.next());
assert(Some[Int]((6)) == r.next());
assert(Some[Int]((3)) == r.next());
assert(r.next().isNone());
}
91 changes: 0 additions & 91 deletions tests/trait/trait-default-wip.dora

This file was deleted.

0 comments on commit 3e06096

Please sign in to comment.