Remove sorting requirement for interface lists (#60541)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Mon, 8 Nov 2021 21:55:37 +0000 (06:55 +0900)
committerGitHub <noreply@github.com>
Mon, 8 Nov 2021 21:55:37 +0000 (13:55 -0800)
Fixes #60454.

docs/design/specs/Ecma-335-Augments.md
src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/Ecma335/MetadataBuilder.Tables.cs
src/libraries/System.Reflection.Metadata/tests/Metadata/Ecma335/MetadataBuilderTests.cs

index 1ebcbc3..1c52fed 100644 (file)
@@ -7,6 +7,7 @@ This is a list of additions and edits to be made in ECMA-335 specifications. It
 - [Signatures](#signatures)
 - [Heap sizes](#heap-sizes)
 - [Metadata merging](#metadata-merging)
+- [Metadata logical format](#metadata-logical-format)
 - [Module Initializer](#module-initializer)
 - [Default Interface Methods](#default-interface-methods)
 - [Static Interface Methods](#static-interface-methods)
@@ -362,6 +363,14 @@ This text should be deleted, and the _metadata merging_ entry should be removed
 > * ~~If there are duplicates and two or more have an accessibility other than
 >   **compilercontrolled**, an error has occurred.~~
 
+## Metadata logical format
+
+The requirement to sort InterfaceImpl table using the Interface column as a secondary key in § II.22 _Metadata logical format: tables_ is a spec bug. The interface declaration order affects resolution and a requirement to sort it would make it impossible to emit certain sequences of interfaces (e.g. not possible to have an interface list I1, I2, while also having interface list I2, I1 elsewhere in the module).
+
+The text should be deleted:
+
+> Furthermore, ~~the InterfaceImpl table is sorted using the Interface column as a secondary key, and~~ the GenericParam table is sorted using the Number column as a secondary key.
+
 ## Module Initializer
 
 All modules may have a module initializer. A module initializer is defined as the type initializer (§ II.10.5.3) of the `<Module>` type (§ II.10.8).
index 56405ca..98e1ba5 100644 (file)
@@ -1434,7 +1434,7 @@ namespace System.Reflection.Metadata.Ecma335
             // GenericParam             Owner, Number                       No**
             // GenericParamConstraint   Owner                               No**
             // ImplMap                  MemberForwarded                     No*
-            // InterfaceImpl            Class, Interface                    No**
+            // InterfaceImpl            Class                               No**
             // MethodImpl               Class                               No*
             // MethodSemantics          Association                         Yes
             // NestedClass              NestedClass                         No*
@@ -1551,27 +1551,12 @@ namespace System.Reflection.Metadata.Ecma335
 
         private void ValidateInterfaceImplTable()
         {
-            if (_interfaceImplTable.Count == 0)
+            for (int i = 1; i < _interfaceImplTable.Count; i++)
             {
-                return;
-            }
-
-            InterfaceImplRow current, previous = _interfaceImplTable[0];
-            for (int i = 1; i < _interfaceImplTable.Count; i++, previous = current)
-            {
-                current = _interfaceImplTable[i];
-
-                if (current.Class > previous.Class)
+                if (_interfaceImplTable[i - 1].Class > _interfaceImplTable[i].Class)
                 {
-                    continue;
+                    Throw.InvalidOperation_TableNotSorted(TableIndex.InterfaceImpl);
                 }
-
-                if (previous.Class == current.Class && current.Interface > previous.Interface)
-                {
-                    continue;
-                }
-
-                Throw.InvalidOperation_TableNotSorted(TableIndex.InterfaceImpl);
             }
         }
 
index 6fcc8fb..924c061 100644 (file)
@@ -804,39 +804,9 @@ namespace System.Reflection.Metadata.Ecma335.Tests
             Assert.Throws<InvalidOperationException>(() => builder.ValidateOrder());
 
             builder = new MetadataBuilder();
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeDefinitionHandle(1));
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeDefinitionHandle(1));
-            Assert.Throws<InvalidOperationException>(() => builder.ValidateOrder());
-
-            builder = new MetadataBuilder();
             builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeDefinitionHandle(2));
             builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeDefinitionHandle(1));
-            Assert.Throws<InvalidOperationException>(() => builder.ValidateOrder());
-
-            builder = new MetadataBuilder();
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeReferenceHandle(2));
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeReferenceHandle(1));
-            Assert.Throws<InvalidOperationException>(() => builder.ValidateOrder());
-
-            builder = new MetadataBuilder();
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeSpecificationHandle(2));
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeSpecificationHandle(1));
-            Assert.Throws<InvalidOperationException>(() => builder.ValidateOrder());
-
-            builder = new MetadataBuilder();
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeReferenceHandle(1));
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeDefinitionHandle(1));
-            Assert.Throws<InvalidOperationException>(() => builder.ValidateOrder());
-
-            builder = new MetadataBuilder();
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeSpecificationHandle(1));
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeReferenceHandle(1));
-            Assert.Throws<InvalidOperationException>(() => builder.ValidateOrder());
-
-            builder = new MetadataBuilder();
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeSpecificationHandle(1));
-            builder.AddInterfaceImplementation(MetadataTokens.TypeDefinitionHandle(1), MetadataTokens.TypeDefinitionHandle(1));
-            Assert.Throws<InvalidOperationException>(() => builder.ValidateOrder());
+            builder.ValidateOrder(); // ok
         }
 
         [Fact]