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: properly print type parameter reference in instanceof #4921

Merged
merged 3 commits into from
Sep 29, 2022

Conversation

SirYwell
Copy link
Collaborator

Since Java 16, it is allowed to have type parameters other than <?> in instanceof checks. This is allowed in cases where the generic type can be carried over from the type on the left side.

I added a test with three such examples, one with pattern matching (the main reason this feature was added), one normal instanceof check with a type parameter as type argument and one with String as type argument.

The current solution is to not enforce wildcard in the printer for compliance mode >= 16. I would personally prefer to not change the printing output there at all, but there are tests relying on it - namely this one:

public void substituteTypeAccessReference() {

And it also might break existing code.

I'm not sure if that's the best solution. If you have any better ideas, please let me know.

@sonatype-lift
Copy link
Contributor

sonatype-lift bot commented Sep 26, 2022

⚠️ 85 God Classes were detected by Lift in this project. Visit the Lift web console for more details.

@SirYwell SirYwell changed the title wip: fix: properly print type parameter reference in instanceof review: fix: properly print type parameter reference in instanceof Sep 26, 2022
@SirYwell SirYwell requested a review from MartinWitt September 26, 2022 20:17
@@ -537,7 +537,7 @@ public <T> void visitCtBinaryOperator(CtBinaryOperator<T> operator) {
printer.writeSpace();
try (Writable _context = context.modify()) {
if (operator.getKind() == BinaryOperatorKind.INSTANCEOF) {
_context.forceWildcardGenerics(true);
_context.forceWildcardGenerics(env.getComplianceLevel() < 16);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please move this in a method containing an explanation why we have this condition?

}

<T> T first(Iterable<T> iterable) {
if (iterable instanceof List<T> list) {
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 test with List<T<U>> or something more complex?

Copy link
Collaborator

@MartinWitt MartinWitt left a comment

Choose a reason for hiding this comment

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

I add some inline comments other LGTM

@SirYwell SirYwell requested a review from MartinWitt September 27, 2022 15:12
@MartinWitt
Copy link
Collaborator

LGTM will merge tomorrow.

@MartinWitt MartinWitt changed the title review: fix: properly print type parameter reference in instanceof fix: properly print type parameter reference in instanceof Sep 29, 2022
@MartinWitt MartinWitt merged commit 2d4af40 into INRIA:master Sep 29, 2022
@MartinWitt MartinWitt deleted the fix/print-generic-rhs-instanceof branch September 29, 2022 14:12
@MartinWitt
Copy link
Collaborator

Thanks @SirYwell. Almost forget merging this PR. Please don't hesitate to ping me next time.

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