Repository navigation
Commit a36e3c1
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
- libraries/System.Private.CoreLib/src/Resources
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | 123 | | |
139 | 124 | | |
140 | 125 | | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | 126 | | |
145 | 127 | | |
146 | 128 | | |
| |||
153 | 135 | | |
154 | 136 | | |
155 | 137 | | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | 138 | | |
160 | 139 | | |
161 | 140 | | |
| |||
168 | 147 | | |
169 | 148 | | |
170 | 149 | | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | 150 | | |
196 | 151 | | |
197 | 152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | 70 | | |
74 | 71 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2739 | 2739 | | |
2740 | 2740 | | |
2741 | 2741 | | |
2742 | | - | |
| 2742 | + | |
2743 | 2743 | | |
2744 | 2744 | | |
2745 | 2745 | | |
| |||
0 commit comments