Skip to content

Show pending obligations as unsatisfied constraints in report_similar_impl_candidates #134348

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion compiler/rustc_infer/src/traits/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::traits::Obligation;
pub enum ScrubbedTraitError<'tcx> {
/// A real error. This goal definitely does not hold.
TrueError,
// A select error with pending obligations
Select(PredicateObligations<'tcx>),
Copy link
Member

Choose a reason for hiding this comment

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

I understand that it's not really documented, but this is antithetical to the design of ScrubbedTraitError. That type should be very lightweight.

Copy link
Author

Choose a reason for hiding this comment

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

Totally understand your concern. I tried this mainly as a quick hack. I did this because through my ad hoc debugging, I uncovered that select_where_possible contains the pending constraints that I needed to display within the error message. But since the returned details are erased via ScrubbedTraitError, I tried to pass on the pending obligations so that I can show it inside the error message.

I'm not sure what is the best way to workaround this, but at least with this quick hack, I'm able to use this fork of the Rust compiler to significantly simplify debugging for my projects. So regardless of which approach is taken, I probably do need to extract the pending obligations in one way or another and then surface it inside my error messages.

Btw, I have also considered alternative approaches, such as writing a compiler plugin to extract the pending obligations for my specific use cases. However, as far as I looked, I couldn't find ways to implement compiler plugins that help recover or better report compile errors. So I do hope that I can find some ways to fix this upstream in Rust, so that I don't need to instruct my users to use my custom fork of Rust compiler to use my project.

/// An ambiguity. This goal may hold if further inference is done.
Ambiguity,
/// An old-solver-style cycle error, which will fatal.
Expand All @@ -26,7 +28,7 @@ pub enum ScrubbedTraitError<'tcx> {
impl<'tcx> ScrubbedTraitError<'tcx> {
pub fn is_true_error(&self) -> bool {
match self {
ScrubbedTraitError::TrueError => true,
ScrubbedTraitError::TrueError | ScrubbedTraitError::Select(_) => true,
ScrubbedTraitError::Ambiguity | ScrubbedTraitError::Cycle(_) => false,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
use rustc_hir::intravisit::Visitor;
use rustc_hir::{self as hir, LangItem, Node};
use rustc_infer::infer::{InferOk, TypeTrace};
use rustc_infer::traits::ScrubbedTraitError;
use rustc_middle::traits::SignatureMismatchData;
use rustc_middle::traits::select::OverflowError;
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
Expand Down Expand Up @@ -1852,7 +1853,21 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
{
terrs.push(terr);
}
if !ocx.select_where_possible().is_empty() {

let errors = ocx.select_where_possible();

if !errors.is_empty() {
for error in errors.iter() {
if let ScrubbedTraitError::Select(obligations) = error {
for obligation in obligations.iter() {
let predicate = &obligation.predicate;
err.help(format!(
"the following constraint is not satisfied: `{}`",
predicate
));
}
}
}
return false;
}
}
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_trait_selection/src/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,13 @@ impl<'tcx> FromSolverError<'tcx, OldSolverError<'tcx>> for FulfillmentError<'tcx
impl<'tcx> FromSolverError<'tcx, OldSolverError<'tcx>> for ScrubbedTraitError<'tcx> {
fn from_solver_error(_infcx: &InferCtxt<'tcx>, error: OldSolverError<'tcx>) -> Self {
match error.0.error {
FulfillmentErrorCode::Select(_)
| FulfillmentErrorCode::Project(_)
FulfillmentErrorCode::Select(_) => {
let pendings_obligations =
error.0.backtrace.into_iter().map(|p| p.obligation).collect();

ScrubbedTraitError::Select(pendings_obligations)
}
FulfillmentErrorCode::Project(_)
| FulfillmentErrorCode::Subtype(_, _)
| FulfillmentErrorCode::ConstEquate(_, _) => ScrubbedTraitError::TrueError,
FulfillmentErrorCode::Ambiguity { overflow: _ } => ScrubbedTraitError::Ambiguity,
Expand Down
Loading