From 109321ac47d0ffff5d03aad60f3df0c56e412db4 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 23 Feb 2024 09:37:33 +1100 Subject: [PATCH] Revert some `span_bug`s to `span_delayed_bug`. Fixes #121445. Fixes #121457. --- .../src/check/compare_impl_item/refine.rs | 12 +++----- compiler/rustc_hir_typeck/src/expr.rs | 13 ++++++-- .../in-trait/span-bug-issue-121457.rs | 18 +++++++++++ .../in-trait/span-bug-issue-121457.stderr | 30 +++++++++++++++++++ tests/ui/never_type/span-bug-issue-121445.rs | 15 ++++++++++ .../never_type/span-bug-issue-121445.stderr | 22 ++++++++++++++ 6 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 tests/ui/impl-trait/in-trait/span-bug-issue-121457.rs create mode 100644 tests/ui/impl-trait/in-trait/span-bug-issue-121457.stderr create mode 100644 tests/ui/never_type/span-bug-issue-121445.rs create mode 100644 tests/ui/never_type/span-bug-issue-121445.stderr diff --git a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs index 7bdbab4325c..b5e69b8e376 100644 --- a/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs +++ b/compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs @@ -154,10 +154,8 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>( trait_m_sig.inputs_and_output, )); if !ocx.select_all_or_error().is_empty() { - // This code path is not reached in any tests, but may be reachable. If - // this is triggered, it should be converted to `delayed_bug` and the - // triggering case turned into a test. - tcx.dcx().bug("encountered errors when checking RPITIT refinement (selection)"); + tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (selection)"); + return; } let outlives_env = OutlivesEnvironment::with_bounds( param_env, @@ -165,10 +163,8 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>( ); let errors = infcx.resolve_regions(&outlives_env); if !errors.is_empty() { - // This code path is not reached in any tests, but may be reachable. If - // this is triggered, it should be converted to `delayed_bug` and the - // triggering case turned into a test. - tcx.dcx().bug("encountered errors when checking RPITIT refinement (regions)"); + tcx.dcx().delayed_bug("encountered errors when checking RPITIT refinement (regions)"); + return; } // Resolve any lifetime variables that may have been introduced during normalization. let Ok((trait_bounds, impl_bounds)) = infcx.fully_resolve((trait_bounds, impl_bounds)) else { diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 89cc46dc5ab..6b52dd4392e 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -76,9 +76,16 @@ pub fn check_expr_has_type_or_error( // While we don't allow *arbitrary* coercions here, we *do* allow // coercions from ! to `expected`. if ty.is_never() { - 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"); + 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) + }; } let adj_ty = self.next_ty_var(TypeVariableOrigin { diff --git a/tests/ui/impl-trait/in-trait/span-bug-issue-121457.rs b/tests/ui/impl-trait/in-trait/span-bug-issue-121457.rs new file mode 100644 index 00000000000..10167ee9352 --- /dev/null +++ b/tests/ui/impl-trait/in-trait/span-bug-issue-121457.rs @@ -0,0 +1,18 @@ +pub trait Iterable { + type Item<'a> + where + Self: 'a; + + fn iter(&self) -> impl Iterator; +} + +impl<'a, I: 'a + Iterable> Iterable for &'a I { + type Item = u32; + //~^ ERROR lifetime parameters or bounds on type `Item` do not match the trait declaration + + fn iter(&self) -> impl for<'missing> Iterator> {} + //~^ ERROR binding for associated type `Item` references lifetime `'missing` + //~| ERROR `()` is not an iterator +} + +fn main() {} diff --git a/tests/ui/impl-trait/in-trait/span-bug-issue-121457.stderr b/tests/ui/impl-trait/in-trait/span-bug-issue-121457.stderr new file mode 100644 index 00000000000..96c3644f893 --- /dev/null +++ b/tests/ui/impl-trait/in-trait/span-bug-issue-121457.stderr @@ -0,0 +1,30 @@ +error[E0582]: binding for associated type `Item` references lifetime `'missing`, which does not appear in the trait input types + --> $DIR/span-bug-issue-121457.rs:13:51 + | +LL | fn iter(&self) -> impl for<'missing> Iterator> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0195]: lifetime parameters or bounds on type `Item` do not match the trait declaration + --> $DIR/span-bug-issue-121457.rs:10:14 + | +LL | type Item<'a> + | ---- lifetimes in impl do not match this type in trait +LL | where +LL | Self: 'a; + | -- this bound might be missing in the impl +... +LL | type Item = u32; + | ^ lifetimes do not match type in trait + +error[E0277]: `()` is not an iterator + --> $DIR/span-bug-issue-121457.rs:13:23 + | +LL | fn iter(&self) -> impl for<'missing> Iterator> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `()` is not an iterator + | + = help: the trait `Iterator` is not implemented for `()` + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0195, E0277, E0582. +For more information about an error, try `rustc --explain E0195`. diff --git a/tests/ui/never_type/span-bug-issue-121445.rs b/tests/ui/never_type/span-bug-issue-121445.rs new file mode 100644 index 00000000000..2fe22529c4e --- /dev/null +++ b/tests/ui/never_type/span-bug-issue-121445.rs @@ -0,0 +1,15 @@ +#![feature(never_type)] + +fn test2() { + let x: !; + let c2 = SingleVariant::Points(0) + | match x { //~ ERROR no implementation for `SingleVariant | ()` + _ => (), + }; +} + +enum SingleVariant { + Points(u32), +} + +fn main() {} diff --git a/tests/ui/never_type/span-bug-issue-121445.stderr b/tests/ui/never_type/span-bug-issue-121445.stderr new file mode 100644 index 00000000000..b211afa236f --- /dev/null +++ b/tests/ui/never_type/span-bug-issue-121445.stderr @@ -0,0 +1,22 @@ +error[E0369]: no implementation for `SingleVariant | ()` + --> $DIR/span-bug-issue-121445.rs:6:9 + | +LL | let c2 = SingleVariant::Points(0) + | ------------------------ SingleVariant +LL | | match x { + | _________^_- +LL | | _ => (), +LL | | }; + | |_________- () + | +note: an implementation of `BitOr<()>` might be missing for `SingleVariant` + --> $DIR/span-bug-issue-121445.rs:11:1 + | +LL | enum SingleVariant { + | ^^^^^^^^^^^^^^^^^^ must implement `BitOr<()>` +note: the trait `BitOr` must be implemented + --> $SRC_DIR/core/src/ops/bit.rs:LL:COL + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0369`.