Commit 4cdc624
Fix superfluous managed address errors for pointer to non-existent type (#80760)
Fix superfluous managed address errors for pointer to non-existent type
## Summary
Fixed issue #38378 where
declaring a pointer to a non-existent type would report both CS0246
(type not found) and CS8500/CS0208 (managed address warning/error). Now
only CS0246 is reported.
## Changes Made
- [x] **Binder_Expressions.cs**: Added error type check in
`CheckManagedAddr()` to skip the warning only for truly missing types
(LookupResultKind.Empty or NotATypeOrNamespace), but still report for
other error scenarios like inaccessible types
- [x] **Binder_Symbols.cs**: Removed redundant check (now handled in
CheckManagedAddr)
- [x] **ConstraintsHelper.cs**: Removed redundant check (now handled in
CheckManagedAddr)
- [x] Added 4 comprehensive test cases in UnsafeTests.cs using raw
string literals with C# pointer style (Type* instead of Type *)
- [x] Updated 2 existing tests (PointerTypeInDeconstruction,
TypeNamedFile_03_CSharp11) to reflect new behavior for error types from
parse errors
## Test Results
- ✅ All 4 originally failing tests now pass
- ✅ All 4 new test cases pass
- ✅ All 491 unsafe tests pass
## Fix Pattern
Centralized the check in `CheckManagedAddr` with refined logic: skip
warning only for types that are truly missing
(Empty/NotATypeOrNamespace), but still report for other error scenarios
(e.g., Inaccessible types). Uses modern C# pattern matching syntax for
clean code.
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Quality of implementation: superfluous errors for pointer
to non-existent type</issue_title>
> <issue_description>Compile the following program:
>
> ```cs
> class Program
> {
> static unsafe void Type(Type *type)
> {
> }
> }
> ```
>
> # Expected
> > Program.cs(3,29): error CS0246: The type or namespace name 'Type'
could not be found (are you missing a using directive or an assembly
reference?) [/Users/hugh/Documents/GitHub/repo/repo.csproj]
>
> A single error that we can't find the type
>
> # Actual
> > Program.cs(3,29): error CS0246: The type or namespace name 'Type'
could not be found (are you missing a using directive or an assembly
reference?) [/Users/hugh/Documents/GitHub/repo/repo.csproj]
> Program.cs(3,29): error CS0208: Cannot take the address of, get the
size of, or declare a pointer to a managed type ('Type')
[/Users/hugh/Documents/GitHub/repo/repo.csproj]
>
> The second error is pointless in this case as the type doesn't exist.
I would suggest not checking if we can take the address, size or declare
a pointer to a non-existent type</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> <comment_new><author>@RikkiGibson</author><body>
> Currently we refrain from giving the ManagedAddr error (CS0208) for
sizeof and stackalloc array creation when the type is an error type. It
seems like we don't need to give the error in this case (pointer to an
error type) either.</body></comment_new>
> </comments>
>
</details>
Fixes #38378
<!-- START COPILOT CODING AGENT TIPS -->
---
💡 You can make Copilot smarter by setting up custom instructions,
customizing its development environment and configuring Model Context
Protocol (MCP) servers. Learn more [Copilot coding agent
tips](https://gh.io/copilot-coding-agent-tips) in the docs.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: Cyrus Najmabadi <cyrus.najmabadi@gmail.com>1 parent dc3c2e5 commit 4cdc624
File tree
5 files changed
+125
-7
lines changed- src/Compilers/CSharp
- Portable/Binder
- Test
- Semantic/Semantics
- Syntax/Parsing
5 files changed
+125
-7
lines changedLines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1560 | 1560 | | |
1561 | 1561 | | |
1562 | 1562 | | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
1563 | 1568 | | |
1564 | 1569 | | |
1565 | 1570 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
| 40 | + | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| |||
Lines changed: 0 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4120 | 4120 | | |
4121 | 4121 | | |
4122 | 4122 | | |
4123 | | - | |
4124 | | - | |
4125 | | - | |
4126 | 4123 | | |
4127 | 4124 | | |
4128 | 4125 | | |
| |||
Lines changed: 119 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14299 | 14299 | | |
14300 | 14300 | | |
14301 | 14301 | | |
| 14302 | + | |
| 14303 | + | |
| 14304 | + | |
| 14305 | + | |
| 14306 | + | |
| 14307 | + | |
| 14308 | + | |
| 14309 | + | |
| 14310 | + | |
| 14311 | + | |
| 14312 | + | |
| 14313 | + | |
| 14314 | + | |
| 14315 | + | |
| 14316 | + | |
| 14317 | + | |
| 14318 | + | |
| 14319 | + | |
| 14320 | + | |
| 14321 | + | |
| 14322 | + | |
| 14323 | + | |
| 14324 | + | |
| 14325 | + | |
| 14326 | + | |
| 14327 | + | |
| 14328 | + | |
| 14329 | + | |
| 14330 | + | |
| 14331 | + | |
| 14332 | + | |
| 14333 | + | |
| 14334 | + | |
| 14335 | + | |
| 14336 | + | |
| 14337 | + | |
| 14338 | + | |
| 14339 | + | |
| 14340 | + | |
| 14341 | + | |
| 14342 | + | |
| 14343 | + | |
| 14344 | + | |
| 14345 | + | |
| 14346 | + | |
| 14347 | + | |
| 14348 | + | |
| 14349 | + | |
| 14350 | + | |
| 14351 | + | |
| 14352 | + | |
| 14353 | + | |
| 14354 | + | |
| 14355 | + | |
| 14356 | + | |
| 14357 | + | |
| 14358 | + | |
| 14359 | + | |
| 14360 | + | |
| 14361 | + | |
| 14362 | + | |
| 14363 | + | |
| 14364 | + | |
| 14365 | + | |
| 14366 | + | |
| 14367 | + | |
| 14368 | + | |
| 14369 | + | |
| 14370 | + | |
| 14371 | + | |
| 14372 | + | |
| 14373 | + | |
| 14374 | + | |
| 14375 | + | |
| 14376 | + | |
| 14377 | + | |
| 14378 | + | |
| 14379 | + | |
| 14380 | + | |
| 14381 | + | |
| 14382 | + | |
| 14383 | + | |
| 14384 | + | |
| 14385 | + | |
| 14386 | + | |
| 14387 | + | |
| 14388 | + | |
| 14389 | + | |
| 14390 | + | |
| 14391 | + | |
| 14392 | + | |
| 14393 | + | |
| 14394 | + | |
| 14395 | + | |
| 14396 | + | |
| 14397 | + | |
| 14398 | + | |
| 14399 | + | |
| 14400 | + | |
| 14401 | + | |
| 14402 | + | |
| 14403 | + | |
| 14404 | + | |
| 14405 | + | |
| 14406 | + | |
| 14407 | + | |
| 14408 | + | |
| 14409 | + | |
| 14410 | + | |
| 14411 | + | |
| 14412 | + | |
| 14413 | + | |
| 14414 | + | |
| 14415 | + | |
| 14416 | + | |
| 14417 | + | |
| 14418 | + | |
| 14419 | + | |
| 14420 | + | |
14302 | 14421 | | |
14303 | 14422 | | |
14304 | 14423 | | |
| |||
Lines changed: 0 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1816 | 1816 | | |
1817 | 1817 | | |
1818 | 1818 | | |
1819 | | - | |
1820 | | - | |
1821 | | - | |
1822 | 1819 | | |
1823 | 1820 | | |
1824 | 1821 | | |
| |||
0 commit comments