Skip to content
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

Correctly change type when adding adjustments on top of NeverToAny #123571

Merged
merged 4 commits into from
Apr 19, 2024
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
33 changes: 23 additions & 10 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
Entry::Occupied(mut entry) => {
debug!(" - composing on top of {:?}", entry.get());
match (&entry.get()[..], &adj[..]) {
// Applying any adjustment on top of a NeverToAny
// is a valid NeverToAny adjustment, because it can't
// be reached.
(&[Adjustment { kind: Adjust::NeverToAny, .. }], _) => return,
match (&mut entry.get_mut()[..], &adj[..]) {
(
&[
[Adjustment { kind: Adjust::NeverToAny, target }],
&[.., Adjustment { target: new_target, .. }],
) => {
// NeverToAny coercion can target any type, so instead of adding a new
// adjustment on top we can change the target.
//
// This is required for things like `a == a` (where `a: !`) to produce
// valid MIR -- we need borrow adjustment from things like `==` to change
// the type to `&!` (or `&()` depending on the fallback). This might be
// relevant even in unreachable code.
*target = new_target;
}

(
&mut [
Adjustment { kind: Adjust::Deref(_), .. },
Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), .. },
],
Expand All @@ -294,11 +304,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.., // Any following adjustments are allowed.
],
) => {
// A reborrow has no effect before a dereference.
// A reborrow has no effect before a dereference, so we can safely replace adjustments.
*entry.get_mut() = adj;
}
// FIXME: currently we never try to compose autoderefs
// and ReifyFnPointer/UnsafeFnPointer, but we could.

_ => {
// FIXME: currently we never try to compose autoderefs
// and ReifyFnPointer/UnsafeFnPointer, but we could.
self.dcx().span_delayed_bug(
expr.span,
format!(
Expand All @@ -308,9 +320,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
adj
),
);

*entry.get_mut() = adj;
}
}
*entry.get_mut() = adj;
}
}

Expand Down
13 changes: 0 additions & 13 deletions tests/crashes/120600-2.rs

This file was deleted.

11 changes: 0 additions & 11 deletions tests/crashes/120600.rs

This file was deleted.

53 changes: 53 additions & 0 deletions tests/mir-opt/building/eq_never_type._f.built.after.mir
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// MIR for `_f` after built

fn _f(_1: !, _2: !) -> () {
debug a => _1;
debug b => _2;
let mut _0: ();
let mut _3: !;
let _4: bool;
let mut _5: &();
let mut _6: !;
let mut _7: &();
let _8: ();
let mut _9: !;

bb0: {
StorageLive(_4);
StorageLive(_5);
StorageLive(_6);
_6 = _1;
unreachable;
}

bb1: {
StorageDead(_6);
StorageLive(_7);
StorageLive(_8);
StorageLive(_9);
_9 = _2;
unreachable;
}

bb2: {
_7 = &_8;
StorageDead(_9);
_4 = <() as PartialEq>::eq(move _5, move _7) -> [return: bb3, unwind: bb5];
}

bb3: {
StorageDead(_7);
StorageDead(_5);
StorageDead(_8);
StorageDead(_4);
unreachable;
}

bb4: {
return;
}

bb5 (cleanup): {
resume;
}
}
13 changes: 13 additions & 0 deletions tests/mir-opt/building/eq_never_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// skip-filecheck
Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to use // CHECK, but could make it work, because they seem to be run on the optimized MIR, at which point everything is dead-code-eliminated.

Copy link
Contributor

Choose a reason for hiding this comment

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

You can add a -Zmir-opt-level=0, which will avoid opts.

Copy link
Member Author

Choose a reason for hiding this comment

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

The call is deleted even with -Zmir-opt-level=0 :(

#![feature(never_type)]
#![allow(unreachable_code)]

// EMIT_MIR eq_never_type._f.built.after.mir
fn _f(a: !, b: !) {
// Both arguments must be references (i.e. == should auto-borrow/coerce-to-ref both arguments)
// (this previously was buggy due to `NeverToAny` coercion incorrectly throwing out other
// coercions)
a == b;
}

fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/never_type/eq-never-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ check-pass
//
// issue: rust-lang/rust#120600

#![allow(internal_features)]
#![feature(never_type, rustc_attrs)]
#![rustc_never_type_options(fallback = "never")]

fn ice(a: !) {
a == a;
}

fn main() {}
Loading