Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot assign to byref-returning type functions #18313

Open
IS4Code opened this issue Feb 13, 2025 · 0 comments
Open

Cannot assign to byref-returning type functions #18313

IS4Code opened this issue Feb 13, 2025 · 0 comments

Comments

@IS4Code
Copy link

IS4Code commented Feb 13, 2025

When a let-bound value with type parameters is of a byref type, assignment to this value incorrectly requires a byref value.

Repro steps

type Storage<'T> =
  static let mutable value = Unchecked.defaultof<'T>
  static member public ValueRef = &value

[<GeneralizableValue>]
let cached<'T>: byref<_> = &Storage<'T>.ValueRef

do
  cached<int> <- 1
  ()

Expected behavior

The assignment succeeds and results in a modification to Storage<int>.value.

Actual behavior

The assignment fails to compile:

error FS0001: This expression was expected to have type
    'byref<int>'    
but here has type
    'int'

The compiler can detect that the value is mutable (otherwise there would be an error about assignment to non-mutable location), but a wrong type is required.

Known workarounds

Turning the value into a normal function (i.e. let cached<'T>() and cached<int>() <- works fine.

Related information

It seems this issue affects all uses of cached<'T> that involve byrefs or assignments, for example:

do
  let mutable r: byref<_> = &cached<int> // Expecting a 'byref<byref<int>>' but given a 'inref<byref<int>>'

  let mutable a = 1
  a <- cached<int> // expected to have type 'int' but here has type 'byref<int>'

  cached<int> <- &a // Type 'byref<byref<int>>' is illegal because in byref<T>, T cannot contain byref types.

Environment: https://sharplab.io/#v2:DYLgZgzgPgLgngBwKYAIDKMD2AnAhgcyQB4ByAFQD4UBeAWACgUUIZcYBLAYxWCRhQC2AV1YAjXigBuuYENTUUAVQB2nABZJOAayQATAHS6kYXEOBYwpSgyYs2XQUgGik2FAiHiHANRlyASsY0KABk0rJIDAwA2kQA4kjKrjLsAF644ki+ERQAugy8/Jy46npWFCAoonDYxkQA+lQKIRg4BMTkFPrZAcZR9LqYNijFpbpE7MowVEQAtCgAjMMAFACUDEA===

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: New
Development

No branches or pull requests

1 participant