Unexpected ref safety error reported when a collection expression is used to construct a ref struct with Add method #76374
Labels
Area-Compilers
Feature - Collection Expressions
Feature - Ref Fields
untriaged
Issues and PRs which have not yet been triaged by a lead
Milestone
Version Used: main 2024-12-10
Steps to Reproduce:
Expected Behavior: Compiles successfully, the
in int x
parameter of theAdd
method cannot be captured into the ref struct. Replacing the collection expression with a collection initializernew() { local };
also works fine. So does making the struct a non-ref struct. See also #76237 (comment).Actual Behavior: Error is reported: "CS9203: A collection expression of type 'R' cannot be used in this context because it may be exposed outside of the current scope."
The text was updated successfully, but these errors were encountered: