From 4ed817406f48ed1e3378001959884000e4fd759b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Tue, 3 Dec 2024 05:24:59 +0100 Subject: [PATCH 1/4] Add ECMA augments regarding instance and type construction. Multiple initializations of instances and types might violate runtime invariants, we should forbid it then as discussed in #109679. Users are not expected to have been relying on the behaviour being legal, especially since multiple type initializations are already resulting in invalid behaviour due to JIT optimizations. --- docs/design/specs/Ecma-335-Augments.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/design/specs/Ecma-335-Augments.md b/docs/design/specs/Ecma-335-Augments.md index fd8a0fd93ca87f..01f9e2ab753f40 100644 --- a/docs/design/specs/Ecma-335-Augments.md +++ b/docs/design/specs/Ecma-335-Augments.md @@ -24,6 +24,8 @@ This is a list of additions and edits to be made in ECMA-335 specifications. It - [Creating arrays using newobj](#creating-arrays-using-newobj) - [API documentation](#api-documentation) - [Debug Interchange Format](#debug-interchange-format) +- [Instance construction](#instance-construction) +- [Type initialization](#type-initialization) ## Signatures @@ -1102,3 +1104,19 @@ The incorrect description of `System.Array.Initialize` API in section "II.13.2 I ## Debug Interchange Format The Debug Interchange Format described in partition V is superseded by the [Portable PDB Format](PortablePdb-Metadata.md). + +## Instance construction + +The following is added to the section "II.10.5.1 Instance constructor": + +> Instance constructors shall not be executed multiple times for a single object instance. Explicit calls to constructors from user code are only permitted when calling instance constructors of the base type inside of instance constructors of the derrived type. + +## Type initialization + +The following is added to the section "II.10.5.3 Type initializer": + +> Type initializers shall not be called explicitly from user code. Users intending to guarantee them running at a particular timing shall use the `System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor` method. + +Section "II.10.5.3.1 Type initialization guarantees" is changed so that the guarantee number 3 now states the following: + +> A type initializer shall be executed exactly once for any given type, unless the previous attempt resulted in a `System.TypeInitializationException` being thrown at the location that triggered it. From 6d0ca80358610fa39b6a47f5a1fd7d1470f82294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Tue, 3 Dec 2024 05:30:35 +0100 Subject: [PATCH 2/4] Update Ecma-335-Augments.md --- docs/design/specs/Ecma-335-Augments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/design/specs/Ecma-335-Augments.md b/docs/design/specs/Ecma-335-Augments.md index 01f9e2ab753f40..43569e705fa3e4 100644 --- a/docs/design/specs/Ecma-335-Augments.md +++ b/docs/design/specs/Ecma-335-Augments.md @@ -1109,7 +1109,7 @@ The Debug Interchange Format described in partition V is superseded by the [Port The following is added to the section "II.10.5.1 Instance constructor": -> Instance constructors shall not be executed multiple times for a single object instance. Explicit calls to constructors from user code are only permitted when calling instance constructors of the base type inside of instance constructors of the derrived type. +> Instance constructors shall not be executed multiple times for a single object instance. Explicit calls to constructors from user code are only permitted when calling instance constructors of the base type inside of instance constructors of the derived type. ## Type initialization From dea5e21ad3ac648a9b37ce2750f3e02f8f857406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Tue, 3 Dec 2024 05:33:45 +0100 Subject: [PATCH 3/4] Update Ecma-335-Augments.md --- docs/design/specs/Ecma-335-Augments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/design/specs/Ecma-335-Augments.md b/docs/design/specs/Ecma-335-Augments.md index 43569e705fa3e4..042c4bdcc63f3f 100644 --- a/docs/design/specs/Ecma-335-Augments.md +++ b/docs/design/specs/Ecma-335-Augments.md @@ -1115,7 +1115,7 @@ The following is added to the section "II.10.5.1 Instance constructor": The following is added to the section "II.10.5.3 Type initializer": -> Type initializers shall not be called explicitly from user code. Users intending to guarantee them running at a particular timing shall use the `System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor` method. +> Type initializers shall not be called explicitly from user code. Users intending to guarantee the type initializer has been executed shall use the `System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor` method. Section "II.10.5.3.1 Type initialization guarantees" is changed so that the guarantee number 3 now states the following: From c3d198b94e20e62145ab6d8ca6d719f264b9cb5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Tue, 3 Dec 2024 05:42:37 +0100 Subject: [PATCH 4/4] Update Ecma-335-Augments.md --- docs/design/specs/Ecma-335-Augments.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/design/specs/Ecma-335-Augments.md b/docs/design/specs/Ecma-335-Augments.md index 042c4bdcc63f3f..484283395f94ea 100644 --- a/docs/design/specs/Ecma-335-Augments.md +++ b/docs/design/specs/Ecma-335-Augments.md @@ -1109,7 +1109,7 @@ The Debug Interchange Format described in partition V is superseded by the [Port The following is added to the section "II.10.5.1 Instance constructor": -> Instance constructors shall not be executed multiple times for a single object instance. Explicit calls to constructors from user code are only permitted when calling instance constructors of the base type inside of instance constructors of the derived type. +> Instance constructors shall not be executed multiple times for a single object instance. Explicit calls to constructors on object instances from user code are only permitted when calling instance constructors of the base type inside of instance constructors of the derived type. ## Type initialization