-
Notifications
You must be signed in to change notification settings - Fork 1
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
@DisallowLambdaCapture
checker example
#22
base: main
Are you sure you want to change the base?
Conversation
# Conflicts: # arrow-reflect-annotations/src/main/kotlin/arrow/meta/Meta.kt # arrow-reflect-annotations/src/main/kotlin/arrow/meta/MetaContext.kt # arrow-reflect-annotations/src/main/kotlin/arrow/meta/TemplateCompiler.kt # arrow-reflect-compiler-plugin/src/main/kotlin/arrow/reflect/compiler/plugin/fir/checkers/FirMetaAdditionalCheckersExtension.kt # arrow-reflect-compiler-plugin/src/main/kotlin/arrow/reflect/compiler/plugin/fir/transformers/FirMetaTransformer.kt
# Conflicts: # arrow-reflect-annotations/src/main/kotlin/MetaModule.kt # arrow-reflect-compiler-plugin/src/main/kotlin/arrow/reflect/compiler/plugin/fir/checkers/FirMetaAdditionalCheckersExtension.kt # arrow-reflect-compiler-plugin/src/testGenerated/arrow/reflect/compiler/plugin/runners/DiagnosticTestGenerated.java
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I am missing expertise to review all changes in this PR.
|
||
@Meta | ||
@Target(AnnotationTarget.FUNCTION) | ||
annotation class DisallowLambdaCapture(val msg: String = "") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this not be more precise as msg: String?
?
contract { | ||
callsInPlace(f, InvocationKind.EXACTLY_ONCE) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure where this is checked in DisallowLambdaCapture.kt
? 🤔
I will review it too, sorry I miss it! |
|
The
@DisallowLambdaCapture
macro is an annotation that can be applied to functions. It checks that functions annotated with@DisallowLambdaCapture
are not called inside a non-inline anonymous function that captures variables. If it finds such a call, it will report an error with a message specified in the@DisallowLambdaCapture
annotation or a default message if the msg parameter is not specified.To implement this macro, the
DisallowLambdaCapture
companion object extends theMeta.Checker.Expression
interface and overrides thecheck
method. If present, this method first retrieves the msg argument of the DisallowLambdaCapture annotation. It then filters the declarations in the current scope to find any non-inline anonymous functions and checks if any of these functions capture variables. If it finds a function that captures variables and the function being checked is annotated with DisallowLambdaCapture, it reports an error with the specified message.The DisallowLambdaCapture macro can be helpful in cases where you want to ensure that certain functions are only called in a context where variable capture is not allowed. This can be useful for avoiding unintended side effects or enforcing a certain code style. For example, you might use this macro to disallow calling functions that modify the global state inside anonymous functions to ensure that the state changes are properly isolated and not leaked through the lambda capture.