Skip to content

Commit c8ced19

Browse files
authored
editions syntax and command line (dlang#21421)
1 parent 430661d commit c8ced19

File tree

11 files changed

+66
-20
lines changed

11 files changed

+66
-20
lines changed

compiler/src/dmd/astbase.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ struct ASTBase
13811381

13821382
const FileName srcfile;
13831383
const(char)[] arg;
1384-
Edition edition = Edition.legacy;
1384+
Edition edition = Edition.min;
13851385

13861386
extern (D) this(Loc loc, const(char)[] filename, Identifier ident, int doDocComment, int doHdrGen)
13871387
{

compiler/src/dmd/astenums.d

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ enum Sizeok : ubyte
1919
}
2020

2121
/// D Language version
22-
enum Edition : ubyte
22+
enum Edition : ushort
2323
{
24-
none,
25-
legacy, /// Before the introduction of editions
26-
v2024, /// Experimental first new edition
27-
latest = v2024 /// Newest edition that this compiler knows of
24+
v2023 = 2023, /// Edition.min for default edition
25+
v2024, /// first Edition
26+
v2025, /// next Edition
27+
/// use Edition.max for latest edition
2828
}
2929

3030
enum Baseok : ubyte

compiler/src/dmd/backend/arm/disasmarm.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,6 +3116,7 @@ unittest
31163116
ubyte[BUFMAX] buf2;
31173117
bool errors;
31183118

3119+
@trusted
31193120
void testcase(int line, string s, uint size)
31203121
{
31213122
//printf("testcase(line %d s: '%.*s'\n", cast(int)line, cast(int)s.length, s.ptr);

compiler/src/dmd/cli.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ dmd -cov -unittest myprog.d
345345
$(LI $(I all): All)
346346
)`,
347347
),
348+
Option("edition[=<NNNN>G[<filename>]]",
349+
"set language edition to edition year, apply to <filename>",
350+
"set edition to default, to a particular year NNNN, apply only to a particular $(I filename)"
351+
),
348352
Option("extern-std=<standard>",
349353
"set C++ name mangling compatibility with <standard>",
350354
"Standards supported are:

compiler/src/dmd/dmodule.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ extern (C++) final class Module : Package
474474
if (doHdrGen)
475475
hdrfile = setOutfilename(global.params.dihdr.name, global.params.dihdr.dir, arg, hdr_ext);
476476

477-
this.edition = Edition.legacy;
477+
this.edition = Edition.min;
478478
}
479479

480480
extern (D) this(const(char)[] filename, Identifier ident, int doDocComment, int doHdrGen)

compiler/src/dmd/frontend.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,12 +1015,11 @@ enum class FileType : uint8_t
10151015
c = 3u,
10161016
};
10171017

1018-
enum class Edition : uint8_t
1018+
enum class Edition : uint16_t
10191019
{
1020-
none = 0u,
1021-
legacy = 1u,
1022-
v2024 = 2u,
1023-
latest = 2u,
1020+
v2023 = 2023u,
1021+
v2024 = 2024u,
1022+
v2025 = 2025u,
10241023
};
10251024

10261025
struct OutBuffer final
@@ -8445,6 +8444,8 @@ struct Param final
84458444
CppStdRevision cplusplus;
84468445
Help help;
84478446
Verbose v;
8447+
Edition edition;
8448+
void* editionFiles;
84488449
FeatureState useDIP25;
84498450
FeatureState useDIP1000;
84508451
bool ehnogc;
@@ -8586,7 +8587,7 @@ struct Param final
85868587
timeTraceFile()
85878588
{
85888589
}
8589-
Param(bool obj, bool readStdin = false, bool multiobj = false, bool trace = false, bool tracegc = false, bool vcg_ast = false, DiagnosticReporting useDeprecated = (DiagnosticReporting)1u, bool useUnitTests = false, bool useInline = false, bool release = false, bool preservePaths = false, DiagnosticReporting useWarnings = (DiagnosticReporting)2u, bool cov = false, uint8_t covPercent = 0u, bool ctfe_cov = false, bool ignoreUnsupportedPragmas = true, bool useModuleInfo = true, bool useTypeInfo = true, bool useExceptions = true, bool useGC = true, bool betterC = false, bool addMain = false, bool allInst = false, bool bitfields = false, CppStdRevision cplusplus = (CppStdRevision)201103u, Help help = Help(), Verbose v = Verbose(), FeatureState useDIP25 = (FeatureState)2u, FeatureState useDIP1000 = (FeatureState)0u, bool ehnogc = false, bool useDIP1021 = false, FeatureState fieldwise = (FeatureState)0u, bool fixAliasThis = false, FeatureState rvalueRefParam = (FeatureState)0u, FeatureState safer = (FeatureState)0u, FeatureState noSharedAccess = (FeatureState)0u, bool previewIn = false, bool inclusiveInContracts = false, bool shortenedMethods = true, bool fixImmutableConv = false, bool fix16997 = true, FeatureState dtorFields = (FeatureState)0u, FeatureState systemVariables = (FeatureState)0u, CHECKENABLE useInvariants = (CHECKENABLE)0u, CHECKENABLE useIn = (CHECKENABLE)0u, CHECKENABLE useOut = (CHECKENABLE)0u, CHECKENABLE useArrayBounds = (CHECKENABLE)0u, CHECKENABLE useAssert = (CHECKENABLE)0u, CHECKENABLE useSwitchError = (CHECKENABLE)0u, CHECKENABLE boundscheck = (CHECKENABLE)0u, CHECKACTION checkAction = (CHECKACTION)0u, CLIIdentifierTable dIdentifierTable = (CLIIdentifierTable)0u, CLIIdentifierTable cIdentifierTable = (CLIIdentifierTable)0u, _d_dynamicArray< const char > argv0 = {}, Array<const char* > modFileAliasStrings = Array<const char* >(), Array<ImportPathInfo > imppath = Array<ImportPathInfo >(), Array<const char* > fileImppath = Array<const char* >(), _d_dynamicArray< const char > objdir = {}, _d_dynamicArray< const char > objname = {}, _d_dynamicArray< const char > libname = {}, Output ddoc = Output(), Output dihdr = Output(), Output cxxhdr = Output(), Output json = Output(), JsonFieldFlags jsonFieldFlags = (JsonFieldFlags)0u, Output makeDeps = Output(), Output mixinOut = Output(), Output moduleDeps = Output(), bool debugEnabled = false, bool run = false, Array<const char* > runargs = Array<const char* >(), Array<const char* > cppswitches = Array<const char* >(), const char* cpp = nullptr, Array<const char* > objfiles = Array<const char* >(), Array<const char* > linkswitches = Array<const char* >(), Array<bool > linkswitchIsForCC = Array<bool >(), Array<const char* > libfiles = Array<const char* >(), Array<const char* > dllfiles = Array<const char* >(), _d_dynamicArray< const char > deffile = {}, _d_dynamicArray< const char > resfile = {}, _d_dynamicArray< const char > exefile = {}, _d_dynamicArray< const char > mapfile = {}, bool fullyQualifiedObjectFiles = false, bool timeTrace = false, uint32_t timeTraceGranularityUs = 500u, const char* timeTraceFile = nullptr) :
8590+
Param(bool obj, bool readStdin = false, bool multiobj = false, bool trace = false, bool tracegc = false, bool vcg_ast = false, DiagnosticReporting useDeprecated = (DiagnosticReporting)1u, bool useUnitTests = false, bool useInline = false, bool release = false, bool preservePaths = false, DiagnosticReporting useWarnings = (DiagnosticReporting)2u, bool cov = false, uint8_t covPercent = 0u, bool ctfe_cov = false, bool ignoreUnsupportedPragmas = true, bool useModuleInfo = true, bool useTypeInfo = true, bool useExceptions = true, bool useGC = true, bool betterC = false, bool addMain = false, bool allInst = false, bool bitfields = false, CppStdRevision cplusplus = (CppStdRevision)201103u, Help help = Help(), Verbose v = Verbose(), Edition edition = (Edition)2023u, void* editionFiles = nullptr, FeatureState useDIP25 = (FeatureState)2u, FeatureState useDIP1000 = (FeatureState)0u, bool ehnogc = false, bool useDIP1021 = false, FeatureState fieldwise = (FeatureState)0u, bool fixAliasThis = false, FeatureState rvalueRefParam = (FeatureState)0u, FeatureState safer = (FeatureState)0u, FeatureState noSharedAccess = (FeatureState)0u, bool previewIn = false, bool inclusiveInContracts = false, bool shortenedMethods = true, bool fixImmutableConv = false, bool fix16997 = true, FeatureState dtorFields = (FeatureState)0u, FeatureState systemVariables = (FeatureState)0u, CHECKENABLE useInvariants = (CHECKENABLE)0u, CHECKENABLE useIn = (CHECKENABLE)0u, CHECKENABLE useOut = (CHECKENABLE)0u, CHECKENABLE useArrayBounds = (CHECKENABLE)0u, CHECKENABLE useAssert = (CHECKENABLE)0u, CHECKENABLE useSwitchError = (CHECKENABLE)0u, CHECKENABLE boundscheck = (CHECKENABLE)0u, CHECKACTION checkAction = (CHECKACTION)0u, CLIIdentifierTable dIdentifierTable = (CLIIdentifierTable)0u, CLIIdentifierTable cIdentifierTable = (CLIIdentifierTable)0u, _d_dynamicArray< const char > argv0 = {}, Array<const char* > modFileAliasStrings = Array<const char* >(), Array<ImportPathInfo > imppath = Array<ImportPathInfo >(), Array<const char* > fileImppath = Array<const char* >(), _d_dynamicArray< const char > objdir = {}, _d_dynamicArray< const char > objname = {}, _d_dynamicArray< const char > libname = {}, Output ddoc = Output(), Output dihdr = Output(), Output cxxhdr = Output(), Output json = Output(), JsonFieldFlags jsonFieldFlags = (JsonFieldFlags)0u, Output makeDeps = Output(), Output mixinOut = Output(), Output moduleDeps = Output(), bool debugEnabled = false, bool run = false, Array<const char* > runargs = Array<const char* >(), Array<const char* > cppswitches = Array<const char* >(), const char* cpp = nullptr, Array<const char* > objfiles = Array<const char* >(), Array<const char* > linkswitches = Array<const char* >(), Array<bool > linkswitchIsForCC = Array<bool >(), Array<const char* > libfiles = Array<const char* >(), Array<const char* > dllfiles = Array<const char* >(), _d_dynamicArray< const char > deffile = {}, _d_dynamicArray< const char > resfile = {}, _d_dynamicArray< const char > exefile = {}, _d_dynamicArray< const char > mapfile = {}, bool fullyQualifiedObjectFiles = false, bool timeTrace = false, uint32_t timeTraceGranularityUs = 500u, const char* timeTraceFile = nullptr) :
85908591
obj(obj),
85918592
readStdin(readStdin),
85928593
multiobj(multiobj),
@@ -8614,6 +8615,8 @@ struct Param final
86148615
cplusplus(cplusplus),
86158616
help(help),
86168617
v(v),
8618+
edition(edition),
8619+
editionFiles(editionFiles),
86178620
useDIP25(useDIP25),
86188621
useDIP1000(useDIP1000),
86198622
ehnogc(ehnogc),

compiler/src/dmd/globals.d

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ extern (C++) struct Param
189189
Help help;
190190
Verbose v;
191191

192+
// Editions
193+
Edition edition; // edition year
194+
Edition[const(char)*] editionFiles; // Edition corresponding to a filespec
195+
196+
192197
// Options for `-preview=/-revert=`
193198
FeatureState useDIP25 = FeatureState.enabled; // implement https://wiki.dlang.org/DIP25
194199
FeatureState useDIP1000; // implement https://dlang.org/spec/memory-safe-d.html#scope-return-params

compiler/src/dmd/globals.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/* Compiler implementation of the D programming language
32
* Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
43
* written by Walter Bright
@@ -196,6 +195,9 @@ struct Param
196195
Help help;
197196
Verbose v;
198197

198+
unsigned short edition; // edition year
199+
void* editionFiles; // Edition corresponding to a filespec
200+
199201
// Options for `-preview=/-revert=`
200202
FeatureState useDIP25; // implement https://wiki.dlang.org/DIP25
201203
FeatureState useDIP1000; // implement https://dlang.org/spec/memory-safe-d.html#scope-return-params

compiler/src/dmd/mars.d

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,24 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, ref Param
855855
error("unknown dllimport '%.*s', must be 'none', 'defaultLibsOnly' or 'all'", cast(int) imp.length, imp.ptr);
856856
}
857857
}
858+
else if (arg == "-edition")
859+
params.edition = Edition.min; // i.e. no edition
860+
else if (startsWith(p + 1, "edition="))
861+
{
862+
if (arg.length >= 9 + 4 &&
863+
!parseDigits!(Edition)(params.edition, arg[9 .. 9 + 4], Edition.max) ||
864+
params.edition < Edition.min)
865+
{
866+
error("edition %d is not in the range %d ... %d", params.edition, Edition.min, Edition.max);
867+
}
868+
869+
if (arg.length > 1+7+1+4) // -edition=YYYYfilename
870+
{
871+
auto filename = p + 1+7+1+4;
872+
files.push(filename);
873+
params.editionFiles[filename] = params.edition;
874+
}
875+
}
858876
else if (arg == "-fIBT")
859877
{
860878
driverParams.ibt = true;

compiler/src/dmd/module.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#pragma once
1212

13+
#include <stdint.h>
14+
1315
#include "dsymbol.h"
1416

1517
struct ModuleDeclaration;
@@ -28,12 +30,11 @@ enum PKG
2830
PKGpackage // already determined that's an actual package
2931
};
3032

31-
enum class Edition : unsigned char
33+
enum class Edition : uint16_t
3234
{
33-
none = 0u,
34-
legacy = 1u,
35-
v2024 = 2u,
36-
latest = 2u,
35+
v2023 = 2023,
36+
v2024,
37+
v2025,
3738
};
3839

3940
class Package : public ScopeDsymbol

0 commit comments

Comments
 (0)