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

take parameter attributes into account in CreateOptionalRefArg #1122

Merged

Conversation

TymurGubayev
Copy link
Contributor

Problem

#1048: Interop method invocations with OptionalAttribute aren't converted

Solution

  • Any comments on the approach taken, its consistency with surrounding code, etc.
    I decided to leave compiler errors in code, so that BC30455: Argument not specified for parameter... becomes CS7036: There is no argument given that corresponds to the required formal parameter....
  • Which part of this PR is most in need of attention/improvement?
    1. I couldn't quite achieve above goal: VB.NET doesn't respect the Optional parameter (because it doesn't need to, I guess), so we have 3 compiler errors in the VB.NET code instead of 1 should we reference a C# assembly. This means the output generated looses some compiler errors -- I'm not quite sure if that's ok. We'd need to setup two different projects to have a proper test, which I'm not too keen on doing because of the problems with Handle C# ref return when converting VB->C# #1116.
    2. For parameters without DefaultParameterValueAttribute, I use default as initializer. I think this is ok...
    3. I'm using _semanticModel.Compilation.GetTypeByMetadataName("System.Runtime.InteropServices.DefaultParameterValueAttribute"); to get the attribute class (to keep it consistent with other places). If it fails to find one, I'm assuming it's not present -- which isn't necessarily true, I think, if at all possible. Alternative would be something like this:
     //var attributeData = p.GetAttributes().FirstOrDefault(a => SymbolEqualityComparer.IncludeNullability.Equals(a.AttributeClass, defaultParameterValueAttribute));
     var attributeData = p.GetAttributes().FirstOrDefault(a => a.AttributeClass.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "global::System.Runtime.InteropServices.DefaultParameterValueAttribute");
    This is IMO marginally safer, removes a call to GetTypeByMetadataName, but is maybe uglier.
  • At least one test covering the code changed

Copy link
Member

@GrahamTheCoder GrahamTheCoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep looks good. At some point it'd be good to PR an udpate to the changelog, but obviously while there are several PRs flying it will just cause unncessary conflicts so it's fine for now

@GrahamTheCoder GrahamTheCoder force-pushed the fix/MissingByRefArgument/1 branch from beda25e to 785ee54 Compare July 22, 2024 22:36
…ument/1

# Conflicts:
#	Tests/CSharp/MemberTests/MemberTests.cs
@GrahamTheCoder GrahamTheCoder merged commit 0658a1b into icsharpcode:master Jul 22, 2024
1 check passed
@TymurGubayev TymurGubayev deleted the fix/MissingByRefArgument/1 branch July 23, 2024 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants