Skip to content

Commit 22574ae

Browse files
authored
Merge pull request #2001 from wilzbach/1040-more
Revive #1040 - spec/{enum,expression}.d + dcompiler.d merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
2 parents c49b350 + a9cd6ad commit 22574ae

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

dcompiler.dd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ dmd -cov -unittest myprog.d
885885
$(D unittest) $(LINK2 spec/version.html#PredefinedVersions, version identifier)
886886
)
887887
$(SWITCH $(SWNAME -v),
888-
verbose
888+
enable verbose output for each compiler pass
889889
)
890890
$(SWITCH $(SWNAME -vcolumns),
891891
print character (column) numbers in diagnostics

spec/enum.dd

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,27 @@ enum size = __traits(classInstanceSize, Foo); // evaluated at compile-time
260260
$(P Using manifest constants is an idiomatic D method
261261
to force compile-time evaluation of an expression.)
262262

263+
---
264+
template Foo(T)
265+
{
266+
// Not bad, but the 'size' variable will be located in the executable.
267+
const size_t size = T.sizeof; // evaluated at compile-time
268+
269+
// ... use of 'size' at compile time ...
270+
}
271+
272+
template Bar(T)
273+
{
274+
// Better, the manifest constant has no runtime location in the executable.
275+
enum size_t size = T.sizeof; // evaluated at compile-time
276+
277+
// ... use of 'size' at compile time ...
278+
279+
// If you take the address of Foo!T.size, it would also go into exe file.
280+
auto p = &Foo!T.size;
281+
}
282+
---
283+
263284

264285
$(SPEC_SUBNAV_PREV_NEXT interface, Interfaces, const3, Type Qualifiers)
265286
)

spec/expression.dd

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,16 +1654,6 @@ $(GNAME AssertExpression):
16541654
Contract Programming) support in D.
16551655
)
16561656

1657-
$(P The expression $(D assert(0)) is a special case; it
1658-
signifies that it is unreachable code.
1659-
Either $(D AssertError) is thrown at runtime if it is reachable,
1660-
or the execution is halted
1661-
(on the x86 processor, a $(D HLT) instruction can be used to halt
1662-
execution).
1663-
The optimization and code generation phases of compilation may
1664-
assume that it is unreachable code.
1665-
)
1666-
16671657
$(P The second $(I AssignExpression), if present, must be implicitly
16681658
convertible to type $(D const(char)[]).
16691659
It is evaluated if the
@@ -1680,9 +1670,20 @@ $(GNAME AssertExpression):
16801670
$(P When compiled and run, it will produce the message:)
16811671

16821672
$(CONSOLE
1683-
Error: AssertError Failure test.d(3) an error message
1673+
core.exception.AssertError@test.d(3) an error message
16841674
)
16851675

1676+
$(P The expression $(D assert(0)) is a special case; it
1677+
signifies that it is unreachable code.
1678+
Without optimizations, an $(D AssertError) is thrown at runtime.
1679+
In release mode ($(LINK2 $(ROOT_DIR)/dmd.html#switch-release, `-release`))
1680+
the execution is halted
1681+
(on the x86 processor, a $(D HLT) instruction can be used to halt
1682+
execution).
1683+
The optimization and code generation phases of compilation may
1684+
assume that it is unreachable code.
1685+
)
1686+
16861687
$(H3 $(LNAME2 mixin_expressions, Mixin Expressions))
16871688

16881689
$(GRAMMAR

0 commit comments

Comments
 (0)