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

Precise bounds #19

Merged
merged 18 commits into from
Jun 1, 2024
Merged

Precise bounds #19

merged 18 commits into from
Jun 1, 2024

Conversation

ijackson
Copy link
Contributor

Fix the bounds for all generated trait impls.

Here, we change the default behaviour (without ...(bound(...))) to say something like: where FieldType: Clone, for each field.

For example, consider this:

#[derive(Educe)]
#[educe(Clone)]
struct Struct<T, U, V> {
    bare: T,
    boxed: Box<U>,
    marker: PhantomData<V>,
}

Before, we would get just where T: Clone, which is not sufficient, because the generated impl needs to clone boxed too.

With this MR, we get where T: Clone, Box<U>: Clone, PhantomData<V>: Clone. The latter is always satisfied since PhantomData is always Clone. The middle one is equivalent to U: Clone.

That the previous ident-hunting approach cannot be made to work can be seen by considering DebugAsDisplay (in the new debug_struct::bound_5 test case in this MR). struct S<T>(DebugAsDisplay<T>) can only be Debug if T: Display. There's no reasonable way to determine this condition and express it in those terms. But instead, if we write where DebugAsDisplay<T>: Debug, the compiler does the work for us.

Fixes #17, choosing option (3). I think this is not a semver break: the bounds we now require are precisely correct, so the effect is to make code compile (and work) which would previously not compile.

I have retained the existing behaviour for fields with method =. Those fields' bounds are not included. In some cases this means that the user needs to restate all of the bounds manually. That's not great but it's a thing we can improve later. Likewise my changes only affect autocomputed bounds.

I've added test cases, each of which fails when attempted without the functional changes.

I've left open the possibility of providing a syntax for #17 option 2. I'm hoping to make a followup MR for that; I think that's important to help migration from educe 0.4.x.

Also addresses the MyDebug part of #18, since I was reworking that part anyway.

We'll want this in a moment.  No functional chnage.
* Make the internal wrapper type have *all* the parent's generics,
  and contain a PhantomData, so we can:
* Put the bounds on the Debug impl for the internal wrapper type.

* Rename the internal wrapper type to avoid name clashes with users' types.
* Rename the formatter variable to avoid name clashes with users's values.
Rather than trying to filter the generic parameters for ones used in
types, state the bounds in terms of the actual field types.

This is precisely correct.  It avoids lossage in situations where the
field type's implementation of the trait has bounds which depend in a
less-than-straightforward way on its generics.

There is still an issue with implementing traits which have
supertraits.  We ought to apply a `Self: Super` bound, but we don't
yet.
When we are implementing traits which have supertraits, add
bounds of the form `where Self: Super` for each supertrait.

While we're touching every call site, abolish the unused parameters.

(I have future plans that will want `params` in
into_where_predicates_by_generic_parameters_check_types, but not in
create_where_predicates_from_generic_parameters_check_types, so leave
that one there for now.)
Use the new precise bounds function.
Use the new precise bounds function, rather than the ident-hunter.

Apply the Clone supertrait explicitly.  This disposes of the oddity
mentioned in the comment, as will be demonstrated in a test.
Use the new precise bounds function.
This is no longer used, now that everything uses the precise bounds
function.
Nothing calls this.  And, nothing should, because it would give wrong
answers.

Rename create_where_predicates_from_generic_parameters to
create_where_predicates_from_all_generic_parameters.  This will be
useful if we implement a `bound(...)` syntax that allows requesting
the "apply a bound for each generic parameter, regardless" mode.

If we don't want to implement such a mode, we should delete this
function, but let's not entangle this already large bugfix MR with
questions about future features and UI.
@magiclen magiclen merged commit 2ee8bd1 into magiclen:master Jun 1, 2024
101 of 155 checks passed
@magiclen
Copy link
Owner

magiclen commented Jun 1, 2024

Thank you so much for your PR! Your contribution is greatly appreciated. I truly value your help and effort!

@ijackson ijackson deleted the precise-bounds branch July 8, 2024 16:40
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.

Wrong bounds applied to derived impls
2 participants