RFR: 8344942: Template-Based Testing Framework [v61]

Emanuel Peter epeter at openjdk.org
Sat May 31 11:51:09 UTC 2025


On Sat, 31 May 2025 11:47:26 GMT, Emanuel Peter <epeter at openjdk.org> wrote:

>> test/hotspot/jtreg/compiler/lib/template_framework/Template.java line 174:
>> 
>>> 172:  * <p>
>>> 173:  * The dollar and hashtag names must have at least one character. The first character must be a letter
>>> 174:  * or underscore (i.e. {@code a-zA-Z_}), the other characters can also be digits (i.e. {@code a-zA-Z0-9_}).
>> 
>> This does not seem to be enforced:
>> 
>>         var testTemplate = Template.make(() -> body(
>>             """
>>             public class Foo {
>>                 public static void main() {
>>                     int $1var = 34;
>>                 }
>>             }
>>             """
>>         ));
>>         System.out.println(testTemplate.render());
>> 
>> Results in:
>> 
>> public class Foo {
>>     public static void main() {
>>         int $1var = 34;
>>     }
>> }
>> 
>> which compiles fine. I can also change it to `$$var` which renders to `$var_1` which also compiles fine.
>
> @chhagedorn
> The current parsing/regex-ing is relatively simple. We only parse the "valid" cases, so the description above is still relevant.
> Your example `$1var` is not a valid pattern, so the regex does not match, and there is no replacement. Sadly, in Java `$1var` is a valid variable name, so there is some chance that the user makes a mistake and gets tripped up by this.
> 
> If the user does a call to `let` or `$` with such a bad string `1var`, then they get a `RendererException`.
> 
> The question is this:
> Should I really try to parse these "bad" patterns, just to validate them as well? All solutions I can think of are really complicated. Is it worth it? Or is it just a mistake by the user, and so the matching does not happen, and that is the users problem?

FYI: `$$var` the first `$` is not a valid pattern, so it is not replaced. But `$var` is, and so that part gets replaced. The result is `$var_1`, which sadly happens to also be valid Java code.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/24217#discussion_r2117740776


More information about the hotspot-compiler-dev mailing list