Skip to content

Commit

Permalink
Merge pull request #16340 from dkorpel/editions-experiment
Browse files Browse the repository at this point in the history
Start implementation of editions
  • Loading branch information
ibuclaw committed Apr 5, 2024
2 parents d5764b0 + 400dd01 commit 0cfdd7a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 2 deletions.
9 changes: 9 additions & 0 deletions compiler/src/dmd/astenums.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ enum Sizeok : ubyte
done, /// size of aggregate is set correctly
}

/// D Language version
enum Edition : ubyte
{
none,
legacy, /// Before the introduction of editions
v2024, /// Experimental first new edition
latest = v2024 /// Newest edition that this compiler knows of
}

enum Baseok : ubyte
{
none, /// base classes not computed yet
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dmd/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ extern (C++) final class Module : Package
FileType filetype; // source file type
bool hasAlwaysInlines; // contains references to functions that must be inlined
bool isPackageFile; // if it is a package.d
Edition edition; // language edition that this module is compiled with
Package pkg; // if isPackageFile is true, the Package that contains this package.d
Strings contentImportedFiles; // array of files whose content was imported
int needmoduleinfo;
Expand Down Expand Up @@ -477,6 +478,8 @@ extern (C++) final class Module : Package
setDocfile();
if (doHdrGen)
hdrfile = setOutfilename(global.params.dihdr.name, global.params.dihdr.dir, arg, hdr_ext);

this.edition = Edition.legacy;
}

extern (D) this(const(char)[] filename, Identifier ident, int doDocComment, int doHdrGen)
Expand Down
10 changes: 8 additions & 2 deletions compiler/src/dmd/dscope.d
Original file line number Diff line number Diff line change
Expand Up @@ -830,12 +830,18 @@ extern (C++) struct Scope
/// Returns: whether to raise DIP1000 warnings (FeatureStabe.default) or errors (FeatureState.enabled)
extern (D) FeatureState useDIP1000()
{
return (flags & SCOPE.dip1000) ? FeatureState.enabled : FeatureState.disabled;
return (flags & SCOPE.dip1000 || hasEdition(Edition.v2024)) ? FeatureState.enabled : FeatureState.disabled;
}

/// Returns: whether to raise DIP25 warnings (FeatureStabe.default) or errors (FeatureState.enabled)
extern (D) FeatureState useDIP25()
{
return (flags & SCOPE.dip25) ? FeatureState.enabled : FeatureState.disabled;
return (flags & SCOPE.dip25 || hasEdition(Edition.v2024)) ? FeatureState.enabled : FeatureState.disabled;
}

/// Returns: whether this scope compiles with `edition` or later
extern (D) bool hasEdition(Edition edition)
{
return _module && _module.edition >= edition;
}
}
9 changes: 9 additions & 0 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,14 @@ enum class FileType : uint8_t
c = 3u,
};

enum class Edition : uint8_t
{
none = 0u,
legacy = 1u,
v2024 = 2u,
latest = 2u,
};

struct OutBuffer final
{
private:
Expand Down Expand Up @@ -7074,6 +7082,7 @@ class Module final : public Package
FileType filetype;
bool hasAlwaysInlines;
bool isPackageFile;
Edition edition;
Package* pkg;
Array<const char* > contentImportedFiles;
int32_t needmoduleinfo;
Expand Down
3 changes: 3 additions & 0 deletions compiler/src/dmd/id.d
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,9 @@ immutable Msgtable[] msgtable =
{ "udaMustUse", "mustuse" },
{ "udaStandalone", "standalone" },

// Editions
{ "__edition_latest_do_not_use", },

// C names, for undefined identifier error messages
{ "NULL" },
{ "TRUE" },
Expand Down
9 changes: 9 additions & 0 deletions compiler/src/dmd/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ enum PKG
PKGpackage // already determined that's an actual package
};

enum class Edition : unsigned char
{
none = 0u,
legacy = 1u,
v2024 = 2u,
latest = 2u,
};

class Package : public ScopeDsymbol
{
public:
Expand Down Expand Up @@ -75,6 +83,7 @@ class Module final : public Package
FileType filetype; // source file type
d_bool hasAlwaysInlines; // contains references to functions that must be inlined
d_bool isPackageFile; // if it is a package.d
Edition edition; // language edition that this module is compiled with
Package *pkg; // if isPackageFile is true, the Package that contains this package.d
Strings contentImportedFiles; // array of files whose content was imported
int needmoduleinfo;
Expand Down
9 changes: 9 additions & 0 deletions compiler/src/dmd/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer
}
else
{
static if (is(typeof(mod.edition)))
if (exps && exps.length > 0)
if (auto id = (*exps)[0].isIdentifierExp())
if (id.ident == Id.__edition_latest_do_not_use)
{
mod.edition = Edition.latest;
continue;
}

udas = AST.UserAttributeDeclaration.concat(udas, exps);
}
if (stc)
Expand Down
16 changes: 16 additions & 0 deletions compiler/test/fail_compilation/editions.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
Test language editions (currently experimental)
TEST_OUTPUT:
---
fail_compilation/editions.d(15): Error: scope parameter `x` may not be returned
---
*/
@__edition_latest_do_not_use
module editions;

@safe:
int* f(scope int* x)
{
return x;
}

0 comments on commit 0cfdd7a

Please sign in to comment.