Skip to content

Commit

Permalink
Unrolled build for rust-lang#134279
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#134279 - jieyouxu:return-adjustment-target, r=compiler-errors

(Re-)return adjustment target if adjust kind is never-to-any

This PR fixes rust-lang#134162 where we ICE'd on

```rs
fn main() {
    struct X;
    let _ = [X] == [panic!(); 2];
}
```

In rust-lang#121208 (comment), there was a change

```diff
- if let Some(adjustments) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
-     let reported = self.dcx().span_delayed_bug(
-         expr.span,
-         "expression with never type wound up being adjusted",
-     );
-     return if let [Adjustment { kind: Adjust::NeverToAny, target }] = &adjustments[..] {
-         target.to_owned()
-     } else {
-         Ty::new_error(self.tcx(), reported)
-     };
- }
+ if let Some(_) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
+     self.dcx()
+         .span_bug(expr.span, "expression with never type wound up being adjusted");
+ }
```

It turned out returning the adjustment target if the adjustment kind is `NeverToAny` is necessary, as otherwise we will go through a series of `delay_bug`s and eventually find that we constructed a `TyKind::Error` without having actually emitted an error.

This PR addresses that by re-returning the adjustment target if the adjustment kind is `NeverToAny`, partially reverting this change from rust-lang#121208.

This PR has two commits:

1. The first commit adds a regression test for rust-lang#134162, which will ICE (on stable 1.83.0, beta and nightly 2024-12-13).
2. The second commit is the partial revert, which will fix the ICE.

cc `@nnethercote` FYI as this is related to rust-lang#121208 changes. The changes from rust-lang#121208 exposed that we lacked test coverage for the code pattern reported in rust-lang#134162.
  • Loading branch information
rust-timer authored Dec 14, 2024
2 parents 85641f7 + d15315c commit 0105692
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
9 changes: 7 additions & 2 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if self.try_structurally_resolve_type(expr.span, ty).is_never()
&& self.expr_guaranteed_to_constitute_read_for_never(expr)
{
if let Some(_) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
if let Some(adjustments) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
let reported = self.dcx().span_delayed_bug(
expr.span,
"expression with never type wound up being adjusted",
);
return Ty::new_error(self.tcx(), reported);

return if let [Adjustment { kind: Adjust::NeverToAny, target }] = &adjustments[..] {
target.to_owned()
} else {
Ty::new_error(self.tcx(), reported)
};
}

let adj_ty = self.next_ty_var(expr.span);
Expand Down
8 changes: 0 additions & 8 deletions tests/crashes/134162.rs

This file was deleted.

11 changes: 11 additions & 0 deletions tests/ui/typeck/rhs-ty-hint-134162.e2018.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0369]: binary operation `==` cannot be applied to type `[X; 1]`
--> $DIR/rhs-ty-hint-134162.rs:16:17
|
LL | let _ = [X] == [panic!(); 2];
| --- ^^ ------------- [_; 2]
| |
| [X; 1]

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0369`.
11 changes: 11 additions & 0 deletions tests/ui/typeck/rhs-ty-hint-134162.e2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0369]: binary operation `==` cannot be applied to type `[X; 1]`
--> $DIR/rhs-ty-hint-134162.rs:16:17
|
LL | let _ = [X] == [panic!(); 2];
| --- ^^ ------------- [_; 2]
| |
| [X; 1]

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0369`.
11 changes: 11 additions & 0 deletions tests/ui/typeck/rhs-ty-hint-134162.e2024.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0369]: binary operation `==` cannot be applied to type `[X; 1]`
--> $DIR/rhs-ty-hint-134162.rs:16:17
|
LL | let _ = [X] == [panic!(); 2];
| --- ^^ ------------- [_; 2]
| |
| [X; 1]

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0369`.
18 changes: 18 additions & 0 deletions tests/ui/typeck/rhs-ty-hint-134162.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Regression test for <https://github.com/rust-lang/rust/issues/134162>.
//!
//! <https://github.com/rust-lang/rust/pull/110877> introduced RHS type hints for when a ty doesn't
//! support a bin op. In the suggestion path, there was a `delay_bug`.
//! <https://github.com/rust-lang/rust/pull/121208> converted this `delay_bug` to `bug`, which did
//! not trigger any test failures as we did not have test coverage for this particular case. This
//! manifested in an ICE as reported in <https://github.com/rust-lang/rust/issues/134162>.
//@ revisions: e2018 e2021 e2024
//@[e2018] edition: 2018
//@[e2021] edition: 2021
//@[e2024] edition: 2024

fn main() {
struct X;
let _ = [X] == [panic!(); 2];
//[e2018,e2021,e2024]~^ ERROR binary operation `==` cannot be applied to type `[X; 1]`
}

0 comments on commit 0105692

Please sign in to comment.