M jasm/assemble/assembler_impl/symbols_impl.cpp +18 -4
@@ 663,11 663,25 @@ void Assembler::evaluate_symbol(bool gen
// and previous pass references must be allowed for constants.
if (variable_reference != nullptr) {
- value.type = ValueType::ValueReference;
- value.set_found_in_current_pass(found_in_current_pass);
- value.variable_reference_hash = combined_hash;
- value.type_hash = _static_value_reference_type;
+ if (variable_reference->type_hash == 0) {
+ // This can happen if the symbol is created and used in the same expression.
+ // So the variable hasn't been properly initialized yet.
+ set_unknown(value);
+ value.global = true;
+ value.storage_type = StorageType::Constant; // Not entirely sure what to set this to. Type must be checked before this.
+
+ if (generate) {
+ std::stringstream ss;
+ ss << "A symbol's definition can't refer to itself.";
+ report_error(component.source_location, AssemblyErrorCodes::SymbolRecursion, ss.str());
+ }
+ } else {
+ value.type = ValueType::ValueReference;
+ value.set_found_in_current_pass(found_in_current_pass);
+ value.variable_reference_hash = combined_hash;
+ value.type_hash = _static_value_reference_type;
+ }
} else {
// symbol was not found or is ambiguous
M jasm/exceptions/error_codes.h +1 -0
@@ 207,6 207,7 @@ enum class AssemblyErrorCodes
AddressingModeRequiresBitSizeArgument,
MacroValueExpected,
ExpectedBooleanResult,
+ SymbolRecursion,
// assembler warnings
SubroutineFallthrough = 3500,
A => jasm/unit_tests/results/test_self_assignment_constant.stdout +2 -0
@@ 0,0 1,2 @@
+unit_tests/test_self_assignment_constant.asm(3,11) : Error 3116 : A symbol's definition can't refer to itself.
+Assembly ended with errors.
A => jasm/unit_tests/results/test_self_assignment_variable.stdout +2 -0
@@ 0,0 1,2 @@
+unit_tests/test_self_assignment_variable.asm(3,9) : Error 3116 : A symbol's definition can't refer to itself.
+Assembly ended with errors.
A => jasm/unit_tests/test_self_assignment_constant.asm +7 -0
@@ 0,0 1,7 @@
+// assembler command line arguments: 65c02 [-v0]
+
+const q = q
+
+section code, "main", 0, 100
+{
+}
A => jasm/unit_tests/test_self_assignment_variable.asm +7 -0
@@ 0,0 1,7 @@
+// assembler command line arguments: 65c02 [-v0]
+
+var q = q
+
+section code, "main", 0, 100
+{
+}