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

DllImportGenerator should error if attribute is applied to an extern #62700

Closed
Tracked by #60595
danmoseley opened this issue Dec 13, 2021 · 6 comments · Fixed by #65915
Closed
Tracked by #60595

DllImportGenerator should error if attribute is applied to an extern #62700

danmoseley opened this issue Dec 13, 2021 · 6 comments · Fixed by #65915
Labels
area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature
Milestone

Comments

@danmoseley
Copy link
Member

Bug #62616 was caused by applying [GeneratedDllImport] to an extern rather than partial method. The compilation succeeded, but the analyzer ignored it, and there was a failure at runtime.

  1. The generator should error if it is applied to any method declaration that does not lead the generator to actually run.
  2. Optionally, I suggest trimming (no RVA). from this message:
    IDS_CLASSLOAD_MISSINGMETHODRVA "Could not load type '%1' from assembly '%2' because the method '%3' has no implementation (no RVA)."

because it's what you see in this case, and it seems a rather technical distraction - not sure how relative virtual address is helpful here, the essential fact is that the implementation is missing.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Dec 13, 2021
@danmoseley
Copy link
Member Author

That seems different, it concerns the emitted code. This is about an error while enumerating the attributes

@AaronRobinsonMSFT AaronRobinsonMSFT added source-generator Indicates an issue with a source generator feature area-Interop-coreclr and removed area-Interop-coreclr untriaged New issue has not been triaged by the area owner labels Dec 16, 2021
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 7.0.0 milestone Dec 16, 2021
@elinor-fung
Copy link
Member

We have an analyzer that should be warning when the attribute is used on a method without partial:

// Must be marked partial
context.ReportDiagnostic(methodSymbol.CreateDiagnostic(GeneratedDllImportMissingModifiers, methodSymbol.Name));

We'll have to check why that didn't seem to do its job in this case.

That analyzer only looks for static and partial right now, so it should also be updated to handle other cases where the generator wouldn't run (for example, if the method already has a body).

@danmoseley
Copy link
Member Author

Will this analyzer always run when the generator is used? A rule like this should always fire.

@elinor-fung
Copy link
Member

Yeah, it should always run when the generator is used. It lives in the same assembly as the generator, so the analyzer reference to that assembly should pull in both the generator and analyzer.

@elinor-fung
Copy link
Member

Ah, it is possible to use the generator, but disable all analyzers by setting RunAnalyzers=false, which was the case for the project - System.ServiceProcess.ServiceController.TestService.csproj - that had this issue.

@danmoseley
Copy link
Member Author

Perhaps some of these basic cases can be error cases in the generator.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Feb 26, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Mar 1, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices source-generator Indicates an issue with a source generator feature
Projects
None yet
3 participants