Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile error when outputting to const vars in for() loops #2124

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// COMPILE ERROR
// COMPILE ERROR OD0501

/datum
var/const/idx = 0
Expand Down
18 changes: 18 additions & 0 deletions Content.Tests/DMProject/Tests/Statements/For/reuse_decl_const2.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// COMPILE ERROR OD0501

/datum
var/const/idx = 0
var/c = 0
proc/do_loop()
for (idx in list(1,2,3))
c += idx

/proc/RunTest()
var/datum/d = new
d.do_loop()

var/const/idx = 0
var/c = 0
for (idx in list(1,2,3))
c += idx

9 changes: 8 additions & 1 deletion DMCompiler/DM/Builders/DMProcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,10 @@ public void ProcessStatementFor(DMASTProcStatementFor statementFor) {

var outputVar = _exprBuilder.Create(outputExpr);

if (outputVar is Local { LocalVar: DMProc.LocalConstVariable } or Field { IsConst: true }) {
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");
}

var start = _exprBuilder.Create(exprRange.StartRange);
var end = _exprBuilder.Create(exprRange.EndRange);
var step = exprRange.Step != null
Expand Down Expand Up @@ -520,7 +524,10 @@ public void ProcessStatementFor(DMASTProcStatementFor statementFor) {

if (outputVar is Local outputLocal) {
outputLocal.LocalVar.ExplicitValueType = statementFor.DMTypes;
}
if(outputLocal.LocalVar is DMProc.LocalConstVariable)
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");
} else if (outputVar is Field { IsConst: true })
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");

ProcessStatementForList(list, outputVar, statementFor.DMTypes, statementFor.Body);
break;
Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DM/Expressions/LValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public override void EmitPushInitial(ExpressionContext ctx) {

// Identifier of field
internal sealed class Field(Location location, DMVariable variable, DMComplexValueType valType) : LValue(location, variable.Type) {
public bool IsConst { get; } = variable.IsConst;
public override DMComplexValueType ValType => valType;

public override void EmitPushInitial(ExpressionContext ctx) {
Expand Down
Loading