Skip to content

Commit fc2be54

Browse files
authored
Merge pull request #4265 from ntrel/impl-conv
[spec/type] Improve Implicit Conversions section
2 parents 08cfdd8 + 0f323d7 commit fc2be54

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
@@ -230,7 +230,8 @@ $(H2 $(LNAME2 type-conversions, Type Conversions))
230230

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

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

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

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

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

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

0 commit comments

Comments
 (0)