Skip to content

Compiler plugins API: init method should have access to Context #20185

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

Closed
WojciechMazur opened this issue Apr 14, 2024 · 1 comment · Fixed by #20330
Closed

Compiler plugins API: init method should have access to Context #20185

WojciechMazur opened this issue Apr 14, 2024 · 1 comment · Fixed by #20330
Assignees
Labels
area:initialization itype:enhancement needs-minor-release This PR cannot be merged until the next minor release
Milestone

Comments

@WojciechMazur
Copy link
Contributor

WojciechMazur commented Apr 14, 2024

Currently StandardPlugin defined an init method

def init(options: List[String]): List[PluginPhase]

When parsing options, users might want to report warnings (e.g. deprecate compiler plugin options), however report.warning requires implicit Context. One available Context instance that can be suggested to users is NoContext, however it's usage would result in runtime exceptions.

Solution 1: redefine StandardPlugin since Scala 3.5.0 to define implicit Context parameter

def init(options: List[String])(using Context): List[PluginPhase]

The issue with this solution is source compatibility with already existing compiler plugins sources, there are now 106 found standar d plugins implementations on GitHub . All of this would be required to modify the builds to support for version specific implementation of init method

Solution 2: create additional method taking Context that delated to old init method by default

def initialize(options: List[String])(using Context): List[PluginPhase] = init(options)

New method needs to have a different name, otherwise we would not be able to delegate contextual init call to old non-contextual method. Another issue is handling of old init method variant - it typically would needed to be overrided with dummy implementation.

Solution 3: create additional method run before / after init with access to context allowing to validate options and report warnings. Provide it with default no-op implimentation to make it backward source-compatible.

def validate(options: List[String])(using Context): Unit = ()

Alternative solutions:
Allow to use report using NoContext

Issue is related to Scala Native compiler plugin, which has used NoContext for reporting. It was workaround by using ContextBase().initialCtx instead of NoContext but it seems to not support logging correctly - warning message has no level modifier, seems to be standard println instead.

@WojciechMazur
Copy link
Contributor Author

During Core Meeting we've decided to go with the 2nd solution and to deprecate the def init(options: List[String]): List[PluginPhase] variant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:initialization itype:enhancement needs-minor-release This PR cannot be merged until the next minor release
Projects
None yet
1 participant