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

Report diagnostic for collection expression with collection initializer target type where Add() returns a value #69136

Closed
cston opened this issue Jul 20, 2023 · 9 comments · Fixed by #70384

Comments

@cston
Copy link
Member

cston commented Jul 20, 2023

Version Used:
c5ec55b

Steps to Reproduce:
Compile and run:

using System.Collections.Immutable;

ImmutableArray<int> a = [1, 2, 3];

Expected Behavior:
Compiler warning or error that ImmutableArray<int>.Add(int) returns a value which might indicate the collection type is immutable.

Actual Behavior:
No compiler diagnostics; NullReferenceException in ImmutableArray<int>.Add(int) at runtime.

Relates to test plan #66418 (collection expressions)

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 20, 2023
@cston
Copy link
Member Author

cston commented Jul 20, 2023

If we report a similar diagnostic for classic collection initializer expressions as well, the diagnostic may need to be a warning wave warning since it is a breaking change.

using System.Collections.Immutable;

ImmutableArray<int> a = new() { 1, 2, 3 }; // warning?

@CyrusNajmabadi
Copy link
Member

Note: Add returns a value for HashSet.

Perhaps only if the return value is the same type as the instance?

@alrz
Copy link
Member

alrz commented Jul 20, 2023

Perhaps only if the return value is the same type as the instance?

That could be a fluent-friendly API just to chain Add methods.

I think itd make sense if void was a requirement here, at least for collection literals. Anything like HashSet would need to use custom create methods or something similar.

@CyrusNajmabadi
Copy link
Member

It's a strong principle that a type that works correctly with collection initializers should just work with collection expressions.

@CyrusNajmabadi
Copy link
Member

 That could be a fluent-friendly API just to chain Add methods.

I'm unfamiliar with any apis that work like that. I'm not saying they don't exist, just that I don't know if any. Can you point me at any? It would be useful for seeing what sorts of patterns already exist.

Note: I'm also ok with this warning being just out of Roslyn-analyzers

@alrz
Copy link
Member

alrz commented Jul 20, 2023

It would be useful for seeing what sorts of patterns already exist.

I mean it's just a convenient choice to return this, it doesn't indicate whether or not the method is mutating or anything.. actually that could be the reason the return type is ignored in the fisrt place.

Definitely agree on this being an analyzer, imo faulty usage patterns could get too narrow for a compiler warning.

@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Jul 21, 2023
@jcouv jcouv added this to the 17.8 milestone Jul 21, 2023
@jnm2
Copy link
Contributor

jnm2 commented Jul 21, 2023

If it helps, it could start out being limited to types implementing System.Collections.Immutable.IImmutableXyz interfaces.

@RikkiGibson
Copy link
Contributor

RikkiGibson commented Oct 12, 2023

To reiterate my thoughts in #70351, I would feel most comfortable with special-casing ImmutableArray to begin with. I want to enable this warning by default in the platform, so I would want to have 99.5% confidence that the warning we are giving is really catching buggy user code. Not just usage of a user collection type where they decided to have a fluent Add.

Also, I would want to figure out what can downlevel users do, including ourselves, to actually make ImmutableArray usable with collection-expressions. Even if we don't consider any solution there to be officially supported, it's something that we ourselves will want. For example, can we declare our own ImmutableCollectionsMarshal.AsImmutableArray internally and have things just light up.

@jnm2
Copy link
Contributor

jnm2 commented Oct 13, 2023

I would want to figure out what can downlevel users do, including ourselves, to actually make ImmutableArray usable with collection-expressions.

We're using collection expressions for ImmutableArray downlevel by referencing the System.Collections.Immutable 8.0.0 NuGet package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment