Skip to content

Roslyn doesn't take into account [DoesNotReturn] annotations on accessors #50018

@TessenR

Description

@TessenR

Version Used:

Branch master (8 Dec 2020)
Latest commit 60d947d by AlekseyTs:
Ensure that each generic local function gets its own container to cache dynamic call-sites. (#49802)

Fixes #49599.

Steps to Reproduce:

#nullable enable
using System.Diagnostics.CodeAnalysis;

public class Base
{
  public virtual string? P { [DoesNotReturn] get { throw null!; } [DoesNotReturn] set { throw null!; } }
    
  void Test()
  {
    P = null;
    P?.ToString();
    P.ToString(); // false CS8602
  }
}

Expected Behavior:
No warnings in the Test method. The method won't execute after the first set accessor is executed

Actual Behavior:
No warnings. Roslyn completely ignores the [DoesNotReturn] annotations and thinks the method can continue.

Notes
Roslyn allows these annotations and even verifies them for inheritance so I think it should take them into account in the nullability analysis as well

#nullable enable
using System.Diagnostics.CodeAnalysis;

public class Base
{
  public virtual string? P { [DoesNotReturn] get { throw null!; } [DoesNotReturn] set { throw null!; } }
}

public class Derived : Base
{
  public override string P  {
    get { throw null!; } // CS8770: Method 'string Derived.P.get' lacks `[DoesNotReturn]` annotation to match implemented or overridden member.
    set { throw null!; } // CS8770: Method 'void Derived.P.set' lacks `[DoesNotReturn]` annotation to match implemented or overridden member.
  }
}

Note: This issue is referenced in source

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions