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

VerifyError with String Templates #2062

Closed
hjohn opened this issue Feb 23, 2024 · 3 comments · Fixed by #2183
Closed

VerifyError with String Templates #2062

hjohn opened this issue Feb 23, 2024 · 3 comments · Fixed by #2183
Assignees
Milestone

Comments

@hjohn
Copy link

hjohn commented Feb 23, 2024

The following code:

package hs.mediasystem.db.base;

import java.lang.StringTemplate.Processor;
import java.util.Map;

public class VerifyBug {

  public static void main(String[] args) throws Exception {
    Map<String, String> stuff = Map.of("a", "b");
    Transaction tx = new Transaction();

    for(Map.Entry<String, String> entry : stuff.entrySet()) {
      String name = entry.getKey();

      String s = tx."""
        INSERT INTO settings (a, b) VALUES (\{name}, \{entry.getValue()})
      """;
    }
  }

  static class Transaction implements Processor<String, Exception> {
    @Override
    public String process(StringTemplate stringTemplate) throws Exception {
      return null;
    }
  }
}

When run will produce a VerifyError:

Error: Unable to initialize main class hs.mediasystem.db.base.VerifyBug
Caused by: java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    hs/mediasystem/db/base/VerifyBug.main([Ljava/lang/String;)V @63: invokedynamic
  Reason:
    Type 'java/lang/Object' (current frame, stack[2]) is not assignable to 'java/lang/String'
  Current Frame:
    bci: @63
    flags: { }
    locals: { '[Ljava/lang/String;', 'java/util/Map', 'hs/mediasystem/db/base/VerifyBug$Transaction', 'java/util/Map$Entry', 'java/util/Iterator', 'java/lang/String' }
    stack: { 'hs/mediasystem/db/base/VerifyBug$Transaction', 'java/lang/String', 'java/lang/Object' }
  Bytecode:
    0000000: 1213 1215 b800 174c bb00 1d59 b700 1f4d
    0000010: 2bb9 0020 0100 b900 2401 003a 04a7 002f
    0000020: 1904 b900 2a01 00c0 0030 4e2d b900 3201
    0000030: 00c0 0035 3a05 2c19 052d b900 3701 00ba
    0000040: 003a 0000 b600 3ec0 0035 3a06 1904 b900
    0000050: 4101 009a ffcd b1                      
  Stackmap Table:
    full_frame(@32,{Object[#83],Object[#24],Object[#29],Top,Object[#43]},{})
    same_frame(@76)

The problem can be fixed by first extracting entry.getValue() into a String like:

    for(Map.Entry<String, String> entry : stuff.entrySet()) {
      String name = entry.getKey();
      String value = entry.getValue();

      String s = tx."""
        INSERT INTO settings (a, b) VALUES (\{name}, \{value})
      """;
    }
@srikanth-sankaran srikanth-sankaran added this to the 4.32 M1 milestone Feb 25, 2024
@srikanth-sankaran
Copy link
Contributor

@jarthana - if you are unable to get to this in time for 4.32 M1, let me know a couple weeks in advance - TIA

@mpalat mpalat modified the milestones: 4.32 M1, MilestoneNxt Mar 1, 2024
@mpalat mpalat modified the milestones: MilestoneNxt, 4.32 M1 Mar 8, 2024
@iloveeclipse
Copy link
Member

@srikanth-sankaran : is this related to / covered by #2171 ?

@srikanth-sankaran
Copy link
Contributor

@srikanth-sankaran : is this related to / covered by #2171 ?

It is not related to it. But now that I looked at the code generated, I know what the problem is that causes the verify error. We are missing a generic cast I think. I'll follow up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants