Skip to content

Recognize annotations on methods that affect nullability analysis #26761

@cston

Description

@cston

Examples, with placeholder attributes:

// ensures arg is true
static void Assert([EnsuresTrue] bool b);
// false if arg is not null
static bool IsNullOrEmpty([NotNullWhenFalse] string? s)
// reference equality for null
[NullEquals] static bool Equals(object? x, object? y)
// result nullability matches arg
static string? NormalizePath([NullInNullOut] string? path)
// ref arg not reset to null
static void InitializeAndAdd<T>([NotNulled] ref List<T>? list, T item)

(jcouv updated): We also need to handle:

  • Interlocked.CompareExchange
  • a ref parameter which has a non-null inbound value, but possibly null outbound value (see FilterAndAppendAndFreeDiagnostics). This way the caller would be warned if passing a maybe-null argument. (Debug.Assert would not do that)
  • TryGetValue on WeakReference
  • object.ReferenceEquals
  • Would it be possible to hint that AsNode() is safe if IsNode is true?
  • Should those attribute affect the inferred type? var x = EnsuresNotNull(something);
  • What about M(x = y, y) where EnsuresNotNull is on the second parameter, or M(y, x = y) where it is on the first?

Filed https://github.com/dotnet/corefx/issues/33880 to track work to annotate the BCL and collect ideas of APIs needing annotations.

We should confirm whether the nullable attributes should be added to mono or not.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions