-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Improve E0277 diagnostics for conditionally implemented traits #158494
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2326,18 +2326,71 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { | |||||
| { | ||||||
| return false; | ||||||
| } | ||||||
| let mut failing_obligations = Vec::new(); | ||||||
| let (desc, mention_castable) = | ||||||
| match (cand.self_ty().kind(), trait_pred.self_ty().skip_binder().kind()) { | ||||||
| (ty::FnPtr(..), ty::FnDef(..)) => { | ||||||
| (" implemented for fn pointer `", ", cast using `as`") | ||||||
| } | ||||||
| (ty::FnPtr(..), _) => (" implemented for fn pointer `", ""), | ||||||
| _ => (" implemented for `", ""), | ||||||
| _ => { | ||||||
| if !self.tcx.predicates_of(def_id).predicates.is_empty() { | ||||||
| failing_obligations = self.probe(|_| { | ||||||
| let ocx = ObligationCtxt::new_with_diagnostics(self); | ||||||
| self.enter_forall(trait_pred, |obligation_trait_ref| { | ||||||
| let impl_args = self.fresh_args_for_item(DUMMY_SP, def_id); | ||||||
| let impl_trait_ref = ocx.normalize( | ||||||
| &ObligationCause::dummy(), | ||||||
| param_env, | ||||||
| ty::EarlyBinder::bind(self.tcx, cand) | ||||||
| .instantiate(self.tcx, impl_args), | ||||||
| ); | ||||||
| if ocx | ||||||
| .eq( | ||||||
| &ObligationCause::dummy(), | ||||||
| param_env, | ||||||
| obligation_trait_ref.trait_ref, | ||||||
| impl_trait_ref, | ||||||
| ) | ||||||
| .is_err() | ||||||
| { | ||||||
| return Vec::new(); | ||||||
| } | ||||||
| ocx.register_obligations( | ||||||
| self.tcx | ||||||
| .predicates_of(def_id) | ||||||
| .instantiate(self.tcx, impl_args) | ||||||
| .into_iter() | ||||||
| .map(|(clause, span)| { | ||||||
| Obligation::new( | ||||||
| self.tcx, | ||||||
| ObligationCause::dummy_with_span(span), | ||||||
| param_env, | ||||||
| clause.skip_norm_wip(), | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| ) | ||||||
| }), | ||||||
| ); | ||||||
| ocx.try_evaluate_obligations() | ||||||
| }) | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| if !failing_obligations.is_empty() { | ||||||
| (" conditionally implemented for `", "") | ||||||
| } else { | ||||||
| (" implemented for `", "") | ||||||
| } | ||||||
| } | ||||||
| }; | ||||||
| let trait_ = self.tcx.short_string(cand.print_trait_sugared(), err.long_ty_path()); | ||||||
| let self_ty = self.tcx.short_string(cand.self_ty(), err.long_ty_path()); | ||||||
| let mut multi_span = MultiSpan::from_span(self.tcx.def_span(def_id)); | ||||||
| for error in failing_obligations { | ||||||
| multi_span | ||||||
| .push_span_label(error.obligation.cause.span, "unsatisfied trait bound"); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And I think we can print |
||||||
| } | ||||||
| err.highlighted_span_help( | ||||||
| self.tcx.def_span(def_id), | ||||||
| multi_span, | ||||||
| vec![ | ||||||
| StringPart::normal(format!("the trait `{trait_}` ")), | ||||||
| StringPart::highlighted("is"), | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| struct Test(i32, i64); | ||
| trait Foo<'a> { | ||
| type Assoc; | ||
| } | ||
|
|
||
| trait SimpleFoo { | ||
| type SimpleAssoc; | ||
| } | ||
| impl<'a, T> Foo<'a> for T where T: SimpleFoo { | ||
| type Assoc = T::SimpleAssoc; | ||
| } | ||
|
|
||
| impl SimpleFoo for i32 { | ||
| type SimpleAssoc = i32; | ||
| } | ||
| impl SimpleFoo for i64 { | ||
| type SimpleAssoc = i32; | ||
| } | ||
|
|
||
| impl<'a> Foo<'a> for Test where i32: Foo<'a, Assoc = i32>, i64: Foo<'a, Assoc = i64> { | ||
| type Assoc = Test; | ||
| } | ||
|
|
||
| fn process<'a, T: Foo<'a>>(_input: T) {} | ||
| fn test() { process(Test(0, 1)) } | ||
| //~^ ERROR the trait bound `Test: Foo<'_>` is not satisfied | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| error[E0277]: the trait bound `Test: Foo<'_>` is not satisfied | ||
| --> $DIR/conditionally-implemented-trait-158423.rs:25:21 | ||
| | | ||
| LL | fn test() { process(Test(0, 1)) } | ||
| | ------- ^^^^^^^^^^ unsatisfied trait bound | ||
| | | | ||
| | required by a bound introduced by this call | ||
| | | ||
| help: the trait `Foo<'_>` is not implemented for `Test` | ||
| --> $DIR/conditionally-implemented-trait-158423.rs:1:1 | ||
| | | ||
| LL | struct Test(i32, i64); | ||
| | ^^^^^^^^^^^ | ||
| help: the trait `Foo<'_>` is conditionally implemented for `Test` | ||
| --> $DIR/conditionally-implemented-trait-158423.rs:20:1 | ||
| | | ||
| LL | impl<'a> Foo<'a> for Test where i32: Foo<'a, Assoc = i32>, i64: Foo<'a, Assoc = i64> { | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------^ | ||
| | | | ||
| | unsatisfied trait bound | ||
| note: required by a bound in `process` | ||
| --> $DIR/conditionally-implemented-trait-158423.rs:24:19 | ||
| | | ||
| LL | fn process<'a, T: Foo<'a>>(_input: T) {} | ||
| | ^^^^^^^ required by this bound in `process` | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0277`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I'd like to move the declaration of
multi_spanhere and movefailing_obligationsinto the arm of the bellowmatch.Then we just need to modify the
multi_spanin the arm, and don't need to makefailing_obligationsmutable. We can write