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

fix: printing of generic records (issue #4281) #4283

Merged
merged 3 commits into from
Nov 13, 2021
Merged

fix: printing of generic records (issue #4281) #4283

merged 3 commits into from
Nov 13, 2021

Conversation

xzel23
Copy link
Contributor

@xzel23 xzel23 commented Nov 12, 2021

Fix #4281

This PR moves the type argument to the correct location when printing generic record declarations. A new unit test is included.

Copy link
Contributor

@algomaster99 algomaster99 left a comment

Choose a reason for hiding this comment

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

Hi @xzel23 !

Thank you for the pull request. Please look at my comment.

EDIT: Sorry, I wasn't aware about the record feature in Java. Let me have a look again.

@@ -0,0 +1,3 @@
package records;

public record GenericRecord<T>(T a, T b) {}

This comment was marked as outdated.

Copy link
Contributor

@algomaster99 algomaster99 left a comment

Choose a reason for hiding this comment

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

It looks good to me. Thanks, @xzel23 ! I have also suggested some changes so please see the comments for that. I am sorry I gave a misinformed review first.

Anyway, @monperrus @slarse , you can also have a look.

Comment on lines +1 to +3
package records;

public record GenericRecord<T>(T a, T b) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please add this test case under src/test/resources/records? It's where we put all the tests related to records.

Copy link
Contributor Author

@xzel23 xzel23 Nov 12, 2021

Choose a reason for hiding this comment

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

Hm... I think the suggested path is the same as where the file is currently located. But I think I should update the package name.

@@ -146,6 +146,16 @@ private CtModel createModelFromPath(String code) {
return model;
}

@Test
void printGenericRecord() {
Copy link
Contributor

Choose a reason for hiding this comment

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

We try to name new tests we create by describing what is happening in the test. I think a better name would be testGenericTypeParametersArePrintedBeforeTheFunctionParameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, will update the PR

@xzel23
Copy link
Contributor Author

xzel23 commented Nov 12, 2021

@algomaster99 Yes, I have seen code in Spoon using "record" as a variable name in the test classes. That code will probably break when compiling in Java 14+ where it was introduced as a keyword. The code in the unit test actually is more or less taken from one of my source files that compile perfectly but cannot be handled by spoon.

@algomaster99
Copy link
Contributor

That code will probably break when compiling in Java 14+

@xzel23 We don't compile our test resources before using them in tests and probably that's why all the tests are green when executed in ubuntu/Java 16 environment. Should we consider compiling our test resources, @slarse? We do it in diffmin, but I know it will really hard to do that in spoon because of the number of resources we have in this project.

cannot be handled by spoon.

Spoon doesn't compile code before building the model. You can even create a model for syntactically incorrect code. For example,

public class A {
    x;
}

The return type of field is missing.

However, we guarantee correct transformations only for syntactically correct code. What you encountered was a bug in the printer (thanks for the fix). In other words, spoon created a valid model for your code. It just failed to print it correctly when you called toString.

@xzel23
Copy link
Contributor Author

xzel23 commented Nov 12, 2021

@algomaster99 I just learned that using record as a variable name is indeed legal even in Java 14+ where it is a keyword. The JLS defines it as a contextual keyword.

@algomaster99
Copy link
Contributor

variable name is indeed legal even in Java 14+ where it is a keyword.

Oh, nice. Our test resources are free of at least one possible error :)

Thanks for renaming the test. Could you also shift the resource to the suggested directory?

@xzel23
Copy link
Contributor Author

xzel23 commented Nov 12, 2021

@algomaster99 I would do it, but I'm afraid I can't ;-)

Please compare the paths. First one is the current resource path, second one the suggested directory:

src/test/resources/records/GenericRecord.java
src/test/resources/records

I don't see any difference. Where should I move the file to?

@algomaster99
Copy link
Contributor

Oh, right. I mistook the path because of diffs in multiple file. 😅 I think we are good to go. Let maintainers approve your PR for workflow and then we will review further, if needed.

@@ -146,6 +146,16 @@ private CtModel createModelFromPath(String code) {
return model;
}

@Test
void testGenericTypeParametersArePrintedBeforeTheFunctionParameters() {
// a record with generic type arguments should be printed correctly
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you add a // contract: ... a record with generic type arguments should be printed correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@slarse
Copy link
Collaborator

slarse commented Nov 12, 2021

We don't compile our test resources before using them in tests

This is by design, a lot of the test resources can't compile as they are missing dependencies. It would however be neat to check the syntax. There's already something along the lines of a syntax check implemented in TreeBuilderCompiler.ignoreSyntaxErrors, could probably start from there.

Approved the workflow, getting back to this later.

@slarse
Copy link
Collaborator

slarse commented Nov 13, 2021

@MartinWitt you approved with a comment, are you happy with the PR? if so I merge.

@MartinWitt
Copy link
Collaborator

yes you can merge.

@slarse
Copy link
Collaborator

slarse commented Nov 13, 2021

@MartinWitt okidoki

Thanks @xzel23 for the fix!

@slarse slarse merged commit 7488fa1 into INRIA:master Nov 13, 2021
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.

[Bug] generic records output is wrong
4 participants