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: shorter this access #1031

Merged
merged 1 commit into from
Dec 12, 2016
Merged

fix: shorter this access #1031

merged 1 commit into from
Dec 12, 2016

Conversation

monperrus
Copy link
Collaborator

@monperrus monperrus commented Dec 7, 2016

This is an important change in the way we print this access.

The current strategy of writing qualified this v.l.pack.n.Foo.this.bar() yields unreadable code even in readable with-imports mode.

This PR fixes this.

In the mid-term, I would like to completely get rid of checks on isImplicit for this accesses, but this requires some additional work on the model. As discussed recently, there are incorrect implicit targets, which nobody detects and reports because they are not printed. Only advanced transformers suffer from them.

The pretty-printing of this accesses was incredibly strongly specified (kudos to @GerardPaligot ), hence the number of changed tests, please review carefully.

closes #826

@monperrus monperrus force-pushed the fix-this branch 2 times, most recently from 502a6aa to 88d7d2d Compare December 7, 2016 21:39
if (thisTarget.isImplicit()) {
// write nothing, "this" is implicit and we unfortunately cannot always know
// what the good target is in JDTTreeBuilder
return;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This return will produce a not compilable code because you always print a "." before the field access.

Will produce .field witch is not compilable

Copy link
Collaborator Author

@monperrus monperrus Dec 8, 2016

Choose a reason for hiding this comment

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

no because the caller checks for implicitness as well (see ctVisitInvocation for instance).

however, this is an interesting question: who is responsible for printing the "."? probably the parent element, as done today (but consistently?).

however instead of

if (target.isImplicit()) {
  scan(target);
  print(".")
}

I would prefer

scan(target);
if (somethingHasBeenWritten()) {
  print(".")
}

This would be less fragile.

(outside the scope of this PR but I'd like to have your opinion on this)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@monperrus
Copy link
Collaborator Author

monperrus commented Dec 8, 2016 via email

@monperrus monperrus force-pushed the fix-this branch 8 times, most recently from b3ada27 to a81e380 Compare December 8, 2016 15:47
@monperrus monperrus changed the title feature: shorter and explicit this access fix: shorter and explicit this access Dec 8, 2016
@@ -55,7 +56,8 @@ public void testQualifiedThisRef() {
ctTypes.add(ctClass);
printer.getElementPrinterHelper().writeHeader(ctTypes, imports);
printer.scan(ctClass);
assertTrue(printer.getResult().contains("Object o = QualifiedThisRef.Sub.this"));
Assert.assertTrue(printer.getResult().contains("Object o = this"));
Assert.assertTrue(printer.getResult().contains("Object o = QualifiedThisRef.this"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

contains is weak...

scan(f.getTarget());
if (!f.getTarget().isImplicit()) {
if (printer.hasNewContent()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice idea. But is it safe to store the snapshot in printer? What if scan will call printer.snapshotLength() too?
What about

int snapshot = printer.getSnapshotLength()
...
if(print.hasNewContent(snapshot)) {...}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

you're right, replaced by a queue.

Copy link
Collaborator

Choose a reason for hiding this comment

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

what about this code?

printer.getSnapshotLength();
{
...some complex code with many ifs, loops 
... and evil
          return;
... added by somebody else after some years, will bring him hot time ...
}
if (printer.hasNewContent()) {...}

this

int snapshot = printer.getSnapshotLength()
...
if(print.hasNewContent(snapshot)) {...}

is little bit more code, but is really safe

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, but looks less elegant. let's wait for the devil to make ugly things :-).

Copy link
Collaborator

Choose a reason for hiding this comment

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

;-))

@pvojtechovsky
Copy link
Collaborator

I tried the code of this PR with my project and printed version compiles well. I also do not see problem in code ... but there are part which I do not understand, or I am lazy to understand ... :-)

@monperrus monperrus force-pushed the fix-this branch 2 times, most recently from 02f17cb to b7f257a Compare December 8, 2016 19:43
@monperrus monperrus changed the title fix: shorter and explicit this access fix: shorter this access Dec 8, 2016
@monperrus monperrus force-pushed the fix-this branch 3 times, most recently from 5e332ba to 58658e4 Compare December 8, 2016 20:31
@monperrus
Copy link
Collaborator Author

I wanted to go first for shorter and more explicit this accesses. I only keep shorter for now because I don't want to break too much test cases from client code.

For handling explicitness during refactoring, we have to remove implicit in the appropriate setters and refactoring methods.

@monperrus
Copy link
Collaborator Author

@tdurieux you can merge

@tdurieux tdurieux merged commit 7b3a8eb into INRIA:master Dec 12, 2016
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.

DefaultJavaPrettyPrinter.visitCtThisAccess
3 participants