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

Add support for unsafe keyword #62

Open
WizzardMaker opened this issue Nov 2, 2024 · 6 comments
Open

Add support for unsafe keyword #62

WizzardMaker opened this issue Nov 2, 2024 · 6 comments

Comments

@WizzardMaker
Copy link

Thanks for implementing init so quickly last time!

I've found out, that the unsafe keyword is missing in the interface generation.
It's necessary for using pointer types

Example:

public class Test {
    public unsafe Test* Function() {
        return (Test*)0;  
    }
}

Should generate:

public interface Test {
    public unsafe Test* Function();
}

On a side note, I've also seen that IntPtr gets converted to nint - they are syntactically the same but slightly differ in semantics, where IntPtr always denotes "unsafe" pointers and nint just being a representation of the native integer size.
Nothing too critical but just wanted to point it out

@ChaseFlorell
Copy link
Contributor

similarly, I've noticed that it doesn't support the params keyword either. Not sure if it can be resolved in the same PR or if it would need to be a separate one.

@ChristianSauer
Copy link
Collaborator

@ChaseFlorell I think that should be a separate PR.

@ChristianSauer
Copy link
Collaborator

as for the IntPR -> nint problem, that might require special handling. The types are taken from the compiler, I do not know why it's falling back to nint.

@simonmckenzie
Copy link
Contributor

as for the IntPR -> nint problem, that might require special handling. The types are taken from the compiler, I do not know why it's falling back to nint.

I think the fallback is due to the use of SymbolDisplayMiscellaneousOptions.UseSpecialTypes in our display options. A generated interface will always use int over Int32, and similarly will use nint over IntPtr - it is a direct alias in C# 11: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-11.0/numeric-intptr#summary

@simonmckenzie
Copy link
Contributor

Getting the IsUnsafe flag looks difficult - I had a bit of a look, and I can see that it's present in method.ConstructedFrom.UnderlyingMethodSymbol.InUnsafe, but it's all internal. I don't see a way to get at that data via the public API.

Given an IMethodSymbol, this is the full path to the flag:

((Microsoft.CodeAnalysis.CSharp.Symbols.SourceMemberMethodSymbol)
((Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel.MethodSymbol)
((Microsoft.CodeAnalysis.IMethodSymbol)method).ConstructedFrom).UnderlyingMethodSymbol).IsUnsafe

😬

You can see the internal implementation here:

https://github.com/dotnet/roslyn/blob/2d7ee0d5da8eebaf39e6d01fb6be6f67c3a64d55/src/Compilers/CSharp/Portable/Symbols/Source/SourceMemberMethodSymbol.cs#L659-L665

@simonmckenzie
Copy link
Contributor

similarly, I've noticed that it doesn't support the params keyword either. Not sure if it can be resolved in the same PR or if it would need to be a separate one.

Hi @ChaseFlorell,

I've added a fix for params in #65.

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

When branches are created from issues, their pull requests are automatically linked.

4 participants