From ae6a91c2cb5bec5b6a5e1e33fef66e1071f116fd Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Wed, 17 Dec 2025 12:02:01 +0000 Subject: [PATCH 1/2] Implement `-edition=YYYY` semantics Set `Module.edition` in constructor. Make `-edition=YYYYfilename` an error as it does nothing (and also wrongly sets `params.edition`). Allow a larger `-edition` to override a module declaration's edition. --- compiler/src/dmd/dmodule.d | 2 +- compiler/src/dmd/mars.d | 2 ++ compiler/src/dmd/parse.d | 4 +++- compiler/test/fail_compilation/edition_switch.d | 13 +++++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 compiler/test/fail_compilation/edition_switch.d diff --git a/compiler/src/dmd/dmodule.d b/compiler/src/dmd/dmodule.d index dc974bc7c646..e6ee54879d75 100644 --- a/compiler/src/dmd/dmodule.d +++ b/compiler/src/dmd/dmodule.d @@ -437,7 +437,7 @@ extern (C++) final class Module : Package if (doHdrGen) hdrfile = setOutfilename(global.params.dihdr.name, global.params.dihdr.dir, arg, hdr_ext); - this.edition = Edition.min; + this.edition = global.params.edition; } extern (D) this(const(char)[] filename, Identifier ident, int doDocComment, int doHdrGen) diff --git a/compiler/src/dmd/mars.d b/compiler/src/dmd/mars.d index 714d2af78717..c3897bd4ade7 100644 --- a/compiler/src/dmd/mars.d +++ b/compiler/src/dmd/mars.d @@ -908,6 +908,8 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, out Param auto filename = p + 1+7+1+4; files.push(filename); params.editionFiles[filename] = params.edition; + // FIXME: params.edition should not be set when there's a filename + error("`-edition` is not supported with a filename yet"); } } else if (arg == "-fIBT") diff --git a/compiler/src/dmd/parse.d b/compiler/src/dmd/parse.d index e5f9b665a097..c06c061a0cff 100644 --- a/compiler/src/dmd/parse.d +++ b/compiler/src/dmd/parse.d @@ -150,7 +150,9 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer error("module edition %lld must be in the range %d ... %d", edition, Edition.min, Edition.max); edition = edition.min; } - mod.edition = cast(Edition)edition; + // ModuleDeclaration edition can't lower -edition flag's edition + if (edition > mod.edition) + mod.edition = cast(Edition)edition; nextToken(); } diff --git a/compiler/test/fail_compilation/edition_switch.d b/compiler/test/fail_compilation/edition_switch.d new file mode 100644 index 000000000000..5c45bccbc3cf --- /dev/null +++ b/compiler/test/fail_compilation/edition_switch.d @@ -0,0 +1,13 @@ +/* +REQUIRED_ARGS: -edition=2024 +TEST_OUTPUT: +--- +fail_compilation/edition_switch.d(13): Error: usage of identifer `body` as a keyword is obsolete. Use `do` instead. +--- +*/ + +// test -edition can override a lower module declaration +module m 2023; + +void test() +in { } body { } From 91209faca291aa3630fb1ea542cf2630bbfbac7e Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Thu, 18 Dec 2025 16:49:32 +0000 Subject: [PATCH 2/2] `-edition` cannot override ModuleDeclaration edition --- compiler/src/dmd/parse.d | 4 +--- compiler/test/fail_compilation/edition_switch.d | 5 +---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/compiler/src/dmd/parse.d b/compiler/src/dmd/parse.d index c06c061a0cff..e5f9b665a097 100644 --- a/compiler/src/dmd/parse.d +++ b/compiler/src/dmd/parse.d @@ -150,9 +150,7 @@ class Parser(AST, Lexer = dmd.lexer.Lexer) : Lexer error("module edition %lld must be in the range %d ... %d", edition, Edition.min, Edition.max); edition = edition.min; } - // ModuleDeclaration edition can't lower -edition flag's edition - if (edition > mod.edition) - mod.edition = cast(Edition)edition; + mod.edition = cast(Edition)edition; nextToken(); } diff --git a/compiler/test/fail_compilation/edition_switch.d b/compiler/test/fail_compilation/edition_switch.d index 5c45bccbc3cf..48695f620bad 100644 --- a/compiler/test/fail_compilation/edition_switch.d +++ b/compiler/test/fail_compilation/edition_switch.d @@ -2,12 +2,9 @@ REQUIRED_ARGS: -edition=2024 TEST_OUTPUT: --- -fail_compilation/edition_switch.d(13): Error: usage of identifer `body` as a keyword is obsolete. Use `do` instead. +fail_compilation/edition_switch.d(10): Error: usage of identifer `body` as a keyword is obsolete. Use `do` instead. --- */ -// test -edition can override a lower module declaration -module m 2023; - void test() in { } body { }