Comments (6)
- Yup. This is just not a case we want to with currently. Will look into ways to make this work in the future. for now, stick with collection initializers here.
Sorry about the hassle!
from roslyn.
This looks to be a regression from 17.9.
In 17.9, for collection types implementing IEnumerable
but not IEnumerable<T>
, the compiler assumed the elements were convertible to the iteration type object
when determining convertibility. In 17.10, the compiler checks that each element is implicitly convertible to object
. For this example, that conversion fails because the type of the delegate cannot be inferred.
from roslyn.
What rule changed that caused this? I thought we were effectively rolling back to the 17.9 rules (just looking for the presence of an Add method now).
from roslyn.
The Add
requirement was relaxed in 17.10p4 but the check that each element is implicitly convertible to the iteration type was retained. In 17.9, that check was skipped if the target type implemented IEnumerable
but not IEnumerable<T>
.
That additional check in 17.10 that the element is implicitly convertible to object
(for the IEnumerable
case) can fail when the element is target typed, and relies on a strongly-typed Add
method parameter type.
from roslyn.
@ufcpp, thanks for reporting this issue.
The break was the result of an intentional design change for collection expressions targeting types that implement IEnumerable
(and that do not have a strongly-typed enumerator) to require that all elements are implicitly convertible to the iteration type object
(see LDM-2024-01-08). That change provided consistency with other target types where we already required collection expression elements to be implicitly convertible to the iteration type of the target.
We had not recognized your breaking change case previously. I’ve added an item based on your example to the breaking change documentation for 17.10 (see #73278).
Did you hit this issue with an existing collection type? We're curious how common this case might be. Thanks.
from roslyn.
Yes, this change broke my team's code.
Extracting the main points, we had the following classes. It used collection initialisers for event handler registration.
using System.Collections;
Reciever reciever = new()
{
(sender, args) =>
{
},
(sender, args, args2) =>
{
}
};
class Reciever : IEnumerable
{
public void Recieve()
{
RecievedEventArgs args = new();
_onRecieved1?.Invoke(this, args);
if (_onRecieved2 is { } r)
{
RecievedEventArgs2 args2 = new(); // high computational cost
_onRecieved2?.Invoke(this, args, args2);
}
}
private Action<Reciever, RecievedEventArgs>? _onRecieved1;
private Action<Reciever, RecievedEventArgs, RecievedEventArgs2>? _onRecieved2;
public void Add(Action<Reciever, RecievedEventArgs> onRecieved) => _onRecieved1 += onRecieved;
public void Add(Action<Reciever, RecievedEventArgs, RecievedEventArgs2> onRecieved) => _onRecieved2 += onRecieved;
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException("dummy for collection initializers");
}
// normal args
struct RecievedEventArgs;
// args with high computational cost
struct RecievedEventArgs2;
VS 17.9 suggested IDE0028 (and 17.10 still makes this suggestion). With this applied, we had the code like the following.
Reciever reciever =
[
(sender, args) =>
{
},
(sender, args, args2) =>
{
}
];
Since there are two overloads of Add
, Implementing IEnumerable<T>
does not solve the problem. The collection builder is also helpless.
from roslyn.
Related Issues (20)
- Overridden != and == do not affect nullability HOT 1
- It looks like PEEventSymbol's constructor assumes that we always have both accessors.
- Long-running IAsyncEnumerable<T> leaks the last seen enumerated value. HOT 4
- Roslyn 4.10.0 does not compile for .NET 6 HOT 3
- [Automated] PRs inserted in VS build feature.debugger.main-35014.68
- Roslyn JSON detection marks strings as JSON even if StringSyntaxAttribute says it's not.
- Visual Basic double to integer conversion tests stopped working in .NET 9 Preview 5 HOT 1
- Source Generators referencing nuget packages cannot be debugged in VS
- Add fixer when for `params` span overloads are used with Expression selected
- Consider having generators run automatically (when in balanced mode) when a particular SG file is open in the editor. HOT 4
- Clicking 'rerun generator' prevent future edits into the regular text buffer. HOT 5
- `GeneratorDriver.GetRunResult()` should not throw
- Inheritance margin doesn't work well with screen scaling HOT 2
- .AsIncrementalGenerator extension method
- Use target-typed new for all elements in an array or a list
- C# interactive window does not open after the last Visual Studio Community 2022 Update HOT 2
- [Automated] PRs inserted in VS build main-35017.309
- False positive CS4007 "Instance of type 'System.ReadOnlySpan<Buffer10<int>>' cannot be preserved across 'await' or 'yield' boundary"
- Remove IBuiltInAnalyzer.OpenFileOnly
- Move static members has incorrect formatting after moving to nested type
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from roslyn.