rust/src/test/codegen
bors c3681d62ee Auto merge of #68122 - Centril:stabilize-transparent-enums, r=petrochenkov
Stabilize `#[repr(transparent)]` on `enum`s in Rust 1.42.0

# Stabilization report

The following is the stabilization report for `#![feature(transparent_enums)]`.

Tracking issue: https://github.com/rust-lang/rust/issues/60405
[Version target](https://forge.rust-lang.org/#current-release-versions): 1.42 (2020-01-30 => beta, 2020-03-12 => stable).

## User guide

A `struct` with only a single non-ZST field (let's call it `foo`) can be marked as `#[repr(transparent)]`. Such a `struct` has the same layout and ABI as `foo`. Here, we also extend this ability to `enum`s with only one variant, subject to the same restrictions as for the equivalent `struct`. That is, you can now write:

```rust
#[repr(transparent)]
enum Foo { Bar(u8) }
```

which, in terms of layout and ABI, is equivalent to:

```rust
#[repr(transparent)]
struct Foo(u8);
```

## Motivation

This is not a major feature that will unlock new and important use-cases. The utility of `repr(transparent)` `enum`s is indeed limited. However, there is still some value in it:

1. It provides conceptual simplification of the language in terms of treating univariant `enum`s and `struct`s the same, as both are product types. Indeed, languages like Haskell only have `data` as the only way to construct user-defined ADTs in the language.

2. In rare occasions, it might be that the user started out with a univariant `enum` for whatever reason (e.g. they thought they might extend it later). Now they want to make this `enum` `transparent` without breaking users by turning it into a `struct`. By lifting the restriction here, now they can.

## Technical specification

The reference specifies [`repr(transparent)` on a `struct`](https://doc.rust-lang.org/nightly/reference/type-layout.html#the-transparent-representation) as:

> ### The transparent Representation
>
>  The `transparent` representation can only be used on `struct`s that have:
>  - a single field with non-zero size, and
>  - any number of fields with size 0 and alignment 1 (e.g. `PhantomData<T>`).
>
> Structs with this representation have the same layout and ABI as the single non-zero sized field.
>
> This is different than the `C` representation because a struct with the `C` representation will always have the ABI of a `C` `struct` while, for example, a struct with the `transparent` representation with a primitive field will have the ABI of the primitive field.
>
> Because this representation delegates type layout to another type, it cannot be used with any other representation.

Here, we amend this to include univariant `enum`s as well with the same static restrictions and the same effects on dynamic semantics.

## Tests

All the relevant tests are adjusted in the PR diff but are recounted here:

- `src/test/ui/repr/repr-transparent.rs` checks that `repr(transparent)` on an `enum` must be univariant, rather than having zero or more than one variant. Restrictions on the fields inside the only variants, like for those on `struct`s, are also checked here.

- A number of codegen tests are provided as well:
    - `src/test/codegen/repr-transparent.rs` (the canonical test)
    - `src/test/codegen/repr-transparent-aggregates-1.rs`
    - `src/test/codegen/repr-transparent-aggregates-2.rs`
    - `src/test/codegen/repr-transparent-aggregates-3.rs`

- `src/test/ui/lint/lint-ctypes-enum.rs` tests the interactions with the `improper_ctypes` lint.

## History

- 2019-04-30, RFC https://github.com/rust-lang/rfcs/pull/2645
  Author: @mjbshaw
  Reviewers: The Language Team

  This is the RFC that proposes allowing `#[repr(transparent)]` on `enum`s and `union`.

- 2019-06-11, PR https://github.com/rust-lang/rust/pull/60463
  Author: @mjbshaw
  Reviewers: @varkor and @rkruppe

  The PR implements the RFC aforementioned in full.

- 2019, PR https://github.com/rust-lang/rust/pull/67323
  Author: @Centril
  Reviewers: @davidtwco

  The PR reorganizes the static checks taking advantage of the fact that `struct`s and `union`s are internally represented as ADTs with a single variant.

- This PR stabilizes `transparent_enums`.

## Related / possible future work

The remaining work here is to figure out the semantics of `#[repr(transparent)]` on `union`s and stabilize those. This work continues to be tracked in https://github.com/rust-lang/rust/issues/60405.
2020-01-27 00:05:57 +00:00
..
auxiliary Remove licenses 2018-12-25 21:08:33 -07:00
dllimports Added ignore-sgx for appropriate tests 2019-05-16 14:29:12 -07:00
intrinsics Account for pointer type suffix in prefetch test 2020-01-07 21:28:22 +01:00
non-terminate Gate llvm.sideeffect under -Z insert-sideeffect 2019-09-28 07:13:53 +08:00
remap_path_prefix Remove unnecessary ignore-tidy-linelength 2019-04-23 11:42:14 +01:00
simd-intrinsic Update the minimum external LLVM to 7 2019-12-02 11:36:21 -08:00
abi-efiapi.rs Do not require extra LLVM backends for x.py test to pass 2019-11-04 16:54:34 +03:00
abi-main-signature-16bit-c-int.rs Remove licenses 2018-12-25 21:08:33 -07:00
abi-main-signature-32bit-c-int.rs Update codegen tests with unnamed arguments 2020-01-07 21:28:22 +01:00
abi-sysv64.rs Remove licenses 2018-12-25 21:08:33 -07:00
abi-x86-interrupt.rs Remove licenses 2018-12-25 21:08:33 -07:00
abi-x86_64_sysv.rs Remove licenses 2018-12-25 21:08:33 -07:00
adjustments.rs codegen: use "_N" (like for other locals) instead of "argN", for argument names. 2019-09-13 19:25:05 +03:00
align-enum.rs Update the minimum external LLVM to 7 2019-12-02 11:36:21 -08:00
align-struct.rs Update the minimum external LLVM to 7 2019-12-02 11:36:21 -08:00
alloc-optimisation.rs Gate llvm.sideeffect under -Z insert-sideeffect 2019-09-28 07:13:53 +08:00
bool-cmp.rs Update bool-cmp.rs codegen 2020-01-07 21:28:22 +01:00
box-maybe-uninit.rs stabilize core parts of MaybeUninit and deprecate mem::uninitialized in the future 2019-05-20 10:44:02 +02:00
c-variadic-copy.rs Expose VaListImpl as the Rust equivalent of __va_list_tag and implement Clone for it. 2019-06-17 16:04:49 -07:00
c-variadic-opt.rs Expose VaListImpl as the Rust equivalent of __va_list_tag and implement Clone for it. 2019-06-17 16:04:49 -07:00
c-variadic.rs Re-enable Emscripten's exception handling support 2019-10-25 15:16:36 -07:00
call-metadata.rs Remove licenses 2018-12-25 21:08:33 -07:00
coercions.rs Remove licenses 2018-12-25 21:08:33 -07:00
consts.rs Make codegen tests wordsize independent 2020-01-11 14:40:07 +01:00
dealloc-no-unwind.rs Gate llvm.sideeffect under -Z insert-sideeffect 2019-09-28 07:13:53 +08:00
drop.rs fix real_drop_in_place in comments 2020-01-19 10:11:16 -06:00
enum-bounds-check.rs Remove licenses 2018-12-25 21:08:33 -07:00
enum-debug-clike.rs Correct minimum system LLVM version in tests 2019-03-22 21:28:25 -05:00
enum-debug-niche-2.rs Revert "Remove #![feature(never_type)] from tests." 2019-12-14 09:01:04 -05:00
enum-debug-niche.rs Remove unnecessary ignore-tidy-linelength 2019-04-23 11:42:14 +01:00
enum-debug-tagged.rs Remove unnecessary ignore-tidy-linelength 2019-04-23 11:42:14 +01:00
export-no-mangle.rs Remove licenses 2018-12-25 21:08:33 -07:00
external-no-mangle-fns.rs test: support both (legacy and v0) choices of mangling. 2019-05-31 18:24:53 +03:00
external-no-mangle-statics.rs Upgrade Emscripten targets to use upstream LLVM backend 2019-10-16 17:06:48 -07:00
fastcall-inreg.rs codegen: use "_N" (like for other locals) instead of "argN", for argument names. 2019-09-13 19:25:05 +03:00
fatptr.rs Remove licenses 2018-12-25 21:08:33 -07:00
ffi-returns-twice.rs Fix attribute check 2019-02-23 15:48:40 +01:00
float_math.rs Remove licenses 2018-12-25 21:08:33 -07:00
fn-impl-trait-self.rs Add codegen test 2019-03-31 20:09:30 -04:00
foo.s Add global_asm tests 2017-04-12 19:12:50 -05:00
force-frame-pointers.rs Use function attribute "frame-pointer" instead of "no-frame-pointer-elim" 2019-12-30 23:16:02 -08:00
function-arguments.rs Update codegen tests with unnamed arguments 2020-01-07 21:28:22 +01:00
gdb_debug_script_load.rs Disable gdb pretty printer global section on wasm targets 2019-12-04 20:15:21 -08:00
generic-debug.rs Remove unnecessary ignore-tidy-linelength 2019-04-23 11:42:14 +01:00
global_asm.rs Remove licenses 2018-12-25 21:08:33 -07:00
global_asm_include.rs Remove licenses 2018-12-25 21:08:33 -07:00
global_asm_x2.rs Remove licenses 2018-12-25 21:08:33 -07:00
i686-macosx-deployment-target.rs choose a more specific LLVM target on OS X when necessary 2019-05-07 11:09:39 -04:00
i686-no-macosx-deployment-target.rs default to $ARCH-apple-macosx10.7.0 LLVM triple for darwin targets 2019-05-13 17:04:59 -04:00
inline-always-works-always.rs Support revisions for codegen tests 2019-01-24 20:13:51 +02:00
instrument-mcount.rs Use function attribute "frame-pointer" instead of "no-frame-pointer-elim" 2019-12-30 23:16:02 -08:00
integer-cmp.rs Small improvement for Ord implementation of integers 2019-08-29 03:52:18 +00:00
internalize-closures.rs test: support both (legacy and v0) choices of mangling. 2019-05-31 18:24:53 +03:00
intrinsic-no-unnamed-attr.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-13018.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-15953.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-32031.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-32364.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-34947-pow-i32.rs Gate llvm.sideeffect under -Z insert-sideeffect 2019-09-28 07:13:53 +08:00
issue-37945.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-44056-macos-tls-align.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-45222.rs Gate llvm.sideeffect under -Z insert-sideeffect 2019-09-28 07:13:53 +08:00
issue-45466.rs ignore some codegen tests in debug mode 2019-07-15 16:56:43 +02:00
issue-47278.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-47442.rs Remove licenses 2018-12-25 21:08:33 -07:00
issue-56267-2.rs rustc_codegen_llvm: don't overalign loads of pair operands. 2018-11-29 00:37:38 +02:00
issue-56267.rs Fix alignment of stores to scalar pair 2018-11-28 00:31:03 +01:00
issue-56927.rs Fix alignment for array indexing 2018-12-21 23:51:55 +01:00
issue-58881.rs Fix LLVM IR generated for C-variadic arguments 2019-03-31 17:37:37 +00:00
iter-fold-closure-no-dupes.rs Add codegen tests for the genericity of fold closures 2019-08-12 15:03:44 -07:00
iter-fold-closure-no-iterator.rs Add codegen tests for the genericity of fold closures 2019-08-12 15:03:44 -07:00
lifetime_start_end.rs Remove licenses 2018-12-25 21:08:33 -07:00
link-dead-code.rs test: support both (legacy and v0) choices of mangling. 2019-05-31 18:24:53 +03:00
link_section.rs Upgrade Emscripten targets to use upstream LLVM backend 2019-10-16 17:06:48 -07:00
loads.rs Remove licenses 2018-12-25 21:08:33 -07:00
local-generics-in-exe-internalized.rs test: support both (legacy and v0) choices of mangling. 2019-05-31 18:24:53 +03:00
lto-removes-invokes.rs Remove licenses 2018-12-25 21:08:33 -07:00
mainsubprogram.rs Adjust codegen tests for DISPFlagMainSubprogram 2019-07-09 21:55:29 +02:00
mainsubprogramstart.rs Adjust codegen tests for DISPFlagMainSubprogram 2019-07-09 21:55:29 +02:00
match-optimizes-away.rs Remove licenses 2018-12-25 21:08:33 -07:00
match.rs Introduce MIR optimizations for simplifying x? on Results. 2019-11-21 20:05:16 +01:00
mir_zst_stores.rs Remove licenses 2018-12-25 21:08:33 -07:00
naked-functions.rs Update codegen tests with unnamed arguments 2020-01-07 21:28:22 +01:00
no-assumes-on-casts.rs Remove licenses 2018-12-25 21:08:33 -07:00
no-dllimport-w-cross-lang-lto.rs Stabilize linker-plugin based LTO. 2019-02-12 15:10:29 +01:00
no-output-asm-is-volatile.rs Upgrade Emscripten targets to use upstream LLVM backend 2019-10-16 17:06:48 -07:00
no-plt.rs Remove licenses 2018-12-25 21:08:33 -07:00
noreturn-uninhabited.rs Remove unnecessary ignore-tidy-linelength 2019-04-23 11:42:14 +01:00
noreturnflag.rs Remove unnecessary ignore-tidy-linelength 2019-04-23 11:42:14 +01:00
nounwind.rs Remove double trailing newlines 2019-04-22 16:57:01 +01:00
optimize-attr-1.rs [mir-opt] Turn on the ConstProp pass by default 2019-11-11 20:57:26 -05:00
packed.rs Update the minimum external LLVM to 7 2019-12-02 11:36:21 -08:00
panic-abort-windows.rs Added ignore-sgx for appropriate tests 2019-05-16 14:29:12 -07:00
personality_lifetimes.rs Re-enable Emscripten's exception handling support 2019-10-25 15:16:36 -07:00
pgo-instrumentation.rs Relax checks in pgo-instrumentation codegen test 2019-07-15 14:01:26 +02:00
README.md add link to FileCheck docs 2019-08-26 20:40:30 +02:00
refs.rs codegen: use "_N" (like for other locals) instead of "argN", for argument names. 2019-09-13 19:25:05 +03:00
repeat-trusted-len.rs Handle extra attributes in repeat-trusted-len.rs test 2020-01-07 21:28:22 +01:00
repr-transparent-aggregates-1.rs stabilize transparent_enums 2020-01-20 11:18:05 +01:00
repr-transparent-aggregates-2.rs stabilize transparent_enums 2020-01-20 11:18:05 +01:00
repr-transparent-aggregates-3.rs stabilize transparent_enums 2020-01-20 11:18:05 +01:00
repr-transparent-sysv64.rs Update codegen tests with unnamed arguments 2020-01-07 21:28:22 +01:00
repr-transparent.rs stabilize transparent_enums 2020-01-20 11:18:05 +01:00
sanitizer-memory-track-orgins.rs Mark __msan_track_origins as an exported symbol for LTO 2020-01-20 23:13:38 +01:00
sanitizer-recover.rs Mark __msan_keep_going as an exported symbol for LTO 2020-01-21 00:00:00 +00:00
scalar-pair-bool.rs codegen: use "_N" (like for other locals) instead of "argN", for argument names. 2019-09-13 19:25:05 +03:00
set-discriminant-invalid.rs use abort instead of unreachable 2019-12-06 00:10:01 +01:00
slice-init.rs Remove licenses 2018-12-25 21:08:33 -07:00
slice-iter-len-eq-zero.rs Help LLVM better optimize slice::Iter(Mut)::len 2019-06-15 21:15:25 -07:00
slice-position-bounds-check.rs Remove licenses 2018-12-25 21:08:33 -07:00
sparc-struct-abi.rs Remove licenses 2018-12-25 21:08:33 -07:00
stack-probes.rs Remove licenses 2018-12-25 21:08:33 -07:00
stores.rs Update the minimum external LLVM to 7 2019-12-02 11:36:21 -08:00
swap-small-types.rs ignore some codegen tests in debug mode 2019-07-15 16:56:43 +02:00
target-cpu-on-functions.rs test: support both (legacy and v0) choices of mangling. 2019-05-31 18:24:53 +03:00
target-feature-on-functions.rs Remove licenses 2018-12-25 21:08:33 -07:00
try_identity.rs Introduce MIR optimizations for simplifying x? on Results. 2019-11-21 20:05:16 +01:00
unchecked-float-casts.rs Remove licenses 2018-12-25 21:08:33 -07:00
union-abi.rs Update codegen tests with unnamed arguments 2020-01-07 21:28:22 +01:00
unwind-extern-exports.rs Re-enable Emscripten's exception handling support 2019-10-25 15:16:36 -07:00
unwind-extern-imports.rs Re-enable Emscripten's exception handling support 2019-10-25 15:16:36 -07:00
var-names.rs rustc_codegen_llvm: give names to non-alloca variable values. 2019-09-06 16:57:20 +03:00
vec-clear.rs Gate llvm.sideeffect under -Z insert-sideeffect 2019-09-28 07:13:53 +08:00
vec-iter-collect-len.rs Gate llvm.sideeffect under -Z insert-sideeffect 2019-09-28 07:13:53 +08:00
vec-optimizes-away.rs Gate llvm.sideeffect under -Z insert-sideeffect 2019-09-28 07:13:53 +08:00
vtabletype.rs Remove unnecessary ignore-tidy-linelength 2019-04-23 11:42:14 +01:00
x86_64-macosx-deployment-target.rs choose a more specific LLVM target on OS X when necessary 2019-05-07 11:09:39 -04:00
x86_64-no-macosx-deployment-target.rs default to $ARCH-apple-macosx10.7.0 LLVM triple for darwin targets 2019-05-13 17:04:59 -04:00
x86_mmx.rs Remove licenses 2018-12-25 21:08:33 -07:00
zip.rs Remove licenses 2018-12-25 21:08:33 -07:00

The files here use the LLVM FileCheck framework, documented at https://llvm.org/docs/CommandGuide/FileCheck.html.