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

Remove member name from transparent associated constants #248

Merged
merged 4 commits into from
Jan 11, 2019

Conversation

grovesNL
Copy link
Contributor

This attempts to fix repr(transparent) for associated constants by unwrapping the first field on structs (see #238 (comment) for more context).

I didn't see obvious ways to infer whether the struct the impl block corresponds with is repr(transparent), and the order isn't guaranteed (impl prior to struct). So we have to parse the struct declaration first, and reference it later, which is why the impl blocks are loaded after the regular loop now.

I'm not sure whether the general approach is correct, so any feedback is greatly appreciated, especially ideas to simplify the branching or remove the Vec allocation.

@grovesNL
Copy link
Contributor Author

cc @kvark @IGI-111 if you'd like to take a look

@kvark
Copy link
Contributor

kvark commented Nov 12, 2018

The error handling seems a bit wonky (hard to catch the actual non-error code path by the eye), but the logic looks good to me.

@IGI-111
Copy link
Contributor

IGI-111 commented Nov 12, 2018

It definitely seems workable, but I'm concerned how big that Vec can grow.

Not sure we can avoid it since as you say the order isn't guaranteed.

@grovesNL
Copy link
Contributor Author

The error handling seems a bit wonky (hard to catch the actual non-error code path by the eye)

Agreed, I tried a couple approaches to simplify some of the if lets, open to ideas :)

It definitely seems workable, but I'm concerned how big that Vec can grow.

I guess an alternative is to simply loop through everything again, checking item.has_test_attr() again and only matching syn::Item::Impl. Not sure if there is another approach, or whether splitting it into two passes makes sense?

@grovesNL
Copy link
Contributor Author

@eqrion @IGI-111 do you have any ideas for improvements on this?

@kvark
Copy link
Contributor

kvark commented Dec 1, 2018

@eqrion ping

Copy link
Collaborator

@eqrion eqrion left a comment

Choose a reason for hiding this comment

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

Sorry for the huge delay. I've added some comments, but mostly it looks good.

}
} else {
error!(
"Cannot find type for {}::{} (impl blocks require the struct declaration to be known).",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was it possible for users to have associated constants for enums and not just structs? It looks like the code didn't check for this constraint before, but I may be missing it.

If so, then this is kind of a breaking change. Not sure if that matters or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm good point – I guess we should handle associated constants for enums and traits too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It would just be good to not have a breaking change here change. As it seems we allowed them on enums and traits, but didn't really expect them.

Copy link
Contributor Author

@grovesNL grovesNL Jan 8, 2019

Choose a reason for hiding this comment

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

This now should handle the case where an associated constant of a transparent struct has been added to the impl of an enum. I assume this is the case we want to preserve and not repr(transparent) on the enum itself?

I also tried adding it to traits but it doesn't seem like there is much in place for handling traits yet, (unless I've missed it). For example, I couldn't see how to know whether a trait has been implemented for a particular type, and without that I can't emit the associated constant for the concrete type.

);
}
}
impls.push(item_impl);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we minimize our memory usage here by filtering out Impls that don't contain associated constants?

I'm not too worried about it though, as IIRC rustc needs to hold the full AST in memory, so if that's too much for us it should be too much for them as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this should be handled now.

@@ -2,4 +2,4 @@
#include <stdlib.h>
#include <stdbool.h>

#define Foo_FOO 0
#define Foo_FOO 42
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this change okay?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd actually prefer to fail in this case and not try to assume which is better to resolve the conflict, what do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That sounds reasonable to me. I was just checking to see if the test expectation change was expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left it for now unless it's preferable to early exit in this case. The current behavior is to emit an error (with error!) and continue.

@grovesNL
Copy link
Contributor Author

grovesNL commented Jan 8, 2019

@eqrion I updated the PR. Please take a look, thanks!

@eqrion
Copy link
Collaborator

eqrion commented Jan 11, 2019

Looks good, thanks!

@eqrion eqrion merged commit 7dc667b into mozilla:master Jan 11, 2019
@grovesNL grovesNL deleted the associated-constants branch January 13, 2019 16:04
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.

4 participants