Snap for 6525849 from 7eb988ad9819ae5c1dd69a4c337e6edd6dac87a0 to rvc-release

Change-Id: I507d2d5d8d1b70ba5c43aacc3c56516ac6cad5b1
diff --git a/aidl_checkapi.cpp b/aidl_checkapi.cpp
index b22c294..f0228d9 100644
--- a/aidl_checkapi.cpp
+++ b/aidl_checkapi.cpp
@@ -182,15 +182,6 @@
     const auto& new_field = new_fields.at(i);
     compatible &= are_compatible_types(old_field->GetType(), new_field->GetType());
 
-    // Note: unlike method argument names, field name change is an incompatible
-    // change, otherwise, we can't detect
-    // parcelable Point {int x; int y;} -> parcelable Point {int y; int x;}
-    if (old_field->GetName() != new_field->GetName()) {
-      AIDL_ERROR(newer) << "Renamed field: " << old_field->GetName() << " to "
-                        << new_field->GetName() << ".";
-      compatible = false;
-    }
-
     const string old_value = old_field->ValueString(AidlConstantValueDecorator);
     const string new_value = new_field->ValueString(AidlConstantValueDecorator);
     if (old_value != new_value) {
@@ -198,6 +189,23 @@
       compatible = false;
     }
   }
+
+  // Reordering of fields is an incompatible change.
+  for (size_t i = 0; i < new_fields.size(); i++) {
+    const auto& new_field = new_fields.at(i);
+    auto found = std::find_if(old_fields.begin(), old_fields.end(), [&new_field](const auto& f) {
+      return new_field->GetName() == f->GetName();
+    });
+    if (found != old_fields.end()) {
+      size_t old_index = std::distance(old_fields.begin(), found);
+      if (old_index != i) {
+        AIDL_ERROR(new_field) << "Reordered " << new_field->GetName() << " from " << old_index
+                              << " to " << i << ".";
+        compatible = false;
+      }
+    }
+  }
+
   return compatible;
 }
 
diff --git a/aidl_unittest.cpp b/aidl_unittest.cpp
index 2cc280e..6827980 100644
--- a/aidl_unittest.cpp
+++ b/aidl_unittest.cpp
@@ -1386,22 +1386,6 @@
   EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
 }
 
-TEST_F(AidlTestIncompatibleChanges, RenamedField) {
-  io_delegate_.SetFileContents("old/p/Data.aidl",
-                               "package p;"
-                               "parcelable Data {"
-                               "  int foo;"
-                               "  int bar;"
-                               "}");
-  io_delegate_.SetFileContents("new/p/Data.aidl",
-                               "package p;"
-                               "parcelable Data {"
-                               "  int foo;"
-                               "  int bar2;"
-                               "}");
-  EXPECT_FALSE(::android::aidl::check_api(options_, io_delegate_));
-}
-
 TEST_F(AidlTestIncompatibleChanges, RenamedType) {
   io_delegate_.SetFileContents("old/p/IFoo.aidl",
                                "package p;"