You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| ^^^^^^^^^^^^^^^^^ required by this bound in `CanUsePerson`
365
-
= note: `CanUsePerson` is a "sealed trait", because to implement it you also need to implement `main::_doctest_main_check_traits_md_229_0::CanFormatToString`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it
390
+
= note: `CanUsePerson` is a "sealed trait", because to implement it you also need to implement `main::_doctest_main_debugging_techniques_md_255_0::CanFormatToString`, which is not accessible; this is usually done to force you to use one of the provided types that already implement it
366
391
= help: the following type implements the trait:
367
392
Context
368
-
= note: this error originates in the attribute macro `derive_component` (in Nightly builds, run with -Z macro-backtrace for more info)
393
+
= note: this error originates in the attribute macro `cgp_component` (in Nightly builds, run with -Z macro-backtrace for more info)
394
+
395
+
error: aborting due to 1 previous error
369
396
```
370
397
371
398
The error message is still pretty confusing, but it is slightly more informative
@@ -525,24 +552,50 @@ pub trait CanUsePerson:
525
552
This technique can hopefully help speed up the debugging process, and determine
526
553
which dependency is missing.
527
554
528
-
## Future Improvements
555
+
## Improving The Compiler Error Message
529
556
530
557
The need of manual debugging using check traits is probably one of the major blockers
531
-
for spreading CGP for wider adoption. Although it is not technically an unsolvable
532
-
problem, it is a matter of allocating sufficient time and resource to improve the
533
-
error messages from Rust.
534
-
535
-
When the opportunity arise, we plan to eventually work on submitting pull requests
536
-
for improving the error messages when the constraints from blanket implementations
537
-
cannot be satisfied. This book will be updated once we get an experimental version
538
-
of the Rust compiler working with improved error messages.
539
-
540
-
We also consider exploring the option of building a custom compiler plugin similar
541
-
to Clippy, which can be used to explain CGP-related errors in more direct ways.
542
-
Similarly, it should not be too challenging to build IDE extensions similar to
543
-
Rust Analyzer, which can provide more help in fixing CGP-related errors.
544
-
545
-
Until improved tooling becomes available, we hope that the use of check traits
546
-
for debugging is at least sufficient for early adopters. From this chapter onward,
547
-
we are just starting to explore what can be done with the basic framework of CGP
548
-
in place.
558
+
for spreading CGP for wider adoption. However, it is possible to improve the error
559
+
messages returned from the Rust compiler so that we can more easily find out what
560
+
went wrong.
561
+
562
+
When Rust fails to resolve the constraints for `Person: CanFormatString`, it in fact
563
+
knows that the reason for the failure is caused by unsatisfied _indirect_ constraints
564
+
such as `Person: Serialize`. So what we need to do is to make Rust prints out the
565
+
unsatisfied constraints.
566
+
567
+
We have a fix for this on an [experimental fork](https://github.com/contextgeneric/rust/tree/show-pending-constraints-in-fulfillment-error)
568
+
of the Rust compiler. The changes made are roughly 30 lines of code, and we are preparing
569
+
to contribute the patch upstream. But until that is merged and stabilized, you can try
570
+
to use the custom Rust compiler to debug any CGP error.
571
+
572
+
Following are the steps to use the modified Rust compiler:
573
+
574
+
- Clone our fork of the Rust compiler at `https://github.com/contextgeneric/rust.git`, or add it as a secondary git remote.
575
+
- Checkout the branch `show-pending-constraints-in-fulfillment-error`.
576
+
- Build the Rust compiler following the [official guide](https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html).
577
+
- Link the compiled custom compiler using `rustup link`, aliasing it to a custom name like `cgp`. e.g. `rustup toolchain link cgp build/host/stage1`.
578
+
- Run your project using the custom compiler, e.g. `cargo +cgp check`.
579
+
580
+
If everything is working, you should see similar error messages as before, but with additional information included:
581
+
582
+
```text
583
+
error[E0277]: the trait bound `FormatAsJsonString: StringFormatter<Person>` is not satisfied
584
+
--> content/debugging-techniques.md:330:23
585
+
|
586
+
77 | impl CanUsePerson for Person {}
587
+
| ^^^^^^ the trait `StringFormatter<Person>` is not implemented for `FormatAsJsonString`
588
+
|
589
+
= help: the following constraint is not satisfied: `Person: Serialize`
590
+
= help: the trait `StringFormatter<Context>` is implemented for `FormatAsJsonString`
591
+
note: required for `PersonComponents` to implement `StringFormatter<Person>`
592
+
```
593
+
594
+
After the main error is shown, we can also see an extra help hint that says:
595
+
`the following constraint is not satisfied: Person: Serialize`. Thanks to that,
596
+
we can now much more easily pin point the source of error, and proceed to fix
597
+
our CGP program.
598
+
599
+
We hope that our patch will soon be accepted by the Rust project, so that future
600
+
versions of Rust will be more accessible for CGP. When that happens, we will
0 commit comments