Skip to content

Open issue: non-negative assumption should apply to both Count and Length #5137

@jcouv

Description

@jcouv

Should the non-negative assumption apply to the used Property or both?

The current behavior is shown below. I think we should have an error for both lines.

interface IIndexable
{
    int this[int i] { get; }
}
interface ICountableViaCount
{
    int Count { get; }
}
interface ICountableViaLength
{
    int Length { get; }
}
class X
{
    public static void Test5<T>(T t) where T : IIndexable, ICountableViaLength, ICountableViaCount
    {
        _ = t is { Length: -1 }; // error CS8518: An expression of type 'T' can never match the provided pattern.
        _ = t is { Count: -1 }; // should Count benefit from the assumption when it is not the preferred property?
    }
}

Should we assume that Length and Count have the same value?

Another related question is whether we should assume Length and Count to have the same value? For example: case { Length: > 0 }: case { Count: 0 }: case { Count: var unreachable }:.

Should we assume non-null slice when we expect a non-zero slice?

Should we consider null or { Length: not 4 } => 0, [_, .. [_, _], _] => 0 exhaustive in a nullable context?
It is possible for a type to claim Length=4 but return a null slice, but that would violate our assumption that lengths and elements are consistent between outer and nested lists.

Options:

  1. this switch is not exhaustive: this is simpler, safer, but goes slightly against our "well-behaved collection" assumption
  2. this switch is exhaustive assuming a well-behaved collection: not sure we want to modify our DAG/codegen for this
  3. reject this slice method because it returns a nullable type: this seems odd, as we generally don't want nullability to affect semantics

FYI @alrz @333fred

Relates to proposal #3435
Relates to test plan dotnet/roslyn#51289

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions