Skip to content

Commit 0f323d7

Browse files
committed
[spec/type] Improve Implicit Conversions section
Fix backwards `noreturn` implicit conversion item (oops). Document qualified array/pointer element type needing qualified void element type. Add example. Add link to qualifier conversions.
1 parent 5807d3a commit 0f323d7

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

spec/arrays.dd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,8 @@ $(H3 $(LNAME2 void_arrays, Void Arrays))
14201420
This is because the void array may hold pointers.
14211421
A void array cannot be $(RELATIVE_LINK2 array-setting, filled).)
14221422

1423-
$(P Arrays of any type can be implicitly converted to a (tail qualified) void array - the
1423+
$(P Arrays of any type can be $(DDSUBLINK spec/type, implicit-conversions,
1424+
implicitly converted) to a (tail qualified) void array - the
14241425
compiler inserts the appropriate calculations so that the $(D .length) of
14251426
the resulting array's size is in bytes rather than number of elements. Void
14261427
arrays cannot be converted back to the original type without using an

spec/type.dd

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ $(H2 $(LNAME2 type-conversions, Type Conversions))
229229

230230
$(H3 $(LEGACY_LNAME2 Pointer Conversions, pointer-conversions, Pointer Conversions))
231231

232-
$(P $(RELATIVE_LINK2 pointers, Pointers) implicitly convert to `void*`.)
232+
$(P Any $(RELATIVE_LINK2 pointers, pointer) implicitly converts to a `void` pointer -
233+
see below.)
233234

234235
$(P Casting between pointers and non-pointers is allowed. Some pointer casts
235236
are disallowed in $(DDLINK spec/memory-safe-d, Memory-Safe-D-Spec, `@safe` code).)
@@ -244,17 +245,43 @@ $(H3 $(LEGACY_LNAME2 Implicit Conversions, implicit-conversions, Implicit Conver
244245
types as required. The rules for integers are detailed in the next sections.
245246
)
246247

247-
$(P An enum can be $(DDSUBLINK spec/enum, named_enums, implicitly converted) to its base
248-
type, but going the other way requires an explicit
249-
conversion.)
250-
251248
$(UL
252-
$(LI All types implicitly convert to $(RELATIVE_LINK2 noreturn, `noreturn`).)
253-
$(LI Static and dynamic arrays implicitly convert to $(DDSUBLINK spec/arrays, void_arrays, `void` arrays).)
249+
$(LI An enum can be $(DDSUBLINK spec/enum, named_enums, implicitly converted) to its base
250+
type (but going the other way requires an explicit conversion).)
251+
$(LI $(RELATIVE_LINK2 noreturn, `noreturn`) implicitly converts to any type.)
252+
$(LI Static and dynamic arrays implicitly convert to $(DDSUBLINK spec/arrays, void_arrays, `void` arrays).
253+
The `void` element type for the array may need a
254+
$(DDLINK spec/const3, Type Qualifiers, type qualifier), depending on the source
255+
element type:
256+
)
257+
* An array with non-mutable elements will implicitly convert to `const(void)[]`, but not `void[]`.
258+
* An array with `shared` elements will implicitly convert to `shared(void)[]`, but not `void[]`.
259+
$(LI Any pointer implicitly converts to a void pointer. As for void arrays, the
260+
`void` element type may need a type qualifier.)
254261
$(LI $(DDSUBLINK spec/function, function-pointers-delegates, Function pointers and delegates)
255262
can convert to covariant types.)
256263
)
257264

265+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
266+
---
267+
void main()
268+
{
269+
noreturn n;
270+
int i = n;
271+
void* p = &i;
272+
273+
const int[] a;
274+
const(void)[] cv = a;
275+
//void[] va = a; // error
276+
}
277+
278+
void f(int x) pure;
279+
void function(int) fp = &f; // pure is covariant with non-pure
280+
---
281+
)
282+
$(P See also $(DDSUBLINK spec/const3, implicit_qualifier_conversions,
283+
Implicit Qualifier Conversions).)
284+
258285
$(H4 $(LNAME2 class-conversions, Class Conversions))
259286

260287
$(P A derived class can be implicitly converted to its base class, but going

0 commit comments

Comments
 (0)