Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 committed Dec 8, 2024
1 parent 85efc53 commit 93d52ba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
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

14 changes: 8 additions & 6 deletions DMCompiler/DM/Builders/DMProcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,12 @@ public void ProcessStatementFor(DMASTProcStatementFor statementFor) {
outputExpr = exprRange.Value;
}

if (outputExpr is DMASTIdentifier identifier &&
dmObject.GetVariable(identifier.Identifier) is { IsConst: true }) {
compiler.Emit(WarningCode.WriteToConstant, outputExpr.Location, "Cannot change constant value");
}

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");

Check notice

Code scanning / InspectCode

Redundant string interpolation Note

Redundant string interpolation
}

var start = _exprBuilder.Create(exprRange.StartRange);
var end = _exprBuilder.Create(exprRange.EndRange);
var step = exprRange.Step != null
Expand Down Expand Up @@ -525,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");

Check notice

Code scanning / InspectCode

Redundant string interpolation Note

Redundant string interpolation
} 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

0 comments on commit 93d52ba

Please sign in to comment.