Skip to content

Commit

Permalink
Disallow var defines on /world and /list (#2206)
Browse files Browse the repository at this point in the history
  • Loading branch information
wixoaGit authored Feb 6, 2025
1 parent 81b476a commit d1ab8fc
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions DMCompiler/DM/DMCodeTree.Vars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public override bool TryDefineVar(DMCompiler compiler, int pass) {
if (!compiler.DMObjectTree.TryGetDMObject(owner, out var dmObject))
return false;

if (AlreadyExists(compiler, dmObject)) {
if (CheckCantDefine(compiler, dmObject)) {
_defined = true;
return true;
}
Expand Down Expand Up @@ -162,12 +162,23 @@ private bool HandleInstanceVar(DMCompiler compiler, DMObject dmObject) {
return true;
}

private bool AlreadyExists(DMCompiler compiler, DMObject dmObject) {
// "type" and "tag" can only be defined in DMStandard
if (VarName is "type" or "tag" && !varDef.Location.InDMStandard) {
compiler.Emit(WarningCode.InvalidVarDefinition, varDef.Location,
$"Cannot redefine built-in var \"{VarName}\"");
return true;
private bool CheckCantDefine(DMCompiler compiler, DMObject dmObject) {
if (!compiler.Settings.NoStandard) {
var inStandard = varDef.Location.InDMStandard;

// "type" and "tag" can only be defined in DMStandard
if (VarName is "type" or "tag" && !inStandard) {
compiler.Emit(WarningCode.InvalidVarDefinition, varDef.Location,
$"Cannot redefine built-in var \"{VarName}\"");
return true;
}

// Vars on /world and /list can only be defined in DMStandard
if ((dmObject.Path == DreamPath.World || dmObject.Path == DreamPath.List) && !inStandard) {
compiler.Emit(WarningCode.InvalidVarDefinition, varDef.Location,
$"Cannot define a var on type {dmObject.Path}");
return true;
}
}

//DMObjects store two bundles of variables; the statics in GlobalVariables and the non-statics in Variables.
Expand Down

0 comments on commit d1ab8fc

Please sign in to comment.