Skip to content

Nullness issue – Publishing an event produces nullness warning #18349

@xperiandri

Description

@xperiandri

Issue description

Publishing an event produces nullness warning

Image

See EventNullabilityMismatch.fs

WrongNonNullConditionRepro.zip

Choose one or more from the following categories of impact

  • Unexpected nullness warning (false positive in nullness checking, code uses --checknulls and langversion:preview).
  • Missing nullness warning in a case which can produce nulls (false negative, code uses --checknulls and langversion:preview).
  • Breaking change related to older null constructs in code not using the checknulls switch.
  • Breaking change related to generic code and explicit type constraints (null, not null).
  • Type inference issue (i.e. code worked without type annotations before, and applying the --checknulls enforces type annotations).
  • C#/F# interop issue related to nullness metadata.
  • Other (none of the categories above apply).

Operating System

Windows (Default)

What .NET runtime/SDK kind are you seeing the issue on

.NET SDK (.NET Core, .NET 5+)

.NET Runtime/SDK version

9.0.200

Reproducible code snippet and actual behavior

open System
open System.Windows.Input

/// A command that optionally hooks into CommandManager.RequerySuggested to
/// automatically trigger CanExecuteChanged whenever the CommandManager detects
/// conditions that might change the output of canExecute. It's necessary to use
/// this feature for command bindings where the CommandParameter is bound to
/// another UI control (e.g. a ListView.SelectedItem).
type internal Command(execute, canExecute) =

  let canExecuteChanged = Event<EventHandler, EventArgs>()

  // CommandManager only keeps a weak reference to the event handler,
  // so a strong reference must be maintained,
  // which is achieved by this mutable let-binding.
  // Can test this via the UiBoundCmdParam sample.
  let mutable _handler = null

  member this.AddRequeryHandler () =
    let handler = EventHandler(fun _ _ -> this.RaiseCanExecuteChanged())
    _handler <- handler

  member this.RaiseCanExecuteChanged () = canExecuteChanged.Trigger(this, EventArgs.Empty)

  interface ICommand with
    [<CLIEvent>]
    member _.CanExecuteChanged = canExecuteChanged.Publish
    member _.CanExecute p = canExecute p
    member _.Execute p = execute p

Possible workarounds

No response

Metadata

Metadata

Assignees

Labels

Area-NullnessIssues related to handling of Nullable Reference TypesBugImpact-Low(Internal MS Team use only) Describes an issue with limited impact on existing code.

Type

No type
No fields configured for issues without a type.

Projects

Status

New

Relationships

None yet

Development

No branches or pull requests

Issue actions