Skip to content

Add missing normalization for union fields types #106938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would ICE if the union has fields which aren't well formed, e.g. something like <T as NotImplemented>::Assoc. Please add a test for this and check whether it currently causes an ICE. If so you can replace this with try_normalize_erasing_regions. I gues it's fine for normalization to ignore region obligations as we should detect these region errors in WF check.

While this also ignore

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you mean:

pub trait Identity {
    type Identity;
}

/*impl<T> Identity for T {
    type Identity = Self;
}*/

pub type Foo = u8;

pub union Bar {
    a:  <Foo as Identity>::Identity,
    b: u8,
}

Then no problem. I'll add a ui test for it though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure when exactly normalize_erasing_regions ends up failing with an ICE, only know that it does. Maybe also from something like

trait Identity {
    type Identity;
}
trait NotImplemented {}

impl<T: NotImplemented> Identity for T {
    type Identity = Self;
}

type Foo = u8;

union Bar {
    a:  <Foo as Identity>::Identity,
    b: u8,
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working too, adding it as a test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, alright. If there's a way to ICE here we will just get a bug report, so that seems fine

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dis you mean #107872 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 yes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That didn't take long. 🤣

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Showed up as new in my daily ICE-screening ^^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!


if !allowed_union_field(field_ty, tcx, param_env) {
let (field_span, ty_span) = match tcx.hir().get_if_local(field.did) {
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/union/projection-as-union-type-error-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Test to ensure that there is no ICE when normalizing a projection
// which is invalid (from <https://github.com/rust-lang/rust/pull/106938>).

#![crate_type = "lib"]

trait Identity {
type Identity;
}
trait NotImplemented {}

impl<T: NotImplemented> Identity for T {
type Identity = Self;
}

type Foo = u8;

union Bar {
a: <Foo as Identity>::Identity, //~ ERROR
b: u8,
}
17 changes: 17 additions & 0 deletions tests/ui/union/projection-as-union-type-error-2.stderr
Original file line number Diff line number Diff line change
@@ -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: <Foo as Identity>::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<T: NotImplemented> Identity for T {
| -------------- ^^^^^^^^ ^
| |
| unsatisfied trait bound introduced here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
15 changes: 15 additions & 0 deletions tests/ui/union/projection-as-union-type-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Test to ensure that there is no ICE when normalizing a projection
// which is invalid (from <https://github.com/rust-lang/rust/pull/106938>).

#![crate_type = "lib"]

pub trait Identity {
type Identity;
}

pub type Foo = u8;

pub union Bar {
a: <Foo as Identity>::Identity, //~ ERROR
b: u8,
}
9 changes: 9 additions & 0 deletions tests/ui/union/projection-as-union-type-error.stderr
Original file line number Diff line number Diff line change
@@ -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: <Foo as Identity>::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`.
19 changes: 19 additions & 0 deletions tests/ui/union/projection-as-union-type.rs
Original file line number Diff line number Diff line change
@@ -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<T> Identity for T {
type Identity = Self;
}

pub type Foo = u8;

pub union Bar {
pub a: <Foo as Identity>::Identity,
pub b: u8,
}