diff --git a/dust-lang/src/compiler.rs b/dust-lang/src/compiler.rs index d6dbe1a..8314966 100644 --- a/dust-lang/src/compiler.rs +++ b/dust-lang/src/compiler.rs @@ -632,7 +632,15 @@ impl<'src> Compiler<'src> { .push((right_instruction, right_type, right_position)); } - let destination = Destination::Register(self.next_register()); + let destination = if is_assignment { + match left { + Argument::Register(register) => Destination::Register(register), + Argument::Local(local_index) => Destination::Local(local_index), + Argument::Constant(_) => Destination::Register(self.next_register()), + } + } else { + Destination::Register(self.next_register()) + }; let instruction = match operator { Token::Plus | Token::PlusEqual => Instruction::add(destination, left, right), Token::Minus | Token::MinusEqual => Instruction::subtract(destination, left, right), @@ -1268,6 +1276,7 @@ impl<'src> Compiler<'src> { should_return_value, }); + self.update_return_type(previous_expression_type)?; self.emit_instruction(r#return, Type::None, self.current_position); } diff --git a/dust-lang/tests/math.rs b/dust-lang/tests/math.rs index 150214f..f17fe8c 100644 --- a/dust-lang/tests/math.rs +++ b/dust-lang/tests/math.rs @@ -59,11 +59,11 @@ fn add_assign() { ), ( Instruction::add( - Destination::Register(0), - Argument::Register(0), + Destination::Local(0), + Argument::Local(0), Argument::Constant(2) ), - Type::Integer, + Type::None, Span(17, 19) ), ( @@ -176,11 +176,11 @@ fn divide_assign() { ), ( Instruction::divide( - Destination::Register(0), - Argument::Register(0), + Destination::Local(0), + Argument::Local(0), Argument::Constant(0) ), - Type::Integer, + Type::None, Span(17, 19) ), ( @@ -339,11 +339,11 @@ fn multiply_assign() { ), ( Instruction::multiply( - Destination::Register(0), - Argument::Register(0), + Destination::Local(0), + Argument::Local(0), Argument::Constant(2) ), - Type::Integer, + Type::None, Span(17, 19) ), ( @@ -440,11 +440,11 @@ fn subtract_assign() { ), ( Instruction::subtract( - Destination::Register(0), - Argument::Register(0), + Destination::Local(0), + Argument::Local(0), Argument::Constant(2) ), - Type::Integer, + Type::None, Span(18, 20) ), (