Skip to content

Commit a36e3c1

Browse files
jeffhandleyCopilotCopilotjkotas
authored
Make the Nullable<T>.Value exception message actionable (#132367)
Fixes #126560 ## Problem `InvalidOperation_NoValue` — the message thrown by `Nullable<T>.Value` when there's no value — reads: > Nullable object must have a value. As #126560 points out, this states an obligation on the *object's state* rather than naming the *invalid action*. It reads as "you must assign a value", when the actual fix is almost always the opposite: don't read `.Value` without checking `.HasValue` first. It also never mentions which member was accessed. ## Change ```diff -Nullable object must have a value. +Cannot read the Value property of a Nullable object that has no value. Check HasValue before reading Value. ``` This follows the wording style already used in the repo for comparable "you called this on something that has no value" failures: - `SqlMisc_NullValueMessage` (`System.Data.Common`) — *"Data is Null. This method or property cannot be called on Null values."* - `ReflectionModel_ExportNotReadable` (`System.ComponentModel.Composition`) — *"Cannot get the value of property '{0}', because the member is not readable. The property must have an accessible getter."* - `NoResultOnFailed` (`System.Text.RegularExpressions`) — *"Result cannot be called on a failed Match."* The issue also floated "Cannot read the Value property of a null Nullable object." I avoided "null Nullable object" because `Nullable<T>` is a struct and is never `null` — "that has no value" keeps the same cadence without the imprecision. The second sentence names the check to perform, per *"DO ensure that exception messages are clear and actionable"* (`docs/coding-guidelines/framework-design-guidelines-digest.md`). The resource **name** is unchanged. ## Scope `InvalidOperation_NoValue` is not shared with any unrelated call site — all definitions and references are `Nullable<T>.Value` semantics: | | Path | |---|---| | def | `src/libraries/System.Private.CoreLib/src/Resources/Strings.resx` | | def | `src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Resources/Strings.resx` | | use | `ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue` — sole caller is the `Nullable<T>.Value` getter | | use | `CustomMethodMapper.Nullable.cs` — NativeAOT reflection-invoke shim for `Nullable<T>.Value` | Both `.resx` definitions are updated. Mono shares the libraries CoreLib `.resx`; there is no third copy. No test asserts the text, and the NativeAOT `FrameworkStrings` string-pinning smoke test pins other resources. Per `docs/coding-guidelines/breaking-change-rules.md`, changing the text of an error message is not a breaking change ("users should not rely on these text messages, and they change anyways based on culture"). ## Validation - Built `System.Private.CoreLib` (`build.cmd clr.corelib -c Release`) — clean. - Confirmed against the produced `System.Private.CoreLib.dll` that the new string is embedded and the old one is gone, so `SR.InvalidOperation_NoValue` still resolves. - The NativeAOT `System.Private.Reflection.Execution` `.resx` was not compiled locally — that project currently fails on a pre-existing missing generated `AsmOffsets.cs` requiring a full NativeAOT native build. The edit there is identical in shape and doesn't change the resource name. CI covers it. > [!NOTE] > This pull request description was generated by GitHub Copilot. --------- Co-authored-by: Copilot App <[email protected]> Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jkotas <[email protected]>
1 parent 86f11fb commit a36e3c1

3 files changed

Lines changed: 1 addition & 49 deletions

File tree

‎src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Resources/Strings.resx‎

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,9 @@
120120
<data name="Reflection_InsufficientMetadata_EdbNeeded" xml:space="preserve">
121121
<value>'{0}' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility</value>
122122
</data>
123-
<data name="Arg_DlgtTargMeth" xml:space="preserve">
124-
<value>Cannot bind to the target method because its signature is not compatible with that of the delegate type.</value>
125-
</data>
126-
<data name="Arg_EmptyArray" xml:space="preserve">
127-
<value>Array may not be empty.</value>
128-
</data>
129-
<data name="MissingMember" xml:space="preserve">
130-
<value>Member not found.</value>
131-
</data>
132-
<data name="Arg_HTCapacityOverflow" xml:space="preserve">
133-
<value>Hashtable's capacity overflowed and went negative. Check load factor, capacity and the current size of the table.</value>
134-
</data>
135-
<data name="InvalidOperation_NoValue" xml:space="preserve">
136-
<value>Nullable object must have a value.</value>
137-
</data>
138123
<data name="RFLCT_Targ_StatFldReqTarg" xml:space="preserve">
139124
<value>Non-static field requires a target.</value>
140125
</data>
141-
<data name="MissingConstructor_Name" xml:space="preserve">
142-
<value>Constructor on type '{0}' not found.</value>
143-
</data>
144126
<data name="MultiDim_Of_This_Rank_Not_Supported" xml:space="preserve">
145127
<value>Multidimensional arrays of rank {0} are not supported.</value>
146128
</data>
@@ -153,9 +135,6 @@
153135
<data name="PlatformNotSupported_CannotInvokeDelegateCtor" xml:space="preserve">
154136
<value>Dynamic invocation of delegate constructors is not supported on this runtime.</value>
155137
</data>
156-
<data name="Argument_AddingDuplicate" xml:space="preserve">
157-
<value>An item with the same key has already been added.</value>
158-
</data>
159138
<data name="Argument_AddingDuplicateWithKey" xml:space="preserve">
160139
<value>An item with the same key has already been added. Key: {0}</value>
161140
</data>
@@ -168,30 +147,6 @@
168147
<data name="Argument_ConstraintFailed" xml:space="preserve">
169148
<value>'{0}', on '{1}' violates the constraint of type '{2}'.</value>
170149
</data>
171-
<data name="Argument_ImplementIComparable" xml:space="preserve">
172-
<value>At least one object must implement IComparable.</value>
173-
</data>
174-
<data name="InvalidOperation_AsyncIOInProgress" xml:space="preserve">
175-
<value>The stream is currently in use by a previous operation on the stream.</value>
176-
</data>
177-
<data name="Argument_StreamNotReadable" xml:space="preserve">
178-
<value>Stream was not readable.</value>
179-
</data>
180-
<data name="ObjectDisposed_ReaderClosed" xml:space="preserve">
181-
<value>Cannot read from a closed TextReader.</value>
182-
</data>
183-
<data name="ArgumentOutOfRange_NeedPosNum" xml:space="preserve">
184-
<value>Positive number required.</value>
185-
</data>
186-
<data name="ArgumentNull_Buffer" xml:space="preserve">
187-
<value>Buffer cannot be null.</value>
188-
</data>
189-
<data name="ArgumentOutOfRange_NeedNonNegNum" xml:space="preserve">
190-
<value>Non-negative number required.</value>
191-
</data>
192-
<data name="Argument_InvalidOffLen" xml:space="preserve">
193-
<value>Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.</value>
194-
</data>
195150
<data name="Acc_ReadOnly" xml:space="preserve">
196151
<value>Cannot set a constant field.</value>
197152
</data>

‎src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,5 @@
6767
<Compile Include="$(CompilerCommonPath)\Internal\LowLevelLinq\LowLevelEnumerable.ToArray.cs">
6868
<Link>Internal\LowLevelLinq\LowLevelEnumerable.ToArray.cs</Link>
6969
</Compile>
70-
<Compile Include="$(LibrariesProjectRoot)\System.Private.CoreLib\src\System\Collections\HashHelpers.cs">
71-
<Link>System\Collections\HashHelpers.cs</Link>
72-
</Compile>
7370
</ItemGroup>
7471
</Project>

‎src/libraries/System.Private.CoreLib/src/Resources/Strings.resx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2739,7 +2739,7 @@
27392739
<value>Underlying type information on enumeration is not specified.</value>
27402740
</data>
27412741
<data name="InvalidOperation_NoValue" xml:space="preserve">
2742-
<value>Nullable object must have a value.</value>
2742+
<value>Cannot read the Value property of a Nullable object that has no value. Check HasValue before reading Value.</value>
27432743
</data>
27442744
<data name="InvalidOperation_NullArray" xml:space="preserve">
27452745
<value>The underlying array is null.</value>

0 commit comments

Comments
 (0)