From 921efd2e67076dca999129f85659a5f1a6b21212 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 16 Jan 2023 15:18:14 +0100 Subject: [PATCH 1/3] Add missing normalization for union fields types --- compiler/rustc_hir_analysis/src/check/check.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 43795cfba3f..248ff1d4cb5 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -121,7 +121,7 @@ fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> b let param_env = tcx.param_env(item_def_id); for field in &def.non_enum_variant().fields { - let field_ty = field.ty(tcx, substs); + let field_ty = tcx.normalize_erasing_regions(param_env, field.ty(tcx, substs)); if !allowed_union_field(field_ty, tcx, param_env) { let (field_span, ty_span) = match tcx.hir().get_if_local(field.did) { From 4653bbfaee990326a95d3dc24e60cf6827aca567 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 16 Jan 2023 15:19:18 +0100 Subject: [PATCH 2/3] Add ui test for projection used as union field type --- tests/ui/union/projection-as-union-type.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/ui/union/projection-as-union-type.rs diff --git a/tests/ui/union/projection-as-union-type.rs b/tests/ui/union/projection-as-union-type.rs new file mode 100644 index 00000000000..143434c96f8 --- /dev/null +++ b/tests/ui/union/projection-as-union-type.rs @@ -0,0 +1,19 @@ +// Ensures that we can use projections as union field's type. +// check-pass + +#![crate_type = "lib"] + +pub trait Identity { + type Identity; +} + +impl Identity for T { + type Identity = Self; +} + +pub type Foo = u8; + +pub union Bar { + pub a: ::Identity, + pub b: u8, +} From 6c63b9497dfcd546199db03174ac11866bf3bfb8 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 16 Jan 2023 18:10:11 +0100 Subject: [PATCH 3/3] Add failing test for invalid projection as union field type --- .../union/projection-as-union-type-error-2.rs | 20 +++++++++++++++++++ .../projection-as-union-type-error-2.stderr | 17 ++++++++++++++++ .../union/projection-as-union-type-error.rs | 15 ++++++++++++++ .../projection-as-union-type-error.stderr | 9 +++++++++ 4 files changed, 61 insertions(+) create mode 100644 tests/ui/union/projection-as-union-type-error-2.rs create mode 100644 tests/ui/union/projection-as-union-type-error-2.stderr create mode 100644 tests/ui/union/projection-as-union-type-error.rs create mode 100644 tests/ui/union/projection-as-union-type-error.stderr diff --git a/tests/ui/union/projection-as-union-type-error-2.rs b/tests/ui/union/projection-as-union-type-error-2.rs new file mode 100644 index 00000000000..b88167b3b54 --- /dev/null +++ b/tests/ui/union/projection-as-union-type-error-2.rs @@ -0,0 +1,20 @@ +// Test to ensure that there is no ICE when normalizing a projection +// which is invalid (from ). + +#![crate_type = "lib"] + +trait Identity { + type Identity; +} +trait NotImplemented {} + +impl Identity for T { + type Identity = Self; +} + +type Foo = u8; + +union Bar { + a: ::Identity, //~ ERROR + b: u8, +} diff --git a/tests/ui/union/projection-as-union-type-error-2.stderr b/tests/ui/union/projection-as-union-type-error-2.stderr new file mode 100644 index 00000000000..bab226f271d --- /dev/null +++ b/tests/ui/union/projection-as-union-type-error-2.stderr @@ -0,0 +1,17 @@ +error[E0277]: the trait bound `u8: NotImplemented` is not satisfied + --> $DIR/projection-as-union-type-error-2.rs:18:8 + | +LL | a: ::Identity, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `NotImplemented` is not implemented for `u8` + | +note: required for `u8` to implement `Identity` + --> $DIR/projection-as-union-type-error-2.rs:11:25 + | +LL | impl Identity for T { + | -------------- ^^^^^^^^ ^ + | | + | unsatisfied trait bound introduced here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/union/projection-as-union-type-error.rs b/tests/ui/union/projection-as-union-type-error.rs new file mode 100644 index 00000000000..17091c35fb2 --- /dev/null +++ b/tests/ui/union/projection-as-union-type-error.rs @@ -0,0 +1,15 @@ +// Test to ensure that there is no ICE when normalizing a projection +// which is invalid (from ). + +#![crate_type = "lib"] + +pub trait Identity { + type Identity; +} + +pub type Foo = u8; + +pub union Bar { + a: ::Identity, //~ ERROR + b: u8, +} diff --git a/tests/ui/union/projection-as-union-type-error.stderr b/tests/ui/union/projection-as-union-type-error.stderr new file mode 100644 index 00000000000..e4fbe9603ad --- /dev/null +++ b/tests/ui/union/projection-as-union-type-error.stderr @@ -0,0 +1,9 @@ +error[E0277]: the trait bound `u8: Identity` is not satisfied + --> $DIR/projection-as-union-type-error.rs:13:9 + | +LL | a: ::Identity, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Identity` is not implemented for `u8` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0277`.