Skip to content

Java frontend: get qualified generic types of static fields #4137

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

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added jbmc/regression/jbmc/generic_static_field/Test.class
Binary file not shown.
12 changes: 12 additions & 0 deletions jbmc/regression/jbmc/generic_static_field/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

public class Test<T> {

public static Test<Integer> static_field;

public static Test<Integer> test() {

return static_field;

}

}
15 changes: 15 additions & 0 deletions jbmc/regression/jbmc/generic_static_field/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CORE
Test.class
--function Test.test --validate-goto-model --show-goto-functions
^EXIT=0$
^SIGNAL=0$
Test\.test:\(\)LTest;#return_value = static_field;
--
^warning: ignoring
--
This checks that the return value type matches the static field -- if it didn't,
either --validate-goto-model would throw because of a mismatching assignment, or
a cast would be used to adjust the type.

The original motivation for this test was that generic field references could
accidentally omit generic arguments that were present in function types.
16 changes: 6 additions & 10 deletions jbmc/src/java_bytecode/java_bytecode_convert_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1529,12 +1529,10 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
const bool is_assertions_disabled_field=
field_name.find("$assertionsDisabled")!=std::string::npos;

const irep_idt field_id(
get_static_field(arg0.get_string(ID_class), field_name));
const symbol_exprt symbol_expr(
get_static_field(arg0.get_string(ID_class), field_name), arg0.type());

INVARIANT(
Copy link
Contributor

Choose a reason for hiding this comment

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

❓ This looks like useful context for this symbol not existing that goes away by folding it into lookup_ref - do you disagree?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems pointless to have an invariant that can never fail, and I think a symbol table lookup returning a reference is widely recognised as find-or-die?

Copy link
Contributor

Choose a reason for hiding this comment

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

My point is "getstatic symbol should have been created before method conversion" is more useful than simply a symbol not existing. Like without that message you might (wrongly) conclude that this conversion should actually be adding to the symbol table

I'm suggesting the invariant goes about the call to lookup_ref in case that wasn't clear. Like,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a clarifying comment

symbol_table.has_symbol(symbol_expr.get_identifier()),
"getstatic symbol should have been created before method conversion");
symbol_table.lookup_ref(field_id).symbol_expr());

convert_getstatic(
arg0, symbol_expr, is_assertions_disabled_field, c, results);
Expand All @@ -1549,12 +1547,10 @@ code_blockt java_bytecode_convert_methodt::convert_instructions(
PRECONDITION(op.size() == 1 && results.empty());
const auto &field_name=arg0.get_string(ID_component_name);

const irep_idt field_id(
get_static_field(arg0.get_string(ID_class), field_name));
const symbol_exprt symbol_expr(
get_static_field(arg0.get_string(ID_class), field_name), arg0.type());

INVARIANT(
symbol_table.has_symbol(symbol_expr.get_identifier()),
"putstatic symbol should have been created before method conversion");
symbol_table.lookup_ref(field_id).symbol_expr());

c = convert_putstatic(i_it->source_location, arg0, op, symbol_expr);
}
Expand Down