On Fri, 28 Oct 2022 19:18:23 GMT, Jim Laskey <jlas...@openjdk.org> wrote:

>> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 4974:
>> 
>>> 4972:         if (processor != null) {
>>> 4973:             resultType = attribTree(processor, env, new 
>>> ResultInfo(KindSelector.VAL, Type.noType));
>>> 4974:             resultType = chk.checkProcessorType(processor, 
>>> resultType, env);
>> 
>> It seems that if this check is erroneous, the type that is considered as 
>> returned by the processor is just `StringTemplate`. This seems odd - if we 
>> have issues type-checking and we get StringTemplate instead of some type T 
>> that the user expects, but doesn't get (e.g. because of raw types), there 
>> could be spurious error messages generated from a type mismatch between T 
>> and StringTemplate.
>
> Not sure where you get `StringTemplate`.  If you specify 
> `TemplateProcessor<String>` the `resultType` will be `String`.  For example:
> 
> 
> public class Processor implements TemplateProcessor<String> {
>     @Override
>     public String process(StringTemplate st) {
>         return st.interpolate();
>     }
> }
> 
> and
> 
> public class Main {
>     public static void main(String... args) throws Throwable {
>         Processor processor = new Processor();
>         System.out.println(processor."1234");
>     }
> }
> 
> works with "1234" as a result.
> 
> If you later change to
> 
> 
> public class Processor implements TemplateProcessor<Integer> {
>     @Override
>     public Integer process(StringTemplate st) {
>         return Integer.valueOf(st.interpolate());
>     }
> }
> 
> 
> Then you get a `java.lang.ClassCastException` as you would expect.

I'm looking at this line:

Type resultType = syms.stringTemplateType;

This assumes that the result type of this AST node will be StringTemplate. Now, 
if a processor is set, we'll call `chk.checkProcessorType` which *might* set a 
new expected type (e.g. a string processor type). But if any error is found in 
that routine (e.g. use of raw string processor), we just log an error, and 
return the same expected type that was passed in - so the expected type stays 
`StringTemplate`.

Something like this:

public class Processor<X> implements TemplateProcessor<X> { ... }


Processor raw = new Processor();
String s = raw."1234".

This will probably print a spurious error like "expected String, found 
StringTemplate".

-------------

PR: https://git.openjdk.org/jdk/pull/10889

Reply via email to