From 8eb15586c6472077916730ca994e7bd5def09220 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 22 Aug 2024 12:34:06 -0400 Subject: [PATCH] Don't trigger refinement lint if predicates reference errors --- .../src/check/compare_impl_item/refine.rs | 7 ++++++- tests/ui/impl-trait/in-trait/refine-err.rs | 14 ++++++++++++++ .../ui/impl-trait/in-trait/refine-err.stderr | 9 +++++++++ .../in-trait/span-bug-issue-121457.rs | 1 - .../in-trait/span-bug-issue-121457.stderr | 19 +------------------ 5 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 tests/ui/impl-trait/in-trait/refine-err.rs create mode 100644 tests/ui/impl-trait/in-trait/refine-err.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 80daaa60324a3..d2b7ede65234f 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 @@ -7,7 +7,8 @@ use rustc_lint_defs::builtin::{REFINING_IMPL_TRAIT_INTERNAL, REFINING_IMPL_TRAIT use rustc_middle::span_bug; use rustc_middle::traits::{ObligationCause, Reveal}; use rustc_middle::ty::{ - self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, TypeVisitor, + self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperVisitable, TypeVisitable, + TypeVisitableExt, TypeVisitor, }; use rustc_span::Span; use rustc_trait_selection::regions::InferCtxtRegionExt; @@ -177,6 +178,10 @@ pub(super) fn check_refining_return_position_impl_trait_in_trait<'tcx>( return; }; + if trait_bounds.references_error() || impl_bounds.references_error() { + return; + } + // For quicker lookup, use an `IndexSet` (we don't use one earlier because // it's not foldable..). // Also, We have to anonymize binders in these types because they may contain diff --git a/tests/ui/impl-trait/in-trait/refine-err.rs b/tests/ui/impl-trait/in-trait/refine-err.rs new file mode 100644 index 0000000000000..7518cee97c444 --- /dev/null +++ b/tests/ui/impl-trait/in-trait/refine-err.rs @@ -0,0 +1,14 @@ +#![deny(refining_impl_trait)] + +trait FromRow { + fn prepare(self) -> impl Fn() -> T; + //~^ ERROR cannot find type `T` in this scope +} + +impl FromRow for T { + fn prepare(self) -> impl Fn() -> T { + || todo!() + } +} + +fn main() {} diff --git a/tests/ui/impl-trait/in-trait/refine-err.stderr b/tests/ui/impl-trait/in-trait/refine-err.stderr new file mode 100644 index 0000000000000..6b4ef74d50afc --- /dev/null +++ b/tests/ui/impl-trait/in-trait/refine-err.stderr @@ -0,0 +1,9 @@ +error[E0412]: cannot find type `T` in this scope + --> $DIR/refine-err.rs:4:38 + | +LL | fn prepare(self) -> impl Fn() -> T; + | ^ not found in this scope + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0412`. 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 index 7a51037324f28..ab21dae7dc50d 100644 --- a/tests/ui/impl-trait/in-trait/span-bug-issue-121457.rs +++ b/tests/ui/impl-trait/in-trait/span-bug-issue-121457.rs @@ -14,7 +14,6 @@ impl<'a, I: 'a + Iterable> Iterable for &'a I { //~^ ERROR binding for associated type `Item` references lifetime `'missing` //~| ERROR binding for associated type `Item` references lifetime `'missing` //~| ERROR `()` is not an iterator - //~| WARNING impl trait in impl method signature does not match trait method signature } 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 index 67c4df0f3a9eb..d8a2eef94a180 100644 --- a/tests/ui/impl-trait/in-trait/span-bug-issue-121457.stderr +++ b/tests/ui/impl-trait/in-trait/span-bug-issue-121457.stderr @@ -32,24 +32,7 @@ LL | fn iter(&self) -> impl for<'missing> Iterator $DIR/span-bug-issue-121457.rs:13:51 - | -LL | fn iter(&self) -> impl Iterator; - | ------------- return type from trait method defined here -... -LL | fn iter(&self) -> impl for<'missing> Iterator> {} - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ this bound is stronger than that defined on the trait - | - = note: add `#[allow(refining_impl_trait)]` if it is intended for this to be part of the public API of this crate - = note: we are soliciting feedback, see issue #121718 for more information - = note: `#[warn(refining_impl_trait_reachable)]` on by default -help: replace the return type so that it matches the trait - | -LL | fn iter(&self) -> impl Iterator {} - | ~~~~~~~~~~~~~ - -error: aborting due to 4 previous errors; 1 warning emitted +error: aborting due to 4 previous errors Some errors have detailed explanations: E0195, E0277, E0582. For more information about an error, try `rustc --explain E0195`.