Skip to content

Commit

Permalink
Lint for invalid datum[] indexing (#1877)
Browse files Browse the repository at this point in the history
Co-authored-by: ike709 <[email protected]>
  • Loading branch information
ike709 and ike709 committed Jul 7, 2024
1 parent 5613d62 commit 20edaaf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Content.Tests/DMProject/Tests/Dereference/DatumIndex.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// COMPILE ERROR
#pragma InvalidIndexOperation error

// Indexing a datum (e.g. datum["foo"]) is not valid in BYOND 515.1641+

/proc/RunTest()
var/datum/meep = new
ASSERT(isnull(meep["foo"]))
1 change: 1 addition & 0 deletions DMCompiler/Compiler/CompilerError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public enum WarningCode {
InvalidRange = 2301,
InvalidSetStatement = 2302,
InvalidOverride = 2303,
InvalidIndexOperation = 2304,
DanglingVarType = 2401, // For types inferred by a particular var definition and nowhere else, that ends up not existing (not forced-fatal because BYOND doesn't always error)
MissingInterpolatedExpression = 2500, // A text macro is missing a required interpolated expression
AmbiguousResourcePath = 2600,
Expand Down
16 changes: 16 additions & 0 deletions DMCompiler/DM/Expressions/Dereference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ private void EmitOperation(DMObject dmObject, DMProc proc, Operation operation,
break;

case IndexOperation indexOperation:
if (NestedPath is not null) {
var obj = DMObjectTree.GetDMObject(NestedPath.Value, false);
if (obj is not null && obj.IsSubtypeOf(DreamPath.Datum) && !obj.HasProc("operator[]")) {
DMCompiler.Emit(WarningCode.InvalidIndexOperation, Location, "Invalid index operation. datum[] index operations are not valid starting in BYOND 515.1641");
}
}

indexOperation.Index.EmitPushValue(dmObject, proc);
proc.DereferenceIndex();
break;
Expand Down Expand Up @@ -159,12 +166,21 @@ public override DMReference EmitReference(DMObject dmObject, DMProc proc, string
if (fieldOperation.Safe) {
ShortCircuitHandler(proc, endLabel, shortCircuitMode);
}

return DMReference.CreateField(fieldOperation.Identifier);

case IndexOperation indexOperation:
if (NestedPath is not null) {
var obj = DMObjectTree.GetDMObject(NestedPath.Value, false);
if (obj is not null && obj.IsSubtypeOf(DreamPath.Datum) && !obj.HasProc("operator[]=")) {
DMCompiler.Emit(WarningCode.InvalidIndexOperation, Location, "Invalid index operation. datum[] index operations are not valid starting in BYOND 515.1641");
}
}

if (indexOperation.Safe) {
ShortCircuitHandler(proc, endLabel, shortCircuitMode);
}

indexOperation.Index.EmitPushValue(dmObject, proc);
return DMReference.ListIndex;

Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DMStandard/DefaultPragmaConfig.dm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#pragma InvalidRange error
#pragma InvalidSetStatement error
#pragma InvalidOverride warning
#pragma InvalidIndexOperation warning
#pragma DanglingVarType warning
#pragma MissingInterpolatedExpression warning
#pragma AmbiguousResourcePath warning
Expand Down

0 comments on commit 20edaaf

Please sign in to comment.