Skip to content
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

Bound for-deref types with Deref/DerefMut for better error messages #28

Merged
merged 11 commits into from
Dec 1, 2022

Conversation

dhardy
Copy link
Contributor

@dhardy dhardy commented Nov 30, 2022

Implements #27. @nsunderland1 thoughts? (And thanks for the suggestions.)

TODO: changelog and release

@nsunderland1
Copy link

error[E0277]: the trait bound `MyWrapper<T>: Deref` is not satisfied
 --> src/lib.rs:255:1
  |
7 | #[autoimpl(for<T: trait + ?Sized> &T, &mut T, Box<T>, MyWrapper<T>)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Deref` is not implemented for `MyWrapper<T>`
  |
note: required by a bound in `TargetImplsDeref`
 --> src/lib.rs:255:1
  |
7 | #[autoimpl(for<T: trait + ?Sized> &T, &mut T, Box<T>, MyWrapper<T>)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `TargetImplsDeref`
  = note: this error originates in the attribute macro `autoimpl` (in Nightly builds, run with -Z macro-backtrace for more info)

This looks great, and thanks for the super fast turnaround on the implementation!

I might be just blind, but I don't see a doctest anywhere in the codebase for custom deref types in autoimpl-on-trait. Would that be a useful addition?

@dhardy
Copy link
Contributor Author

dhardy commented Dec 1, 2022

Good point about documenting explicit Target for Deref.

This diagnostic should work for DerefMut too:

error[E0277]: the trait bound `MyGreatType<T>: DerefMut` is not satisfied
 --> tests/error_for_deref.rs:6:1
  |
6 | #[autoimpl(for<T: trait + ?Sized> MyGreatType<T>)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `DerefMut` is not implemented for `MyGreatType<T>`
  |
note: required by a bound in `TargetMustImplDeref`
 --> tests/error_for_deref.rs:6:1
  |
6 | #[autoimpl(for<T: trait + ?Sized> MyGreatType<T>)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `TargetMustImplDeref`
  = note: this error originates in the attribute macro `autoimpl` (in Nightly builds, run with -Z macro-backtrace for more info)

And wrong Target:

error[E0271]: type mismatch resolving `<MyGreatType<T> as Deref>::Target == T`
 --> tests/error_for_deref.rs:6:1
  |
6 | #[autoimpl(for<T: trait + ?Sized> MyGreatType<T>)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found struct `Box`
  |
  = note: expected type parameter `T`
                     found struct `Box<T>`
note: required by a bound in `TargetMustImplDeref`
 --> tests/error_for_deref.rs:6:1
  |
6 | #[autoimpl(for<T: trait + ?Sized> MyGreatType<T>)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `TargetMustImplDeref`
  = note: this error originates in the attribute macro `autoimpl` (in Nightly builds, run with -Z macro-backtrace for more info)

Admittedly, the latter error may not be crystal clear, especially where autoimpl is used to impl Deref as in:

#[autoimpl(Deref using self.0)]
struct MyGreatType<T: ?Sized>(Box<T>);

Correct version:

#[autoimpl(Deref<Target = T> using self.0)]
struct MyGreatType<T: ?Sized>(Box<T>);

I should probably try adding tests involving the error message, but I'm too lazy.

@dhardy dhardy merged commit daa26b2 into master Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants