Initial support for non-boxing union access pattern HasValue property#82011
Conversation
|
@RikkiGibson, @333fred, @dotnet/roslyn-compiler Please review #Closed |
2 similar comments
|
@RikkiGibson, @333fred, @dotnet/roslyn-compiler Please review #Closed |
|
@RikkiGibson, @333fred, @dotnet/roslyn-compiler Please review #Closed |
| IsStatic: false, | ||
| DeclaredAccessibility: Accessibility.Public, | ||
| GetMethod: not null, | ||
| SetMethod: null, |
There was a problem hiding this comment.
This seems unusual. Why would we checking this? For example, the foreach pattern doesn't ensure that Enumerator.Value has a get but no set, it only checks for the get being present. #Resolved
There was a problem hiding this comment.
This seems unusual. Why would we checking this?
This is how the spec defines the API. I can see that there could be other interpretations. I will capture a clarification question in the spec.
| input.Type is NamedTypeSymbol { IsUnionTypeNoUseSiteDiagnostics: true } unionType && | ||
| Binder.IsUnionTypeValueProperty(unionType, property)) | ||
| { | ||
| // This sub-puttern is a union matching |
There was a problem hiding this comment.
| // This sub-puttern is a union matching | |
| // This sub-pattern is a union matching |
| } | ||
| "; | ||
| var comp = CreateCompilation([src, IUnionSource], options: TestOptions.ReleaseExe); | ||
| var verifier = CompileAndVerify(comp, expectedOutput: "FalseTrueFalse").VerifyDiagnostics(); |
There was a problem hiding this comment.
Nit: verifier is unused. And a few others.
#Resolved
| // s.Value.ToString(); | ||
| Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s.Value").WithLocation(400, 13) | ||
| ); | ||
| } |
There was a problem hiding this comment.
Consider a generic constraint case, something like:
interface IA
{
bool HasValue { get; }
}
static void Test1<T>(T t) where T : IA, IUnion
{
_ = s is null;
}
``` #ResolvedThere was a problem hiding this comment.
Consider a generic constraint case, something like ...
Perhaps I am missing something. Type parameters aren't union types regardless of constraints, and aren't subject for union matching.
There was a problem hiding this comment.
Perhaps I am missing something. Type parameters aren't union types regardless of constraints, and aren't subject for union matching.
Interesting. The wording in the spec is "Any non-abstract class or struct type that implements the IUnion interface is considered a union type.". I think that means that a type parameter constrained to IUnion, struct should be considered a union type, no?
There was a problem hiding this comment.
In general, type parameters are not classes or structures. They can be reference types or value types. Also, some union behaviors require constructors, type parameters do not have those. Since the spec doesn't explicitly say what scenarios are supposed to work with type parameters, I interpret that as: "Union behaviors aren't applicable to type parameters." However, I requested a clarification - https://github.com/dotnet/csharplang/blob/main/proposals/unions.md#confirm-that-a-type-parameter-is-never-a-union-type-even-when-constrained-to-one
There was a problem hiding this comment.
There is also a test reflecting the fact - UnionMatching_35_TypeParameter
|
@RikkiGibson, @dotnet/roslyn-compiler For a second review #Closed |
3 similar comments
|
@RikkiGibson, @dotnet/roslyn-compiler For a second review #Closed |
|
@RikkiGibson, @dotnet/roslyn-compiler For a second review #Closed |
|
@RikkiGibson, @dotnet/roslyn-compiler For a second review #Closed |
No description provided.