Bug 569498 - field "cannot be resolved to a variable" in ternary expression as method argument with String expression type
Summary: field "cannot be resolved to a variable" in ternary expression as method argu...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.18   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 4.19 M1   Edit
Assignee: Jay Arthanareeswaran CLA
QA Contact:
URL:
Whiteboard:
Keywords: regression
: 569758 569797 569824 569877 569920 569941 570089 570175 570848 571837 (view as bug list)
Depends on:
Blocks:
 
Reported: 2020-12-05 14:31 EST by Martin Walch CLA
Modified: 2021-03-10 07:35 EST (History)
15 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Walch CLA 2020-12-05 14:31:11 EST
When these things come together:

1. A ternary expression serves as method argument.
2. The condition or the alternatives of that ternary operator reference one or more explicitly final static or not static fields or explicitly final variables that all have their values assigned at their respective declarations.
3. All, condition and both alternatives, contain only literals or variables as those described in 2 and hence can be easily evaluated at compile time.
4. The value type of the alternatives (and of the ternary expression as a whole) is String.
5. No null appears anywhere in the ternary expression.
6. No boxing or unboxing is done anywhere in the ternary expression.
7. There is no explicit non-String cast anywhere in the ternary expression and there is no cast at all of the result of ternary expression (i.e. not even (String)).

then Eclipse tells me for each field as described in 2 that it "cannot be resolved to a variable" and for each method local variable that it "cannot be resolved or is not a field", although the Compiler from OpenJDK 11 successfully compiles the code.

(I suppose there are more aspects and details)

Here is a minimalistic example class:

public class C {
	final String s1 = "";

	public void m(Object s) {
		final boolean b = false;
		final String s2 = "";

		m(b? s1 : s2);
	}
}

b cannot be resolved or is not a field
s1 cannot be resolved to a variable
s2 cannot be resolved or is not a field


"About Eclipse IDE" says:

Eclipse IDE for Java Developers (includes Incubating components)

Version: 2020-12 RC1 (4.18.0 RC1)
Build id: 20201203-2138

(This is Linux with OpenJDK 11.)
Comment 1 Andrey Loskutov CLA 2020-12-08 07:11:33 EST
Thanks, I can confirm on master. This is a regression in 4.18, it compiles fine in 4.17.
Comment 2 Andrey Loskutov CLA 2020-12-08 07:20:29 EST
Works in I20200916-1800, broken in I20200920-1800, so bug 566332 is our friend.
Reverting commit 0757e29b4d81fa91ed32b517ff2e5b8787913848 fixes the compilation.

Jay, could you please check?
Comment 3 Eclipse Genie CLA 2020-12-09 02:01:24 EST
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/173585
Comment 4 Jay Arthanareeswaran CLA 2020-12-09 02:04:22 EST
(In reply to Eclipse Genie from comment #3)
> New Gerrit change created:
> https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/173585

This patch undoes what bug 566332 changed and introduces one small yet significant change. ConditionalExpression.isPolyExpression() returns false by default instead of true. I haven't gone through all the relevant spec, but if all tests run fine it is convincing enough for me.
Comment 5 Luke Usherwood CLA 2020-12-09 08:26:32 EST
Upgraded to 2012-02 RC2 as I figured 2012-02 would be stable by now, but hit this in our code base. It occurred where someone had this kind of declaration:

  private static final boolean DISABLED = false;

(And then modified a logged String based upon that.)
Comment 7 Jay Arthanareeswaran CLA 2020-12-10 01:03:33 EST
(In reply to Luke Usherwood from comment #5)
> Upgraded to 2012-02 RC2 as I figured 2012-02 would be stable by now, but hit
> this in our code base. It occurred where someone had this kind of
> declaration:
> 
>   private static final boolean DISABLED = false;
> 
> (And then modified a logged String based upon that.)

I am not sure if this is related. Can you post a small testcase for us to try?
Comment 8 Luke Usherwood CLA 2020-12-12 03:46:52 EST
Sure, I'd narrowed it down to the following minimal code before finding this bugzilla (with a very similar example):

    public class Snippet {
        private static final boolean DISABLED = false;
        public static void main(String[] args) {
            print(DISABLED ? "Y" : "N");
        }
        static void print(String s) { }
    }

The original code went something like:

  if (something || DISABLED) {
     LOGGER.info("blah {} blah", DISABLED  ? "Y" : "N");
Comment 9 Andrey Loskutov CLA 2020-12-16 17:57:56 EST
*** Bug 569758 has been marked as a duplicate of this bug. ***
Comment 10 Andrey Loskutov CLA 2020-12-17 16:00:22 EST
*** Bug 569797 has been marked as a duplicate of this bug. ***
Comment 11 Daniel Segall CLA 2020-12-17 16:06:58 EST
Will there be a patch release with the fix for this bug? I can't compile my codebase at work because of it.
Comment 12 Andrey Loskutov CLA 2020-12-18 16:09:43 EST
*** Bug 569824 has been marked as a duplicate of this bug. ***
Comment 13 Andrey Loskutov CLA 2020-12-22 10:57:58 EST
*** Bug 569877 has been marked as a duplicate of this bug. ***
Comment 14 Tobias Weimer CLA 2020-12-24 10:21:47 EST
(In reply to Daniel Segall from comment #11)
> Will there be a patch release with the fix for this bug? I can't compile my
> codebase at work because of it.

I second that. The codebase at our work contains too much code to refactor it in a style that compiles. This is refinately worth a hotfix release of eclipse.

If you need Java 15, going down to 2020-09 isn't an option either because the plugin for Java 15 support is not available any longer:

https://www.eclipse.org/forums/index.php?t=msg&th=1106384&goto=1836201&#msg_1836201
Comment 15 Andrey Loskutov CLA 2020-12-24 17:32:09 EST
(In reply to Daniel Segall from comment #11)
> Will there be a patch release with the fix for this bug? I can't compile my
> codebase at work because of it.

Unfortunately not, except some company decides to spent some money (== time) to maintain and produce maintenance releases. Everyone can do this, it is just matter of available resources.

We will have the next release in few months. You can help to prevent such bad regressions in the future by using milestones, M1 is in January.
Comment 16 Andrey Loskutov CLA 2020-12-25 07:07:42 EST
*** Bug 569920 has been marked as a duplicate of this bug. ***
Comment 17 Luke Usherwood CLA 2020-12-28 04:37:36 EST
In case it helps others who need to stick with Eclipse 2020-12, the trivial workaround is to extract the ternary expression to a local variable:

From:
    m(b? s1 : s2);

To:
    String s = b ? s1 : s2;
    m(s);

Tip: the keyboard accelerators  Alt+Shift+(Up,L) to help select the ternary + extract to local make very short work of this.
Comment 18 Andrey Loskutov CLA 2020-12-28 08:43:31 EST
*** Bug 569941 has been marked as a duplicate of this bug. ***
Comment 19 Jay Arthanareeswaran CLA 2021-01-04 00:44:38 EST
(In reply to Luke Usherwood from comment #8)
> Sure, I'd narrowed it down to the following minimal code before finding this
> bugzilla (with a very similar example):
> 
>     public class Snippet {
>         private static final boolean DISABLED = false;
>         public static void main(String[] args) {
>             print(DISABLED ? "Y" : "N");
>         }
>         static void print(String s) { }
>     }

So, this issue is same as comment #0 and is fixed by this bug. If that's not the case, please let me know.
Comment 20 Andrey Loskutov CLA 2021-01-05 09:42:57 EST
*** Bug 570089 has been marked as a duplicate of this bug. ***
Comment 21 Manoj N Palat CLA 2021-01-07 00:48:24 EST
Verified for Eclipse Version: 2021-03 (4.19) M1 with Build id: I20210106-1800
Comment 22 Andrey Loskutov CLA 2021-01-07 11:14:06 EST
*** Bug 570175 has been marked as a duplicate of this bug. ***
Comment 23 Eclipse Genie CLA 2021-01-11 02:30:15 EST
New Gerrit change created: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/174572
Comment 24 Jay Arthanareeswaran CLA 2021-01-11 06:54:31 EST
(In reply to Eclipse Genie from comment #23)
> New Gerrit change created:
> https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/174572

This is a backport to R4_18_maintenance just in case someone is interested in.
Comment 25 Tobias Weimer CLA 2021-01-11 10:16:24 EST
(In reply to Jayaprakash Arthanareeswaran from comment #24)
> (In reply to Eclipse Genie from comment #23)
> > New Gerrit change created:
> > https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/174572
> 
> This is a backport to R4_18_maintenance just in case someone is interested
> in.

Cool, but there is still no hotfix release?
Comment 27 Jay Arthanareeswaran CLA 2021-01-12 03:53:59 EST
(In reply to Tobias Weimer from comment #25)
> Cool, but there is still no hotfix release?

Not with just this fix. However, you can pick up the 4.19 M1 milestone update from here:

https://download.eclipse.org/eclipse/updates/4.19milestones/S-4.19M1-202101061800/
Comment 28 Andrey Loskutov CLA 2021-02-10 16:23:11 EST
*** Bug 570848 has been marked as a duplicate of this bug. ***
Comment 29 Andrey Loskutov CLA 2021-03-10 07:35:10 EST
*** Bug 571837 has been marked as a duplicate of this bug. ***