Project IReference of TypeName/HResult as Type/Exception#2461
Draft
Sergio0694 wants to merge 3 commits into
Draft
Project IReference of TypeName/HResult as Type/Exception#2461Sergio0694 wants to merge 3 commits into
Sergio0694 wants to merge 3 commits into
Conversation
0e19ee7 to
2b39a90
Compare
Add members to TestComponentCSharp exercising the scalar IReference<T> projection for the two WinRT value types that map to .NET reference types: IReference<Windows.UI.Xaml.Interop.TypeName> -> System.Type and IReference<Windows.Foundation.HResult> -> System.Exception. Each adds a native-boxed property (BoxedTypeName / BoxedHResult) and a round-trip method (RoundtripTypeName / RoundtripHResult), with matching UnitTest coverage including the null case. These tests fail to build until the projection writer stops emitting the invalid Nullable<Type> / Nullable<Exception> for these instantiations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2b39a90 to
a8db136
Compare
…able<> Windows.UI.Xaml.Interop.TypeName and Windows.Foundation.HResult are Windows Runtime value types that project to the .NET reference types System.Type and System.Exception. IReference<T> of those must project to the inner type directly (Type / Exception), not the invalid System.Nullable<Type/Exception> (the type argument of Nullable<T> must be a non-nullable value type). Generalize the TypedefNameWriter IReference special-case to drop the Nullable<> wrapper whenever the inner argument projects to a .NET reference type (TypeName -> Type, HResult -> Exception, or a literal System.Type). The runtime marshalling was already in place (TypeMarshaller / ExceptionMarshaller BoxToUnmanaged / UnboxToManaged); only the projected type name was wrong. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add IVector<IReference<TypeName>> and IVector<IReference<HResult>> members to TestComponentCSharp (return and parameter, exercised in isolation) plus matching UnitTest coverage, to verify the generic projection compiles and to capture the runtime round-trip behavior. The public type collapses to IList<Type> / IList<Exception> via the scalar fix, while the ABI marshaller keeps the Nullable<T> element marker and so targets the IVector<IReference<T>> IID with per-element box/unbox. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix the C# projection of
IReference<T>for the two Windows Runtime value types that map to .NET reference types:Windows.UI.Xaml.Interop.TypeName->System.TypeandWindows.Foundation.HResult->System.Exception. These now project toType/Exceptioninstead of the invalidSystem.Nullable<Type>/System.Nullable<Exception>.Motivation
IReference<T>normally projects toSystem.Nullable<T>, which is correct for value-type inners. ButTypeNameandHResultare Windows Runtime value types that project to the .NET reference typesSystem.TypeandSystem.Exception.Nullable<Type>/Nullable<Exception>are not valid C# (the type argument ofNullable<T>must be a non-nullable value type), so any metadata containingIReference<TypeName>orIReference<HResult>produced a projection that failed to compile. A reference type is already nullable, so the projection should simply be the inner type. This mirrors the equivalent CsWinRT 2.x fix (#2103).The marshalling itself was already fully supported by the runtime (
TypeMarshaller/ExceptionMarshallerBoxToUnmanaged/UnboxToManagedviaIID_IReferenceOfType/IID_IReferenceOfException); only the projected type name was wrong.Changes
src/WinRT.Projection.Writer/Helpers/TypedefNameWriter.cs: when projectingWindows.Foundation.IReference<T>, detect inners whose projection is a .NET reference type (TypeName->Type,HResult->Exception, plus a literalSystem.Type) and emit the inner type directly instead of wrapping it inSystem.Nullable<>.src/Tests/TestComponentCSharp/(TestComponentCSharp.idl,Class.h,Class.cpp): add scalarIReference<TypeName>andIReference<HResult>members - a native-boxed property (BoxedTypeName/BoxedHResult) and a round-trip method (RoundtripTypeName/RoundtripHResult).src/Tests/UnitTest/TestComponentCSharp_Tests.cs: addReferenceTypeNameProjectsAsTypeandReferenceHResultProjectsAsException, covering both marshalling directions and the null case.Notes
The test members and the projection-writer fix are committed separately so CI can demonstrate the tests failing to build (invalid
Nullable<Type>/Nullable<Exception>) before the fix lands, then passing afterward. Collections of these types (IVector<IReference<TypeName>>etc.) are intentionally out of scope: sinceType/Exceptionare reference types there is no distinctNullable<>managed shape, so such a collection is indistinguishable fromIVector<TypeName>/IVector<HResult>at the interop layer.