Skip to content

Commit cbb7acd

Browse files
committed
add -preview=return switch for return ref scope disambiguation
1 parent 68b5957 commit cbb7acd

File tree

8 files changed

+23
-2
lines changed

8 files changed

+23
-2
lines changed

changelog/dmd.returnRefScope.dd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Enforce ordering of `return ref` and `return scope` with `-preview=return`
2+
3+
The `return` attribute is associated with the `ref` attribute or the `scope`
4+
attribute. In order to reduce the confusing permutations of the keywords `return`,
5+
`scope`, and `ref`, only the lexical combinations `return ref` and `return scope` are allowed
6+
when the -preview=return switch is applied.

compiler/src/dmd/cli.d

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,9 @@ dmd -cov -unittest myprog.d
775775
done for system and trusted functions, and assertion failures
776776
are undefined behaviour.`
777777
),
778+
Option("return",
779+
"enforce 'return ref' and 'return scope' syntax"
780+
),
778781
Option("revert=<name>",
779782
"revert language change identified by 'name'",
780783
`Revert language change identified by $(I id)`,

compiler/src/dmd/frontend.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6304,6 +6304,7 @@ struct CompileEnv final
63046304
_d_dynamicArray< const char > vendor;
63056305
_d_dynamicArray< const char > timestamp;
63066306
bool previewIn;
6307+
bool returnRefScope;
63076308
bool transitionIn;
63086309
bool ddocOutput;
63096310
bool masm;
@@ -6316,20 +6317,22 @@ struct CompileEnv final
63166317
vendor(),
63176318
timestamp(),
63186319
previewIn(),
6320+
returnRefScope(),
63196321
transitionIn(),
63206322
ddocOutput(),
63216323
masm(),
63226324
cCharLookupTable(),
63236325
dCharLookupTable()
63246326
{
63256327
}
6326-
CompileEnv(uint32_t versionNumber, _d_dynamicArray< const char > date = {}, _d_dynamicArray< const char > time = {}, _d_dynamicArray< const char > vendor = {}, _d_dynamicArray< const char > timestamp = {}, bool previewIn = false, bool transitionIn = false, bool ddocOutput = false, bool masm = false, IdentifierCharLookup cCharLookupTable = IdentifierCharLookup(), IdentifierCharLookup dCharLookupTable = IdentifierCharLookup()) :
6328+
CompileEnv(uint32_t versionNumber, _d_dynamicArray< const char > date = {}, _d_dynamicArray< const char > time = {}, _d_dynamicArray< const char > vendor = {}, _d_dynamicArray< const char > timestamp = {}, bool previewIn = false, bool returnRefScope = false, bool transitionIn = false, bool ddocOutput = false, bool masm = false, IdentifierCharLookup cCharLookupTable = IdentifierCharLookup(), IdentifierCharLookup dCharLookupTable = IdentifierCharLookup()) :
63276329
versionNumber(versionNumber),
63286330
date(date),
63296331
time(time),
63306332
vendor(vendor),
63316333
timestamp(timestamp),
63326334
previewIn(previewIn),
6335+
returnRefScope(returnRefScope),
63336336
transitionIn(transitionIn),
63346337
ddocOutput(ddocOutput),
63356338
masm(masm),
@@ -8453,6 +8456,7 @@ struct Param final
84538456
FeatureState safer;
84548457
FeatureState noSharedAccess;
84558458
bool previewIn;
8459+
bool returnRefScope;
84568460
bool inclusiveInContracts;
84578461
bool shortenedMethods;
84588462
bool fixImmutableConv;
@@ -8536,6 +8540,7 @@ struct Param final
85368540
useDIP1021(),
85378541
fixAliasThis(),
85388542
previewIn(),
8543+
returnRefScope(),
85398544
inclusiveInContracts(),
85408545
shortenedMethods(true),
85418546
fixImmutableConv(),
@@ -8584,7 +8589,7 @@ struct Param final
85848589
timeTraceFile()
85858590
{
85868591
}
8587-
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) :
8592+
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 returnRefScope = 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) :
85888593
obj(obj),
85898594
readStdin(readStdin),
85908595
multiobj(multiobj),
@@ -8622,6 +8627,7 @@ struct Param final
86228627
safer(safer),
86238628
noSharedAccess(noSharedAccess),
86248629
previewIn(previewIn),
8630+
returnRefScope(returnRefScope),
86258631
inclusiveInContracts(inclusiveInContracts),
86268632
shortenedMethods(shortenedMethods),
86278633
fixImmutableConv(fixImmutableConv),

compiler/src/dmd/globals.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ extern (C++) struct Param
205205
// https://github.com/WalterBright/documents/blob/38f0a846726b571f8108f6e63e5e217b91421c86/safer.md
206206
FeatureState noSharedAccess; // read/write access to shared memory objects
207207
bool previewIn; // `in` means `[ref] scope const`, accepts rvalues
208+
bool returnRefScope; // -preview=return switch, must use `return ref` or `return scope`
208209
bool inclusiveInContracts; // 'in' contracts of overridden methods must be a superset of parent contract
209210
bool shortenedMethods = true; // allow => in normal function declarations
210211
bool fixImmutableConv; // error on unsound immutable conversion - https://github.com/dlang/dmd/pull/14070

compiler/src/dmd/globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ struct Param
213213

214214
FeatureState noSharedAccess; // read/write access to shared memory objects
215215
d_bool previewIn; // `in` means `[ref] scope const`, accepts rvalues
216+
d_bool returnRefScope; // -preview=return switch, must use `return ref` or `return scope`
216217
d_bool inclusiveInContracts; // 'in' contracts of overridden methods must be a superset of parent contract
217218
d_bool shortenedMethods; // allow => in normal function declarations
218219
d_bool fixImmutableConv; // error on unsound immutable conversion - https://github.com/dlang/dmd/pull/14070

compiler/src/dmd/lexer.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct CompileEnv
4848
const(char)[] timestamp; /// __TIMESTAMP__
4949

5050
bool previewIn; /// `in` means `[ref] scope const`, accepts rvalues
51+
bool returnRefScope; /// -preview=return is on
5152
bool transitionIn; /// `-transition=in` is active, `in` parameters are listed
5253
bool ddocOutput; /// collect embedded documentation comments
5354
bool masm; /// use MASM inline asm syntax

compiler/src/dmd/main.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ private int tryMain(size_t argc, const(char)** argv, ref Param params)
188188
return EXIT_FAILURE;
189189

190190
global.compileEnv.previewIn = global.params.previewIn;
191+
global.compileEnv.returnRefScope = global.params.returnRefScope;
191192
global.compileEnv.transitionIn = global.params.v.vin;
192193
global.compileEnv.ddocOutput = global.params.ddoc.doOutput;
193194

compiler/src/dmd/mars.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,8 @@ bool parseCommandLine(const ref Strings arguments, const size_t argc, ref Param
15351535
}
15361536
else if (arg == "-release") // https://dlang.org/dmd.html#switch-release
15371537
params.release = true;
1538+
else if (arg == "-return") // https://dlang.org/dmd.html#switch-returnRefScope
1539+
params.returnRefScope = true;
15381540
else if (arg == "-betterC") // https://dlang.org/dmd.html#switch-betterC
15391541
{
15401542
params.betterC = true;

0 commit comments

Comments
 (0)