diff --git a/dora-frontend/src/generator.rs b/dora-frontend/src/generator.rs index 217209462..bec60d83d 100644 --- a/dora-frontend/src/generator.rs +++ b/dora-frontend/src/generator.rs @@ -1778,7 +1778,7 @@ impl<'a> AstBytecodeGen<'a> { CallType::Method(..) | CallType::GenericMethod(..) | CallType::TraitObjectMethod(..) => { - let obj_expr = expr.object().expect("method target required"); + let obj_expr = expr.object().unwrap_or(expr.callee()); let reg = gen_expr(self, obj_expr, DataDest::Alloc); Some(reg) diff --git a/tests/ops/ops-index-get1.dora b/tests/ops/ops-index-get1.dora new file mode 100644 index 000000000..7bf801b1a --- /dev/null +++ b/tests/ops/ops-index-get1.dora @@ -0,0 +1,29 @@ +struct Foo { a: Float64, b: Float64 } + +impl std::traits::IndexGet for Foo { + type Index = Int; + type Item = Float64; + + fn get(index: Self::Index): Self::Item { + if index == 0 { + self.a + } else { + assert(index == 1); + self.b + } + } +} + +fn get0(x: Foo): Float64 { + x(0) +} + +fn get1(x: Foo): Float64 { + x(1) +} + +fn main() { + let x = Foo(a=12.0, b=17.0); + assert(get0(x) == 12.0); + assert(get1(x) == 17.0); +} diff --git a/tests/ops/ops-index-set1.dora b/tests/ops/ops-index-set1.dora new file mode 100644 index 000000000..f8bd8b3a7 --- /dev/null +++ b/tests/ops/ops-index-set1.dora @@ -0,0 +1,34 @@ +class Foo { a: Float64, b: Float64 } + +impl std::traits::IndexSet for Foo { + type Index = Int; + type Item = Float64; + + fn set(index: Self::Index, value: Self::Item) { + if index == 0 { + self.a = value; + } else { + assert(index == 1); + self.b = value; + } + } +} + +fn set0(x: Foo, value: Float64) { + x(0) = value; +} + +fn set1(x: Foo, value: Float64) { + x(1) = value; +} + +fn main() { + let x = Foo(a=12.0, b=18.0); + set0(x, 16.0); + assert(x.a == 16.0); + assert(x.b == 18.0); + + set1(x, 32.0); + assert(x.a == 16.0); + assert(x.b == 32.0); +}