[vala/0.36] codegen: Use G_TYPE_CHECK_INSTANCE_CAST for comparisons with interfaces
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/0.36] codegen: Use G_TYPE_CHECK_INSTANCE_CAST for comparisons with interfaces
- Date: Tue, 13 Aug 2019 15:21:40 +0000 (UTC)
commit c52355b6ea3e12c77d2ba504b7b6865b7d7ce848
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Mon Jul 22 12:49:53 2019 +0200
codegen: Use G_TYPE_CHECK_INSTANCE_CAST for comparisons with interfaces
Avoids "comparison of distinct pointer types lacks a cast" warning for
such cases.
codegen/valaccodebasemodule.vala | 25 ++++++++--------
tests/Makefile.am | 1 +
tests/objects/instance-comparison.vala | 54 ++++++++++++++++++++++++++++++++++
3 files changed, 67 insertions(+), 13 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index ca9a56c74..282c49378 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2767,19 +2767,18 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
}
void make_comparable_cexpression (ref DataType left_type, ref CCodeExpression cleft, ref DataType
right_type, ref CCodeExpression cright) {
- var left_type_as_struct = left_type.data_type as Struct;
- var right_type_as_struct = right_type.data_type as Struct;
-
- if (left_type.data_type is Class && !((Class) left_type.data_type).is_compact &&
- right_type.data_type is Class && !((Class) right_type.data_type).is_compact) {
- var left_cl = (Class) left_type.data_type;
- var right_cl = (Class) right_type.data_type;
-
- if (left_cl != right_cl) {
- if (left_cl.is_subtype_of (right_cl)) {
- cleft = generate_instance_cast (cleft, right_cl);
- } else if (right_cl.is_subtype_of (left_cl)) {
- cright = generate_instance_cast (cright, left_cl);
+ unowned Struct? left_type_as_struct = left_type.data_type as Struct;
+ unowned Struct? right_type_as_struct = right_type.data_type as Struct;
+ unowned ObjectTypeSymbol? left_type_as_object_type = left_type.data_type as ObjectTypeSymbol;
+ unowned ObjectTypeSymbol? right_type_as_object_type = right_type.data_type as
ObjectTypeSymbol;
+
+ if (left_type_as_object_type != null && (!(left_type_as_object_type is Class) || !((Class)
left_type_as_object_type).is_compact)
+ && right_type_as_object_type != null && (!(right_type_as_object_type is Class) ||
!((Class) right_type_as_object_type).is_compact)) {
+ if (left_type_as_object_type != right_type_as_object_type) {
+ if (left_type_as_object_type.is_subtype_of (right_type_as_object_type)) {
+ cleft = generate_instance_cast (cleft, right_type_as_object_type);
+ } else if (right_type_as_object_type.is_subtype_of
(left_type_as_object_type)) {
+ cright = generate_instance_cast (cright, left_type_as_object_type);
}
}
} else if (left_type_as_struct != null && right_type_as_struct != null) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5acdd5f53..e9dbbef09 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -234,6 +234,7 @@ TESTS = \
objects/initially-unowned.vala \
objects/fields.vala \
objects/gsource.vala \
+ objects/instance-comparison.vala \
objects/interfaces.vala \
objects/methods.vala \
objects/paramspec.vala \
diff --git a/tests/objects/instance-comparison.vala b/tests/objects/instance-comparison.vala
new file mode 100644
index 000000000..51f02c00c
--- /dev/null
+++ b/tests/objects/instance-comparison.vala
@@ -0,0 +1,54 @@
+interface IFoo : Object {
+}
+
+class Bar : Object, IFoo {
+}
+
+class Manam : Bar {
+}
+
+interface IFaz : Object, IFoo {
+}
+
+class Baz : Object, IFoo, IFaz {
+}
+
+void main () {
+ {
+ Bar bar = new Bar ();
+ IFoo foo = bar;
+
+ if (foo != bar) {
+ assert_not_reached ();
+ } else if (foo == bar) {
+ // Well done
+ } else {
+ assert_not_reached ();
+ }
+ }
+ {
+ IFaz faz = new Baz ();
+ IFoo foo = faz;
+
+ if (faz != foo) {
+ assert_not_reached ();
+ } else if (foo == faz) {
+ // Well done
+ } else {
+ assert_not_reached ();
+ }
+ }
+ {
+ Manam manam = new Manam ();
+ Bar bar = manam;
+
+ if (manam != bar) {
+ assert_not_reached ();
+ } else if (manam == bar) {
+ // Well done
+ } else {
+ assert_not_reached ();
+ }
+ }
+}
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]