From 96b8225d8dd971bc2ecc7aa12068adcda9a9f20f Mon Sep 17 00:00:00 2001 From: Veera Date: Mon, 11 Mar 2024 23:05:43 -0400 Subject: [PATCH] Don't Create `ParamCandidate` When Obligation Contains Errors Fixes #121941 --- .../src/traits/select/candidate_assembly.rs | 7 +++++++ ...r-ty-with-calller-supplied-obligation-issue-121941.rs | 5 +++++ ...-with-calller-supplied-obligation-issue-121941.stderr | 9 +++++++++ 3 files changed, 21 insertions(+) create mode 100644 tests/ui/traits/dont-match-error-ty-with-calller-supplied-obligation-issue-121941.rs create mode 100644 tests/ui/traits/dont-match-error-ty-with-calller-supplied-obligation-issue-121941.stderr diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs index 66f740b761d32..89654ed61aeb9 100644 --- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs +++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs @@ -219,6 +219,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { ) -> Result<(), SelectionError<'tcx>> { debug!(?stack.obligation); + // An error type will unify with anything. So, avoid + // matching an error type with `ParamCandidate`. + // This helps us avoid spurious errors like issue #121941. + if stack.obligation.predicate.references_error() { + return Ok(()); + } + let all_bounds = stack .obligation .param_env diff --git a/tests/ui/traits/dont-match-error-ty-with-calller-supplied-obligation-issue-121941.rs b/tests/ui/traits/dont-match-error-ty-with-calller-supplied-obligation-issue-121941.rs new file mode 100644 index 0000000000000..a08407683d8fe --- /dev/null +++ b/tests/ui/traits/dont-match-error-ty-with-calller-supplied-obligation-issue-121941.rs @@ -0,0 +1,5 @@ +fn function() { + foo == 2; //~ ERROR cannot find value `foo` in this scope [E0425] +} + +fn main() {} diff --git a/tests/ui/traits/dont-match-error-ty-with-calller-supplied-obligation-issue-121941.stderr b/tests/ui/traits/dont-match-error-ty-with-calller-supplied-obligation-issue-121941.stderr new file mode 100644 index 0000000000000..2da731dcc4b14 --- /dev/null +++ b/tests/ui/traits/dont-match-error-ty-with-calller-supplied-obligation-issue-121941.stderr @@ -0,0 +1,9 @@ +error[E0425]: cannot find value `foo` in this scope + --> $DIR/dont-match-error-ty-with-calller-supplied-obligation-issue-121941.rs:2:5 + | +LL | foo == 2; + | ^^^ not found in this scope + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0425`.